1
+ local utils = require (' blink.cmp.utils' )
1
2
local keymap = {}
2
3
3
4
local insert_commands = {
@@ -41,41 +42,49 @@ function keymap.setup(opts)
41
42
-- apply keymaps
42
43
-- we set on the buffer directly to avoid buffer-local keymaps (such as from autopairs)
43
44
-- from overriding our mappings
44
- local set_buffer_keymap = function ()
45
- -- insert mode
46
- for key , _ in pairs (insert_keys_to_commands ) do
47
- keymap .set (' i' , key , 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
-
57
- return keymap .run_non_blink_keymap (' i' , key )
58
- end )
59
- end
45
+ keymap .apply_keymap_to_current_buffer (insert_keys_to_commands , snippet_keys_to_commands ) -- apply immediately since the plugin starts asynchronously
46
+ vim .api .nvim_create_autocmd (' BufEnter' , {
47
+ callback = function () keymap .apply_keymap_to_current_buffer (insert_keys_to_commands , snippet_keys_to_commands ) end ,
48
+ })
49
+ end
60
50
61
- -- snippet mode
62
- for key , _ in pairs (snippet_keys_to_commands ) do
63
- keymap .set (' s' , key , function ()
64
- for _ , command in ipairs (snippet_keys_to_commands [key ] or {}) do
65
- local did_run = require (' blink.cmp' )[command ]()
66
- if did_run then return end
67
- end
51
+ --- Applies the keymaps to the current buffer
52
+ --- @param insert_keys_to_commands table<string , string[]>
53
+ --- @param snippet_keys_to_commands table<string , string[]>
54
+ function keymap .apply_keymap_to_current_buffer (insert_keys_to_commands , snippet_keys_to_commands )
55
+ -- insert mode: uses both snippet and insert commands
56
+ for _ , key in ipairs (utils .union_keys (insert_keys_to_commands , snippet_keys_to_commands )) do
57
+ keymap .set (' i' , key , function ()
58
+ for _ , command in ipairs (insert_keys_to_commands [key ] or {}) do
59
+ local did_run = require (' blink.cmp' )[command ]()
60
+ if did_run then return end
61
+ end
62
+ for _ , command in ipairs (snippet_keys_to_commands [key ] or {}) do
63
+ local did_run = require (' blink.cmp' )[command ]()
64
+ if did_run then return end
65
+ end
68
66
69
- return keymap .run_non_blink_keymap (' s' , key )
70
- end )
71
- end
67
+ return keymap .run_non_blink_keymap (' i' , key )
68
+ end )
72
69
end
73
70
74
- set_buffer_keymap () -- apply immediately since the plugin starts asynchronously
75
- vim .api .nvim_create_autocmd (' BufEnter' , { callback = set_buffer_keymap })
71
+ -- snippet mode
72
+ for key , _ in pairs (snippet_keys_to_commands ) do
73
+ keymap .set (' s' , key , function ()
74
+ for _ , command in ipairs (snippet_keys_to_commands [key ] or {}) do
75
+ local did_run = require (' blink.cmp' )[command ]()
76
+ if did_run then return end
77
+ end
78
+
79
+ return keymap .run_non_blink_keymap (' s' , key )
80
+ end )
81
+ end
76
82
end
77
83
78
84
--- Gets the first non blink.cmp keymap for the given mode and key
85
+ --- @param mode string
86
+ --- @param key string
87
+ --- @return vim.api.keyset.keymap | nil
79
88
function keymap .get_non_blink_mapping_for_key (mode , key )
80
89
local normalized_key = vim .api .nvim_replace_termcodes (key , true , true , true )
81
90
@@ -90,6 +99,9 @@ function keymap.get_non_blink_mapping_for_key(mode, key)
90
99
end
91
100
92
101
--- Runs the first non blink.cmp keymap for the given mode and key
102
+ --- @param mode string
103
+ --- @param key string
104
+ --- @return string | nil
93
105
function keymap .run_non_blink_keymap (mode , key )
94
106
local mapping = keymap .get_non_blink_mapping_for_key (mode , key ) or {}
95
107
@@ -100,6 +112,7 @@ function keymap.run_non_blink_keymap(mode, key)
100
112
local expr = mapping .callback ()
101
113
if mapping .replace_keycodes then expr = vim .api .nvim_replace_termcodes (expr , true , true , true ) end
102
114
if mapping .expr then return expr end
115
+ return
103
116
elseif mapping .rhs then
104
117
return vim .api .nvim_eval (vim .api .nvim_replace_termcodes (mapping .rhs , true , true , true ))
105
118
end
@@ -108,6 +121,9 @@ function keymap.run_non_blink_keymap(mode, key)
108
121
return vim .api .nvim_replace_termcodes (key , true , true , true )
109
122
end
110
123
124
+ --- @param mode string
125
+ --- @param key string
126
+ --- @param callback fun (): string | nil
111
127
function keymap .set (mode , key , callback )
112
128
vim .api .nvim_buf_set_keymap (0 , mode , key , ' ' , {
113
129
callback = callback ,
0 commit comments