Skip to content

Commit

Permalink
feat: add show_on_keyword and show_on_trigger_character trigger options
Browse files Browse the repository at this point in the history
Closes #402
  • Loading branch information
Saghen committed Nov 28, 2024
1 parent 9bf108b commit 69a69dd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,19 @@ MiniDeps.add({
trigger = {
-- When false, will not show the completion window automatically when in a snippet
show_in_snippet = true,
-- When true, will show the completion window after typing a character that matches the `keyword.regex`
show_on_keyword = true,
-- When true, will show the completion window after typing a trigger character
show_on_trigger_character = true,
-- 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.
show_on_blocked_trigger_characters = { ' ', '\n', '\t' },
-- When true, will show the completion window when the cursor comes after a trigger character
-- after accepting an item
-- When both this and show_on_trigger_character are 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
-- When both this and show_on_trigger_character are 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 trigger characters (on top of `show_on_blocked_trigger_characters`) that won't trigger
-- the completion window when the cursor comes after a trigger character when
Expand Down
14 changes: 10 additions & 4 deletions lua/blink/cmp/completion/trigger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ function trigger.activate()
if trigger.context ~= nil then trigger.show({ send_upstream = false }) end

-- character forces a trigger according to the sources, create a fresh context
elseif trigger.is_trigger_character(char) then
elseif trigger.is_trigger_character(char) and config.show_on_trigger_character then
trigger.context = nil
trigger.show({ trigger_character = char })

-- character is part of the current context OR in an existing context
elseif char:match(keyword_config.regex) ~= nil then
elseif char:match(keyword_config.regex) ~= nil and config.show_on_keyword then
trigger.show()

-- nothing matches so hide
Expand All @@ -84,7 +84,8 @@ function trigger.activate()
local is_on_trigger_for_show_on_insert = trigger.is_trigger_character(char_under_cursor, true)
local is_on_context_char = char_under_cursor:match(keyword_config.regex) ~= nil

local insert_enter_on_trigger_character = config.show_on_insert_on_trigger_character
local insert_enter_on_trigger_character = config.show_on_trigger_character
and config.show_on_insert_on_trigger_character
and is_on_trigger_for_show_on_insert
and event == 'InsertEnter'

Expand Down Expand Up @@ -132,7 +133,12 @@ function trigger.suppress_events_for_callback(cb)
end

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
if
(opts and opts.is_accept)
and (not config.show_on_trigger_character or 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)
Expand Down
10 changes: 8 additions & 2 deletions lua/blink/cmp/config/completion/trigger.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
--- @class (exact) blink.cmp.CompletionTriggerConfig
--- @field show_in_snippet boolean When false, will not show the completion window when in a snippet
--- @field show_on_keyword boolean When true, will show the completion window after typing a character that matches the `keyword.regex`
--- @field show_on_trigger_character boolean When true, will show the completion window after typing a trigger character
--- @field show_on_blocked_trigger_characters 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.
--- @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_accept_on_trigger_character boolean When both this and show_on_trigger_character are 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 both this and show_on_trigger_character are true, will show 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 trigger characters (on top of `show_on_blocked_trigger_characters`) that won't trigger the completion window when the cursor comes after a trigger character when entering insert mode/accepting an item

local validate = require('blink.cmp.config.utils').validate
local trigger = {
--- @type blink.cmp.CompletionTriggerConfig
default = {
show_in_snippet = true,
show_on_keyword = true,
show_on_trigger_character = true,
show_on_blocked_trigger_characters = { ' ', '\n', '\t' },
show_on_accept_on_trigger_character = true,
show_on_insert_on_trigger_character = true,
Expand All @@ -20,6 +24,8 @@ local trigger = {
function trigger.validate(config)
validate('completion.trigger', {
show_in_snippet = { config.show_in_snippet, 'boolean' },
show_on_keyword = { config.show_on_keyword, 'boolean' },
show_on_trigger_character = { config.show_on_trigger_character, 'boolean' },
show_on_blocked_trigger_characters = { config.show_on_blocked_trigger_characters, 'table' },
show_on_accept_on_trigger_character = { config.show_on_accept_on_trigger_character, 'boolean' },
show_on_insert_on_trigger_character = { config.show_on_insert_on_trigger_character, 'boolean' },
Expand Down

0 comments on commit 69a69dd

Please sign in to comment.