Skip to content

Commit 96ceb56

Browse files
committed
fix: buffer events suppression, auto_insert selection
Closes #415
1 parent bb5407d commit 96ceb56

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

lua/blink/cmp/completion/list.lua

+15-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
--- @field hide fun()
1616
---
1717
--- @field get_selected_item fun(): blink.cmp.CompletionItem?
18-
--- @field select fun(idx?: number)
18+
--- @field select fun(idx?: number, opts?: { undo_preview?: boolean })
1919
--- @field select_next fun()
2020
--- @field select_prev fun()
2121
---
@@ -74,7 +74,7 @@ function list.show(context, items)
7474
end
7575

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

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

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

94-
function list.select(idx)
94+
function list.select(idx, opts)
95+
opts = opts or {}
9596
local item = list.items[idx]
9697

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

100104
list.selected_item_idx = idx
101105
list.select_emitter:emit({ idx = idx, item = item, items = list.items, context = list.context })
@@ -155,14 +159,12 @@ function list.undo_preview()
155159
end
156160

157161
function list.apply_preview(item)
158-
require('blink.cmp.completion.trigger').suppress_events_for_callback(function()
159-
-- undo the previous preview if it exists
160-
if list.preview_undo_text_edit ~= nil then
161-
require('blink.cmp.lib.text_edits').apply({ list.preview_undo_text_edit })
162-
end
163-
-- apply the new preview
164-
list.preview_undo_text_edit = require('blink.cmp.completion.accept.preview')(item)
165-
end)
162+
-- undo the previous preview if it exists
163+
if list.preview_undo_text_edit ~= nil then
164+
require('blink.cmp.lib.text_edits').apply({ list.preview_undo_text_edit })
165+
end
166+
-- apply the new preview
167+
list.preview_undo_text_edit = require('blink.cmp.completion.accept.preview')(item)
166168
end
167169

168170
---------- Accept ----------

lua/blink/cmp/lib/buffer_events.lua

+8-4
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,30 @@ function buffer_events:listen(opts)
5252
if utils.is_blocked_buffer() then return end
5353
if snippet.active() and not self.show_in_snippet and not self.has_context() then return end
5454

55+
local is_ignored = self.ignore_next_text_changed
56+
self.ignore_next_text_changed = false
57+
5558
-- no characters added so let cursormoved handle it
5659
if last_char == '' then return end
5760

58-
opts.on_char_added(last_char, self.ignore_next_text_changed)
59-
self.ignore_next_text_changed = false
61+
opts.on_char_added(last_char, is_ignored)
6062

6163
last_char = ''
6264
end,
6365
})
6466

6567
vim.api.nvim_create_autocmd({ 'CursorMovedI', 'InsertEnter' }, {
6668
callback = function(ev)
69+
local is_ignored = ev.event == 'CursorMovedI' and self.ignore_next_cursor_moved
70+
if ev.event == 'CursorMovedI' then self.ignore_next_cursor_moved = false end
71+
6772
-- characters added so let textchanged handle it
6873
if last_char ~= '' then return end
6974

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

73-
opts.on_cursor_moved(ev.event, ev.event == 'CursorMovedI' and self.ignore_next_cursor_moved)
74-
if ev.event == 'CursorMovedI' then self.ignore_next_cursor_moved = false end
78+
opts.on_cursor_moved(ev.event, is_ignored)
7579
end,
7680
})
7781

0 commit comments

Comments
 (0)