Skip to content

Commit 2c6ee0d

Browse files
committed
feat: check for brackets in front of item
1 parent 3e55028 commit 2c6ee0d

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

lua/blink/cmp/accept/brackets.lua

+22-7
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,28 @@ local brackets = {
3939
--- @param item blink.cmp.CompletionItem
4040
--- @return 'added' | 'check_semantic_token' | 'skipped', lsp.TextEdit | lsp.InsertReplaceEdit, number
4141
function brackets.add_brackets(filetype, item)
42-
if not brackets.should_run_resolution(filetype, 'kind') then return 'check_semantic_token', item.textEdit, 0 end
42+
local text_edit = item.textEdit
43+
assert(text_edit ~= nil, 'Got nil text edit while adding brackets via kind')
44+
local brackets_for_filetype = brackets.get_for_filetype(filetype, item)
4345

46+
-- if there's already the correct brackets in front, skip but indicate the cursor should move in front of the bracket
47+
-- TODO: what if the brackets_for_filetype[1] == '' or ' ' (haskell/ocaml)?
48+
if brackets.has_brackets_in_front(text_edit, brackets_for_filetype[1]) then
49+
return 'skipped', text_edit, #brackets_for_filetype[1]
50+
end
51+
-- check if configuration incidates we should skip
52+
if not brackets.should_run_resolution(filetype, 'kind') then return 'check_semantic_token', text_edit, 0 end
4453
-- not a function, skip
4554
if
4655
item.kind ~= vim.lsp.protocol.CompletionItemKind.Function
4756
and item.kind ~= vim.lsp.protocol.CompletionItemKind.Method
4857
then
49-
return 'check_semantic_token', item.textEdit, 0
58+
return 'check_semantic_token', text_edit, 0
5059
end
5160

52-
local brackets_for_filetype = brackets.get_for_filetype(filetype, item)
53-
local text_edit = item.textEdit
54-
assert(text_edit ~= nil, 'Got nil text edit while adding brackets via kind')
55-
56-
-- if already contains the brackets, conservatively skip adding brackets
61+
-- if the item already contains the brackets, conservatively skip adding brackets
5762
-- todo: won't work for snippets when the brackets_for_filetype is { '{', '}' }
63+
-- I've never seen a language like that though
5864
if brackets_for_filetype[1] ~= ' ' and text_edit.newText:match('[\\' .. brackets_for_filetype[1] .. ']') ~= nil then
5965
return 'skipped', text_edit, 0
6066
end
@@ -171,4 +177,13 @@ function brackets.should_run_resolution(filetype, resolution_method)
171177
return not vim.tbl_contains(config.blocked_filetypes, filetype)
172178
end
173179

180+
--- @param text_edit lsp.TextEdit | lsp.InsertReplaceEdit
181+
--- @param bracket string
182+
--- @return boolean
183+
function brackets.has_brackets_in_front(text_edit, bracket)
184+
local line = vim.api.nvim_get_current_line()
185+
local col = text_edit.range['end'].character + 1
186+
return line:sub(col, col) == bracket
187+
end
188+
174189
return brackets

lua/blink/cmp/accept/init.lua

+1-12
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,7 @@ local brackets_lib = require('blink.cmp.accept.brackets')
55
--- @param item blink.cmp.CompletionItem
66
local function accept(item)
77
item = vim.deepcopy(item)
8-
9-
-- Adjust the position of the text edit to be the current cursor position
10-
-- since the data might be outdated. We compare the cursor column position
11-
-- from when the items were fetched versus the current.
12-
-- hack: figure out a better way
13-
if item.textEdit ~= nil then
14-
local offset = vim.api.nvim_win_get_cursor(0)[2] - item.cursor_column
15-
item.textEdit.range['end'].character = item.textEdit.range['end'].character + offset
16-
-- No text edit so we fallback to our own resolution
17-
else
18-
item.textEdit = text_edits_lib.guess_text_edit(vim.api.nvim_get_current_buf(), item)
19-
end
8+
item.textEdit = text_edits_lib.get_from_item(item)
209

2110
-- Add brackets to the text edit if needed
2211
local brackets_status, text_edit_with_brackets, offset = brackets_lib.add_brackets(vim.bo.filetype, item)

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

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1+
local utils = require('blink.cmp.utils')
12
local text_edits = {}
23

4+
function text_edits.get_from_item(item)
5+
-- Adjust the position of the text edit to be the current cursor position
6+
-- since the data might be outdated. We compare the cursor column position
7+
-- from when the items were fetched versus the current.
8+
-- hack: is there a better way?
9+
if item.textEdit ~= nil then
10+
local text_edit = utils.shallow_copy(item.textEdit)
11+
local offset = vim.api.nvim_win_get_cursor(0)[2] - item.cursor_column
12+
text_edit.range['end'].character = text_edit.range['end'].character + offset
13+
return text_edit
14+
end
15+
16+
-- No text edit so we fallback to our own resolution
17+
return text_edits.guess_text_edit(vim.api.nvim_get_current_buf(), item)
18+
end
19+
320
function text_edits.apply_text_edits(client_id, edits)
421
local client = vim.lsp.get_client_by_id(client_id)
522
local offset_encoding = client ~= nil and client.offset_encoding or 'utf-16'

0 commit comments

Comments
 (0)