Skip to content

Commit

Permalink
fix(keymap): incorrect merging strategy
Browse files Browse the repository at this point in the history
Related to #599
  • Loading branch information
Saghen committed Dec 16, 2024
1 parent 596a7ab commit f88bd66
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lua/blink/cmp/keymap/init.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
local keymap = {}

--- Lowercases all keys in the mappings table
--- @param mappings table<string, blink.cmp.KeymapCommand[]>
--- @param existing_mappings table<string, blink.cmp.KeymapCommand[]>
--- @param new_mappings table<string, blink.cmp.KeymapCommand[]>
--- @return table<string, blink.cmp.KeymapCommand[]>
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 <C-a> == <c-a>
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
Expand All @@ -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
Expand Down

0 comments on commit f88bd66

Please sign in to comment.