Skip to content

Commit

Permalink
fix(auto-insert): restore content of context instead of undo
Browse files Browse the repository at this point in the history
  • Loading branch information
xzbdmw committed Jan 8, 2025
1 parent 47efef8 commit 091b3ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
29 changes: 9 additions & 20 deletions lua/blink/cmp/completion/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
--- @field context? blink.cmp.Context
--- @field items blink.cmp.CompletionItem[]
--- @field selected_item_idx? number
--- @field preview_undo? { text_edit: lsp.TextEdit, cursor: integer[]?}
---
--- @field show fun(context: blink.cmp.Context, items: table<string, blink.cmp.CompletionItem[]>)
--- @field fuzzy fun(context: blink.cmp.Context, items: table<string, blink.cmp.CompletionItem[]>): blink.cmp.CompletionItem[]
Expand All @@ -22,7 +21,7 @@
--- @field select_next fun(opts?: blink.cmp.CompletionListSelectOpts)
--- @field select_prev fun(opts?: blink.cmp.CompletionListSelectOpts)
---
--- @field undo_preview fun()
--- @field restore_content fun()
--- @field apply_preview fun(item: blink.cmp.CompletionItem)
--- @field accept fun(opts?: blink.cmp.CompletionListAcceptOpts): boolean Applies the currently selected item, returning true if it succeeded

Expand Down Expand Up @@ -63,18 +62,14 @@ local list = {
context = nil,
items = {},
is_explicitly_selected = false,
preview_undo = nil,
}

---------- State ----------

function list.show(context, items_by_source)
-- reset state for new context
local is_new_context = not list.context or list.context.id ~= context.id
if is_new_context then
list.preview_undo = nil
list.is_explicitly_selected = false
end
if is_new_context then list.is_explicitly_selected = false end

-- if the keyword changed, the list is no longer explicitly selected
local bounds_equal = list.context ~= nil
Expand Down Expand Up @@ -157,7 +152,7 @@ function list.select(idx, opts)
if auto_insert == nil then auto_insert = list.get_selection_mode(list.context).auto_insert end

require('blink.cmp.completion.trigger').suppress_events_for_callback(function()
if opts.undo_preview ~= false then list.undo_preview() end
if opts.undo_preview ~= false then list.restore_content() end
if auto_insert and item ~= nil then list.apply_preview(item) end
end)

Expand Down Expand Up @@ -213,22 +208,16 @@ end

---------- Preview ----------

function list.undo_preview()
if list.preview_undo == nil then return end

require('blink.cmp.lib.text_edits').apply({ list.preview_undo.text_edit })
if list.preview_undo.cursor then
require('blink.cmp.completion.trigger.context').set_cursor(list.preview_undo.cursor)
end
list.preview_undo = nil
function list.restore_content()
local row = list.context.cursor[1] - 1
vim.api.nvim_buf_set_text(0, row, 0, row, -1, { list.context.get_line() })
end

function list.apply_preview(item)
-- undo the previous preview if it exists
list.undo_preview()
-- restore the original line content before auto_insert.
list.restore_content()
-- apply the new preview
local undo_text_edit, undo_cursor = require('blink.cmp.completion.accept.preview')(item)
list.preview_undo = { text_edit = undo_text_edit, cursor = undo_cursor }
end

---------- Accept ----------
Expand All @@ -238,7 +227,7 @@ function list.accept(opts)
local item = list.items[opts.index or list.selected_item_idx]
if item == nil then return false end

list.undo_preview()
list.restore_content()
local accept = require('blink.cmp.completion.accept')
accept(list.context, item, function()
list.accept_emitter:emit({ item = item, context = list.context })
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ end
function cmp.cancel(opts)
if not cmp.is_visible() then return end
vim.schedule(function()
require('blink.cmp.completion.list').undo_preview()
require('blink.cmp.completion.list').restore_content()
require('blink.cmp.completion.trigger').hide()
if opts and opts.callback then opts.callback() end
end)
Expand Down

0 comments on commit 091b3ce

Please sign in to comment.