Skip to content

Commit

Permalink
fix: buffer events suppression, auto_insert selection
Browse files Browse the repository at this point in the history
Closes #415
  • Loading branch information
Saghen committed Dec 2, 2024
1 parent bb5407d commit 96ceb56
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
28 changes: 15 additions & 13 deletions lua/blink/cmp/completion/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
--- @field hide fun()
---
--- @field get_selected_item fun(): blink.cmp.CompletionItem?
--- @field select fun(idx?: number)
--- @field select fun(idx?: number, opts?: { undo_preview?: boolean })
--- @field select_next fun()
--- @field select_prev fun()
---
Expand Down Expand Up @@ -74,7 +74,7 @@ function list.show(context, items)
end

-- todo: some logic to maintain the selection if the user moved the cursor?
list.select(list.config.selection == 'preselect' and 1 or nil)
list.select(list.config.selection == 'preselect' and 1 or nil, { undo_preview = false })
end

function list.fuzzy(context, items)
Expand All @@ -91,11 +91,15 @@ function list.hide() list.hide_emitter:emit({ context = list.context }) end

function list.get_selected_item() return list.items[list.selected_item_idx] end

function list.select(idx)
function list.select(idx, opts)
opts = opts or {}
local item = list.items[idx]

list.undo_preview()
if list.config.selection == 'auto_insert' and item then list.apply_preview(item) end
require('blink.cmp.completion.trigger').suppress_events_for_callback(function()
-- default to undoing the preview
if opts.undo_preview ~= false then list.undo_preview() end
if list.config.selection == 'auto_insert' and item then list.apply_preview(item) end
end)

list.selected_item_idx = idx
list.select_emitter:emit({ idx = idx, item = item, items = list.items, context = list.context })
Expand Down Expand Up @@ -155,14 +159,12 @@ function list.undo_preview()
end

function list.apply_preview(item)
require('blink.cmp.completion.trigger').suppress_events_for_callback(function()
-- undo the previous preview if it exists
if list.preview_undo_text_edit ~= nil then
require('blink.cmp.lib.text_edits').apply({ list.preview_undo_text_edit })
end
-- apply the new preview
list.preview_undo_text_edit = require('blink.cmp.completion.accept.preview')(item)
end)
-- undo the previous preview if it exists
if list.preview_undo_text_edit ~= nil then
require('blink.cmp.lib.text_edits').apply({ list.preview_undo_text_edit })
end
-- apply the new preview
list.preview_undo_text_edit = require('blink.cmp.completion.accept.preview')(item)
end

---------- Accept ----------
Expand Down
12 changes: 8 additions & 4 deletions lua/blink/cmp/lib/buffer_events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,30 @@ function buffer_events:listen(opts)
if utils.is_blocked_buffer() then return end
if snippet.active() and not self.show_in_snippet and not self.has_context() then return end

local is_ignored = self.ignore_next_text_changed
self.ignore_next_text_changed = false

-- no characters added so let cursormoved handle it
if last_char == '' then return end

opts.on_char_added(last_char, self.ignore_next_text_changed)
self.ignore_next_text_changed = false
opts.on_char_added(last_char, is_ignored)

last_char = ''
end,
})

vim.api.nvim_create_autocmd({ 'CursorMovedI', 'InsertEnter' }, {
callback = function(ev)
local is_ignored = ev.event == 'CursorMovedI' and self.ignore_next_cursor_moved
if ev.event == 'CursorMovedI' then self.ignore_next_cursor_moved = false end

-- characters added so let textchanged handle it
if last_char ~= '' then return end

if utils.is_blocked_buffer() then return end
if snippet.active() and not self.show_in_snippet and not self.has_context() then return end

opts.on_cursor_moved(ev.event, ev.event == 'CursorMovedI' and self.ignore_next_cursor_moved)
if ev.event == 'CursorMovedI' then self.ignore_next_cursor_moved = false end
opts.on_cursor_moved(ev.event, is_ignored)
end,
})

Expand Down

0 comments on commit 96ceb56

Please sign in to comment.