diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-11-09 12:28:43 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-09 12:28:43 +0800 |
commit | 43b0e2752c06ec5460a417fb9a8c60856011f563 (patch) | |
tree | cdb64e9eb5aab6f6fbd0e6f8ebe0c4837b81171a | |
parent | 77bb69d7b0d1ae8d9526b658eeea07b2d58ae78d (diff) | |
download | rneovim-43b0e2752c06ec5460a417fb9a8c60856011f563.tar.gz rneovim-43b0e2752c06ec5460a417fb9a8c60856011f563.tar.bz2 rneovim-43b0e2752c06ec5460a417fb9a8c60856011f563.zip |
refactor(api): simplify nvim_set_keymap shortname check (#25945)
-rw-r--r-- | src/nvim/mapping.c | 45 | ||||
-rw-r--r-- | test/functional/api/keymap_spec.lua | 28 |
2 files changed, 34 insertions, 39 deletions
diff --git a/src/nvim/mapping.c b/src/nvim/mapping.c index da98f5e6c0..4435e91993 100644 --- a/src/nvim/mapping.c +++ b/src/nvim/mapping.c @@ -2080,7 +2080,7 @@ static Dictionary mapblock_fill_dict(const mapblock_T *const mp, const char *lhs Dictionary dict = ARRAY_DICT_INIT; char *const lhs = str2special_save(mp->m_keys, compatible, !compatible); char *const mapmode = map_mode_to_chars(mp->m_mode); - varnumber_T noremap_value; + int noremap_value; if (compatible) { // Keep old compatible behavior @@ -2112,9 +2112,9 @@ static Dictionary mapblock_fill_dict(const mapblock_T *const mp, const char *lhs PUT(dict, "script", INTEGER_OBJ(mp->m_noremap == REMAP_SCRIPT ? 1 : 0)); PUT(dict, "expr", INTEGER_OBJ(mp->m_expr ? 1 : 0)); PUT(dict, "silent", INTEGER_OBJ(mp->m_silent ? 1 : 0)); - PUT(dict, "sid", INTEGER_OBJ((varnumber_T)mp->m_script_ctx.sc_sid)); - PUT(dict, "lnum", INTEGER_OBJ((varnumber_T)mp->m_script_ctx.sc_lnum)); - PUT(dict, "buffer", INTEGER_OBJ((varnumber_T)buffer_value)); + PUT(dict, "sid", INTEGER_OBJ(mp->m_script_ctx.sc_sid)); + PUT(dict, "lnum", INTEGER_OBJ(mp->m_script_ctx.sc_lnum)); + PUT(dict, "buffer", INTEGER_OBJ(buffer_value)); PUT(dict, "nowait", INTEGER_OBJ(mp->m_nowait ? 1 : 0)); if (mp->m_replace_keycodes) { PUT(dict, "replace_keycodes", INTEGER_OBJ(1)); @@ -2593,30 +2593,21 @@ void modify_keymap(uint64_t channel_id, Buffer buffer, bool is_unmap, String mod goto fail_and_free; } - bool is_abbrev = false; - if (mode.size > 2) { - api_set_error(err, kErrorTypeValidation, "Shortname is too long: %s", mode.data); + char *p = mode.size > 0 ? mode.data : "m"; + bool forceit = *p == '!'; + // integer value of the mapping mode, to be passed to do_map() + int mode_val = get_map_mode(&p, forceit); + if (forceit) { + assert(p == mode.data); + p++; + } + bool is_abbrev = (mode_val & (MODE_INSERT | MODE_CMDLINE)) != 0 && *p == 'a'; + if (is_abbrev) { + p++; + } + if (mode.size > 0 && (size_t)(p - mode.data) != mode.size) { + api_set_error(err, kErrorTypeValidation, "Invalid mode shortname: \"%s\"", mode.data); goto fail_and_free; - } else if (mode.size == 2) { - if ((mode.data[0] != '!' && mode.data[0] != 'i' && mode.data[0] != 'c') - || mode.data[1] != 'a') { - api_set_error(err, kErrorTypeValidation, "Shortname is too long: %s", mode.data); - goto fail_and_free; - } - is_abbrev = true; - } - int mode_val; // integer value of the mapping mode, to be passed to do_map() - char *p = (mode.size) ? mode.data : "m"; - if (*p == '!') { - mode_val = get_map_mode(&p, true); // mapmode-ic - } else { - mode_val = get_map_mode(&p, false); - if (mode_val == (MODE_VISUAL | MODE_SELECT | MODE_NORMAL | MODE_OP_PENDING) && mode.size > 0) { - // get_map_mode() treats unrecognized mode shortnames as ":map". - // This is an error unless the given shortname was empty string "". - api_set_error(err, kErrorTypeValidation, "Invalid mode shortname: \"%s\"", p); - goto fail_and_free; - } } if (parsed_args.lhs_len == 0) { diff --git a/test/functional/api/keymap_spec.lua b/test/functional/api/keymap_spec.lua index 953402ada3..811c4210fa 100644 --- a/test/functional/api/keymap_spec.lua +++ b/test/functional/api/keymap_spec.lua @@ -488,18 +488,22 @@ describe('nvim_set_keymap, nvim_del_keymap', function() end) it('throws errors when given too-long mode shortnames', function() - eq('Shortname is too long: map', - pcall_err(meths.set_keymap, 'map', 'lhs', 'rhs', {})) - - eq('Shortname is too long: vmap', - pcall_err(meths.set_keymap, 'vmap', 'lhs', 'rhs', {})) - - eq('Shortname is too long: xnoremap', - pcall_err(meths.set_keymap, 'xnoremap', 'lhs', 'rhs', {})) - - eq('Shortname is too long: map', pcall_err(meths.del_keymap, 'map', 'lhs')) - eq('Shortname is too long: vmap', pcall_err(meths.del_keymap, 'vmap', 'lhs')) - eq('Shortname is too long: xnoremap', pcall_err(meths.del_keymap, 'xnoremap', 'lhs')) + eq('Invalid mode shortname: "a"', pcall_err(meths.set_keymap, 'a', 'lhs', 'rhs', {})) + eq('Invalid mode shortname: "oa"', pcall_err(meths.set_keymap, 'oa', 'lhs', 'rhs', {})) + eq('Invalid mode shortname: "!o"', pcall_err(meths.set_keymap, '!o', 'lhs', 'rhs', {})) + eq('Invalid mode shortname: "!i"', pcall_err(meths.set_keymap, '!i', 'lhs', 'rhs', {})) + eq('Invalid mode shortname: "!!"', pcall_err(meths.set_keymap, '!!', 'lhs', 'rhs', {})) + eq('Invalid mode shortname: "map"', pcall_err(meths.set_keymap, 'map', 'lhs', 'rhs', {})) + eq('Invalid mode shortname: "vmap"', pcall_err(meths.set_keymap, 'vmap', 'lhs', 'rhs', {})) + eq('Invalid mode shortname: "xnoremap"', pcall_err(meths.set_keymap, 'xnoremap', 'lhs', 'rhs', {})) + eq('Invalid mode shortname: "a"', pcall_err(meths.del_keymap, 'a', 'lhs')) + eq('Invalid mode shortname: "oa"', pcall_err(meths.del_keymap, 'oa', 'lhs')) + eq('Invalid mode shortname: "!o"', pcall_err(meths.del_keymap, '!o', 'lhs')) + eq('Invalid mode shortname: "!i"', pcall_err(meths.del_keymap, '!i', 'lhs')) + eq('Invalid mode shortname: "!!"', pcall_err(meths.del_keymap, '!!', 'lhs')) + eq('Invalid mode shortname: "map"', pcall_err(meths.del_keymap, 'map', 'lhs')) + eq('Invalid mode shortname: "vmap"', pcall_err(meths.del_keymap, 'vmap', 'lhs')) + eq('Invalid mode shortname: "xnoremap"', pcall_err(meths.del_keymap, 'xnoremap', 'lhs')) end) it('error on invalid mode shortname', function() |