Skip to content

Commit ecb3510

Browse files
committed
fix: use buffer-local keymaps
Closes #20
1 parent 8632278 commit ecb3510

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

lua/blink/cmp/keymap.lua

+40-22
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function keymap.setup(opts)
2525
-- convert string to string[] for consistency
2626
if type(keys) == 'string' then keys = { keys } end
2727

28-
-- add keymaps
28+
-- reverse the command -> key[] mapping into key -> command[]
2929
for _, key in ipairs(keys) do
3030
if is_insert_command then
3131
if insert_keys_to_commands[key] == nil then insert_keys_to_commands[key] = {} end
@@ -38,28 +38,46 @@ function keymap.setup(opts)
3838
end
3939
end
4040

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
6277
end
78+
79+
set_buffer_keymap() -- apply immediately since the plugin starts asynchronously
80+
vim.api.nvim_create_autocmd('BufEnter', { callback = set_buffer_keymap })
6381
end
6482

6583
return keymap

0 commit comments

Comments
 (0)