diff --git a/lua/blink/cmp/accept/init.lua b/lua/blink/cmp/accept/init.lua index ae06c9f6..31d364b9 100644 --- a/lua/blink/cmp/accept/init.lua +++ b/lua/blink/cmp/accept/init.lua @@ -11,8 +11,14 @@ local function accept(item) local brackets_status, text_edit_with_brackets, offset = brackets_lib.add_brackets(vim.bo.filetype, item) item.textEdit = text_edit_with_brackets + local current_word = require('blink.cmp.trigger.completion').get_current_word() + if current_word == item.textEdit.newText then + -- Hide the completion window and don't apply the text edit because + -- the new text is already inserted + require('blink.cmp.trigger.completion').hide() + -- Snippet - if item.insertTextFormat == vim.lsp.protocol.InsertTextFormat.Snippet then + elseif item.insertTextFormat == vim.lsp.protocol.InsertTextFormat.Snippet then -- We want to handle offset_encoding and the text edit api can do this for us -- so we empty the newText and apply local temp_text_edit = vim.deepcopy(item.textEdit) @@ -22,7 +28,7 @@ local function accept(item) -- Expand the snippet vim.snippet.expand(item.textEdit.newText) - -- OR Normal: Apply the text edit and move the cursor + -- OR Normal: Apply the text edit and move the cursor else text_edits_lib.apply_text_edits(item.client_id, { item.textEdit }) vim.api.nvim_win_set_cursor(0, { diff --git a/lua/blink/cmp/accept/text-edits.lua b/lua/blink/cmp/accept/text-edits.lua index 3f72d7b3..b310b9a5 100644 --- a/lua/blink/cmp/accept/text-edits.lua +++ b/lua/blink/cmp/accept/text-edits.lua @@ -1,4 +1,3 @@ -local utils = require('blink.cmp.utils') local text_edits = {} function text_edits.get_from_item(item) diff --git a/lua/blink/cmp/trigger/completion.lua b/lua/blink/cmp/trigger/completion.lua index f9ac33a9..e9e0379c 100644 --- a/lua/blink/cmp/trigger/completion.lua +++ b/lua/blink/cmp/trigger/completion.lua @@ -193,6 +193,13 @@ function trigger.within_query_bounds(cursor) return row == bounds.line_number and col >= bounds.start_col and col <= bounds.end_col end +---@return string? +function trigger.get_current_word() + if not trigger.context then return end + + local bounds = trigger.context.bounds + return trigger.context.line:sub(bounds.start_col, bounds.end_col) +end --- Moves forward and backwards around the cursor looking for word boundaries --- @param regex string --- @return blink.cmp.ContextBounds