Skip to content

Commit 77080a5

Browse files
committed
feat: support keyword_length on sources
1 parent 78f498f commit 77080a5

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

lua/blink/cmp/config.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
--- @field timeout_ms number How long to wait for semantic tokens to return before assuming no brackets should be added
3131

3232
--- @class blink.cmp.CompletionTriggerConfig
33-
--- @field context_regex string
33+
--- @field keyword_regex string
3434
--- @field blocked_trigger_characters string[]
3535
--- @field show_on_insert_on_trigger_character boolean When true, will show the completion window when the cursor comes after a trigger character when entering insert mode
3636
---
@@ -165,7 +165,7 @@ local config = {
165165
-- regex used to get the text when fuzzy matching
166166
-- changing this may break some sources, so please report if you run into issues
167167
-- todo: shouldnt this also affect the accept command? should this also be per language?
168-
context_regex = '[%w_\\-]',
168+
keyword_regex = '[%w_\\-]',
169169
-- LSPs can indicate when to show the completion window via trigger characters
170170
-- however, some LSPs (*cough* tsserver *cough*) return characters that would essentially
171171
-- always show the window. We block these by default

lua/blink/cmp/sources/lib/context.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function sources_context:get_completions_for_group(sources_group_idx, sources_gr
9595
--- @type blink.cmp.CompletionResponse
9696
local response = task_result.result
9797
response.items = source:filter_completions(response)
98-
if source:should_show_completions(response) then vim.list_extend(items, response.items) end
98+
if source:should_show_completions(context, response) then vim.list_extend(items, response.items) end
9999
end
100100
end
101101
return { sources_group_idx = sources_group_idx, is_cached = is_cached, items = items }

lua/blink/cmp/sources/lib/source.lua

+12-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ end
1818

1919
--- @return string[]
2020
function source:get_trigger_characters()
21-
if self.module.get_trigger_characters == nil then return {} end
22-
return self.module:get_trigger_characters()
21+
if self.module.get_trigger_characters == nil then return self.config.trigger_characters or {} end
22+
local trigger_characters = self.module:get_trigger_characters()
23+
vim.list_extend(trigger_characters, self.config.trigger_characters or {})
24+
return trigger_characters
2325
end
2426

2527
--- @param context blink.cmp.Context
@@ -59,11 +61,17 @@ function source:filter_completions(response)
5961
return self.module:filter_completions(response)
6062
end
6163

64+
--- @param context blink.cmp.Context
6265
--- @param response blink.cmp.CompletionResponse
6366
--- @return boolean
64-
function source:should_show_completions(response)
67+
function source:should_show_completions(context, response)
68+
-- if keyword length is configured, check if the context is long enough
69+
local min_keyword_length = self.config.keyword_length or 0
70+
local current_keyword_length = context.bounds.end_col - context.bounds.start_col
71+
if self.config.keyword_length ~= nil and current_keyword_length < min_keyword_length then return false end
72+
6573
if self.module.should_show_completions == nil then return true end
66-
return self.module:should_show_completions(response)
74+
return self.module:should_show_completions(context, response)
6775
end
6876

6977
--- Resolve ---

lua/blink/cmp/sources/lib/types.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
--- @field get_trigger_characters (fun(self: blink.cmp.Source): string[]) | nil
1414
--- @field get_completions fun(self: blink.cmp.Source, context: blink.cmp.Context, callback: fun(response: blink.cmp.CompletionResponse)): (fun(): nil) | nil
1515
--- @field filter_completions (fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): blink.cmp.CompletionItem[]) | nil
16-
--- @field should_show_completions (fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): boolean) | nil
16+
--- @field should_show_completions (fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean) | nil
1717
--- @field resolve (fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem, callback: fun(resolved_item: lsp.CompletionItem | nil)): ((fun(): nil) | nil)) | nil

lua/blink/cmp/trigger/completion.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Handles hiding and showing the completion window. When a user types a trigger character
2-
-- (provided by the sources) or anything matching the `context_regex`, we create a new `context`.
2+
-- (provided by the sources) or anything matching the `keyword_regex`, we create a new `context`.
33
-- This can be used downstream to determine if we should make new requests to the sources or not.
44

55
local config = require('blink.cmp.config').trigger.completion
@@ -38,7 +38,7 @@ function trigger.activate_autocmds()
3838
trigger.context = nil
3939
trigger.show({ trigger_character = last_char })
4040
-- character is part of the current context OR in an existing context
41-
elseif last_char:match(config.context_regex) ~= nil then
41+
elseif last_char:match(config.keyword_regex) ~= nil then
4242
trigger.show()
4343
-- nothing matches so hide
4444
else
@@ -60,7 +60,7 @@ function trigger.activate_autocmds()
6060
local cursor_col = vim.api.nvim_win_get_cursor(0)[2]
6161
local char_under_cursor = vim.api.nvim_get_current_line():sub(cursor_col, cursor_col)
6262
local is_on_trigger = vim.tbl_contains(sources.get_trigger_characters(), char_under_cursor)
63-
local is_on_context_char = char_under_cursor:match(config.context_regex) ~= nil
63+
local is_on_context_char = char_under_cursor:match(config.keyword_regex) ~= nil
6464

6565
if is_within_bounds or (is_on_trigger and trigger.context ~= nil) then
6666
trigger.show()
@@ -94,7 +94,7 @@ function trigger.show(opts)
9494
bufnr = vim.api.nvim_get_current_buf(),
9595
cursor = cursor,
9696
line = vim.api.nvim_buf_get_lines(0, cursor[1] - 1, cursor[1], false)[1],
97-
bounds = trigger.get_context_bounds(config.context_regex),
97+
bounds = trigger.get_context_bounds(config.keyword_regex),
9898
trigger = {
9999
kind = opts.trigger_character and vim.lsp.protocol.CompletionTriggerKind.TriggerCharacter
100100
or vim.lsp.protocol.CompletionTriggerKind.Invoked,

0 commit comments

Comments
 (0)