Skip to content

Commit 863bad7

Browse files
committed
fix: keymaps
1 parent 4d1b785 commit 863bad7

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

lua/blink/cmp/keymap.lua

+41-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,38 @@ local snippet_commands = { 'snippet_forward', 'snippet_backward' }
1515

1616
--- @param opts blink.cmp.KeymapConfig
1717
function keymap.setup(opts)
18+
local function handle_key(mode, key)
19+
local key_insert_commands = {}
20+
local key_snippet_commands = {}
21+
22+
for command, keys in pairs(opts) do
23+
keys = type(keys) == 'string' and { keys } or keys
24+
if vim.tbl_contains(keys, key) then
25+
if vim.tbl_contains(snippet_commands, command) then
26+
table.insert(key_snippet_commands, command)
27+
elseif mode == 'i' and vim.tbl_contains(insert_commands, command) then
28+
table.insert(key_insert_commands, command)
29+
else
30+
error('Invalid command in keymap config: ' .. command)
31+
end
32+
end
33+
end
34+
35+
for _, command in ipairs(key_insert_commands) do
36+
local did_run = require('blink.cmp')[command]()
37+
if did_run then return end
38+
end
39+
40+
for _, command in ipairs(key_snippet_commands) do
41+
local did_run = require('blink.cmp')[command]()
42+
if did_run then return end
43+
end
44+
45+
return key
46+
end
47+
48+
local snippet_keys = {}
49+
local insert_keys = {}
1850
for command, keys in pairs(opts) do
1951
local is_snippet_command = vim.tbl_contains(snippet_commands, command)
2052
local is_insert_command = vim.tbl_contains(insert_commands, command)
@@ -25,13 +57,17 @@ function keymap.setup(opts)
2557

2658
-- add keymaps
2759
for _, key in ipairs(keys) do
28-
local mode = is_snippet_command and 's' or 'i'
29-
vim.keymap.set(mode, key, function()
30-
local did_run = require('blink.cmp')[command]()
31-
if not did_run then return key end
32-
end, { expr = true, silent = true })
60+
insert_keys[key] = true
61+
if is_snippet_command then snippet_keys[key] = true end
3362
end
3463
end
64+
65+
for key, _ in pairs(insert_keys) do
66+
vim.keymap.set('i', key, function() return handle_key('i', key) end, { expr = true, silent = true })
67+
end
68+
for key, _ in pairs(snippet_keys) do
69+
vim.keymap.set('s', key, function() return handle_key('s', key) end, { expr = true, silent = true })
70+
end
3571
end
3672

3773
return keymap

0 commit comments

Comments
 (0)