Skip to content

Commit

Permalink
fix: unexpected behavior from BufReadPost autocmds
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Jan 29, 2023
1 parent 20e4ff1 commit 716dd8f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
39 changes: 23 additions & 16 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -587,24 +587,31 @@ local function load_oil_buffer(bufnr)
keymap_util.set_keymaps("", config.keymaps, bufnr)
end
loading.set_loading(bufnr, true)
local winid = vim.api.nvim_get_current_win()
local function finish(new_url)
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
-- 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 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
bufname = new_url
end
if vim.endswith(bufname, "/") then
vim.cmd.doautocmd({ args = { "BufReadPre", bufname }, mods = { emsg_silent = true } })
view.initialize(bufnr)
vim.cmd.doautocmd({ args = { "BufReadPost", bufname }, mods = { emsg_silent = true } })
else
vim.bo[bufnr].buftype = "acwrite"
adapter.read_file(bufnr)
end
restore_alt_buf()
if vim.endswith(bufname, "/") then
vim.cmd.doautocmd({ args = { "BufReadPre", bufname }, mods = { emsg_silent = true } })
view.initialize(bufnr)
vim.cmd.doautocmd({ args = { "BufReadPost", bufname }, mods = { emsg_silent = true } })
else
vim.bo[bufnr].buftype = "acwrite"
adapter.read_file(bufnr)
end
restore_alt_buf()
end)
end

adapter.normalize_url(bufname, finish)
Expand Down
24 changes: 24 additions & 0 deletions lua/oil/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,28 @@ M.get_preview_win = function()
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

0 comments on commit 716dd8f

Please sign in to comment.