fix: some autocmds skipped when opening files from oil (#120)
This commit is contained in:
parent
8882f6c89e
commit
61f8655e03
2 changed files with 85 additions and 60 deletions
|
|
@ -403,10 +403,19 @@ end
|
||||||
--- preview boolean Open the buffer in a preview window
|
--- preview boolean Open the buffer in a preview window
|
||||||
--- tab boolean Open the buffer in a new tab
|
--- tab boolean Open the buffer in a new tab
|
||||||
--- close boolean Close the original oil buffer once selection is made
|
--- close boolean Close the original oil buffer once selection is made
|
||||||
M.select = function(opts)
|
---@param callback nil|fun(err: nil|string) Called once all entries have been opened
|
||||||
|
M.select = function(opts, callback)
|
||||||
local cache = require("oil.cache")
|
local cache = require("oil.cache")
|
||||||
local config = require("oil.config")
|
local config = require("oil.config")
|
||||||
opts = vim.tbl_extend("keep", opts or {}, {})
|
opts = vim.tbl_extend("keep", opts or {}, {})
|
||||||
|
local function finish(err)
|
||||||
|
if err then
|
||||||
|
vim.notify(err, vim.log.levels.ERROR)
|
||||||
|
end
|
||||||
|
if callback then
|
||||||
|
callback(err)
|
||||||
|
end
|
||||||
|
end
|
||||||
if opts.horizontal or opts.vertical or opts.preview then
|
if opts.horizontal or opts.vertical or opts.preview then
|
||||||
opts.split = opts.split or "belowright"
|
opts.split = opts.split or "belowright"
|
||||||
end
|
end
|
||||||
|
|
@ -414,19 +423,18 @@ M.select = function(opts)
|
||||||
opts.vertical = true
|
opts.vertical = true
|
||||||
end
|
end
|
||||||
if opts.tab and (opts.preview or opts.split) then
|
if opts.tab and (opts.preview or opts.split) then
|
||||||
error("Cannot set preview or split when tab = true")
|
return finish("Cannot set preview or split when tab = true")
|
||||||
end
|
end
|
||||||
if opts.close and opts.preview then
|
if opts.close and opts.preview then
|
||||||
error("Cannot use close=true with preview=true")
|
return finish("Cannot use close=true with preview=true")
|
||||||
end
|
end
|
||||||
local util = require("oil.util")
|
local util = require("oil.util")
|
||||||
if util.is_floating_win() and opts.preview then
|
if util.is_floating_win() and opts.preview then
|
||||||
vim.notify("oil preview doesn't work in a floating window", vim.log.levels.ERROR)
|
return finish("oil preview doesn't work in a floating window")
|
||||||
return
|
|
||||||
end
|
end
|
||||||
local adapter = util.get_adapter(0)
|
local adapter = util.get_adapter(0)
|
||||||
if not adapter then
|
if not adapter then
|
||||||
return
|
return finish("Could not find adapter for current buffer")
|
||||||
end
|
end
|
||||||
|
|
||||||
local mode = vim.api.nvim_get_mode().mode
|
local mode = vim.api.nvim_get_mode().mode
|
||||||
|
|
@ -454,8 +462,7 @@ M.select = function(opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if vim.tbl_isempty(entries) then
|
if vim.tbl_isempty(entries) then
|
||||||
vim.notify("Could not find entry under cursor", vim.log.levels.ERROR)
|
return finish("Could not find entry under cursor")
|
||||||
return
|
|
||||||
end
|
end
|
||||||
if #entries > 1 and opts.preview then
|
if #entries > 1 and opts.preview then
|
||||||
vim.notify("Cannot preview multiple entries", vim.log.levels.WARN)
|
vim.notify("Cannot preview multiple entries", vim.log.levels.WARN)
|
||||||
|
|
@ -477,10 +484,10 @@ M.select = function(opts)
|
||||||
if any_moved and not opts.preview and config.prompt_save_on_select_new_entry then
|
if any_moved and not opts.preview and config.prompt_save_on_select_new_entry then
|
||||||
local ok, choice = pcall(vim.fn.confirm, "Save changes?", "Yes\nNo", 1)
|
local ok, choice = pcall(vim.fn.confirm, "Save changes?", "Yes\nNo", 1)
|
||||||
if not ok then
|
if not ok then
|
||||||
return
|
return finish()
|
||||||
elseif choice == 1 then
|
elseif choice == 1 then
|
||||||
M.save()
|
M.save()
|
||||||
return
|
return finish()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -490,7 +497,15 @@ M.select = function(opts)
|
||||||
vim.api.nvim_win_close(preview_win, true)
|
vim.api.nvim_win_close(preview_win, true)
|
||||||
end
|
end
|
||||||
local prev_win = vim.api.nvim_get_current_win()
|
local prev_win = vim.api.nvim_get_current_win()
|
||||||
for _, entry in ipairs(entries) do
|
|
||||||
|
-- Async iter over entries so we can normalize the url before opening
|
||||||
|
local i = 1
|
||||||
|
local function open_next_entry(cb)
|
||||||
|
local entry = entries[i]
|
||||||
|
i = i + 1
|
||||||
|
if not entry then
|
||||||
|
return cb()
|
||||||
|
end
|
||||||
local scheme, dir = util.parse_url(bufname)
|
local scheme, dir = util.parse_url(bufname)
|
||||||
local child = dir .. entry.name
|
local child = dir .. entry.name
|
||||||
local url = scheme .. child
|
local url = scheme .. child
|
||||||
|
|
@ -507,8 +522,7 @@ M.select = function(opts)
|
||||||
-- entry. This prevents the case of MOVE /foo -> /bar + CREATE /foo.
|
-- entry. This prevents the case of MOVE /foo -> /bar + CREATE /foo.
|
||||||
-- If you enter the new /foo, it will show the contents of the old /foo.
|
-- If you enter the new /foo, it will show the contents of the old /foo.
|
||||||
if not entry.id and cache.list_url(bufname)[entry.name] then
|
if not entry.id and cache.list_url(bufname)[entry.name] then
|
||||||
vim.notify("Please save changes before entering new directory", vim.log.levels.ERROR)
|
return cb("Please save changes before entering new directory")
|
||||||
return
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if vim.w.is_oil_win then
|
if vim.w.is_oil_win then
|
||||||
|
|
@ -516,6 +530,9 @@ M.select = function(opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Normalize the url before opening to prevent needing to rename them inside the BufReadCmd
|
||||||
|
-- Renaming buffers during opening can lead to missed autocmds
|
||||||
|
adapter.normalize_url(url, function(normalized_url)
|
||||||
local mods = {
|
local mods = {
|
||||||
vertical = opts.vertical,
|
vertical = opts.vertical,
|
||||||
horizontal = opts.horizontal,
|
horizontal = opts.horizontal,
|
||||||
|
|
@ -524,7 +541,7 @@ M.select = function(opts)
|
||||||
}
|
}
|
||||||
if opts.preview and preview_win then
|
if opts.preview and preview_win then
|
||||||
vim.api.nvim_set_current_win(preview_win)
|
vim.api.nvim_set_current_win(preview_win)
|
||||||
vim.cmd.edit({ args = { util.escape_filename(url) }, mods = mods })
|
vim.cmd.edit({ args = { util.escape_filename(normalized_url) }, mods = mods })
|
||||||
else
|
else
|
||||||
if vim.tbl_isempty(mods) then
|
if vim.tbl_isempty(mods) then
|
||||||
mods = nil
|
mods = nil
|
||||||
|
|
@ -539,7 +556,7 @@ M.select = function(opts)
|
||||||
end
|
end
|
||||||
vim.cmd({
|
vim.cmd({
|
||||||
cmd = cmd,
|
cmd = cmd,
|
||||||
args = { util.escape_filename(url) },
|
args = { util.escape_filename(normalized_url) },
|
||||||
mods = mods,
|
mods = mods,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
@ -548,8 +565,14 @@ M.select = function(opts)
|
||||||
vim.w.oil_entry_id = entry.id
|
vim.w.oil_entry_id = entry.id
|
||||||
vim.api.nvim_set_current_win(prev_win)
|
vim.api.nvim_set_current_win(prev_win)
|
||||||
end
|
end
|
||||||
|
open_next_entry(cb)
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
open_next_entry(function(err)
|
||||||
|
if err then
|
||||||
|
return finish(err)
|
||||||
|
end
|
||||||
if
|
if
|
||||||
opts.close
|
opts.close
|
||||||
and vim.api.nvim_win_is_valid(prev_win)
|
and vim.api.nvim_win_is_valid(prev_win)
|
||||||
|
|
@ -559,6 +582,8 @@ M.select = function(opts)
|
||||||
M.close()
|
M.close()
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
finish()
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ a.describe("oil select", function()
|
||||||
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
||||||
-- Go to the bottom, so the cursor is not on a directory
|
-- Go to the bottom, so the cursor is not on a directory
|
||||||
vim.cmd.normal({ args = { "G" } })
|
vim.cmd.normal({ args = { "G" } })
|
||||||
oil.select()
|
a.wrap(oil.select, 2)()
|
||||||
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
|
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
|
||||||
assert.not_equals("oil", vim.bo.filetype)
|
assert.not_equals("oil", vim.bo.filetype)
|
||||||
end)
|
end)
|
||||||
|
|
@ -21,7 +21,7 @@ a.describe("oil select", function()
|
||||||
oil.open()
|
oil.open()
|
||||||
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
||||||
local tabpage = vim.api.nvim_get_current_tabpage()
|
local tabpage = vim.api.nvim_get_current_tabpage()
|
||||||
oil.select({ tab = true })
|
a.wrap(oil.select, 2)({ tab = true })
|
||||||
assert.equals(2, #vim.api.nvim_list_tabpages())
|
assert.equals(2, #vim.api.nvim_list_tabpages())
|
||||||
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
|
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
|
||||||
assert.not_equals(tabpage, vim.api.nvim_get_current_tabpage())
|
assert.not_equals(tabpage, vim.api.nvim_get_current_tabpage())
|
||||||
|
|
@ -31,7 +31,7 @@ a.describe("oil select", function()
|
||||||
oil.open()
|
oil.open()
|
||||||
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
||||||
local winid = vim.api.nvim_get_current_win()
|
local winid = vim.api.nvim_get_current_win()
|
||||||
oil.select({ vertical = true })
|
a.wrap(oil.select, 2)({ vertical = true })
|
||||||
assert.equals(1, #vim.api.nvim_list_tabpages())
|
assert.equals(1, #vim.api.nvim_list_tabpages())
|
||||||
assert.equals(2, #vim.api.nvim_tabpage_list_wins(0))
|
assert.equals(2, #vim.api.nvim_tabpage_list_wins(0))
|
||||||
assert.not_equals(winid, vim.api.nvim_get_current_win())
|
assert.not_equals(winid, vim.api.nvim_get_current_win())
|
||||||
|
|
@ -42,7 +42,7 @@ a.describe("oil select", function()
|
||||||
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
||||||
vim.api.nvim_feedkeys("Vj", "x", true)
|
vim.api.nvim_feedkeys("Vj", "x", true)
|
||||||
local tabpage = vim.api.nvim_get_current_tabpage()
|
local tabpage = vim.api.nvim_get_current_tabpage()
|
||||||
oil.select({ tab = true })
|
a.wrap(oil.select, 2)({ tab = true })
|
||||||
assert.equals(3, #vim.api.nvim_list_tabpages())
|
assert.equals(3, #vim.api.nvim_list_tabpages())
|
||||||
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
|
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
|
||||||
assert.not_equals(tabpage, vim.api.nvim_get_current_tabpage())
|
assert.not_equals(tabpage, vim.api.nvim_get_current_tabpage())
|
||||||
|
|
@ -53,7 +53,7 @@ a.describe("oil select", function()
|
||||||
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
||||||
vim.api.nvim_feedkeys("Vj", "x", true)
|
vim.api.nvim_feedkeys("Vj", "x", true)
|
||||||
local winid = vim.api.nvim_get_current_win()
|
local winid = vim.api.nvim_get_current_win()
|
||||||
oil.select({ vertical = true })
|
a.wrap(oil.select, 2)({ vertical = true })
|
||||||
assert.equals(1, #vim.api.nvim_list_tabpages())
|
assert.equals(1, #vim.api.nvim_list_tabpages())
|
||||||
assert.equals(3, #vim.api.nvim_tabpage_list_wins(0))
|
assert.equals(3, #vim.api.nvim_tabpage_list_wins(0))
|
||||||
assert.not_equals(winid, vim.api.nvim_get_current_win())
|
assert.not_equals(winid, vim.api.nvim_get_current_win())
|
||||||
|
|
@ -67,7 +67,7 @@ a.describe("oil select", function()
|
||||||
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
||||||
-- Go to the bottom, so the cursor is not on a directory
|
-- Go to the bottom, so the cursor is not on a directory
|
||||||
vim.cmd.normal({ args = { "G" } })
|
vim.cmd.normal({ args = { "G" } })
|
||||||
oil.select({ close = true })
|
a.wrap(oil.select, 2)({ close = true })
|
||||||
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
|
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
|
||||||
-- This one we actually don't expect the buffer to be the same as the initial buffer, because
|
-- This one we actually don't expect the buffer to be the same as the initial buffer, because
|
||||||
-- we opened a file
|
-- we opened a file
|
||||||
|
|
@ -81,7 +81,7 @@ a.describe("oil select", function()
|
||||||
local winid = vim.api.nvim_get_current_win()
|
local winid = vim.api.nvim_get_current_win()
|
||||||
oil.open()
|
oil.open()
|
||||||
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
||||||
oil.select({ vertical = true, close = true })
|
a.wrap(oil.select, 2)({ vertical = true, close = true })
|
||||||
assert.equals(2, #vim.api.nvim_tabpage_list_wins(0))
|
assert.equals(2, #vim.api.nvim_tabpage_list_wins(0))
|
||||||
assert.equals(bufnr, vim.api.nvim_win_get_buf(winid))
|
assert.equals(bufnr, vim.api.nvim_win_get_buf(winid))
|
||||||
end)
|
end)
|
||||||
|
|
@ -92,7 +92,7 @@ a.describe("oil select", function()
|
||||||
local tabpage = vim.api.nvim_get_current_tabpage()
|
local tabpage = vim.api.nvim_get_current_tabpage()
|
||||||
oil.open()
|
oil.open()
|
||||||
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
|
||||||
oil.select({ tab = true, close = true })
|
a.wrap(oil.select, 2)({ tab = true, close = true })
|
||||||
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
|
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
|
||||||
assert.equals(2, #vim.api.nvim_list_tabpages())
|
assert.equals(2, #vim.api.nvim_list_tabpages())
|
||||||
vim.api.nvim_set_current_tabpage(tabpage)
|
vim.api.nvim_set_current_tabpage(tabpage)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue