1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
|