Skip to content

Commit

Permalink
feat: ignore repeated call at cursor position in trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Oct 11, 2024
1 parent 41edb65 commit 4883420
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions lua/blink/cmp/trigger/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,11 @@ function trigger.activate_autocmds()
end,
})

-- hack: 'a' triggers both the InsertEnter and CursorMovedI event
-- However, the cursor_col hasn't changed. When we're on a trigger character,
-- this causes two show() calls, one with the trigger character and one without.
-- To prevent this, we ignore the first CursorMovedI event when 'a' is pressed
local ignore_next_cursor_moved = false
vim.api.nvim_set_keymap('n', 'a', '', {
callback = function()
ignore_next_cursor_moved = true
return 'a'
end,
expr = true,
noremap = true,
silent = true,
})

vim.api.nvim_create_autocmd({ 'CursorMovedI', 'InsertEnter' }, {
callback = function(ev)
-- characters added so let textchanged handle it
if last_char ~= '' then return end

-- ignore CursorMovedI event when flag is enabled
if ev.event == 'CursorMovedI' and ignore_next_cursor_moved then
ignore_next_cursor_moved = false
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)
Expand Down Expand Up @@ -128,8 +107,13 @@ end
function trigger.show(opts)
opts = opts or {}

-- update context
local cursor = vim.api.nvim_win_get_cursor(0)
-- already triggered at this position, ignore
if trigger.context ~= nil and cursor[1] == trigger.context.cursor[1] and cursor[2] == trigger.context.cursor[2] then
return
end

-- update context
if trigger.context == nil then trigger.current_context_id = trigger.current_context_id + 1 end
trigger.context = {
id = trigger.current_context_id,
Expand Down

0 comments on commit 4883420

Please sign in to comment.