Skip to content

Commit

Permalink
feat: support disabling accept on trigger character, block parenthesis
Browse files Browse the repository at this point in the history
closes #212
  • Loading branch information
Saghen committed Oct 30, 2024
1 parent 6f0fc86 commit 125d4f1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},

Expand Down
4 changes: 2 additions & 2 deletions lua/blink/cmp/accept/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions lua/blink/cmp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
Expand Down
9 changes: 7 additions & 2 deletions lua/blink/cmp/trigger/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 125d4f1

Please sign in to comment.