Skip to content

Commit

Permalink
feat(cmdline): allow configuring separate cmdline preset (#532)
Browse files Browse the repository at this point in the history
* feat(cmdline): allow configuring separate cmdline preset

* fix: remove debug

* fix(cmdline): remove up/down from default cmdline keymaps

* feat: add `keymap.cmdline` option, remove arrow keys from default presets

* chore: remove unused cmdline_keymap

---------

Closes #514

Co-authored-by: Liam Dyer <[email protected]>
  • Loading branch information
folke and Saghen authored Dec 13, 2024
1 parent f99b03c commit 13b3e57
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ MiniDeps.add({
--
-- -- show with a list of providers
-- ['<C-space>'] = { 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.
--
Expand Down Expand Up @@ -250,7 +255,7 @@ MiniDeps.add({
--
-- ['<C-b>'] = { 'scroll_documentation_up', 'fallback' },
-- ['<C-f>'] = { '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,
Expand Down
36 changes: 27 additions & 9 deletions lua/blink/cmp/config/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
--- {
--- ['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
--- ['<C-e>'] = { 'hide' },
--- ['<C-e>'] = { 'cancel', 'fallback' },
--- ['<C-y>'] = { 'select_and_accept' },
---
--- ['<C-p>'] = { 'select_prev', 'fallback' },
Expand All @@ -39,7 +40,7 @@
--- ```lua
--- {
--- ['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
--- ['<C-e>'] = { 'hide', 'fallback' },
--- ['<C-e>'] = { 'cancel', 'fallback' },
---
--- ['<Tab>'] = {
--- function(cmp)
Expand All @@ -66,7 +67,7 @@
--- ```lua
--- {
--- ['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
--- ['<C-e>'] = { 'hide', 'fallback' },
--- ['<C-e>'] = { 'cancel', 'fallback' },
--- ['<CR>'] = { 'accept', 'fallback' },
---
--- ['<Tab>'] = { 'snippet_forward', 'fallback' },
Expand All @@ -88,20 +89,30 @@
---
--- Example:
---
--- ```lua
--- keymap = {
--- preset = 'default',
--- ['<Up>'] = { 'select_prev', 'fallback' },
--- ['<Down>'] = { 'select_next', 'fallback' },
---
--- -- disable a keymap from the preset
--- ['<C-e>'] = {},
--- },
---
--- -- 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 = {
Expand Down Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions lua/blink/cmp/keymap/init.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
10 changes: 5 additions & 5 deletions lua/blink/cmp/keymap/presets.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local presets = {
none = {},

default = {
['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
['<C-e>'] = { 'hide', 'fallback' },
['<C-e>'] = { 'cancel', 'fallback' },
['<C-y>'] = { 'select_and_accept' },

['<Up>'] = { 'select_prev', 'fallback' },
['<Down>'] = { 'select_next', 'fallback' },
['<C-p>'] = { 'select_prev', 'fallback' },
['<C-n>'] = { 'select_next', 'fallback' },

Expand All @@ -18,7 +18,7 @@ local presets = {

['super-tab'] = {
['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
['<C-e>'] = { 'hide', 'fallback' },
['<C-e>'] = { 'cancel', 'fallback' },

['<Tab>'] = {
function(cmp)
Expand All @@ -44,7 +44,7 @@ local presets = {

enter = {
['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
['<C-e>'] = { 'hide', 'fallback' },
['<C-e>'] = { 'cancel', 'fallback' },
['<CR>'] = { 'accept', 'fallback' },

['<Tab>'] = { 'snippet_forward', 'fallback' },
Expand Down

0 comments on commit 13b3e57

Please sign in to comment.