diff --git a/doc/configuration/completion.md b/doc/configuration/completion.md index 0b0977b2..0fdf0a8e 100644 --- a/doc/configuration/completion.md +++ b/doc/configuration/completion.md @@ -137,7 +137,7 @@ The completion list in **cmdline mode** does **not** inherit the following setti :::tabs == Preselect, Auto Insert (default) ```lua -completion.list.selection = { preselect = true, auto_insert = true } +completion.list.selection = { preselect = true, auto_insert = true, auto_insert_blacklist = {} } ``` Selects the first item automatically, and inserts a preview of the item on selection. The `cancel` keymap (default ``) will close the menu and undo the preview. @@ -234,6 +234,16 @@ completion.list.selection = { } ``` +To blacklist certain big or misbehaving completion sources (like Copilot) from being automatically inserted regardless of `auto_insert` being true, use `auto_insert_blacklist`. +The blacklist accepts `client_name` and `source_name` values allowing you to blacklist entire sources or specific lsp clients: + +```lua +completion.list.selection = { + preselect = false, + auto_insert = true, + auto_insert_blacklist = { 'copilot' }, +} +``` ## Accept Go to default configuration diff --git a/lua/blink/cmp/completion/list.lua b/lua/blink/cmp/completion/list.lua index ce0c9fa5..03ca71bd 100644 --- a/lua/blink/cmp/completion/list.lua +++ b/lua/blink/cmp/completion/list.lua @@ -258,6 +258,17 @@ function list.apply_preview(item) -- undo the previous preview if it exists list.undo_preview() + -- Skip applying preview when the suggestion is blacklisted from being previewed + local blacklist = list.config.selection.auto_insert_blacklist + if blacklist ~= nil and #blacklist > 0 then + for _, v in ipairs(blacklist) do + if v == item.client_name or v == item.source_name then + list.preview_undo = nil + return + end + end + end + -- apply the new preview local undo_text_edit, undo_cursor = require('blink.cmp.completion.accept.preview')(item) list.preview_undo = { diff --git a/lua/blink/cmp/config/completion/list.lua b/lua/blink/cmp/config/completion/list.lua index c0c5690c..153a7b98 100644 --- a/lua/blink/cmp/config/completion/list.lua +++ b/lua/blink/cmp/config/completion/list.lua @@ -6,6 +6,7 @@ --- @class (exact) blink.cmp.CompletionListSelectionConfig --- @field preselect boolean | fun(ctx: blink.cmp.Context): boolean When `true`, will automatically select the first item in the completion list --- @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 ) when using this option, which will both undo the selection and hide the completion menu +--- @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` --- @class (exact) blink.cmp.CompletionListCycleConfig --- @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 = { selection = { preselect = true, auto_insert = true, + auto_insert_blacklist = {}, }, cycle = { from_bottom = true, @@ -43,6 +45,7 @@ function list.validate(config) validate('completion.list.selection', { preselect = { config.selection.preselect, { 'boolean', 'function' } }, auto_insert = { config.selection.auto_insert, { 'boolean', 'function' } }, + auto_insert_blacklist = { config.selection.auto_insert_blacklist, 'table' }, }, config.selection) validate('completion.list.cycle', { diff --git a/lua/blink/cmp/config/modes/types.lua b/lua/blink/cmp/config/modes/types.lua index 2b383f95..92476728 100644 --- a/lua/blink/cmp/config/modes/types.lua +++ b/lua/blink/cmp/config/modes/types.lua @@ -16,6 +16,7 @@ --- @class blink.cmp.ModeCompletionListSelectionConfig --- @field preselect? boolean | fun(ctx: blink.cmp.Context, items: blink.cmp.CompletionItem[]): boolean Whether to preselect the first item when the list is shown --- @field auto_insert? boolean | fun(ctx: blink.cmp.Context, items: blink.cmp.CompletionItem[]): boolean When `true`, inserts the completion item automatically when selecting it +--- @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` --- @class blink.cmp.ModeCompletionTriggerConfig --- @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.