fix(defaults): omit extraneous info from unimpaired mapping errors (#30983)

This commit is contained in:
Gregory Anders 2024-10-29 10:06:14 -05:00 committed by GitHub
parent 4c7f5032af
commit ff93cccbc1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 170 additions and 95 deletions

View File

@ -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

View File

@ -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)