Skip to content

Commit 506ea74

Browse files
committed
fix: keymaps replacing buffer local bindings
closes #39
1 parent 791ab6b commit 506ea74

File tree

1 file changed

+65
-30
lines changed

1 file changed

+65
-30
lines changed

lua/blink/cmp/keymap.lua

+65-30
Original file line numberDiff line numberDiff line change
@@ -42,44 +42,79 @@ function keymap.setup(opts)
4242
-- we set on the buffer directly to avoid buffer-local keymaps (such as from autopairs)
4343
-- from overriding our mappings
4444
local set_buffer_keymap = function()
45+
-- insert mode
4546
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-
replace_keycodes = true,
62-
})
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)
6359
end
60+
61+
-- snippet mode
6462
for key, _ in pairs(snippet_keys_to_commands) do
65-
vim.api.nvim_buf_set_keymap(0, 's', key, '', {
66-
callback = function()
67-
for _, command in ipairs(snippet_keys_to_commands[key] or {}) do
68-
local did_run = require('blink.cmp')[command]()
69-
if did_run then return end
70-
end
71-
return key
72-
end,
73-
expr = true,
74-
silent = true,
75-
noremap = true,
76-
replace_keycodes = true,
77-
})
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
68+
69+
return keymap.run_non_blink_keymap('s', key)
70+
end)
7871
end
7972
end
8073

8174
set_buffer_keymap() -- apply immediately since the plugin starts asynchronously
8275
vim.api.nvim_create_autocmd('BufEnter', { callback = set_buffer_keymap })
8376
end
8477

78+
--- Gets the first non blink.cmp keymap for the given mode and key
79+
function keymap.get_non_blink_mapping_for_key(mode, key)
80+
local normalized_key = vim.api.nvim_replace_termcodes(key, true, true, true)
81+
82+
-- get buffer local and global mappings
83+
local mappings = vim.api.nvim_buf_get_keymap(0, mode)
84+
vim.list_extend(mappings, vim.api.nvim_get_keymap(mode))
85+
86+
for _, mapping in ipairs(mappings) do
87+
local mapping_key = vim.api.nvim_replace_termcodes(mapping.lhs, true, true, true)
88+
if mapping_key == normalized_key and mapping.desc ~= 'blink.cmp' then return mapping end
89+
end
90+
end
91+
92+
--- Runs the first non blink.cmp keymap for the given mode and key
93+
function keymap.run_non_blink_keymap(mode, key)
94+
local mapping = keymap.get_non_blink_mapping_for_key(mode, key) or {}
95+
96+
-- todo: there's likely many edge cases here. the nvim-cmp version is lacking documentation
97+
-- and is quite complex. we should look to see if we can simplify their logic
98+
-- https://github.com/hrsh7th/nvim-cmp/blob/ae644feb7b67bf1ce4260c231d1d4300b19c6f30/lua/cmp/utils/keymap.lua
99+
if type(mapping.callback) == 'function' then
100+
return mapping.callback()
101+
elseif mapping.rhs then
102+
return vim.api.nvim_eval(vim.api.nvim_replace_termcodes(mapping.rhs, true, true, true))
103+
end
104+
105+
-- pass the key along as usual
106+
return vim.api.nvim_replace_termcodes(key, true, true, true)
107+
end
108+
109+
function keymap.set(mode, key, callback)
110+
vim.api.nvim_buf_set_keymap(0, mode, key, '', {
111+
callback = callback,
112+
expr = true,
113+
silent = true,
114+
noremap = true,
115+
replace_keycodes = false,
116+
desc = 'blink.cmp',
117+
})
118+
end
119+
85120
return keymap

0 commit comments

Comments
 (0)