Skip to content

Commit ebbce90

Browse files
committed
fix: double send on append on trigger character
closes #25
1 parent cd15078 commit ebbce90

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

lua/blink/cmp/trigger/completion.lua

+35-9
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,58 @@ function trigger.activate_autocmds()
4949
end,
5050
})
5151

52-
-- check if we've moved outside of the context by diffing against the query boundary
52+
-- hack: 'a' triggers both the InsertEnter and CursorMovedI event
53+
-- However, the cursor_col hasn't changed. When we're on a trigger character,
54+
-- this causes two show() calls, one with the trigger character and one without.
55+
-- To prevent this, we ignore the first CursorMovedI event when 'a' is pressed
56+
local ignore_next_cursor_moved = false
57+
vim.api.nvim_set_keymap('n', 'a', '', {
58+
callback = function()
59+
ignore_next_cursor_moved = true
60+
return 'a'
61+
end,
62+
expr = true,
63+
noremap = true,
64+
silent = true,
65+
})
66+
5367
vim.api.nvim_create_autocmd({ 'CursorMovedI', 'InsertEnter' }, {
5468
callback = function(ev)
5569
-- characters added so let textchanged handle it
5670
if last_char ~= '' then return end
5771

58-
local is_within_bounds = trigger.within_query_bounds(vim.api.nvim_win_get_cursor(0))
72+
-- ignore CursorMovedI event when flag is enabled
73+
if ev.event == 'CursorMovedI' and ignore_next_cursor_moved then
74+
ignore_next_cursor_moved = false
75+
return
76+
end
5977

6078
local cursor_col = vim.api.nvim_win_get_cursor(0)[2]
6179
local char_under_cursor = vim.api.nvim_get_current_line():sub(cursor_col, cursor_col)
6280
local is_on_trigger = vim.tbl_contains(sources.get_trigger_characters(), char_under_cursor)
81+
local is_on_trigger_for_show_on_insert = is_on_trigger
6382
and not vim.tbl_contains(config.show_on_insert_blocked_trigger_characters, char_under_cursor)
6483
local is_on_context_char = char_under_cursor:match(config.keyword_regex) ~= nil
6584

66-
if is_within_bounds then
85+
-- check if we're still within the bounds of the query used for the context
86+
if trigger.within_query_bounds(vim.api.nvim_win_get_cursor(0)) then
6787
trigger.show()
88+
89+
-- check if we've entered insert mode on a trigger character
90+
-- or if we've moved onto a trigger character
6891
elseif
69-
-- check if we've gone 1 char behind the context and we're still on a context char
70-
(is_on_context_char and trigger.context ~= nil and cursor_col == trigger.context.bounds.start_col - 1)
71-
-- or if we've moved onto a trigger character
92+
(config.show_on_insert_on_trigger_character and is_on_trigger_for_show_on_insert and ev.event == 'InsertEnter')
7293
or (is_on_trigger and trigger.context ~= nil)
7394
then
7495
trigger.context = nil
75-
trigger.show()
76-
elseif config.show_on_insert_on_trigger_character and is_on_trigger and ev.event == 'InsertEnter' then
7796
trigger.show({ trigger_character = char_under_cursor })
97+
98+
-- show if we currently have a context, and we've moved outside of it's bounds by 1 char
99+
elseif is_on_context_char and trigger.context ~= nil and cursor_col == trigger.context.bounds.start_col - 1 then
100+
trigger.context = nil
101+
trigger.show()
102+
103+
-- otherwise hide
78104
else
79105
trigger.hide()
80106
end
@@ -84,7 +110,7 @@ function trigger.activate_autocmds()
84110
-- definitely leaving the context
85111
vim.api.nvim_create_autocmd({ 'InsertLeave', 'BufLeave' }, { callback = trigger.hide })
86112

87-
-- trigger InsertLeave autocommand when exiting insert mode with ctrl+c
113+
-- manually hide when exiting insert mode with ctrl+c, since it doesn't trigger InsertLeave
88114
local ctrl_c = vim.api.nvim_replace_termcodes('<C-c>', true, true, true)
89115
vim.on_key(function(key)
90116
if key == ctrl_c then

0 commit comments

Comments
 (0)