Skip to content

Commit 3fd92f0

Browse files
committed
feat!: rework keymap config
1 parent e20d773 commit 3fd92f0

File tree

4 files changed

+239
-101
lines changed

4 files changed

+239
-101
lines changed

README.md

+54-19
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
---@module 'blink.cmp'
4747
---@type blink.cmp.Config
4848
opts = {
49+
-- 'default' for mappings similar to built-in completion
50+
-- 'super-tab' for mappings similar to vscode (tab to accept, arrow keys to navigate)
51+
-- see the "default configuration" section below for full documentation on how to define
52+
-- your own keymap
53+
keymap = 'default',
54+
4955
highlight = {
5056
-- sets the fallback highlight groups to nvim-cmp's highlight groups
5157
-- useful for when your theme doesn't support blink.cmp
@@ -135,24 +141,53 @@ MiniDeps.add({
135141

136142
```lua
137143
{
138-
-- for keymap, all values may be string | string[]
139-
-- use an empty table to disable a keymap
140-
keymap = {
141-
show = '<C-space>',
142-
hide = '<C-e>',
143-
accept = '<Tab>',
144-
select_and_accept = {},
145-
select_prev = { '<Up>', '<C-p>' },
146-
select_next = { '<Down>', '<C-n>' },
147-
148-
show_documentation = '<C-space>',
149-
hide_documentation = '<C-space>',
150-
scroll_documentation_up = '<C-b>',
151-
scroll_documentation_down = '<C-f>',
152-
153-
snippet_forward = '<Tab>',
154-
snippet_backward = '<S-Tab>',
155-
},
144+
-- the keymap may be a preset ('default' | 'super-tab') or a table of keys => command[]
145+
-- additionally, you may pass a function in the command array where returning true
146+
-- will prevent the next command from running
147+
--
148+
-- "default" keymap
149+
-- ['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
150+
-- ['<C-e>'] = { 'hide' },
151+
-- ['<C-y>'] = { 'accept' },
152+
--
153+
-- ['<C-p>'] = { 'select_prev', 'fallback' },
154+
-- ['<C-n>'] = { 'select_next', 'fallback' },
155+
--
156+
-- ['<C-b>'] = { 'scroll_documentation_up', 'fallback' },
157+
-- ['<C-f>'] = { 'scroll_documentation_down', 'fallback' },
158+
--
159+
-- ['<Tab>'] = { 'snippet_forward', 'fallback' },
160+
-- ['<S-Tab>'] = { 'snippet_backward', 'fallback' },
161+
--
162+
-- "super-tab" keymap
163+
-- you may want to set `trigger.show_in_snippet = false` when using "super-tab"
164+
-- or use `window.autocomplete.selection = "manual" | "auto_insert"`
165+
--
166+
-- ['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
167+
-- ['<C-e>'] = { 'hide' },
168+
--
169+
-- ['<Tab>'] = {
170+
-- function(cmp)
171+
-- if cmp.is_in_snippet() then return cmp.accept()
172+
-- else return cmp.select_and_accept() end
173+
-- end,
174+
-- 'snippet_forward',
175+
-- 'fallback'
176+
-- },
177+
-- ['<S-Tab>'] = { 'snippet_backward', 'fallback' },
178+
--
179+
-- ['<Up>'] = { 'select_prev', 'fallback' },
180+
-- ['<Down>'] = { 'select_next', 'fallback' },
181+
-- ['<C-p>'] = { 'select_prev', 'fallback' },
182+
-- ['<C-n>'] = { 'select_next', 'fallback' },
183+
--
184+
-- ['<C-b>'] = { 'scroll_documentation_up', 'fallback' },
185+
-- ['<C-f>'] = { 'scroll_documentation_down', 'fallback' },
186+
--
187+
-- available commands:
188+
-- show, hide, accept, select_and_accept, select_prev, select_next, show_documentation, hide_documentation,
189+
-- scroll_documentation_up, scroll_documentation_down, snippet_forward, snippet_backward, fallback
190+
keymap = 'default',
156191

157192
accept = {
158193
create_undo_point = true,
@@ -199,7 +234,7 @@ MiniDeps.add({
199234
-- list of additional trigger characters that won't trigger the completion window when the cursor comes after a trigger character when entering insert mode
200235
show_on_insert_blocked_trigger_characters = { "'", '"' },
201236
-- when false, will not show the completion window when in a snippet
202-
show_in_snippet = false,
237+
show_in_snippet = true,
203238
},
204239

205240
signature_help = {

lua/blink/cmp/config.lua

+68-30
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
--- @class blink.cmp.KeymapConfig
2-
--- @field show? string | string[]
3-
--- @field accept? string | string[]
4-
--- @field select_prev? string | string[]
5-
--- @field select_next? string | string[]
6-
--- @field show_documentation? string | string[]
7-
--- @field hide_documentation? string | string[]
8-
--- @field scroll_documentation_up? string | string[]
9-
--- @field scroll_documentation_down? string | string[]
10-
--- @field snippet_forward? string | string[]
11-
--- @field snippet_backward? string | string[]
1+
--- @alias blink.cmp.KeymapCommand
2+
--- | 'fallback' Fallback to the built-in behavior
3+
--- | 'show' Show the completion window
4+
--- | 'hide' Hide the completion window
5+
--- | 'accept' Accept the current completion item
6+
--- | 'select_and_accept' Select the current completion item and accept it
7+
--- | 'select_prev' Select the previous completion item
8+
--- | 'select_next' Select the next completion item
9+
--- | 'show_documentation' Show the documentation window
10+
--- | 'hide_documentation' Hide the documentation window
11+
--- | 'scroll_documentation_up' Scroll the documentation window up
12+
--- | 'scroll_documentation_down' Scroll the documentation window down
13+
--- | 'snippet_forward' Move the cursor forward to the next snippet placeholder
14+
--- | 'snippet_backward' Move the cursor backward to the previous snippet placeholder
15+
--- | (fun(cmp: table): boolean?) Custom function where returning true will prevent the next command from running
16+
---
17+
--- @alias blink.cmp.KeymapConfig
18+
--- | table<string, blink.cmp.KeymapCommand[]> Table of keys => commands[]
19+
--- | 'default' mappings similar to built-in completion
20+
--- | 'super-tab' mappings similar to vscode (tab to accept, arrow keys to navigate)
1221

1322
--- @class blink.cmp.AcceptConfig
1423
--- @field create_undo_point? boolean Create an undo point when accepting a completion item
@@ -142,7 +151,7 @@
142151
--- @field enabled? boolean
143152

144153
--- @class blink.cmp.Config
145-
--- @field keymap? blink.cmp.KeymapConfig
154+
--- @field keymap? blink.cmp.KeymapConfig | 'default' | 'super-tab'
146155
--- @field accept? blink.cmp.AcceptConfig
147156
--- @field trigger? blink.cmp.TriggerConfig
148157
--- @field fuzzy? blink.cmp.FuzzyConfig
@@ -155,24 +164,53 @@
155164

156165
--- @type blink.cmp.Config
157166
local config = {
158-
-- for keymap, all values may be string | string[]
159-
-- use an empty table to disable a keymap
160-
keymap = {
161-
show = '<C-space>',
162-
hide = '<C-e>',
163-
accept = '<Tab>',
164-
select_and_accept = {},
165-
select_prev = { '<Up>', '<C-p>' },
166-
select_next = { '<Down>', '<C-n>' },
167-
168-
show_documentation = '<C-space>',
169-
hide_documentation = '<C-space>',
170-
scroll_documentation_up = '<C-b>',
171-
scroll_documentation_down = '<C-f>',
172-
173-
snippet_forward = '<Tab>',
174-
snippet_backward = '<S-Tab>',
175-
},
167+
-- the keymap may be a preset ('default' | 'super-tab') or a table of keys => command[]
168+
-- additionally, you may pass a function in the command array where returning true
169+
-- will prevent the next command from running
170+
--
171+
-- "default" keymap
172+
-- ['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
173+
-- ['<C-e>'] = { 'hide' },
174+
-- ['<C-y>'] = { 'accept' },
175+
--
176+
-- ['<C-p>'] = { 'select_prev', 'fallback' },
177+
-- ['<C-n>'] = { 'select_next', 'fallback' },
178+
--
179+
-- ['<C-b>'] = { 'scroll_documentation_up', 'fallback' },
180+
-- ['<C-f>'] = { 'scroll_documentation_down', 'fallback' },
181+
--
182+
-- ['<Tab>'] = { 'snippet_forward', 'fallback' },
183+
-- ['<S-Tab>'] = { 'snippet_backward', 'fallback' },
184+
--
185+
-- "super-tab" keymap
186+
-- you may want to set `trigger.show_in_snippet = false` when using "super-tab"
187+
-- or use `window.autocomplete.selection = "manual" | "auto_insert"`
188+
--
189+
-- ['<C-space>'] = { 'show', 'show_documentation', 'hide_documentation' },
190+
-- ['<C-e>'] = { 'hide' },
191+
--
192+
-- ['<Tab>'] = {
193+
-- function(cmp)
194+
-- if cmp.is_in_snippet() then return cmp.accept()
195+
-- else return cmp.select_and_accept() end
196+
-- end,
197+
-- 'snippet_forward',
198+
-- 'fallback'
199+
-- },
200+
-- ['<S-Tab>'] = { 'snippet_backward', 'fallback' },
201+
--
202+
-- ['<Up>'] = { 'select_prev', 'fallback' },
203+
-- ['<Down>'] = { 'select_next', 'fallback' },
204+
-- ['<C-p>'] = { 'select_prev', 'fallback' },
205+
-- ['<C-n>'] = { 'select_next', 'fallback' },
206+
--
207+
-- ['<C-b>'] = { 'scroll_documentation_up', 'fallback' },
208+
-- ['<C-f>'] = { 'scroll_documentation_down', 'fallback' },
209+
--
210+
-- available commands:
211+
-- show, hide, accept, select_and_accept, select_prev, select_next, show_documentation, hide_documentation,
212+
-- scroll_documentation_up, scroll_documentation_down, snippet_forward, snippet_backward, fallback
213+
keymap = 'default',
176214

177215
accept = {
178216
create_undo_point = true,

lua/blink/cmp/init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ cmp.scroll_documentation_down = function()
208208
return true
209209
end
210210

211+
cmp.is_in_snippet = function() return vim.snippet.active() end
212+
211213
cmp.snippet_forward = function()
212214
if not vim.snippet.active({ direction = 1 }) then return end
213215
vim.schedule(function() vim.snippet.jump(1) end)

0 commit comments

Comments
 (0)