Skip to content

Commit 2f1b85b

Browse files
committed
fix: close completion if the accepted item matches the current word
fixes #41
1 parent 65e9605 commit 2f1b85b

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

lua/blink/cmp/accept/init.lua

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ local function accept(item)
1111
local brackets_status, text_edit_with_brackets, offset = brackets_lib.add_brackets(vim.bo.filetype, item)
1212
item.textEdit = text_edit_with_brackets
1313

14+
local current_word = require('blink.cmp.trigger.completion').get_current_word()
15+
if current_word == item.textEdit.newText then
16+
-- Hide the completion window and don't apply the text edit because
17+
-- the new text is already inserted
18+
require('blink.cmp.trigger.completion').hide()
19+
1420
-- Snippet
15-
if item.insertTextFormat == vim.lsp.protocol.InsertTextFormat.Snippet then
21+
elseif item.insertTextFormat == vim.lsp.protocol.InsertTextFormat.Snippet then
1622
-- We want to handle offset_encoding and the text edit api can do this for us
1723
-- so we empty the newText and apply
1824
local temp_text_edit = vim.deepcopy(item.textEdit)
@@ -22,7 +28,7 @@ local function accept(item)
2228
-- Expand the snippet
2329
vim.snippet.expand(item.textEdit.newText)
2430

25-
-- OR Normal: Apply the text edit and move the cursor
31+
-- OR Normal: Apply the text edit and move the cursor
2632
else
2733
text_edits_lib.apply_text_edits(item.client_id, { item.textEdit })
2834
vim.api.nvim_win_set_cursor(0, {

lua/blink/cmp/accept/text-edits.lua

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local utils = require('blink.cmp.utils')
21
local text_edits = {}
32

43
function text_edits.get_from_item(item)

lua/blink/cmp/trigger/completion.lua

+7
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ function trigger.within_query_bounds(cursor)
193193
return row == bounds.line_number and col >= bounds.start_col and col <= bounds.end_col
194194
end
195195

196+
---@return string?
197+
function trigger.get_current_word()
198+
if not trigger.context then return end
199+
200+
local bounds = trigger.context.bounds
201+
return trigger.context.line:sub(bounds.start_col, bounds.end_col)
202+
end
196203
--- Moves forward and backwards around the cursor looking for word boundaries
197204
--- @param regex string
198205
--- @return blink.cmp.ContextBounds

0 commit comments

Comments
 (0)