Skip to content

Commit

Permalink
feat: allow merging of keymap preset with custom keymap (#233)
Browse files Browse the repository at this point in the history
* feat: allow merging of keymap preset with custom keymap

* refactor: extract getting preset into function

* docs: remove duplciate keymap documentation
  • Loading branch information
kedom1337 authored Nov 1, 2024
1 parent a253b35 commit 6b46164
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
-- ['<CR>'] = { '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',
-- ['<cr>'] = { 'select_and_accept', 'fallback' },
-- },
--
-- In this example, the 'default' preset is used, and the `<cr>` 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
-- ['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
Expand Down Expand Up @@ -227,10 +239,6 @@ MiniDeps.add({
--
-- ['<C-b>'] = { 'scroll_documentation_up', 'fallback' },
-- ['<C-f>'] = { '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 = {
Expand Down
39 changes: 30 additions & 9 deletions lua/blink/cmp/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<string, blink.cmp.KeymapCommand[]>
function keymap.apply_keymap_to_current_buffer(keys_to_commands)
Expand Down

0 comments on commit 6b46164

Please sign in to comment.