Skip to content

Commit 091c97b

Browse files
Add auto insert blacklisting functionality for misbehaving completion engines
1 parent 4e9edba commit 091c97b

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

doc/configuration/completion.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ The completion list in **cmdline mode** does **not** inherit the following setti
137137
:::tabs
138138
== Preselect, Auto Insert (default)
139139
```lua
140-
completion.list.selection = { preselect = true, auto_insert = true }
140+
completion.list.selection = { preselect = true, auto_insert = true, auto_insert_blacklist = {} }
141141
```
142142
Selects the first item automatically, and inserts a preview of the item on selection. The `cancel` keymap (default `<C-e>`) will close the menu and undo the preview.
143143

@@ -234,6 +234,16 @@ completion.list.selection = {
234234
}
235235
```
236236

237+
To blacklist certain big or misbehaving completion sources (like Copilot) from being automatically inserted regardless of `auto_insert` being true, use `auto_insert_blacklist`.
238+
The blacklist accepts `client_name` and `source_name` values allowing you to blacklist entire sources or specific lsp clients:
239+
240+
```lua
241+
completion.list.selection = {
242+
preselect = false,
243+
auto_insert = true,
244+
auto_insert_blacklist = { 'copilot' },
245+
}
246+
```
237247

238248
## Accept <!-- panvimdoc-ignore-start --><Badge type="info"><a href="./reference#completion-accept">Go to default configuration</a></Badge><!-- panvimdoc-ignore-end -->
239249

lua/blink/cmp/completion/list.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,17 @@ function list.apply_preview(item)
258258
-- undo the previous preview if it exists
259259
list.undo_preview()
260260

261+
-- Skip applying preview when the suggestion is blacklisted from being previewed
262+
local blacklist = list.config.selection.auto_insert_blacklist
263+
if blacklist ~= nil and #blacklist > 0 then
264+
for _, v in ipairs(blacklist) do
265+
if v == item.client_name or v == item.source_name then
266+
list.preview_undo = nil
267+
return
268+
end
269+
end
270+
end
271+
261272
-- apply the new preview
262273
local undo_text_edit, undo_cursor = require('blink.cmp.completion.accept.preview')(item)
263274
list.preview_undo = {

lua/blink/cmp/config/completion/list.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
--- @class (exact) blink.cmp.CompletionListSelectionConfig
77
--- @field preselect boolean | fun(ctx: blink.cmp.Context): boolean When `true`, will automatically select the first item in the completion list
88
--- @field auto_insert boolean | fun(ctx: blink.cmp.Context): boolean When `true`, inserts the completion item automatically when selecting it. You may want to bind a key to the `cancel` command (default <C-e>) when using this option, which will both undo the selection and hide the completion menu
9+
--- @field auto_insert_blacklist? string[] List of mixed client_names and source_names that won't auto-insert when selected, even if `auto_insert` is `true`
910

1011
--- @class (exact) blink.cmp.CompletionListCycleConfig
1112
--- @field from_bottom boolean When `true`, calling `select_next` at the *bottom* of the completion list will select the *first* completion item.
@@ -19,6 +20,7 @@ local list = {
1920
selection = {
2021
preselect = true,
2122
auto_insert = true,
23+
auto_insert_blacklist = {},
2224
},
2325
cycle = {
2426
from_bottom = true,
@@ -43,6 +45,7 @@ function list.validate(config)
4345
validate('completion.list.selection', {
4446
preselect = { config.selection.preselect, { 'boolean', 'function' } },
4547
auto_insert = { config.selection.auto_insert, { 'boolean', 'function' } },
48+
auto_insert_blacklist = { config.selection.auto_insert_blacklist, 'table' },
4649
}, config.selection)
4750

4851
validate('completion.list.cycle', {

lua/blink/cmp/config/modes/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
--- @class blink.cmp.ModeCompletionListSelectionConfig
1717
--- @field preselect? boolean | fun(ctx: blink.cmp.Context, items: blink.cmp.CompletionItem[]): boolean Whether to preselect the first item when the list is shown
1818
--- @field auto_insert? boolean | fun(ctx: blink.cmp.Context, items: blink.cmp.CompletionItem[]): boolean When `true`, inserts the completion item automatically when selecting it
19+
--- @field auto_insert_blacklist? string[] List of mixed client_names and source_names that won't auto-insert when selected, even if `auto_insert` is `true`
1920

2021
--- @class blink.cmp.ModeCompletionTriggerConfig
2122
--- @field show_on_blocked_trigger_characters? string[] | (fun(): string[]) LSPs can indicate when to show the completion window via trigger characters. However, some LSPs (i.e. tsserver) return characters that would essentially always show the window. We block these by default.

0 commit comments

Comments
 (0)