diff options
author | Gregory Anders <greg@gpanders.com> | 2024-10-29 10:06:14 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-29 10:06:14 -0500 |
commit | ff93cccbc1ce4bb217d503e2ac5f81f9023d4d43 (patch) | |
tree | 0fd744fe01c5a780625e765174f217d82e9bad0f | |
parent | 4c7f5032af6baca61764cde5122765b2ec684e4a (diff) | |
download | rneovim-ff93cccbc1ce4bb217d503e2ac5f81f9023d4d43.tar.gz rneovim-ff93cccbc1ce4bb217d503e2ac5f81f9023d4d43.tar.bz2 rneovim-ff93cccbc1ce4bb217d503e2ac5f81f9023d4d43.zip |
fix(defaults): omit extraneous info from unimpaired mapping errors (#30983)
-rw-r--r-- | runtime/lua/vim/_defaults.lua | 204 | ||||
-rw-r--r-- | test/functional/editor/defaults_spec.lua | 61 |
2 files changed, 170 insertions, 95 deletions
diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 03b2803f3e..d3b7bda871 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -219,132 +219,150 @@ do --- vim-unimpaired style mappings. See: https://github.com/tpope/vim-unimpaired do - ---@param lhs string - ---@param rhs function - ---@param desc string - local function create_unimpaired_mapping(lhs, rhs, desc) - vim.keymap.set('n', lhs, function() - local _, err = pcall(rhs) ---@type any, string - if err then - vim.api.nvim_err_writeln(err) - end - end, { desc = desc }) + --- Execute a command and print errors without a stacktrace. + --- @param opts table Arguments to |nvim_cmd()| + local function cmd(opts) + local _, err = pcall(vim.api.nvim_cmd, opts, {}) + if err then + vim.api.nvim_err_writeln(err:sub(#'Vim:' + 1)) + end end -- Quickfix mappings - create_unimpaired_mapping('[q', function() - vim.cmd.cprevious({ count = vim.v.count1 }) - end, ':cprevious') - create_unimpaired_mapping(']q', function() - vim.cmd.cnext({ count = vim.v.count1 }) - end, ':cnext') - create_unimpaired_mapping('[Q', function() - vim.cmd.crewind({ count = vim.v.count ~= 0 and vim.v.count or nil }) - end, ':crewind') - create_unimpaired_mapping(']Q', function() - vim.cmd.clast({ count = vim.v.count ~= 0 and vim.v.count or nil }) - end, ':clast') - create_unimpaired_mapping('[<C-Q>', function() - vim.cmd.cpfile({ count = vim.v.count1 }) - end, ':cpfile') - create_unimpaired_mapping(']<C-Q>', function() - vim.cmd.cnfile({ count = vim.v.count1 }) - end, ':cnfile') + vim.keymap.set('n', '[q', function() + cmd({ cmd = 'cprevious', count = vim.v.count1 }) + end, { desc = ':cprevious' }) + + vim.keymap.set('n', ']q', function() + cmd({ cmd = 'cnext', count = vim.v.count1 }) + end, { desc = ':cnext' }) + + vim.keymap.set('n', '[Q', function() + cmd({ cmd = 'crewind', count = vim.v.count ~= 0 and vim.v.count or nil }) + end, { desc = ':crewind' }) + + vim.keymap.set('n', ']Q', function() + cmd({ cmd = 'clast', count = vim.v.count ~= 0 and vim.v.count or nil }) + end, { desc = ':clast' }) + + vim.keymap.set('n', '[<C-Q>', function() + cmd({ cmd = 'cpfile', count = vim.v.count1 }) + end, { desc = ':cpfile' }) + + vim.keymap.set('n', ']<C-Q>', function() + cmd({ cmd = 'cnfile', count = vim.v.count1 }) + end, { desc = ':cnfile' }) -- Location list mappings - create_unimpaired_mapping('[l', function() - vim.cmd.lprevious({ count = vim.v.count1 }) - end, ':lprevious') - create_unimpaired_mapping(']l', function() - vim.cmd.lnext({ count = vim.v.count1 }) - end, ':lnext') - create_unimpaired_mapping('[L', function() - vim.cmd.lrewind({ count = vim.v.count ~= 0 and vim.v.count or nil }) - end, ':lrewind') - create_unimpaired_mapping(']L', function() - vim.cmd.llast({ count = vim.v.count ~= 0 and vim.v.count or nil }) - end, ':llast') - create_unimpaired_mapping('[<C-L>', function() - vim.cmd.lpfile({ count = vim.v.count1 }) - end, ':lpfile') - create_unimpaired_mapping(']<C-L>', function() - vim.cmd.lnfile({ count = vim.v.count1 }) - end, ':lnfile') + vim.keymap.set('n', '[l', function() + cmd({ cmd = 'lprevious', count = vim.v.count1 }) + end, { desc = ':lprevious' }) + + vim.keymap.set('n', ']l', function() + cmd({ cmd = 'lnext', count = vim.v.count1 }) + end, { desc = ':lnext' }) + + vim.keymap.set('n', '[L', function() + cmd({ cmd = 'lrewind', count = vim.v.count ~= 0 and vim.v.count or nil }) + end, { desc = ':lrewind' }) + + vim.keymap.set('n', ']L', function() + cmd({ cmd = 'llast', count = vim.v.count ~= 0 and vim.v.count or nil }) + end, { desc = ':llast' }) + + vim.keymap.set('n', '[<C-L>', function() + cmd({ cmd = 'lpfile', count = vim.v.count1 }) + end, { desc = ':lpfile' }) + + vim.keymap.set('n', ']<C-L>', function() + cmd({ cmd = 'lnfile', count = vim.v.count1 }) + end, { desc = ':lnfile' }) -- Argument list - create_unimpaired_mapping('[a', function() - vim.cmd.previous({ count = vim.v.count1 }) - end, ':previous') - create_unimpaired_mapping(']a', function() + vim.keymap.set('n', '[a', function() + cmd({ cmd = 'previous', count = vim.v.count1 }) + end, { desc = ':previous' }) + + vim.keymap.set('n', ']a', function() -- count doesn't work with :next, must use range. See #30641. - vim.cmd.next({ range = { vim.v.count1 } }) - end, ':next') - create_unimpaired_mapping('[A', function() + cmd({ cmd = 'next', range = { vim.v.count1 } }) + end, { desc = ':next' }) + + vim.keymap.set('n', '[A', function() if vim.v.count ~= 0 then - vim.cmd.argument({ count = vim.v.count }) + cmd({ cmd = 'argument', count = vim.v.count }) else - vim.cmd.rewind() + cmd({ cmd = 'rewind' }) end - end, ':rewind') - create_unimpaired_mapping(']A', function() + end, { desc = ':rewind' }) + + vim.keymap.set('n', ']A', function() if vim.v.count ~= 0 then - vim.cmd.argument({ count = vim.v.count }) + cmd({ cmd = 'argument', count = vim.v.count }) else - vim.cmd.last() + cmd({ cmd = 'last' }) end - end, ':last') + end, { desc = ':last' }) -- Tags - create_unimpaired_mapping('[t', function() + vim.keymap.set('n', '[t', function() -- count doesn't work with :tprevious, must use range. See #30641. - vim.cmd.tprevious({ range = { vim.v.count1 } }) - end, ':tprevious') - create_unimpaired_mapping(']t', function() + cmd({ cmd = 'tprevious', range = { vim.v.count1 } }) + end, { desc = ':tprevious' }) + + vim.keymap.set('n', ']t', function() -- count doesn't work with :tnext, must use range. See #30641. - vim.cmd.tnext({ range = { vim.v.count1 } }) - end, ':tnext') - create_unimpaired_mapping('[T', function() + cmd({ cmd = 'tnext', range = { vim.v.count1 } }) + end, { desc = ':tnext' }) + + vim.keymap.set('n', '[T', function() -- count doesn't work with :trewind, must use range. See #30641. - vim.cmd.trewind({ range = vim.v.count ~= 0 and { vim.v.count } or nil }) - end, ':trewind') - create_unimpaired_mapping(']T', function() + cmd({ cmd = 'trewind', range = vim.v.count ~= 0 and { vim.v.count } or nil }) + end, { desc = ':trewind' }) + + vim.keymap.set('n', ']T', function() -- :tlast does not accept a count, so use :trewind if count given if vim.v.count ~= 0 then - vim.cmd.trewind({ range = { vim.v.count } }) + cmd({ cmd = 'trewind', range = { vim.v.count } }) else - vim.cmd.tlast() + cmd({ cmd = 'tlast' }) end - end, ':tlast') - create_unimpaired_mapping('[<C-T>', function() + end, { desc = ':tlast' }) + + vim.keymap.set('n', '[<C-T>', function() -- count doesn't work with :ptprevious, must use range. See #30641. - vim.cmd.ptprevious({ range = { vim.v.count1 } }) - end, ' :ptprevious') - create_unimpaired_mapping(']<C-T>', function() + cmd({ cmd = 'ptprevious', range = { vim.v.count1 } }) + end, { desc = ' :ptprevious' }) + + vim.keymap.set('n', ']<C-T>', function() -- count doesn't work with :ptnext, must use range. See #30641. - vim.cmd.ptnext({ range = { vim.v.count1 } }) - end, ':ptnext') + cmd({ cmd = 'ptnext', range = { vim.v.count1 } }) + end, { desc = ':ptnext' }) -- Buffers - create_unimpaired_mapping('[b', function() - vim.cmd.bprevious({ count = vim.v.count1 }) - end, ':bprevious') - create_unimpaired_mapping(']b', function() - vim.cmd.bnext({ count = vim.v.count1 }) - end, ':bnext') - create_unimpaired_mapping('[B', function() + vim.keymap.set('n', '[b', function() + cmd({ cmd = 'bprevious', count = vim.v.count1 }) + end, { desc = ':bprevious' }) + + vim.keymap.set('n', ']b', function() + cmd({ cmd = 'bnext', count = vim.v.count1 }) + end, { desc = ':bnext' }) + + vim.keymap.set('n', '[B', function() if vim.v.count ~= 0 then - vim.cmd.buffer({ count = vim.v.count }) + cmd({ cmd = 'buffer', count = vim.v.count }) else - vim.cmd.brewind() + cmd({ cmd = 'brewind' }) end - end, ':brewind') - create_unimpaired_mapping(']B', function() + end, { desc = ':brewind' }) + + vim.keymap.set('n', ']B', function() if vim.v.count ~= 0 then - vim.cmd.buffer({ count = vim.v.count }) + cmd({ cmd = 'buffer', count = vim.v.count }) else - vim.cmd.blast() + cmd({ cmd = 'blast' }) end - end, ':blast') + end, { desc = ':blast' }) end end diff --git a/test/functional/editor/defaults_spec.lua b/test/functional/editor/defaults_spec.lua index 47fd177f7b..70f12ab475 100644 --- a/test/functional/editor/defaults_spec.lua +++ b/test/functional/editor/defaults_spec.lua @@ -95,6 +95,63 @@ describe('default', function() end) end) - -- describe('key mappings', function() - -- end) + describe('key mappings', function() + describe('unimpaired-style mappings', function() + it('do not show a full stack trace #30625', function() + n.clear({ args_rm = { '--cmd' } }) + local screen = Screen.new(40, 8) + screen:attach() + screen:set_default_attr_ids({ + [1] = { foreground = Screen.colors.NvimDarkGray4 }, + [2] = { + background = Screen.colors.NvimLightGrey3, + foreground = Screen.colors.NvimDarkGray3, + }, + [3] = { foreground = Screen.colors.NvimLightRed }, + [4] = { foreground = Screen.colors.NvimLightCyan }, + }) + + n.feed('[a') + screen:expect({ + grid = [[ + | + {1:~ }|*4 + {2: }| + {3:E163: There is only one file to edit} | + {4:Press ENTER or type command to continue}^ | + ]], + }) + + n.feed('[q') + screen:expect({ + grid = [[ + ^ | + {1:~ }|*5 + {2:[No Name] 0,0-1 All}| + {3:E42: No Errors} | + ]], + }) + + n.feed('[l') + screen:expect({ + grid = [[ + ^ | + {1:~ }|*5 + {2:[No Name] 0,0-1 All}| + {3:E776: No location list} | + ]], + }) + + n.feed('[t') + screen:expect({ + grid = [[ + ^ | + {1:~ }|*5 + {2:[No Name] 0,0-1 All}| + {3:E73: Tag stack empty} | + ]], + }) + end) + end) + end) end) |