Skip to content

Commit 0d9a7e4

Browse files
soifouSaghen
andauthored
fix(keymap): disable preset keymaps with false (#1859)
Previously, setting a keymap to `{}` did not actually disable the mapping. You can now set a keymap to `false` to explicitly disable it. This is clearer and works as expected. Closes #1855 --------- Co-authored-by: Liam Dyer <[email protected]>
1 parent d88916e commit 0d9a7e4

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

doc/configuration/keymap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ keymap = {
2121
['<Down>'] = { 'select_next', 'fallback' },
2222

2323
-- disable a keymap from the preset
24-
['<C-e>'] = {},
24+
['<C-e>'] = false, -- or {}
2525

2626
-- show with a list of providers
2727
['<C-space>'] = { function(cmp) cmp.show({ providers = { 'snippets' } }) end },

lua/blink/cmp/config/keymap.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
--- When defining your own keymaps without a preset, no keybinds will be assigned automatically.
154154
--- @class (exact) blink.cmp.KeymapConfig
155155
--- @field preset? blink.cmp.KeymapPreset
156-
--- @field [string] blink.cmp.KeymapCommand[] Table of keys => commands[]
156+
--- @field [string] blink.cmp.KeymapCommand[] | false Table of keys => commands[] or false to disable
157157

158158
local keymap = {
159159
--- @type blink.cmp.KeymapConfig
@@ -210,12 +210,14 @@ function keymap.validate(config, is_mode)
210210
validation_schema[key] = {
211211
value,
212212
function(key_commands)
213+
if key_commands == false then return true end
214+
if type(key_commands) ~= 'table' then return false end
213215
for _, command in ipairs(key_commands) do
214216
if type(command) ~= 'function' and not vim.tbl_contains(commands, command) then return false end
215217
end
216218
return true
217219
end,
218-
'commands must be one of: ' .. table.concat(commands, ', '),
220+
'commands must be one of: ' .. table.concat(commands, ', ') .. ' or false to disable',
219221
}
220222
end
221223
end

lua/blink/cmp/keymap/apply.lua

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ function apply.keymap_to_current_buffer(keys_to_commands)
1212

1313
-- insert mode: uses both snippet and insert commands
1414
for key, commands in pairs(keys_to_commands) do
15-
if #commands == 0 then goto continue end
16-
1715
local fallback = require('blink.cmp.keymap.fallback').wrap('i', key)
1816
apply.set('i', key, function()
1917
if not require('blink.cmp.config').enabled() then return fallback() end
@@ -33,13 +31,11 @@ function apply.keymap_to_current_buffer(keys_to_commands)
3331
end
3432
end
3533
end)
36-
37-
::continue::
3834
end
3935

4036
-- snippet mode: uses only snippet commands
4137
for key, commands in pairs(keys_to_commands) do
42-
if not apply.has_snippet_commands(commands) or #commands == 0 then goto continue end
38+
if not apply.has_snippet_commands(commands) then goto continue end
4339

4440
local fallback = require('blink.cmp.keymap.fallback').wrap('s', key)
4541
apply.set('s', key, function()
@@ -88,7 +84,7 @@ function apply.term_keymaps(keys_to_commands)
8884

8985
-- terminal mode: uses insert commands only
9086
for key, commands in pairs(keys_to_commands) do
91-
if not apply.has_insert_command(commands) or #commands == 0 then goto continue end
87+
if not apply.has_insert_command(commands) then goto continue end
9288

9389
local fallback = require('blink.cmp.keymap.fallback').wrap('i', key)
9490
apply.set('t', key, function()
@@ -120,7 +116,7 @@ function apply.cmdline_keymaps(keys_to_commands)
120116

121117
-- cmdline mode: uses only insert commands
122118
for key, commands in pairs(keys_to_commands) do
123-
if not apply.has_insert_command(commands) or #commands == 0 then goto continue end
119+
if not apply.has_insert_command(commands) then goto continue end
124120

125121
local fallback = require('blink.cmp.keymap.fallback').wrap('c', key)
126122
apply.set('c', key, function()

lua/blink/cmp/keymap/init.lua

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
local keymap = {}
22

33
--- Lowercases all keys in the mappings table
4-
--- @param existing_mappings table<string, blink.cmp.KeymapCommand[]>
5-
--- @param new_mappings table<string, blink.cmp.KeymapCommand[]>
6-
--- @return table<string, blink.cmp.KeymapCommand[]>
4+
--- @param existing_mappings table<string, blink.cmp.KeymapCommand[] | false>
5+
--- @param new_mappings table<string, blink.cmp.KeymapCommand[] | false>
6+
--- @return table<string, blink.cmp.KeymapCommand[] | false>
77
function keymap.merge_mappings(existing_mappings, new_mappings)
88
local merged_mappings = vim.deepcopy(existing_mappings)
99
for new_key, new_mapping in pairs(new_mappings) do
@@ -28,13 +28,19 @@ end
2828

2929
--- @param keymap_config blink.cmp.KeymapConfig
3030
--- @param mode blink.cmp.Mode
31+
--- @return table<string, blink.cmp.KeymapCommand[]>
3132
function keymap.get_mappings(keymap_config, mode)
3233
local mappings = vim.deepcopy(keymap_config)
3334

34-
-- Remove unused keys
35+
-- Remove unused keys, but keep keys set to false or empty tables (to disable them)
3536
if mode ~= 'default' then
3637
for key, commands in pairs(mappings) do
37-
if key ~= 'preset' and not require('blink.cmp.keymap.apply').has_insert_command(commands) and #commands ~= 0 then
38+
if
39+
key ~= 'preset'
40+
and commands ~= false
41+
and #commands ~= 0
42+
and not require('blink.cmp.keymap.apply').has_insert_command(commands)
43+
then
3844
mappings[key] = nil
3945
end
4046
end
@@ -51,6 +57,14 @@ function keymap.get_mappings(keymap_config, mode)
5157
-- User-defined keymaps overwrite the preset keymaps
5258
mappings = keymap.merge_mappings(preset_keymap, mappings)
5359
end
60+
--- @cast mappings table<string, blink.cmp.KeymapCommand[] | false>
61+
62+
-- Remove keys explicitly disabled by user (set to false or no commands)
63+
for key, commands in pairs(mappings) do
64+
if commands == false or #commands == 0 then mappings[key] = nil end
65+
end
66+
--- @cast mappings table<string, blink.cmp.KeymapCommand[]>
67+
5468
return mappings
5569
end
5670

lua/blink/cmp/keymap/presets.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
--- @type table<string, table<string, blink.cmp.KeymapCommand[]>>
2-
local presets = {
1+
local presets = {}
2+
3+
--- @type table<string, table<string, blink.cmp.KeymapCommand[] | false>>
4+
local presets_keymaps = {
35
none = {},
46

57
default = {
@@ -86,9 +88,9 @@ local presets = {
8688

8789
--- Gets the preset keymap for the given preset name
8890
--- @param name string
89-
--- @return table<string, blink.cmp.KeymapCommand[]>
91+
--- @return table<string, blink.cmp.KeymapCommand[] | false>
9092
function presets.get(name)
91-
local preset = presets[name]
93+
local preset = presets_keymaps[name]
9294
if preset == nil then error('Invalid blink.cmp keymap preset: ' .. name) end
9395
return preset
9496
end

0 commit comments

Comments
 (0)