Skip to content

Commit a89ae20

Browse files
committed
fix: snippet keymaps not applying in insert
closes #70
1 parent 5ffd608 commit a89ae20

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

lua/blink/cmp/keymap.lua

+44-28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local utils = require('blink.cmp.utils')
12
local keymap = {}
23

34
local insert_commands = {
@@ -41,41 +42,49 @@ function keymap.setup(opts)
4142
-- apply keymaps
4243
-- we set on the buffer directly to avoid buffer-local keymaps (such as from autopairs)
4344
-- 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
6050

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
6866

69-
return keymap.run_non_blink_keymap('s', key)
70-
end)
71-
end
67+
return keymap.run_non_blink_keymap('i', key)
68+
end)
7269
end
7370

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
7682
end
7783

7884
--- 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
7988
function keymap.get_non_blink_mapping_for_key(mode, key)
8089
local normalized_key = vim.api.nvim_replace_termcodes(key, true, true, true)
8190

@@ -90,6 +99,9 @@ function keymap.get_non_blink_mapping_for_key(mode, key)
9099
end
91100

92101
--- 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
93105
function keymap.run_non_blink_keymap(mode, key)
94106
local mapping = keymap.get_non_blink_mapping_for_key(mode, key) or {}
95107

@@ -100,6 +112,7 @@ function keymap.run_non_blink_keymap(mode, key)
100112
local expr = mapping.callback()
101113
if mapping.replace_keycodes then expr = vim.api.nvim_replace_termcodes(expr, true, true, true) end
102114
if mapping.expr then return expr end
115+
return
103116
elseif mapping.rhs then
104117
return vim.api.nvim_eval(vim.api.nvim_replace_termcodes(mapping.rhs, true, true, true))
105118
end
@@ -108,6 +121,9 @@ function keymap.run_non_blink_keymap(mode, key)
108121
return vim.api.nvim_replace_termcodes(key, true, true, true)
109122
end
110123

124+
--- @param mode string
125+
--- @param key string
126+
--- @param callback fun(): string | nil
111127
function keymap.set(mode, key, callback)
112128
vim.api.nvim_buf_set_keymap(0, mode, key, '', {
113129
callback = callback,

0 commit comments

Comments
 (0)