feat(dir): set buffer-local CWD #41174

This commit is contained in:
Barrett Ruth
2026-08-06 02:39:53 -05:00
committed by GitHub
parent 54373e2453
commit 8b0f33a1ab
3 changed files with 19 additions and 2 deletions

View File

@@ -76,6 +76,10 @@ The listing is read-only and does not modify the filesystem.
Opening an entry leaves the |alternate-file| on the buffer you came from, so
that |CTRL-^| returns to it rather than to the listing.
The listed directory is the buffer's directory (|:bcd|), so entry names resolve
without a prefix for |gf|, |:!|, and friends. Being buffer-local it is not
"sticky": it does not follow you out of the listing.
Directory buffers follow the global 'hidden' option by default. To delete them
after use: >vim
autocmd FileType directory setlocal bufhidden=delete

View File

@@ -97,10 +97,18 @@ function M.open_parent(_, path)
end
---@param buf integer
function M.init(buf)
---@param path string
function M.init(buf, path)
if api.nvim_get_option_value('filetype', { buf = buf }) ~= 'directory' then
api.nvim_set_option_value('filetype', 'directory', { buf = buf })
end
api.nvim_buf_call(buf, function()
pcall(api.nvim_cmd, {
cmd = 'bcd',
args = { path },
magic = { file = false, bar = false },
})
end)
end
return M

View File

@@ -45,7 +45,7 @@ end
local function assert_directory(path)
local ffname = path:sub(-1) == '/' and path or path .. '/'
eq(ffname, api.nvim_buf_get_name(0))
eq(path, vim.fs.normalize(fn.bufname('%')))
eq(path, vim.fs.normalize(fn.fnamemodify(fn.bufname('%'), ':p')))
eq('directory', bufopt('filetype'))
eq(true, bufopt('buflisted'))
end
@@ -576,19 +576,24 @@ describe('nvim.dir', function()
it('navigates entries and refreshes the listing', function()
make_fixture()
n.clear({ args = { '--clean' } })
local cwd = fn.getcwd()
edit(root)
assert_directory(root)
eq(vim.uv.fs_realpath(root), vim.uv.fs_realpath(fn.getcwd()))
eq('alpha.txt', fn.findfile('alpha.txt'))
api.nvim_win_set_cursor(0, { line_of('alpha.txt'), 0 })
feed('<CR>')
poke_eventloop()
eq(file, api.nvim_buf_get_name(0))
eq({ 'alpha' }, lines())
eq(cwd, fn.getcwd())
edit(subdir)
assert_directory(subdir)
eq({ '' }, lines())
eq(vim.uv.fs_realpath(subdir), vim.uv.fs_realpath(fn.getcwd()))
feed('-')
poke_eventloop()