Skip to content

Commit 405bd23

Browse files
committed
fix: remove unused keys from mode specific keymaps
1 parent 7bf9d6c commit 405bd23

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

lua/blink/cmp/keymap/apply.lua

+13-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ function apply.keymap_to_current_buffer(keys_to_commands)
3939

4040
-- snippet mode: uses only snippet commands
4141
for key, commands in pairs(keys_to_commands) do
42-
local has_snippet_command = false
43-
for _, command in ipairs(commands) do
44-
if vim.tbl_contains(snippet_commands, command) or type(command) == 'function' then has_snippet_command = true end
45-
end
46-
if not has_snippet_command or #commands == 0 then goto continue end
42+
if not apply.has_snippet_commands(commands) or #commands == 0 then goto continue end
4743

4844
local fallback = require('blink.cmp.keymap.fallback').wrap('s', key)
4945
apply.set('s', key, function()
@@ -77,6 +73,13 @@ function apply.has_insert_command(commands)
7773
return false
7874
end
7975

76+
function apply.has_snippet_commands(commands)
77+
for _, command in ipairs(commands) do
78+
if vim.tbl_contains(snippet_commands, command) or type(command) == 'function' then return true end
79+
end
80+
return false
81+
end
82+
8083
function apply.term_keymaps(keys_to_commands)
8184
-- skip if we've already applied the keymaps
8285
for _, mapping in ipairs(vim.api.nvim_buf_get_keymap(0, 't')) do
@@ -110,6 +113,11 @@ function apply.term_keymaps(keys_to_commands)
110113
end
111114

112115
function apply.cmdline_keymaps(keys_to_commands)
116+
-- skip if we've already applied the keymaps
117+
for _, mapping in ipairs(vim.api.nvim_get_keymap('c')) do
118+
if mapping.desc == 'blink.cmp' then return end
119+
end
120+
113121
-- cmdline mode: uses only insert commands
114122
for key, commands in pairs(keys_to_commands) do
115123
if not apply.has_insert_command(commands) or #commands == 0 then goto continue end

lua/blink/cmp/keymap/init.lua

+15-5
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@ function keymap.merge_mappings(existing_mappings, new_mappings)
2626
return merged_mappings
2727
end
2828

29-
---@param keymap_config blink.cmp.KeymapConfig
30-
function keymap.get_mappings(keymap_config)
29+
--- @param keymap_config blink.cmp.KeymapConfig
30+
--- @param mode blink.cmp.Mode
31+
function keymap.get_mappings(keymap_config, mode)
3132
local mappings = vim.deepcopy(keymap_config)
3233

34+
-- Remove unused keys
35+
if mode ~= 'default' then
36+
for key, commands in pairs(mappings) do
37+
if key ~= 'preset' and not require('blink.cmp.keymap.apply').has_insert_command(commands) then
38+
mappings[key] = nil
39+
end
40+
end
41+
end
42+
3343
-- Handle preset
3444
if mappings.preset then
3545
local preset_keymap = require('blink.cmp.keymap.presets').get(mappings.preset)
@@ -48,7 +58,7 @@ function keymap.setup()
4858
local config = require('blink.cmp.config')
4959
local apply = require('blink.cmp.keymap.apply')
5060

51-
local mappings = keymap.get_mappings(config.keymap)
61+
local mappings = keymap.get_mappings(config.keymap, 'default')
5262

5363
-- We set on the buffer directly to avoid buffer-local keymaps (such as from autopairs)
5464
-- from overriding our mappings. We also use InsertEnter to avoid conflicts with keymaps
@@ -70,14 +80,14 @@ function keymap.setup()
7080
for _, mode in ipairs({ 'cmdline', 'term' }) do
7181
local mode_config = config[mode]
7282
if mode_config.enabled then
73-
local mode_keymap = mode_config.keymap
83+
local mode_keymap = vim.deepcopy(mode_config.keymap)
7484

7585
if mode_config.keymap.preset == 'inherit' then
7686
mode_keymap = vim.tbl_deep_extend('force', config.keymap, mode_config.keymap)
7787
mode_keymap.preset = config.keymap.preset
7888
end
7989

80-
local mode_mappings = keymap.get_mappings(mode_keymap)
90+
local mode_mappings = keymap.get_mappings(mode_keymap, mode)
8191
apply[mode .. '_keymaps'](mode_mappings)
8292
end
8393
end

0 commit comments

Comments
 (0)