Skip to content

Commit

Permalink
fix: auto insert breaking on single line text edit
Browse files Browse the repository at this point in the history
closes #169
  • Loading branch information
Saghen committed Oct 22, 2024
1 parent 7d6b50b commit 78ac56e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lua/blink/cmp/accept/preview.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- @param item blink.cmp.CompletionItem
local function preview(item)
local function preview(item, previous_text_edit)
local text_edits_lib = require('blink.cmp.accept.text-edits')
local text_edit = text_edits_lib.get_from_item(item)

Expand All @@ -9,6 +9,8 @@ local function preview(item)
text_edit.newText = expanded_snippet and tostring(expanded_snippet) or text_edit.newText
end

if previous_text_edit ~= nil then text_edit.range = text_edits_lib.get_undo_text_edit_range(previous_text_edit) end

text_edits_lib.apply_text_edits(item.client_id, { text_edit })
vim.api.nvim_win_set_cursor(0, {
text_edit.range.start.line + 1,
Expand Down
10 changes: 5 additions & 5 deletions lua/blink/cmp/accept/text-edits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ function text_edits.apply_text_edits(client_id, edits)
end

--- @param text_edit lsp.TextEdit
function text_edits.undo_text_edit(text_edit)
function text_edits.get_undo_text_edit_range(text_edit)
text_edit = vim.deepcopy(text_edit)
local lines = vim.split(text_edit.newText, '\n')
local range = text_edit.range
local last_line_len = lines[#lines] and #lines[#lines] or 0

local range = text_edit.range
range['end'].line = range.start.line + #lines - 1
range['end'].character = lines[#lines] and #lines[#lines] or 0
text_edit.newText = ''
range['end'].character = #lines > 1 and last_line_len or range.start.character + last_line_len

vim.lsp.util.apply_text_edits({ text_edit }, vim.api.nvim_get_current_buf(), 'utf-16')
return range
end

--- @param item blink.cmp.CompletionItem
Expand Down
7 changes: 3 additions & 4 deletions lua/blink/cmp/windows/autocomplete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,9 @@ local function select(line, skip_auto_insert)
-- when auto_insert is enabled, we immediately apply the text edit
if config.windows.autocomplete.selection == 'auto_insert' and selected_item ~= nil and not skip_auto_insert then
require('blink.cmp.trigger.completion').suppress_events_for_callback(function()
if autocomplete.preview_text_edit ~= nil and autocomplete.preview_context_id == autocomplete.context.id then
text_edits_lib.undo_text_edit(autocomplete.preview_text_edit)
end
autocomplete.preview_text_edit = require('blink.cmp.accept.preview')(selected_item)
if autocomplete.preview_context_id ~= autocomplete.context.id then autocomplete.preview_text_edit = nil end
autocomplete.preview_text_edit =
require('blink.cmp.accept.preview')(selected_item, autocomplete.preview_text_edit)
autocomplete.preview_context_id = autocomplete.context.id
end)
end
Expand Down

0 comments on commit 78ac56e

Please sign in to comment.