Skip to content

Commit

Permalink
fix: completion auto_insert replace incorrect range (#621)
Browse files Browse the repository at this point in the history
Closes #460
  • Loading branch information
Goose97 authored Dec 17, 2024
1 parent b4c6fe6 commit 5926869
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
12 changes: 10 additions & 2 deletions lua/blink/cmp/completion/accept/preview.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
--- @param item blink.cmp.CompletionItem
--- @return lsp.TextEdit undo_text_edit, integer[]? undo_cursor_pos The text edit to apply and the original cursor
--- position to move to when undoing the preview,
local function preview(item)
local text_edits_lib = require('blink.cmp.lib.text_edits')
local text_edit = text_edits_lib.get_from_item(item)
Expand All @@ -18,10 +20,16 @@ local function preview(item)

text_edits_lib.apply({ text_edit })

local original_cursor = vim.api.nvim_win_get_cursor(0)
local cursor_moved = false

-- TODO: remove when text_edits_lib.apply begins setting cursor position
if vim.api.nvim_get_mode().mode ~= 'c' then vim.api.nvim_win_set_cursor(0, cursor_pos) end
if vim.api.nvim_get_mode().mode ~= 'c' then
vim.api.nvim_win_set_cursor(0, cursor_pos)
cursor_moved = true
end

return undo_text_edit
return undo_text_edit, cursor_moved and original_cursor or nil
end

return preview
20 changes: 10 additions & 10 deletions lua/blink/cmp/completion/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
--- @field context? blink.cmp.Context
--- @field items blink.cmp.CompletionItem[]
--- @field selected_item_idx? number
--- @field preview_undo_text_edit? lsp.TextEdit
--- @field preview_undo? { text_edit: lsp.TextEdit, cursor: integer[]?}
--- @field show_emitter blink.cmp.EventEmitter<blink.cmp.CompletionListShowEvent>
--- @field hide_emitter blink.cmp.EventEmitter<blink.cmp.CompletionListHideEvent>
--- @field select_emitter blink.cmp.EventEmitter<blink.cmp.CompletionListSelectEvent>
Expand Down Expand Up @@ -59,7 +59,7 @@ local list = {
items = {},
selected_item_idx = nil,
is_explicitly_selected = false,
preview_undo_text_edit = nil,
preview_undo = nil,
}

---------- State ----------
Expand All @@ -68,7 +68,7 @@ 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_text_edit = nil
list.preview_undo = nil
list.is_explicitly_selected = false
end

Expand Down Expand Up @@ -189,19 +189,19 @@ end
---------- Preview ----------

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

require('blink.cmp.lib.text_edits').apply({ list.preview_undo_text_edit })
list.preview_undo_text_edit = nil
require('blink.cmp.lib.text_edits').apply({ list.preview_undo.text_edit })
if list.preview_undo.cursor then vim.api.nvim_win_set_cursor(0, list.preview_undo.cursor) end
list.preview_undo = nil
end

function list.apply_preview(item)
-- 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
list.undo_preview()
-- apply the new preview
list.preview_undo_text_edit = require('blink.cmp.completion.accept.preview')(item)
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 Down

0 comments on commit 5926869

Please sign in to comment.