From f88bd66d88e9248276996c0f5b5c2b7fa5aa851f Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Mon, 16 Dec 2024 18:24:03 -0500 Subject: [PATCH] fix(keymap): incorrect merging strategy Related to #599 --- lua/blink/cmp/keymap/init.lua | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/lua/blink/cmp/keymap/init.lua b/lua/blink/cmp/keymap/init.lua index 9336dec0..a5e7009e 100644 --- a/lua/blink/cmp/keymap/init.lua +++ b/lua/blink/cmp/keymap/init.lua @@ -1,14 +1,29 @@ local keymap = {} --- Lowercases all keys in the mappings table ---- @param mappings table +--- @param existing_mappings table +--- @param new_mappings table --- @return table -function keymap.normalize_mappings(mappings) - local normalized_mappings = {} - for key, map in pairs(mappings) do - normalized_mappings[key:lower()] = map +function keymap.merge_mappings(existing_mappings, new_mappings) + local merged_mappings = vim.deepcopy(existing_mappings) + for new_key, new_mapping in pairs(new_mappings) do + -- normalize the keys and replace, since naively merging would not handle == + for existing_key, _ in pairs(existing_mappings) do + if + vim.api.nvim_replace_termcodes(existing_key, true, true, true) + == vim.api.nvim_replace_termcodes(new_key, true, true, true) + then + merged_mappings[existing_key] = new_mapping + goto continue + end + end + + -- key wasn't found, add it as per usual + merged_mappings[new_key] = new_mapping + + ::continue:: end - return normalized_mappings + return merged_mappings end ---@param keymap_config blink.cmp.BaseKeymapConfig @@ -24,7 +39,7 @@ function keymap.get_mappings(keymap_config) -- Merge the preset keymap with the user-defined keymaps -- User-defined keymaps overwrite the preset keymaps - mappings = vim.tbl_extend('force', keymap.normalize_mappings(preset_keymap), keymap.normalize_mappings(mappings)) + mappings = keymap.merge_mappings(preset_keymap, mappings) end return mappings end