@@ -25,7 +25,7 @@ function keymap.setup(opts)
25
25
-- convert string to string[] for consistency
26
26
if type (keys ) == ' string' then keys = { keys } end
27
27
28
- -- add keymaps
28
+ -- reverse the command -> key[] mapping into key -> command[]
29
29
for _ , key in ipairs (keys ) do
30
30
if is_insert_command then
31
31
if insert_keys_to_commands [key ] == nil then insert_keys_to_commands [key ] = {} end
@@ -38,28 +38,46 @@ function keymap.setup(opts)
38
38
end
39
39
end
40
40
41
- for key , _ in pairs (insert_keys_to_commands ) do
42
- vim .keymap .set (' i' , key , function ()
43
- for _ , command in ipairs (insert_keys_to_commands [key ] or {}) do
44
- local did_run = require (' blink.cmp' )[command ]()
45
- if did_run then return end
46
- end
47
- for _ , command in ipairs (snippet_keys_to_commands [key ] or {}) do
48
- local did_run = require (' blink.cmp' )[command ]()
49
- if did_run then return end
50
- end
51
- return key
52
- end , { expr = true , silent = true })
53
- end
54
- for key , _ in pairs (snippet_keys_to_commands ) do
55
- vim .keymap .set (' s' , key , function ()
56
- for _ , command in ipairs (snippet_keys_to_commands [key ] or {}) do
57
- local did_run = require (' blink.cmp' )[command ]()
58
- if did_run then return end
59
- end
60
- return key
61
- end , { expr = true , silent = true })
41
+ -- apply keymaps
42
+ -- we set on the buffer directly to avoid buffer-local keymaps (such as from autopairs)
43
+ -- from overriding our mappings
44
+ local set_buffer_keymap = function ()
45
+ for key , _ in pairs (insert_keys_to_commands ) do
46
+ vim .api .nvim_buf_set_keymap (0 , ' i' , key , ' ' , {
47
+ callback = function ()
48
+ for _ , command in ipairs (insert_keys_to_commands [key ] or {}) do
49
+ local did_run = require (' blink.cmp' )[command ]()
50
+ if did_run then return end
51
+ end
52
+ for _ , command in ipairs (snippet_keys_to_commands [key ] or {}) do
53
+ local did_run = require (' blink.cmp' )[command ]()
54
+ if did_run then return end
55
+ end
56
+ return key
57
+ end ,
58
+ expr = true ,
59
+ silent = true ,
60
+ noremap = true ,
61
+ })
62
+ end
63
+ for key , _ in pairs (snippet_keys_to_commands ) do
64
+ vim .api .nvim_buf_set_keymap (0 , ' s' , key , ' ' , {
65
+ callback = function ()
66
+ for _ , command in ipairs (snippet_keys_to_commands [key ] or {}) do
67
+ local did_run = require (' blink.cmp' )[command ]()
68
+ if did_run then return end
69
+ end
70
+ return key
71
+ end ,
72
+ expr = true ,
73
+ silent = true ,
74
+ noremap = true ,
75
+ })
76
+ end
62
77
end
78
+
79
+ set_buffer_keymap () -- apply immediately since the plugin starts asynchronously
80
+ vim .api .nvim_create_autocmd (' BufEnter' , { callback = set_buffer_keymap })
63
81
end
64
82
65
83
return keymap
0 commit comments