diff --git a/lua/oil/view.lua b/lua/oil/view.lua index d2cd1a4e..56993bd0 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -222,6 +222,10 @@ M.initialize = function(bufnr) if not vim.api.nvim_buf_is_valid(bufnr) then return end + vim.api.nvim_clear_autocmds({ + buffer = bufnr, + group = "Oil", + }) vim.bo[bufnr].buftype = "acwrite" vim.bo[bufnr].syntax = "oil" vim.bo[bufnr].filetype = "oil" @@ -231,10 +235,6 @@ M.initialize = function(bufnr) vim.api.nvim_buf_set_option(bufnr, k, v) end M.set_win_options() - vim.api.nvim_clear_autocmds({ - buffer = bufnr, - group = "Oil", - }) vim.api.nvim_create_autocmd("BufHidden", { desc = "Delete oil buffers when no longer in use", group = "Oil", diff --git a/tests/regression_spec.lua b/tests/regression_spec.lua index 9ee1cb0e..380a3eef 100644 --- a/tests/regression_spec.lua +++ b/tests/regression_spec.lua @@ -105,4 +105,13 @@ a.describe("regression tests", function() -- The first oil buffer should not be modified anymore assert.falsy(vim.bo[first_dir].modified) end) + + a.it("refreshing buffer doesn't lose track of it", function() + vim.cmd.edit({ args = { "." } }) + test_util.wait_for_autocmd("BufReadPost") + local bufnr = vim.api.nvim_get_current_buf() + vim.cmd.edit({ bang = true }) + test_util.wait_for_autocmd("BufReadPost") + assert.are.same({ bufnr }, require("oil.view").get_all_buffers()) + end) end)