From ae98d0a560b08d901ee9aae85df634de0ae3fe0a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 28 Mar 2025 08:08:36 +0800 Subject: vim-patch:9.1.1247: fragile setup to get (preferred) keys from key_name_entry (#33102) Problem: fragile setup to get (preferred) keys from key_name_entry (after v9.1.1179) Solution: refactor the code further, fix a bug with "pref_name" key entry introduced in v9.1.1180 (Yee Cheng Chin) The optimization introduced for using bsearch() with key_name_entry in vim/vim#16788 was fragile as it required synchronizing a non-obvious index (e.g. IDX_KEYNAME_SWU) with the array that could be accidentally changed by any one adding a key to it. Furthermore, the "pref_name" that was introduced in that change was unnecessary, and in fact introduced a bug, as we don't always want to use the canonical name. The bug is triggered when the user triggers auto-complete using a keycode, such as `:set `. The bug would end up showing two copies of `` because both entries end up using the canonical name. In this change, remove `pref_name`, and simply use a boolean to track whether an entry is an alt name or not and modify logic to respect that. Add test to make sure auto-complete works with alt names closes: vim/vim#16987 https://github.com/vim/vim/commit/7d8e7df55190e8e4e5a66f443e4440b52edf2fdb In Nvim there is no `enabled` field, so put `is_alt` before `name` to reduce the size of the struct. Co-authored-by: Yee Cheng Chin --- src/gen/gen_keycodes.lua | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) (limited to 'src/gen/gen_keycodes.lua') diff --git a/src/gen/gen_keycodes.lua b/src/gen/gen_keycodes.lua index 6fbfb5190f..584f0a3d36 100644 --- a/src/gen/gen_keycodes.lua +++ b/src/gen/gen_keycodes.lua @@ -38,43 +38,26 @@ hashorder, hashfun = hashy.hashy_hash('get_special_key_code', hashorder, functio return 'key_names_table[' .. idx .. '].name.data' end, true) ---- @type table ---- Maps keys to the (after hash) indexes of the entries with preferred names. -local key_hash_idx = {} - -local name_orig_idx_ = vim.deepcopy(name_orig_idx) -for i, lower_name in ipairs(hashorder) do - local orig_idx = table.remove(name_orig_idx_[lower_name], 1) - local keycode = keycode_names[orig_idx] - local key = keycode[1] - if key_orig_idx[key] == orig_idx then - key_hash_idx[key] = i - end -end -assert(vim.iter(vim.tbl_values(name_orig_idx_)):all(vim.tbl_isempty)) - local names_tgt = assert(io.open(names_file, 'w')) names_tgt:write([[ static const struct key_name_entry { - int key; ///< Special key code or ascii value - String name; ///< Name of key - const String *pref_name; ///< Pointer to preferred key name - ///< (may be NULL or point to the name in another entry) + int key; ///< Special key code or ascii value + bool is_alt; ///< Is an alternative name + String name; ///< Name of key } key_names_table[] = {]]) -name_orig_idx_ = vim.deepcopy(name_orig_idx) -for i, lower_name in ipairs(hashorder) do +local name_orig_idx_ = vim.deepcopy(name_orig_idx) +for _, lower_name in ipairs(hashorder) do local orig_idx = table.remove(name_orig_idx_[lower_name], 1) local keycode = keycode_names[orig_idx] local key = keycode[1] local name = keycode[2] - local pref_idx = key_hash_idx[key] names_tgt:write( - ('\n {%s, {"%s", %u}, %s},'):format( + ('\n {%s, %s, {"%s", %u}},'):format( key, + key_orig_idx[key] == orig_idx and 'false' or 'true', name, - #name, - pref_idx == i and 'NULL' or ('&key_names_table[%u].name'):format(pref_idx - 1) + #name ) ) end -- cgit