From bb6666d9937e767e913e909ee645ce7757381198 Mon Sep 17 00:00:00 2001 From: kedom1337 Date: Fri, 1 Nov 2024 21:46:23 +0100 Subject: [PATCH 1/3] feat: allow merging of keymap preset with custom keymap --- README.md | 29 +++++++++++++++++++++-------- lua/blink/cmp/keymap.lua | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ea0e1d35..0ac0c25a 100644 --- a/README.md +++ b/README.md @@ -160,10 +160,27 @@ MiniDeps.add({ ```lua { - -- the keymap may be a preset ('default' | 'super-tab') 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 keymap can be: + -- - A preset ('default' | 'super-tab') + -- - 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' }, @@ -203,10 +220,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 c672c033..5c99b545 100644 --- a/lua/blink/cmp/keymap.lua +++ b/lua/blink/cmp/keymap.lua @@ -71,6 +71,25 @@ 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 + if opts.preset == 'default' then + preset_keymap = default_keymap + elseif opts.preset == 'super-tab' then + preset_keymap = super_tab_keymap + else + error('Invalid blink.cmp keymap preset: ' .. opts.preset) + end + + -- 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 From 0d34d285f9e30fcf0ab4f19cecb6cc189119e904 Mon Sep 17 00:00:00 2001 From: kedom1337 Date: Fri, 1 Nov 2024 22:07:19 +0100 Subject: [PATCH 2/3] refactor: extract getting preset into function --- lua/blink/cmp/keymap.lua | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/lua/blink/cmp/keymap.lua b/lua/blink/cmp/keymap.lua index 5c99b545..22adbee7 100644 --- a/lua/blink/cmp/keymap.lua +++ b/lua/blink/cmp/keymap.lua @@ -74,18 +74,10 @@ function keymap.setup(opts) -- Handle preset inside table if opts.preset then - local preset_keymap - if opts.preset == 'default' then - preset_keymap = default_keymap - elseif opts.preset == 'super-tab' then - preset_keymap = super_tab_keymap - else - error('Invalid blink.cmp keymap preset: ' .. opts.preset) - end + 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) @@ -94,13 +86,7 @@ function keymap.setup(opts) -- handle presets if type(opts) == 'string' then - if opts == 'default' then - mappings = default_keymap - elseif opts == 'super-tab' then - mappings = super_tab_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 +100,19 @@ 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) + if preset_name == 'default' then + return default_keymap + elseif preset_name == 'super-tab' then + return super_tab_keymap + else + error('Invalid blink.cmp keymap preset: ' .. preset_name) + end +end + --- Applies the keymaps to the current buffer --- @param keys_to_commands table function keymap.apply_keymap_to_current_buffer(keys_to_commands) From 7bc32d092981e071958c923ae662c9bb1bffcc0f Mon Sep 17 00:00:00 2001 From: kedom1337 Date: Fri, 1 Nov 2024 22:16:56 +0100 Subject: [PATCH 3/3] docs: remove duplciate keymap documentation --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index 78335146..2ba9b35b 100644 --- a/README.md +++ b/README.md @@ -239,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 = {