diff --git a/README.md b/README.md index 08fd2368..c4b6b74a 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,12 @@ MiniDeps.add({ -- -- -- show with a list of providers -- [''] = { function(cmp) cmp.show({ providers = { 'snippets' } }) end }, - -- }, + -- + -- -- optionally, define different keymaps for cmdline + -- cmdline = { + -- preset = 'super-tab' + -- } + -- } -- -- When defining your own keymaps without a preset, no keybinds will be assigned automatically. -- @@ -250,7 +255,7 @@ MiniDeps.add({ -- -- [''] = { 'scroll_documentation_up', 'fallback' }, -- [''] = { 'scroll_documentation_down', 'fallback' }, - keymap = 'default', + keymap = { preset = 'default' }, -- Enables keymaps, completions and signature help when true enabled = function() return vim.bo.buftype ~= "prompt" end, diff --git a/lua/blink/cmp/config/keymap.lua b/lua/blink/cmp/config/keymap.lua index e7301aa5..a51ffb56 100644 --- a/lua/blink/cmp/config/keymap.lua +++ b/lua/blink/cmp/config/keymap.lua @@ -16,11 +16,12 @@ --- | (fun(cmp: table): boolean?) Custom function where returning true will prevent the next command from running --- @alias blink.cmp.KeymapPreset +--- | 'none' No keymaps --- Mappings similar to the built-in completion: --- ```lua --- { --- [''] = { 'show', 'show_documentation', 'hide_documentation' }, ---- [''] = { 'hide' }, +--- [''] = { 'cancel', 'fallback' }, --- [''] = { 'select_and_accept' }, --- --- [''] = { 'select_prev', 'fallback' }, @@ -39,7 +40,7 @@ --- ```lua --- { --- [''] = { 'show', 'show_documentation', 'hide_documentation' }, ---- [''] = { 'hide', 'fallback' }, +--- [''] = { 'cancel', 'fallback' }, --- --- [''] = { --- function(cmp) @@ -66,7 +67,7 @@ --- ```lua --- { --- [''] = { 'show', 'show_documentation', 'hide_documentation' }, ---- [''] = { 'hide', 'fallback' }, +--- [''] = { 'cancel', 'fallback' }, --- [''] = { 'accept', 'fallback' }, --- --- [''] = { 'snippet_forward', 'fallback' }, @@ -88,6 +89,7 @@ --- --- Example: --- +--- ```lua --- keymap = { --- preset = 'default', --- [''] = { 'select_prev', 'fallback' }, @@ -95,13 +97,22 @@ --- --- -- disable a keymap from the preset --- [''] = {}, ---- }, +--- +--- -- optionally, define different keymaps for cmdline +--- cmdline = { +--- preset = 'cmdline', +--- } +--- } +--- ``` --- --- When defining your own keymaps without a preset, no keybinds will be assigned automatically. ---- @class (exact) blink.cmp.KeymapConfig +--- @class (exact) blink.cmp.BaseKeymapConfig --- @field preset? blink.cmp.KeymapPreset --- @field [string] blink.cmp.KeymapCommand[]> Table of keys => commands[] +--- @class (exact) blink.cmp.KeymapConfig : blink.cmp.BaseKeymapConfig +--- @field cmdline? blink.cmp.BaseKeymapConfig Optionally, define a separate keymap for cmdline + local keymap = { --- @type blink.cmp.KeymapConfig default = { @@ -130,16 +141,23 @@ function keymap.validate(config) local presets = { 'default', 'super-tab', 'enter' } local validation_schema = {} - for key, command_or_preset in pairs(config) do - if key == 'preset' then + for key, value in pairs(config) do + -- nested cmdline keymap + if key == 'cmdline' then + keymap.validate(value) + + -- preset + elseif key == 'preset' then validation_schema[key] = { - command_or_preset, + value, function(preset) return vim.tbl_contains(presets, preset) end, 'one of: ' .. table.concat(presets, ', '), } + + -- key else validation_schema[key] = { - command_or_preset, + value, function(key_commands) for _, command in ipairs(key_commands) do if type(command) ~= 'function' and not vim.tbl_contains(commands, command) then return false end diff --git a/lua/blink/cmp/keymap/init.lua b/lua/blink/cmp/keymap/init.lua index 303dda18..f08876b7 100644 --- a/lua/blink/cmp/keymap/init.lua +++ b/lua/blink/cmp/keymap/init.lua @@ -1,7 +1,8 @@ local keymap = {} -function keymap.setup() - local mappings = vim.deepcopy(require('blink.cmp.config').keymap) +---@param keymap_config blink.cmp.BaseKeymapConfig +function keymap.get_mappings(keymap_config) + local mappings = vim.deepcopy(keymap_config) -- Handle preset if mappings.preset then @@ -14,7 +15,12 @@ function keymap.setup() -- User-defined keymaps overwrite the preset keymaps mappings = vim.tbl_extend('force', preset_keymap, mappings) end + return mappings +end +function keymap.setup() + local config = require('blink.cmp.config') + local mappings = keymap.get_mappings(config.keymap) -- We set on the buffer directly to avoid buffer-local keymaps (such as from autopairs) -- from overriding our mappings. We also use InsertEnter to avoid conflicts with keymaps -- applied on other autocmds, such as LspAttach used by nvim-lspconfig and most configs @@ -34,7 +40,8 @@ function keymap.setup() -- Apply cmdline keymaps since they're global, if any sources are defined local cmdline_sources = require('blink.cmp.config').sources.cmdline if type(cmdline_sources) ~= 'table' or #cmdline_sources > 0 then - require('blink.cmp.keymap.apply').cmdline_keymaps(mappings) + local cmdline_mappings = keymap.get_mappings(config.keymap.cmdline or config.keymap) + require('blink.cmp.keymap.apply').cmdline_keymaps(cmdline_mappings) end end diff --git a/lua/blink/cmp/keymap/presets.lua b/lua/blink/cmp/keymap/presets.lua index 60a67957..1e7de161 100644 --- a/lua/blink/cmp/keymap/presets.lua +++ b/lua/blink/cmp/keymap/presets.lua @@ -1,11 +1,11 @@ local presets = { + none = {}, + default = { [''] = { 'show', 'show_documentation', 'hide_documentation' }, - [''] = { 'hide', 'fallback' }, + [''] = { 'cancel', 'fallback' }, [''] = { 'select_and_accept' }, - [''] = { 'select_prev', 'fallback' }, - [''] = { 'select_next', 'fallback' }, [''] = { 'select_prev', 'fallback' }, [''] = { 'select_next', 'fallback' }, @@ -18,7 +18,7 @@ local presets = { ['super-tab'] = { [''] = { 'show', 'show_documentation', 'hide_documentation' }, - [''] = { 'hide', 'fallback' }, + [''] = { 'cancel', 'fallback' }, [''] = { function(cmp) @@ -44,7 +44,7 @@ local presets = { enter = { [''] = { 'show', 'show_documentation', 'hide_documentation' }, - [''] = { 'hide', 'fallback' }, + [''] = { 'cancel', 'fallback' }, [''] = { 'accept', 'fallback' }, [''] = { 'snippet_forward', 'fallback' },