refactor(api): simplify nvim_set_keymap shortname check (#25945)

This commit is contained in:
zeertzjq 2023-11-09 12:28:43 +08:00 committed by GitHub
parent 77bb69d7b0
commit 43b0e2752c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 39 deletions

View File

@ -2080,7 +2080,7 @@ static Dictionary mapblock_fill_dict(const mapblock_T *const mp, const char *lhs
Dictionary dict = ARRAY_DICT_INIT; Dictionary dict = ARRAY_DICT_INIT;
char *const lhs = str2special_save(mp->m_keys, compatible, !compatible); char *const lhs = str2special_save(mp->m_keys, compatible, !compatible);
char *const mapmode = map_mode_to_chars(mp->m_mode); char *const mapmode = map_mode_to_chars(mp->m_mode);
varnumber_T noremap_value; int noremap_value;
if (compatible) { if (compatible) {
// Keep old compatible behavior // 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, "script", INTEGER_OBJ(mp->m_noremap == REMAP_SCRIPT ? 1 : 0));
PUT(dict, "expr", INTEGER_OBJ(mp->m_expr ? 1 : 0)); PUT(dict, "expr", INTEGER_OBJ(mp->m_expr ? 1 : 0));
PUT(dict, "silent", INTEGER_OBJ(mp->m_silent ? 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, "sid", INTEGER_OBJ(mp->m_script_ctx.sc_sid));
PUT(dict, "lnum", INTEGER_OBJ((varnumber_T)mp->m_script_ctx.sc_lnum)); PUT(dict, "lnum", INTEGER_OBJ(mp->m_script_ctx.sc_lnum));
PUT(dict, "buffer", INTEGER_OBJ((varnumber_T)buffer_value)); PUT(dict, "buffer", INTEGER_OBJ(buffer_value));
PUT(dict, "nowait", INTEGER_OBJ(mp->m_nowait ? 1 : 0)); PUT(dict, "nowait", INTEGER_OBJ(mp->m_nowait ? 1 : 0));
if (mp->m_replace_keycodes) { if (mp->m_replace_keycodes) {
PUT(dict, "replace_keycodes", INTEGER_OBJ(1)); PUT(dict, "replace_keycodes", INTEGER_OBJ(1));
@ -2593,31 +2593,22 @@ void modify_keymap(uint64_t channel_id, Buffer buffer, bool is_unmap, String mod
goto fail_and_free; goto fail_and_free;
} }
bool is_abbrev = false; char *p = mode.size > 0 ? mode.data : "m";
if (mode.size > 2) { bool forceit = *p == '!';
api_set_error(err, kErrorTypeValidation, "Shortname is too long: %s", mode.data); // integer value of the mapping mode, to be passed to do_map()
goto fail_and_free; int mode_val = get_map_mode(&p, forceit);
} else if (mode.size == 2) { if (forceit) {
if ((mode.data[0] != '!' && mode.data[0] != 'i' && mode.data[0] != 'c') assert(p == mode.data);
|| mode.data[1] != 'a') { p++;
api_set_error(err, kErrorTypeValidation, "Shortname is too long: %s", mode.data);
goto fail_and_free;
} }
is_abbrev = true; bool is_abbrev = (mode_val & (MODE_INSERT | MODE_CMDLINE)) != 0 && *p == 'a';
if (is_abbrev) {
p++;
} }
int mode_val; // integer value of the mapping mode, to be passed to do_map() if (mode.size > 0 && (size_t)(p - mode.data) != mode.size) {
char *p = (mode.size) ? mode.data : "m"; api_set_error(err, kErrorTypeValidation, "Invalid mode shortname: \"%s\"", mode.data);
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; goto fail_and_free;
} }
}
if (parsed_args.lhs_len == 0) { if (parsed_args.lhs_len == 0) {
api_set_error(err, kErrorTypeValidation, "Invalid (empty) LHS"); api_set_error(err, kErrorTypeValidation, "Invalid (empty) LHS");

View File

@ -488,18 +488,22 @@ describe('nvim_set_keymap, nvim_del_keymap', function()
end) end)
it('throws errors when given too-long mode shortnames', function() it('throws errors when given too-long mode shortnames', function()
eq('Shortname is too long: map', eq('Invalid mode shortname: "a"', pcall_err(meths.set_keymap, 'a', 'lhs', 'rhs', {}))
pcall_err(meths.set_keymap, 'map', '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('Shortname is too long: vmap', eq('Invalid mode shortname: "!i"', pcall_err(meths.set_keymap, '!i', 'lhs', 'rhs', {}))
pcall_err(meths.set_keymap, 'vmap', '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('Shortname is too long: xnoremap', eq('Invalid mode shortname: "vmap"', pcall_err(meths.set_keymap, 'vmap', 'lhs', 'rhs', {}))
pcall_err(meths.set_keymap, 'xnoremap', '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('Shortname is too long: map', pcall_err(meths.del_keymap, 'map', 'lhs')) eq('Invalid mode shortname: "oa"', pcall_err(meths.del_keymap, 'oa', 'lhs'))
eq('Shortname is too long: vmap', pcall_err(meths.del_keymap, 'vmap', 'lhs')) eq('Invalid mode shortname: "!o"', pcall_err(meths.del_keymap, '!o', 'lhs'))
eq('Shortname is too long: xnoremap', pcall_err(meths.del_keymap, 'xnoremap', '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) end)
it('error on invalid mode shortname', function() it('error on invalid mode shortname', function()