-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathapply.lua
167 lines (139 loc) · 5.36 KB
/
apply.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
local apply = {}
local snippet_commands = { 'snippet_forward', 'snippet_backward', 'show_signature', 'hide_signature' }
--- Applies the keymaps to the current buffer
--- @param keys_to_commands table<string, blink.cmp.KeymapCommand[]>
function apply.keymap_to_current_buffer(keys_to_commands)
-- skip if we've already applied the keymaps
for _, mapping in ipairs(vim.api.nvim_buf_get_keymap(0, 'i')) do
if mapping.desc == 'blink.cmp' then return end
end
-- insert mode: uses both snippet and insert commands
for key, commands in pairs(keys_to_commands) do
if #commands == 0 then goto continue end
local fallback = require('blink.cmp.keymap.fallback').wrap('i', key)
apply.set('i', key, function()
if not require('blink.cmp.config').enabled() then return fallback() end
for _, command in ipairs(commands) do
-- special case for fallback
if command == 'fallback' or command == 'fallback_to_mappings' then
return fallback(command == 'fallback_to_mappings')
-- run user defined functions
elseif type(command) == 'function' then
if command(require('blink.cmp')) then return end
-- otherwise, run the built-in command
elseif require('blink.cmp')[command]() then
return
end
end
end)
::continue::
end
-- snippet mode: uses only snippet commands
for key, commands in pairs(keys_to_commands) do
local has_snippet_command = false
for _, command in ipairs(commands) do
if vim.tbl_contains(snippet_commands, command) or type(command) == 'function' then has_snippet_command = true end
end
if not has_snippet_command or #commands == 0 then goto continue end
local fallback = require('blink.cmp.keymap.fallback').wrap('s', key)
apply.set('s', key, function()
if not require('blink.cmp.config').enabled() then return fallback() end
for _, command in ipairs(keys_to_commands[key] or {}) do
-- special case for fallback
if command == 'fallback' or command == 'fallback_to_mappings' then
return fallback(command == 'fallback_to_mappings')
-- run user defined functions
elseif type(command) == 'function' then
if command(require('blink.cmp')) then return end
-- only run snippet commands
elseif vim.tbl_contains(snippet_commands, command) then
local did_run = require('blink.cmp')[command]()
if did_run then return end
end
end
end)
::continue::
end
end
function apply.has_insert_command(commands)
for _, command in ipairs(commands) do
if not vim.tbl_contains(snippet_commands, command) and command ~= 'fallback' then return true end
end
return false
end
function apply.term_keymaps(keys_to_commands)
-- skip if we've already applied the keymaps
for _, mapping in ipairs(vim.api.nvim_buf_get_keymap(0, 't')) do
if mapping.desc == 'blink.cmp' then return end
end
-- terminal mode: uses insert commands only
for key, commands in pairs(keys_to_commands) do
if not apply.has_insert_command(commands) or #commands == 0 then goto continue end
local fallback = require('blink.cmp.keymap.fallback').wrap('i', key)
apply.set('t', key, function()
for _, command in ipairs(commands) do
-- special case for fallback
if command == 'fallback' or command == 'fallback_to_mappings' then
return fallback(command == 'fallback_to_mappings')
-- run user defined functions
elseif type(command) == 'function' then
if command(require('blink.cmp')) then return end
-- otherwise, run the built-in command
elseif require('blink.cmp')[command]() then
return
end
end
end)
::continue::
end
end
function apply.cmdline_keymaps(keys_to_commands)
-- cmdline mode: uses only insert commands
for key, commands in pairs(keys_to_commands) do
if not apply.has_insert_command(commands) or #commands == 0 then goto continue end
local fallback = require('blink.cmp.keymap.fallback').wrap('c', key)
apply.set('c', key, function()
for _, command in ipairs(commands) do
-- special case for fallback
if command == 'fallback' or command == 'fallback_to_mappings' then
return fallback(command == 'fallback_to_mappings')
-- run user defined functions
elseif type(command) == 'function' then
if command(require('blink.cmp')) then return end
-- otherwise, run the built-in command
elseif not vim.tbl_contains(snippet_commands, command) then
local did_run = require('blink.cmp')[command]()
if did_run then return end
end
end
end)
::continue::
end
end
--- @param mode string
--- @param key string
--- @param callback fun(): string | nil
function apply.set(mode, key, callback)
if mode == 'c' or mode == 't' then
vim.api.nvim_set_keymap(mode, key, '', {
callback = callback,
expr = true,
-- silent must be false for fallback to work
-- otherwise, you get very weird behavior
silent = false,
noremap = true,
replace_keycodes = false,
desc = 'blink.cmp',
})
else
vim.api.nvim_buf_set_keymap(0, mode, key, '', {
callback = callback,
expr = true,
silent = true,
noremap = true,
replace_keycodes = false,
desc = 'blink.cmp',
})
end
end
return apply