From 78ac56e96144ed7475bb6d11981d3c8154bfd366 Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Tue, 22 Oct 2024 11:46:40 -0400 Subject: [PATCH] fix: auto insert breaking on single line text edit closes #169 --- lua/blink/cmp/accept/preview.lua | 4 +++- lua/blink/cmp/accept/text-edits.lua | 10 +++++----- lua/blink/cmp/windows/autocomplete.lua | 7 +++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lua/blink/cmp/accept/preview.lua b/lua/blink/cmp/accept/preview.lua index 0c9fb75e..06aec00a 100644 --- a/lua/blink/cmp/accept/preview.lua +++ b/lua/blink/cmp/accept/preview.lua @@ -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) @@ -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, diff --git a/lua/blink/cmp/accept/text-edits.lua b/lua/blink/cmp/accept/text-edits.lua index 0c525d66..698e51fd 100644 --- a/lua/blink/cmp/accept/text-edits.lua +++ b/lua/blink/cmp/accept/text-edits.lua @@ -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 diff --git a/lua/blink/cmp/windows/autocomplete.lua b/lua/blink/cmp/windows/autocomplete.lua index d2d51a64..ab2c6de9 100644 --- a/lua/blink/cmp/windows/autocomplete.lua +++ b/lua/blink/cmp/windows/autocomplete.lua @@ -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