fix: unexpected behavior from BufReadPost autocmds
This commit is contained in:
parent
20e4ff1838
commit
716dd8f9cf
2 changed files with 47 additions and 16 deletions
|
|
@ -587,24 +587,31 @@ local function load_oil_buffer(bufnr)
|
||||||
keymap_util.set_keymaps("", config.keymaps, bufnr)
|
keymap_util.set_keymaps("", config.keymaps, bufnr)
|
||||||
end
|
end
|
||||||
loading.set_loading(bufnr, true)
|
loading.set_loading(bufnr, true)
|
||||||
|
local winid = vim.api.nvim_get_current_win()
|
||||||
local function finish(new_url)
|
local function finish(new_url)
|
||||||
if new_url ~= bufname then
|
-- Since this was async, we may have left the window with this buffer. People often write
|
||||||
if util.rename_buffer(bufnr, new_url) then
|
-- BufReadPre/Post autocmds with the expectation that the current window is the one that
|
||||||
-- If the buffer was replaced then don't initialize it. It's dead. The replacement will
|
-- contains the buffer. Let's then do our best to make sure that that assumption isn't violated.
|
||||||
-- have BufReadCmd called for it
|
winid = util.buf_get_win(bufnr, winid) or vim.api.nvim_get_current_win()
|
||||||
return
|
vim.api.nvim_win_call(winid, function()
|
||||||
|
if new_url ~= bufname then
|
||||||
|
if util.rename_buffer(bufnr, new_url) then
|
||||||
|
-- If the buffer was replaced then don't initialize it. It's dead. The replacement will
|
||||||
|
-- have BufReadCmd called for it
|
||||||
|
return
|
||||||
|
end
|
||||||
|
bufname = new_url
|
||||||
end
|
end
|
||||||
bufname = new_url
|
if vim.endswith(bufname, "/") then
|
||||||
end
|
vim.cmd.doautocmd({ args = { "BufReadPre", bufname }, mods = { emsg_silent = true } })
|
||||||
if vim.endswith(bufname, "/") then
|
view.initialize(bufnr)
|
||||||
vim.cmd.doautocmd({ args = { "BufReadPre", bufname }, mods = { emsg_silent = true } })
|
vim.cmd.doautocmd({ args = { "BufReadPost", bufname }, mods = { emsg_silent = true } })
|
||||||
view.initialize(bufnr)
|
else
|
||||||
vim.cmd.doautocmd({ args = { "BufReadPost", bufname }, mods = { emsg_silent = true } })
|
vim.bo[bufnr].buftype = "acwrite"
|
||||||
else
|
adapter.read_file(bufnr)
|
||||||
vim.bo[bufnr].buftype = "acwrite"
|
end
|
||||||
adapter.read_file(bufnr)
|
restore_alt_buf()
|
||||||
end
|
end)
|
||||||
restore_alt_buf()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
adapter.normalize_url(bufname, finish)
|
adapter.normalize_url(bufname, finish)
|
||||||
|
|
|
||||||
|
|
@ -525,4 +525,28 @@ M.get_preview_win = function()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param bufnr integer
|
||||||
|
---@param preferred_win nil|integer
|
||||||
|
---@return nil|integer
|
||||||
|
M.buf_get_win = function(bufnr, preferred_win)
|
||||||
|
if
|
||||||
|
preferred_win
|
||||||
|
and vim.api.nvim_win_is_valid(preferred_win)
|
||||||
|
and vim.api.nvim_win_get_buf(preferred_win) == bufnr
|
||||||
|
then
|
||||||
|
return preferred_win
|
||||||
|
end
|
||||||
|
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
|
||||||
|
if vim.api.nvim_win_is_valid(winid) and vim.api.nvim_win_get_buf(winid) == bufnr then
|
||||||
|
return winid
|
||||||
|
end
|
||||||
|
end
|
||||||
|
for _, winid in ipairs(vim.api.nvim_list_wins()) do
|
||||||
|
if vim.api.nvim_win_is_valid(winid) and vim.api.nvim_win_get_buf(winid) == bufnr then
|
||||||
|
return winid
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue