fix: oil.close() sometimes closes window too (#64)

This commit is contained in:
Steven Arcangeli 2023-03-16 08:53:51 -07:00
parent 383971b0cf
commit d48fa09c82
2 changed files with 23 additions and 3 deletions

View file

@ -313,7 +313,12 @@ M.close = function()
end
return
end
vim.api.nvim_buf_delete(0, { force = true })
-- Deleting the buffer closes all windows with that buffer open, so navigate to a different
-- buffer first
local oilbuf = vim.api.nvim_get_current_buf()
vim.cmd.bprev()
vim.api.nvim_buf_delete(oilbuf, { force = true })
end
---Select the entry under the cursor
@ -697,7 +702,7 @@ M.setup = function(opts)
end
end,
})
vim.api.nvim_create_autocmd("BufWinLeave", {
vim.api.nvim_create_autocmd("BufLeave", {
desc = "Save alternate buffer for later",
group = aug,
pattern = "*",
@ -710,7 +715,7 @@ M.setup = function(opts)
end
end,
})
vim.api.nvim_create_autocmd("BufWinEnter", {
vim.api.nvim_create_autocmd("BufEnter", {
desc = "Set/unset oil window options and restore alternate buffer",
group = aug,
pattern = "*",

View file

@ -47,4 +47,19 @@ a.describe("regression tests", function()
a.util.sleep(10)
assert.equals(winid, vim.api.nvim_get_current_win())
end)
-- https://github.com/stevearc/oil.nvim/issues/64
a.it("doesn't close splits on oil.close", function()
vim.cmd.edit({ args = { "README.md" } })
vim.cmd.vsplit()
local winid = vim.api.nvim_get_current_win()
local bufnr = vim.api.nvim_get_current_buf()
oil.open()
a.util.sleep(10)
oil.close()
a.util.sleep(10)
assert.equals(2, #vim.api.nvim_tabpage_list_wins(0))
assert.equals(winid, vim.api.nvim_get_current_win())
assert.equals(bufnr, vim.api.nvim_get_current_buf())
end)
end)