diff --git a/README.md b/README.md index 7c310bc3..2ba9b35b 100644 --- a/README.md +++ b/README.md @@ -161,15 +161,27 @@ MiniDeps.add({ ```lua { - -- the keymap may be a preset ('default' | 'super-tab' | 'enter') OR a table of keys => command[] - -- when defining your own, no keybinds will be assigned automatically. - -- you may pass a function in the command array where returning true - -- will prevent the next command from running - -- - -- The "fallback" command will run the next non-blink keymap. - -- For example, to accept the current completion item with "enter", or create a new line, - -- when the blink window is closed, you would define it as: - -- [''] = { 'accept', 'fallback' } + -- The keymap can be: + -- - A preset ('default' | 'super-tab' | 'enter') + -- - A table of keys => command[] + -- - A table that includes a 'preset' key and custom key mappings + -- + -- When specifying 'preset' in the keymap table, the custom key mappings are merged with the preset, + -- and any conflicting keys will overwrite the preset mappings. + -- + -- Example: + -- + -- keymap = { + -- preset = 'default', + -- [''] = { 'select_and_accept', 'fallback' }, + -- }, + -- + -- In this example, the 'default' preset is used, and the `` key mapping is added or overwrites the existing one from the preset. + -- When defining your own keymaps without a preset, no keybinds will be assigned automatically. + -- + -- Available commands: + -- show, hide, accept, select_and_accept, select_prev, select_next, show_documentation, hide_documentation, + -- scroll_documentation_up, scroll_documentation_down, snippet_forward, snippet_backward, fallback -- -- "default" keymap -- [''] = { 'show', 'show_documentation', 'hide_documentation' }, @@ -227,10 +239,6 @@ MiniDeps.add({ -- -- [''] = { 'scroll_documentation_up', 'fallback' }, -- [''] = { 'scroll_documentation_down', 'fallback' }, - -- - -- available commands: - -- show, hide, accept, select_and_accept, select_prev, select_next, show_documentation, hide_documentation, - -- scroll_documentation_up, scroll_documentation_down, snippet_forward, snippet_backward, fallback keymap = 'default', accept = { diff --git a/lua/blink/cmp/keymap.lua b/lua/blink/cmp/keymap.lua index 530a6b75..ed0bc20a 100644 --- a/lua/blink/cmp/keymap.lua +++ b/lua/blink/cmp/keymap.lua @@ -88,19 +88,22 @@ function keymap.setup(opts) error('The blink.cmp keymap recently got reworked. Please see the README for the updated configuration') end end + + -- Handle preset inside table + if opts.preset then + local preset_keymap = keymap.get_preset_keymap(opts.preset) + + -- Remove 'preset' key from opts to prevent it from being treated as a keymap + opts.preset = nil + -- Merge the preset keymap with the user-defined keymaps + -- User-defined keymaps overwrite the preset keymaps + mappings = vim.tbl_extend('force', preset_keymap, opts) + end end -- handle presets if type(opts) == 'string' then - if opts == 'default' then - mappings = default_keymap - elseif opts == 'super-tab' then - mappings = super_tab_keymap - elseif opts == 'enter' then - mappings = enter_keymap - else - error('Invalid blink.cmp keymap preset: ' .. opts) - end + mappings = keymap.get_preset_keymap(opts) end -- we set on the buffer directly to avoid buffer-local keymaps (such as from autopairs) @@ -114,6 +117,24 @@ function keymap.setup(opts) }) end +--- Gets the preset keymap for the given preset name +--- @param preset_name string +--- @return table +function keymap.get_preset_keymap(preset_name) + local mappings + if preset_name == 'default' then + mappings = default_keymap + elseif preset_name == 'super-tab' then + mappings = super_tab_keymap + elseif preset_name == 'enter' then + mappings = enter_keymap + else + error('Invalid blink.cmp keymap preset: ' .. preset_name) + end + + return mappings +end + --- Applies the keymaps to the current buffer --- @param keys_to_commands table function keymap.apply_keymap_to_current_buffer(keys_to_commands)