diff options
Diffstat (limited to 'lua/blink/cmp/keymap/init.lua')
| -rw-r--r-- | lua/blink/cmp/keymap/init.lua | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/lua/blink/cmp/keymap/init.lua b/lua/blink/cmp/keymap/init.lua new file mode 100644 index 0000000..a5e7009 --- /dev/null +++ b/lua/blink/cmp/keymap/init.lua @@ -0,0 +1,74 @@ +local keymap = {} + +--- Lowercases all keys in the mappings table +--- @param existing_mappings table<string, blink.cmp.KeymapCommand[]> +--- @param new_mappings table<string, blink.cmp.KeymapCommand[]> +--- @return table<string, blink.cmp.KeymapCommand[]> +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 merged_mappings +end + +---@param keymap_config blink.cmp.BaseKeymapConfig +function keymap.get_mappings(keymap_config) + local mappings = vim.deepcopy(keymap_config) + + -- Handle preset + if mappings.preset then + local preset_keymap = require('blink.cmp.keymap.presets').get(mappings.preset) + + -- Remove 'preset' key from opts to prevent it from being treated as a keymap + mappings.preset = nil + + -- Merge the preset keymap with the user-defined keymaps + -- User-defined keymaps overwrite the preset keymaps + mappings = keymap.merge_mappings(preset_keymap, mappings) + end + return mappings +end + +function keymap.setup() + local config = require('blink.cmp.config') + local mappings = keymap.get_mappings(config.keymap) + -- We set on the buffer directly to avoid buffer-local keymaps (such as from autopairs) + -- from overriding our mappings. We also use InsertEnter to avoid conflicts with keymaps + -- applied on other autocmds, such as LspAttach used by nvim-lspconfig and most configs + vim.api.nvim_create_autocmd('InsertEnter', { + callback = function() + if not require('blink.cmp.config').enabled() then return end + require('blink.cmp.keymap.apply').keymap_to_current_buffer(mappings) + end, + }) + + -- This is not called when the plugin loads since it first checks if the binary is + -- installed. As a result, when lazy-loaded on InsertEnter, the event may be missed + if vim.api.nvim_get_mode().mode == 'i' and require('blink.cmp.config').enabled() then + require('blink.cmp.keymap.apply').keymap_to_current_buffer(mappings) + end + + -- Apply cmdline keymaps since they're global, if any sources are defined + local cmdline_sources = require('blink.cmp.config').sources.cmdline + if type(cmdline_sources) ~= 'table' or #cmdline_sources > 0 then + local cmdline_mappings = keymap.get_mappings(config.keymap.cmdline or config.keymap) + require('blink.cmp.keymap.apply').cmdline_keymaps(cmdline_mappings) + end +end + +return keymap |
