Skip to content

Commit

Permalink
fix: oil.close doesn't error when no other buffers exist (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Mar 20, 2023
1 parent 08c4b71 commit 4b05ebd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ M.close = function()
-- 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()
ok = pcall(vim.cmd.bprev)
if not ok then
-- If `bprev` failed, there are no buffers open so we should create a new one with enew
vim.cmd.enew()
end
vim.api.nvim_buf_delete(oilbuf, { force = true })
end

Expand Down
9 changes: 9 additions & 0 deletions tests/regression_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,13 @@ a.describe("regression tests", function()
assert.equals(winid, vim.api.nvim_get_current_win())
assert.equals(bufnr, vim.api.nvim_get_current_buf())
end)

-- https://github.com/stevearc/oil.nvim/issues/79
a.it("Returns to empty buffer on close", function()
oil.open()
test_util.wait_for_autocmd("BufReadPost")
oil.close()
assert.not_equals("oil", vim.bo.filetype)
assert.equals("", vim.api.nvim_buf_get_name(0))
end)
end)

0 comments on commit 4b05ebd

Please sign in to comment.