fix: unexpected behavior from BufReadPost autocmds

This commit is contained in:
Steven Arcangeli 2023-01-28 18:07:18 -08:00
parent 20e4ff1838
commit 716dd8f9cf
2 changed files with 47 additions and 16 deletions

View file

@ -587,7 +587,13 @@ 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)
-- Since this was async, we may have left the window with this buffer. People often write
-- BufReadPre/Post autocmds with the expectation that the current window is the one that
-- contains the buffer. Let's then do our best to make sure that that assumption isn't violated.
winid = util.buf_get_win(bufnr, winid) or vim.api.nvim_get_current_win()
vim.api.nvim_win_call(winid, function()
if new_url ~= bufname then if new_url ~= bufname then
if util.rename_buffer(bufnr, new_url) 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 -- If the buffer was replaced then don't initialize it. It's dead. The replacement will
@ -605,6 +611,7 @@ local function load_oil_buffer(bufnr)
adapter.read_file(bufnr) adapter.read_file(bufnr)
end end
restore_alt_buf() restore_alt_buf()
end)
end end
adapter.normalize_url(bufname, finish) adapter.normalize_url(bufname, finish)

View file

@ -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