From 125d4f1288b3b309d219848559adfca3cc61f8b5 Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Wed, 30 Oct 2024 11:59:57 -0400 Subject: [PATCH] feat: support disabling accept on trigger character, block parenthesis closes #212 --- README.md | 8 +++++--- lua/blink/cmp/accept/init.lua | 4 ++-- lua/blink/cmp/config.lua | 9 ++++++--- lua/blink/cmp/trigger/completion.lua | 9 +++++++-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 29b39f4e..b67f8110 100644 --- a/README.md +++ b/README.md @@ -230,11 +230,13 @@ MiniDeps.add({ -- however, some LSPs (*cough* tsserver *cough*) return characters that would essentially -- always show the window. We block these by default blocked_trigger_characters = { ' ', '\n', '\t' }, + -- when true, will show the completion window when the cursor comes after a trigger character after accepting an item + show_on_accept_on_trigger_character = true, -- when true, will show the completion window when the cursor comes after a trigger character when entering insert mode show_on_insert_on_trigger_character = true, - -- list of additional trigger characters that won't trigger the completion window when the cursor comes after a trigger character when entering insert mode - show_on_insert_blocked_trigger_characters = { "'", '"' }, - -- when false, will not show the completion window when in a snippet + -- list of additional trigger characters that won't trigger the completion window when the cursor comes after a trigger character when entering insert mode/accepting an item + show_on_x_blocked_trigger_characters = { "'", '"', '(' }, + -- when false, will not show the completion window automatically when in a snippet show_in_snippet = true, }, diff --git a/lua/blink/cmp/accept/init.lua b/lua/blink/cmp/accept/init.lua index 7aa04de9..73201af1 100644 --- a/lua/blink/cmp/accept/init.lua +++ b/lua/blink/cmp/accept/init.lua @@ -53,11 +53,11 @@ local function accept(item) -- TODO: since we apply the additional text edits after, auto imported functions will not -- get auto brackets. If we apply them before, we have to modify the textEdit to compensate brackets_lib.add_brackets_via_semantic_token(vim.bo.filetype, item, function() - require('blink.cmp.trigger.completion').show_if_on_trigger_character() + require('blink.cmp.trigger.completion').show_if_on_trigger_character({ is_accept = true }) require('blink.cmp.trigger.signature').show_if_on_trigger_character() end) else - require('blink.cmp.trigger.completion').show_if_on_trigger_character() + require('blink.cmp.trigger.completion').show_if_on_trigger_character({ is_accept = true }) require('blink.cmp.trigger.signature').show_if_on_trigger_character() end diff --git a/lua/blink/cmp/config.lua b/lua/blink/cmp/config.lua index 29f905ea..35dec8d1 100644 --- a/lua/blink/cmp/config.lua +++ b/lua/blink/cmp/config.lua @@ -44,8 +44,9 @@ --- @field keyword_regex? string --- @field exclude_from_prefix_regex? string --- @field blocked_trigger_characters? string[] +--- @field show_on_accept_on_trigger_character? boolean When true, will show the completion window when the cursor comes after a trigger character after accepting an item --- @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 ---- @field show_on_insert_blocked_trigger_characters? string[] List of additional trigger characters that won't trigger the completion window when the cursor comes after a trigger character when entering insert mode +--- @field show_on_x_blocked_trigger_characters? string[] List of additional trigger characters that won't trigger the completion window when the cursor comes after a trigger character when entering insert mode/accepting an item --- @field show_in_snippet? boolean When false, will not show the completion window when in a snippet --- --- @class blink.cmp.SignatureHelpTriggerConfig @@ -253,10 +254,12 @@ local config = { -- however, some LSPs (*cough* tsserver *cough*) return characters that would essentially -- always show the window. We block these by default blocked_trigger_characters = { ' ', '\n', '\t' }, + -- when true, will show the completion window when the cursor comes after a trigger character after accepting an item + show_on_accept_on_trigger_character = true, -- when true, will show the completion window when the cursor comes after a trigger character when entering insert mode show_on_insert_on_trigger_character = true, - -- list of additional trigger characters that won't trigger the completion window when the cursor comes after a trigger character when entering insert mode - show_on_insert_blocked_trigger_characters = { "'", '"' }, + -- list of additional trigger characters that won't trigger the completion window when the cursor comes after a trigger character when entering insert mode/accepting an item + show_on_x_blocked_trigger_characters = { "'", '"', '(' }, -- when false, will not show the completion window when in a snippet show_in_snippet = false, }, diff --git a/lua/blink/cmp/trigger/completion.lua b/lua/blink/cmp/trigger/completion.lua index 772b3845..1da81dbe 100644 --- a/lua/blink/cmp/trigger/completion.lua +++ b/lua/blink/cmp/trigger/completion.lua @@ -86,7 +86,7 @@ function trigger.activate_autocmds() local char_under_cursor = vim.api.nvim_get_current_line():sub(cursor_col, cursor_col) local is_on_trigger = vim.tbl_contains(sources.get_trigger_characters(), char_under_cursor) local is_on_trigger_for_show_on_insert = is_on_trigger - and not vim.tbl_contains(config.show_on_insert_blocked_trigger_characters, char_under_cursor) + and not vim.tbl_contains(config.show_on_x_blocked_trigger_characters, char_under_cursor) local is_on_context_char = char_under_cursor:match(config.keyword_regex) ~= nil local insert_enter_on_trigger_character = config.show_on_insert_on_trigger_character @@ -152,10 +152,15 @@ function trigger.suppress_events_for_callback(cb) and is_insert_mode end -function trigger.show_if_on_trigger_character() +--- @param opts { is_accept?: boolean } | nil +function trigger.show_if_on_trigger_character(opts) + if opts and opts.is_accept and not config.show_on_accept_on_trigger_character then return end + local cursor_col = vim.api.nvim_win_get_cursor(0)[2] local char_under_cursor = vim.api.nvim_get_current_line():sub(cursor_col, cursor_col) local is_on_trigger = vim.tbl_contains(sources.get_trigger_characters(), char_under_cursor) + and not vim.tbl_contains(config.show_on_x_blocked_trigger_characters, char_under_cursor) + if is_on_trigger then trigger.show({ trigger_character = char_under_cursor }) end return is_on_trigger end