Skip to content

Commit

Permalink
fix: close completion if the accepted item matches the current word
Browse files Browse the repository at this point in the history
fixes #41
  • Loading branch information
lopi-py committed Oct 12, 2024
1 parent 65e9605 commit 2f1b85b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lua/blink/cmp/accept/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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, {
Expand Down
1 change: 0 additions & 1 deletion lua/blink/cmp/accept/text-edits.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local utils = require('blink.cmp.utils')
local text_edits = {}

function text_edits.get_from_item(item)
Expand Down
7 changes: 7 additions & 0 deletions lua/blink/cmp/trigger/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2f1b85b

Please sign in to comment.