diff --git a/runtime/lua/nvim/dir.lua b/runtime/lua/nvim/dir.lua index cdc8713c77..2a36f6cab2 100644 --- a/runtime/lua/nvim/dir.lua +++ b/runtime/lua/nvim/dir.lua @@ -85,7 +85,14 @@ local function render(buf, dir) then return false end - api.nvim_buf_set_name(buf, dir) + api.nvim_buf_call(buf, function() + api.nvim_cmd({ + cmd = 'file', + args = { dir }, + mods = { keepalt = true }, + magic = { file = false, bar = false }, + }, {}) + end) if not api.nvim_buf_is_valid(buf) then return false end diff --git a/test/functional/plugin/dir_spec.lua b/test/functional/plugin/dir_spec.lua index 04dcd7616f..f9c96aa0bf 100644 --- a/test/functional/plugin/dir_spec.lua +++ b/test/functional/plugin/dir_spec.lua @@ -164,6 +164,19 @@ describe('nvim.dir', function() eq(false, exec_lua([[return package.loaded['nvim.dir'] ~= nil]])) end) + it('preserves alternate buffer when opening a parent directory', function() + make_fixture() + n.clear({ args_rm = { '-u', '--cmd' } }) + + edit(file) + feed('-') + poke_eventloop() + + assert_directory(root) + -- Keep the alternate buffer on the file we navigated up from. + eq(file, api.nvim_buf_get_name(fn.bufnr('#'))) + end) + it('uses an absolute buffer name for a relative startup directory argument', function() make_fixture() local cwd = assert(vim.uv.cwd())