Skip to content

Commit

Permalink
fix: don't show when moving on trigger character, hide on no items af…
Browse files Browse the repository at this point in the history
…ter trigger

Closes #545
  • Loading branch information
Saghen committed Dec 17, 2024
1 parent 85176f7 commit 7a04612
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lua/blink/cmp/completion/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function completion.setup()
end
end

if not triggering_source_returned_items then return end
if not triggering_source_returned_items then return list.hide() end
end

list.show(event.context, event.items)
Expand Down
9 changes: 3 additions & 6 deletions lua/blink/cmp/completion/trigger/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,27 @@ function trigger.activate()
local function on_cursor_moved(event, is_ignored)
-- we were told to ignore the cursor moved event, so we update the context
-- but don't send an on_show event upstream
if is_ignored and event == 'CursorMovedI' then
if is_ignored and event == 'CursorMoved' then
if trigger.context ~= nil then trigger.show({ send_upstream = false }) end
return
end

local cursor = context.get_cursor()
local cursor_col = cursor[2]
local char_under_cursor = context.get_line():sub(cursor_col, cursor_col)
local is_on_trigger_for_show = trigger.is_trigger_character(char_under_cursor)
local is_on_trigger_for_show_on_insert = trigger.is_trigger_character(char_under_cursor, true)
local is_on_keyword_char = keyword_regex:match_str(char_under_cursor) ~= nil

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'
and trigger.is_trigger_character(char_under_cursor, true)

-- check if we're still within the bounds of the query used for the context
if trigger.context ~= nil and trigger.context:within_query_bounds(cursor) then
trigger.show()

-- check if we've entered insert mode on a trigger character
-- or if we've moved onto a trigger character (by accepting for example)
elseif insert_enter_on_trigger_character or (is_on_trigger_for_show and trigger.context ~= nil) then
elseif insert_enter_on_trigger_character then
trigger.context = nil
trigger.show({ trigger_character = char_under_cursor })

Expand Down
7 changes: 4 additions & 3 deletions lua/blink/cmp/lib/buffer_events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

--- @class blink.cmp.BufferEventsListener
--- @field on_char_added fun(char: string, is_ignored: boolean)
--- @field on_cursor_moved fun(event: 'CursorMovedI' | 'InsertEnter', is_ignored: boolean)
--- @field on_cursor_moved fun(event: 'CursorMoved' | 'InsertEnter', is_ignored: boolean)
--- @field on_insert_leave fun()

--- @type blink.cmp.BufferEvents
Expand Down Expand Up @@ -72,9 +72,10 @@ function buffer_events:listen(opts)
if last_char ~= '' then return end

if not require('blink.cmp.config').enabled() then return end
if snippet.active() and not self.show_in_snippet and not self.has_context() then return end
if not self.show_in_snippet and not self.has_context() and snippet.active() then return end

opts.on_cursor_moved(ev.event, is_ignored)
local event = ev.event == 'CursorMovedI' and 'CursorMoved' or ev.event
opts.on_cursor_moved(event, is_ignored)
end,
})

Expand Down
4 changes: 2 additions & 2 deletions lua/blink/cmp/lib/cmdline_events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

--- @class blink.cmp.CmdlineEventsListener
--- @field on_char_added fun(char: string, is_ignored: boolean)
--- @field on_cursor_moved fun(event: 'CursorMovedI' | 'InsertEnter', is_ignored: boolean)
--- @field on_cursor_moved fun(event: 'CursorMoved' | 'InsertEnter', is_ignored: boolean)
--- @field on_leave fun()

--- @type blink.cmp.CmdlineEvents
Expand Down Expand Up @@ -75,7 +75,7 @@ function cmdline_events:listen(opts)
local is_ignored = self.ignore_next_cursor_moved
self.ignore_next_cursor_moved = false

opts.on_cursor_moved('CursorMovedI', is_ignored)
opts.on_cursor_moved('CursorMoved', is_ignored)
end)
timer:start(16, 0, callback)

Expand Down
9 changes: 3 additions & 6 deletions lua/blink/cmp/signature/trigger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,14 @@ function trigger.activate()
})
trigger.buffer_events:listen({
on_char_added = function(char)
local is_on_trigger = trigger.is_trigger_character(char)
local is_on_retrigger = trigger.is_trigger_character(char, true)

-- ignore if disabled
if not require('blink.cmp.config').enabled() then
return trigger.hide()
-- character forces a trigger according to the sources, refresh the existing context if it exists
elseif is_on_trigger then
elseif trigger.is_trigger_character(char) then
return trigger.show({ trigger_character = char })
-- character forces a re-trigger according to the sources, show if we have a context
elseif is_on_retrigger and trigger.context ~= nil then
elseif trigger.is_trigger_character(char, true) and trigger.context ~= nil then
return trigger.show()
end
end,
Expand All @@ -66,7 +63,7 @@ function trigger.activate()

if config.show_on_insert_on_trigger_character and is_on_trigger and event == 'InsertEnter' then
trigger.show({ trigger_character = char_under_cursor })
elseif event == 'CursorMovedI' and trigger.context ~= nil then
elseif event == 'CursorMoved' and trigger.context ~= nil then
trigger.show()
end
end,
Expand Down

0 comments on commit 7a04612

Please sign in to comment.