From 20a4b1bc5eb4e84d7c49455a34516b7369761882 Mon Sep 17 00:00:00 2001 From: Barrett Ruth <62671086+barrettruth@users.noreply.github.com> Date: Fri, 10 Jul 2026 10:52:24 -0500 Subject: [PATCH] fix(dir): user/plugin can override default "-" mapping #40676 --- runtime/lua/vim/_core/defaults.lua | 7 ------- runtime/plugin/dir.lua | 4 ++++ test/functional/plugin/dir_spec.lua | 20 ++++++++++++++------ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/runtime/lua/vim/_core/defaults.lua b/runtime/lua/vim/_core/defaults.lua index 2916511d39..b1e705411b 100644 --- a/runtime/lua/vim/_core/defaults.lua +++ b/runtime/lua/vim/_core/defaults.lua @@ -129,13 +129,6 @@ do --- See |&-default| vim.keymap.set('n', '&', ':&&', { desc = ':help &-default' }) - vim.keymap.set('n', '-', function() - if vim.fn.maparg('(nvim-dir-up)', 'n') ~= '' then - return '(nvim-dir-up)' - end - return (vim.v.count == 0 and '' or vim.v.count) .. '-' - end, { expr = true, silent = true, desc = 'Open parent directory' }) - --- Use Q in Visual mode to execute a macro on each line of the selection. #21422 --- This only make sense in linewise Visual mode. #28287 --- diff --git a/runtime/plugin/dir.lua b/runtime/plugin/dir.lua index 022da9c7d3..85f3b9e6a1 100644 --- a/runtime/plugin/dir.lua +++ b/runtime/plugin/dir.lua @@ -18,6 +18,10 @@ vim.keymap.set('n', '(nvim-dir-reload)', function() require('nvim.dir')._reload() end, { silent = true, desc = 'Reload directory' }) +if vim.fn.mapcheck('-', 'n') == '' and vim.fn.hasmapto('(nvim-dir-up)', 'n') == 0 then + vim.keymap.set('n', '-', '(nvim-dir-up)', { silent = true, desc = 'Open parent directory' }) +end + ---@param buf integer ---@param path string ---@return boolean diff --git a/test/functional/plugin/dir_spec.lua b/test/functional/plugin/dir_spec.lua index fa4819b066..68ee235c0a 100644 --- a/test/functional/plugin/dir_spec.lua +++ b/test/functional/plugin/dir_spec.lua @@ -157,14 +157,22 @@ describe('nvim.dir', function() -- Ensure the cursor stays on the entry we navigated up from. eq('alpha.txt', api.nvim_get_current_line()) + end) - n.clear({ args_rm = { '--cmd' }, args = { '--noplugin' } }) - api.nvim_buf_set_lines(0, 0, -1, false, { ' alpha', ' beta' }) - api.nvim_win_set_cursor(0, { 2, 7 }) - feed('-') + it('lets startup plugins replace the - mapping', function() + local config_dir = fn.stdpath('config') + local plugin_dir = vim.fs.joinpath(config_dir, 'plugin') + fn.mkdir(plugin_dir, 'p') + fn.writefile({ + [[if vim.fn.mapcheck('-', 'n') == '' and vim.fn.hasmapto('(dirvish_up)', 'n') == 0 then]], + [[ vim.keymap.set('n', '-', '(dirvish_up)')]], + [[end]], + }, vim.fs.joinpath(plugin_dir, 'dirvish.lua')) - eq({ 1, 2 }, api.nvim_win_get_cursor(0)) - eq(false, exec_lua([[return package.loaded['nvim.dir'] ~= nil]])) + n.clear({ args_rm = { '-u', '--cmd' } }) + + eq('(dirvish_up)', fn.mapcheck('-', 'n')) + n.rmdir(config_dir) end) it('preserves alternate buffer when opening a parent directory', function()