@@ -15,6 +15,38 @@ local snippet_commands = { 'snippet_forward', 'snippet_backward' }
15
15
16
16
--- @param opts blink.cmp.KeymapConfig
17
17
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 = {}
18
50
for command , keys in pairs (opts ) do
19
51
local is_snippet_command = vim .tbl_contains (snippet_commands , command )
20
52
local is_insert_command = vim .tbl_contains (insert_commands , command )
@@ -25,13 +57,17 @@ function keymap.setup(opts)
25
57
26
58
-- add keymaps
27
59
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
33
62
end
34
63
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
35
71
end
36
72
37
73
return keymap
0 commit comments