fix(dir): respect directory buffer lifetime #40453

Problem:
dir.lua leaves previously-navigated directory buffers around.
This is fine by default, but users need a simple way to opt out.

Solution:
1. Respect `set hidden` (via `'bufhidden'`) as one way to make
   previously-navigated dir buffers from showing up.
2. Document a one-liner to hide these buffers
This commit is contained in:
Barrett Ruth
2026-06-27 13:08:28 -05:00
committed by GitHub
parent b138e7a251
commit 9202e2c80c
4 changed files with 47 additions and 2 deletions

View File

@@ -215,6 +215,46 @@ describe('nvim.dir', function()
line_of('beta.txt')
end)
it("follows global 'hidden' when abandoned", function()
make_fixture()
n.clear({ args_rm = { '-u' } })
command('set hidden')
edit(root)
local root_buf = api.nvim_get_current_buf()
eq('', bufopt('bufhidden'))
edit(subdir)
eq(true, api.nvim_buf_is_loaded(root_buf))
eq(1, fn.getbufinfo(root_buf)[1].hidden)
n.clear({ args_rm = { '-u' } })
command('set nohidden')
edit(root)
root_buf = api.nvim_get_current_buf()
eq('', bufopt('bufhidden'))
edit(subdir)
eq(false, api.nvim_buf_is_loaded(root_buf))
end)
it("preserves custom 'bufhidden' when reloading", function()
make_fixture()
n.clear({
args_rm = { '-u' },
args = { '--cmd', [[autocmd FileType directory ++once setlocal bufhidden=delete]] },
})
edit(root)
eq('delete', bufopt('bufhidden'))
feed('R')
poke_eventloop()
eq('delete', bufopt('bufhidden'))
end)
it('reports an error and keeps the buffer when reloading a removed directory', function()
make_fixture()
n.clear({ args_rm = { '-u' } })