aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Anders <greg@gpanders.com>2025-03-29 08:29:06 -0500
committerGitHub <noreply@github.com>2025-03-29 06:29:06 -0700
commit0d73ec583465f06227845b7171dcea59d7b24e97 (patch)
treed5178f44dc44fbd9918beeadf300fa6c25701498
parent89bc9455543abbd98bba752367ab5f2b83943931 (diff)
downloadrneovim-0d73ec583465f06227845b7171dcea59d7b24e97.tar.gz
rneovim-0d73ec583465f06227845b7171dcea59d7b24e97.tar.bz2
rneovim-0d73ec583465f06227845b7171dcea59d7b24e97.zip
fix(api): use original LHS in keymap error message #33135
When setting a keymap with "unique" that already exists the error message contains the LHS of the keymap with termcodes replaced. In particular this means that keys like <Tab> show as an actual tab character, meaning the error message displays as "Mapping already exists for ", which is hard to debug for users. Instead, display the original LHS (without any simplification or parsed termcodes). This rperesents exactly what the user passed to the `lhs` argument of `nvim_set_keymap`, which makes it easier to find where the offending keymap is.
-rw-r--r--src/nvim/mapping.c3
-rw-r--r--test/functional/api/keymap_spec.lua8
2 files changed, 8 insertions, 3 deletions
diff --git a/src/nvim/mapping.c b/src/nvim/mapping.c
index ab9d68f911..147985377e 100644
--- a/src/nvim/mapping.c
+++ b/src/nvim/mapping.c
@@ -2804,8 +2804,7 @@ void modify_keymap(uint64_t channel_id, Buffer buffer, bool is_unmap, String mod
api_set_error(err, kErrorTypeException, e_nomap, 0);
goto fail_and_free;
case 5:
- api_set_error(err, kErrorTypeException,
- "E227: mapping already exists for %s", parsed_args.lhs);
+ api_set_error(err, kErrorTypeException, e_mapping_already_exists_for_str, lhs.data);
goto fail_and_free;
default:
assert(false && "Unrecognized return code!");
diff --git a/test/functional/api/keymap_spec.lua b/test/functional/api/keymap_spec.lua
index 995711507f..73dae0dd60 100644
--- a/test/functional/api/keymap_spec.lua
+++ b/test/functional/api/keymap_spec.lua
@@ -803,11 +803,17 @@ describe('nvim_set_keymap, nvim_del_keymap', function()
it('throws appropriate error messages when setting <unique> maps', function()
api.nvim_set_keymap('l', 'lhs', 'rhs', {})
eq(
- 'E227: mapping already exists for lhs',
+ 'E227: Mapping already exists for lhs',
pcall_err(api.nvim_set_keymap, 'l', 'lhs', 'rhs', { unique = true })
)
-- different mapmode, no error should be thrown
api.nvim_set_keymap('t', 'lhs', 'rhs', { unique = true })
+
+ api.nvim_set_keymap('n', '<tab>', 'rhs', {})
+ eq(
+ 'E227: Mapping already exists for <tab>',
+ pcall_err(api.nvim_set_keymap, 'n', '<tab>', 'rhs', { unique = true })
+ )
end)
it('can set <expr> mappings whose RHS change dynamically', function()