Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow merging of keymap preset with custom keymap #233

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
-- ['<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 @@ -203,10 +220,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
19 changes: 19 additions & 0 deletions lua/blink/cmp/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Saghen marked this conversation as resolved.
Show resolved Hide resolved

-- 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
Expand Down