1
1
local fallback = {}
2
2
3
- --- Gets the first non blink.cmp keymap for the given mode and key
3
+ --- Add missing types. Remove when fixed upstream
4
+ --- @class vim.api.keyset.keymap
5
+ --- @field lhs string
6
+ --- @field mode string
7
+ --- @field rhs ? string
8
+ --- @field lhsraw ? string
9
+ --- @field buffer ? number
10
+
11
+ --- Gets the non blink.cmp global keymap for the given mode and key
4
12
--- @param mode string
5
13
--- @param key string
6
14
--- @return vim.api.keyset.keymap | nil
7
- function fallback .get_non_blink_mapping_for_key (mode , key )
15
+ function fallback .get_non_blink_global_mapping_for_key (mode , key )
8
16
local normalized_key = vim .api .nvim_replace_termcodes (key , true , true , true )
9
17
10
- -- get buffer local and global mappings
11
- local mappings = vim .api .nvim_buf_get_keymap (0 , mode )
12
- vim .list_extend (mappings , vim .api .nvim_get_keymap (mode ))
18
+ -- get global mappings
19
+ local mappings = vim .api .nvim_get_keymap (mode )
13
20
14
21
for _ , mapping in ipairs (mappings ) do
15
22
local mapping_key = vim .api .nvim_replace_termcodes (mapping .lhs , true , true , true )
16
23
if mapping_key == normalized_key and mapping .desc ~= ' blink.cmp' then return mapping end
17
24
end
18
25
end
19
26
20
- --- Runs the first non blink.cmp keymap for the given mode and key
27
+ --- Gets the non blink.cmp buffer keymap for the given mode and key
21
28
--- @param mode string
22
29
--- @param key string
23
- --- @return string | nil
24
- function fallback .run_non_blink_keymap (mode , key )
25
- local mapping = fallback .get_non_blink_mapping_for_key (mode , key ) or {}
30
+ --- @return vim.api.keyset.keymap ?
31
+ function fallback .get_non_blink_buffer_mapping_for_key (mode , key )
32
+ local ret = vim .fn .maparg (key , mode , false , true ) --[[ @as vim.api.keyset.keymap]]
33
+ if ret and ret .buffer == 0 then return end
34
+ if ret and ret .desc and ret .desc == ' blink.cmp' then return end
35
+ return ret .lhs ~= nil and ret or nil
36
+ end
26
37
38
+ --- Returns a function that will run the first non blink.cmp keymap for the given mode and key
39
+ --- @param mode string
40
+ --- @param key string
41
+ --- @return fun (): string ?
42
+ function fallback .wrap (mode , key )
43
+ local buffer_mapping = fallback .get_non_blink_buffer_mapping_for_key (mode , key )
44
+ return function ()
45
+ local mapping = buffer_mapping or fallback .get_non_blink_global_mapping_for_key (mode , key )
46
+ if mapping then return fallback .run_non_blink_keymap (mapping , key ) end
47
+ end
48
+ end
49
+
50
+ --- Runs the first non blink.cmp keymap for the given mode and key
51
+ --- @param mapping vim.api.keyset.keymap
52
+ --- @param key string
53
+ --- @return string | nil
54
+ function fallback .run_non_blink_keymap (mapping , key )
27
55
-- TODO: there's likely many edge cases here. the nvim-cmp version is lacking documentation
28
56
-- and is quite complex. we should look to see if we can simplify their logic
29
57
-- https://github.com/hrsh7th/nvim-cmp/blob/ae644feb7b67bf1ce4260c231d1d4300b19c6f30/lua/cmp/utils/keymap.lua
@@ -36,7 +64,9 @@ function fallback.run_non_blink_keymap(mode, key)
36
64
end
37
65
38
66
local expr = mapping .callback ()
39
- if mapping .replace_keycodes == 1 then expr = vim .api .nvim_replace_termcodes (expr , true , true , true ) end
67
+ if type (expr ) == ' string' and mapping .replace_keycodes == 1 then
68
+ expr = vim .api .nvim_replace_termcodes (expr , true , true , true )
69
+ end
40
70
return expr
41
71
elseif mapping .rhs then
42
72
local rhs = vim .api .nvim_replace_termcodes (mapping .rhs , true , true , true )
0 commit comments