Skip to content

Commit 6b46164

Browse files
authored
feat: allow merging of keymap preset with custom keymap (#233)
* feat: allow merging of keymap preset with custom keymap * refactor: extract getting preset into function * docs: remove duplciate keymap documentation
1 parent a253b35 commit 6b46164

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

README.md

+21-13
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,27 @@ MiniDeps.add({
161161

162162
```lua
163163
{
164-
-- the keymap may be a preset ('default' | 'super-tab' | 'enter') OR a table of keys => command[]
165-
-- when defining your own, no keybinds will be assigned automatically.
166-
-- you may pass a function in the command array where returning true
167-
-- will prevent the next command from running
168-
--
169-
-- The "fallback" command will run the next non-blink keymap.
170-
-- For example, to accept the current completion item with "enter", or create a new line,
171-
-- when the blink window is closed, you would define it as:
172-
-- ['<CR>'] = { 'accept', 'fallback' }
164+
-- The keymap can be:
165+
-- - A preset ('default' | 'super-tab' | 'enter')
166+
-- - A table of keys => command[]
167+
-- - A table that includes a 'preset' key and custom key mappings
168+
--
169+
-- When specifying 'preset' in the keymap table, the custom key mappings are merged with the preset,
170+
-- and any conflicting keys will overwrite the preset mappings.
171+
--
172+
-- Example:
173+
--
174+
-- keymap = {
175+
-- preset = 'default',
176+
-- ['<cr>'] = { 'select_and_accept', 'fallback' },
177+
-- },
178+
--
179+
-- In this example, the 'default' preset is used, and the `<cr>` key mapping is added or overwrites the existing one from the preset.
180+
-- When defining your own keymaps without a preset, no keybinds will be assigned automatically.
181+
--
182+
-- Available commands:
183+
-- show, hide, accept, select_and_accept, select_prev, select_next, show_documentation, hide_documentation,
184+
-- scroll_documentation_up, scroll_documentation_down, snippet_forward, snippet_backward, fallback
173185
--
174186
-- "default" keymap
175187
-- ['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
@@ -227,10 +239,6 @@ MiniDeps.add({
227239
--
228240
-- ['<C-b>'] = { 'scroll_documentation_up', 'fallback' },
229241
-- ['<C-f>'] = { 'scroll_documentation_down', 'fallback' },
230-
--
231-
-- available commands:
232-
-- show, hide, accept, select_and_accept, select_prev, select_next, show_documentation, hide_documentation,
233-
-- scroll_documentation_up, scroll_documentation_down, snippet_forward, snippet_backward, fallback
234242
keymap = 'default',
235243

236244
accept = {

lua/blink/cmp/keymap.lua

+30-9
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,22 @@ function keymap.setup(opts)
8888
error('The blink.cmp keymap recently got reworked. Please see the README for the updated configuration')
8989
end
9090
end
91+
92+
-- Handle preset inside table
93+
if opts.preset then
94+
local preset_keymap = keymap.get_preset_keymap(opts.preset)
95+
96+
-- Remove 'preset' key from opts to prevent it from being treated as a keymap
97+
opts.preset = nil
98+
-- Merge the preset keymap with the user-defined keymaps
99+
-- User-defined keymaps overwrite the preset keymaps
100+
mappings = vim.tbl_extend('force', preset_keymap, opts)
101+
end
91102
end
92103

93104
-- handle presets
94105
if type(opts) == 'string' then
95-
if opts == 'default' then
96-
mappings = default_keymap
97-
elseif opts == 'super-tab' then
98-
mappings = super_tab_keymap
99-
elseif opts == 'enter' then
100-
mappings = enter_keymap
101-
else
102-
error('Invalid blink.cmp keymap preset: ' .. opts)
103-
end
106+
mappings = keymap.get_preset_keymap(opts)
104107
end
105108

106109
-- we set on the buffer directly to avoid buffer-local keymaps (such as from autopairs)
@@ -114,6 +117,24 @@ function keymap.setup(opts)
114117
})
115118
end
116119

120+
--- Gets the preset keymap for the given preset name
121+
--- @param preset_name string
122+
--- @return table
123+
function keymap.get_preset_keymap(preset_name)
124+
local mappings
125+
if preset_name == 'default' then
126+
mappings = default_keymap
127+
elseif preset_name == 'super-tab' then
128+
mappings = super_tab_keymap
129+
elseif preset_name == 'enter' then
130+
mappings = enter_keymap
131+
else
132+
error('Invalid blink.cmp keymap preset: ' .. preset_name)
133+
end
134+
135+
return mappings
136+
end
137+
117138
--- Applies the keymaps to the current buffer
118139
--- @param keys_to_commands table<string, blink.cmp.KeymapCommand[]>
119140
function keymap.apply_keymap_to_current_buffer(keys_to_commands)

0 commit comments

Comments
 (0)