Skip to content

Commit d6bad7b

Browse files
committed
feat: keymaps in config
1 parent 14a014d commit d6bad7b

File tree

3 files changed

+63
-32
lines changed

3 files changed

+63
-32
lines changed

lua/blink/cmp/config.lua

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
--- @class 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 snippet_forward string | string[]
7+
--- @field snippet_backward string | string[]
8+
19
--- @class TriggerConfig
210
--- @field context_regex string
311
--- @field blocked_trigger_characters string[]
@@ -63,6 +71,15 @@
6371

6472
--- @type CmpConfig
6573
local config = {
74+
keymap = {
75+
show = '<C-space>',
76+
hide = '<C-e>',
77+
accept = '<Tab>',
78+
select_prev = { '<Up>', '<C-j>' },
79+
select_next = { '<Down>', '<C-k>' },
80+
snippet_forward = '<Tab>',
81+
snippet_backward = '<S-Tab>',
82+
},
6683
trigger = {
6784
context_regex = '[%w_\\-]',
6885
blocked_trigger_characters = { ' ', '\n', '\t' },
@@ -142,6 +159,6 @@ local config = {
142159
local M = {}
143160

144161
--- @param opts CmpConfig
145-
function M.merge_with(opts) end
162+
function M.merge_with(opts) config = vim.tbl_deep_extend('force', config, opts or {}) end
146163

147164
return setmetatable(M, { __index = function(_, k) return config[k] end })

lua/blink/cmp/init.lua

+18-31
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ local cmp = {}
22

33
--- @param opts CmpConfig
44
cmp.setup = function(opts)
5-
require('blink.cmp.config').merge_with(opts)
5+
local config = require('blink.cmp.config')
6+
config.merge_with(opts)
7+
8+
require('blink.cmp.keymap').setup(config.keymap)
69

710
cmp.add_default_highlights()
811
vim.api.nvim_create_autocmd('ColorScheme', { callback = cmp.add_default_highlights })
912

13+
-- STRUCTURE
1014
-- trigger -> sources -> fuzzy (filter/sort) -> windows (render)
11-
--
12-
-- trigger controls when to show the window and the current context
13-
-- for caching
15+
16+
-- trigger controls when to show the window and the current context for caching
1417
cmp.trigger = require('blink.cmp.trigger').activate_autocmds()
1518

1619
-- sources fetch autocomplete items and documentation
@@ -28,11 +31,7 @@ cmp.setup = function(opts)
2831
cmp.fuzzy = require('blink.cmp.fuzzy')
2932
cmp.fuzzy.init_db(vim.fn.stdpath('data') .. '/blink/cmp/fuzzy.db')
3033

31-
local start_time = vim.loop.hrtime()
32-
cmp.trigger.listen_on_show(function(context)
33-
start_time = vim.loop.hrtime()
34-
cmp.sources.completions(context)
35-
end)
34+
cmp.trigger.listen_on_show(function(context) cmp.sources.completions(context) end)
3635
cmp.trigger.listen_on_hide(function()
3736
cmp.sources.cancel_completions()
3837
cmp.windows.autocomplete.close()
@@ -50,30 +49,13 @@ cmp.setup = function(opts)
5049
end)
5150
end
5251

53-
-- todo: dont default to cmp, use new hl groups
5452
cmp.add_default_highlights = function()
55-
--- @class Opts
56-
--- @field name string
57-
--- @field cmp_name string | nil
58-
--- @field default_name string
59-
60-
--- @param opts Opts
61-
local function default_to_cmp(opts)
62-
local cmp_hl_name = 'CmpItem' .. (opts.cmp_name or opts.name)
63-
local blink_hl_name = 'BlinkCmp' .. opts.name
64-
if vim.api.nvim_get_hl(0, { name = cmp_hl_name, create = false }) ~= nil then
65-
vim.api.nvim_set_hl(0, blink_hl_name, { link = cmp_hl_name, default = true })
66-
else
67-
vim.api.nvim_set_hl(0, blink_hl_name, { link = opts.default_name, default = true })
68-
end
69-
end
70-
71-
default_to_cmp({ name = 'Label', cmp_name = 'Abbr', default_name = 'Pmenu' })
72-
default_to_cmp({ name = 'LabelDeprecated', cmp_name = 'AbbrDeprecated', default_name = 'Comment' })
73-
default_to_cmp({ name = 'LabelMatch', cmp_name = 'AbbrMatch', default_name = 'Pmenu' })
74-
default_to_cmp({ name = 'Kind', default_name = 'Special' })
53+
vim.api.nvim_set_hl(0, 'BlinkCmpLabel', { link = 'Pmenu', default = true })
54+
vim.api.nvim_set_hl(0, 'BlinkCmpLabelDeprecated', { link = 'Comment', default = true })
55+
vim.api.nvim_set_hl(0, 'BlinkCmpLabelMatch', { link = 'Pmenu', default = true })
56+
vim.api.nvim_set_hl(0, 'BlinkCmpKind', { link = 'Special', default = true })
7557
for _, kind in pairs(vim.lsp.protocol.CompletionItemKind) do
76-
default_to_cmp({ name = 'Kind' .. kind, default_name = 'BlinkCmpItemKind' })
58+
vim.api.nvim_set_hl(0, 'BlinkCmpKind' .. kind, { link = 'BlinkCmpItemKind', default = true })
7759
end
7860
end
7961

@@ -82,6 +64,11 @@ cmp.show = function()
8264
return true
8365
end
8466

67+
cmp.hide = function()
68+
vim.schedule(function() cmp.trigger.hide() end)
69+
return true
70+
end
71+
8572
cmp.accept = function()
8673
local item = cmp.windows.autocomplete.get_selected_item()
8774
if item == nil then return end

lua/blink/cmp/keymap.lua

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local keymap = {}
2+
3+
local insert_commands = { 'show', 'hide', 'accept', 'select_prev', 'select_next' }
4+
local snippet_commands = { 'snippet_forward', 'snippet_backward' }
5+
6+
--- @param opts KeymapConfig
7+
function keymap.setup(opts)
8+
for command, keys in pairs(opts) do
9+
local is_snippet_command = vim.tbl_contains(snippet_commands, command)
10+
local is_insert_command = vim.tbl_contains(insert_commands, command)
11+
if not is_snippet_command and not is_insert_command then error('Invalid command in keymap config: ' .. command) end
12+
13+
-- convert string to string[] for consistency
14+
if type(keys) == 'string' then keys = { keys } end
15+
16+
-- add keymaps
17+
for _, key in ipairs(keys) do
18+
local mode = is_snippet_command and 's' or 'i'
19+
vim.keymap.set(mode, key, function()
20+
local did_run = require('blink.cmp')[command]()
21+
if not did_run then return key end
22+
end, { expr = true, silent = true })
23+
end
24+
end
25+
end
26+
27+
return keymap

0 commit comments

Comments
 (0)