Skip to content

Commit 3927e23

Browse files
committed
fix: accept auto brackets
1 parent 13304d4 commit 3927e23

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

lua/blink/cmp/accept/brackets.lua

+8-6
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function brackets.add_brackets(filetype, item)
5555

5656
-- if already contains the brackets, conservatively skip adding brackets
5757
-- todo: won't work for snippets when the brackets_for_filetype is { '{', '}' }
58-
if brackets_for_filetype[1] ~= ' ' and text_edit.newText:match('\\' .. brackets_for_filetype[1]) then
58+
if brackets_for_filetype[1] ~= ' ' and text_edit.newText:match('[\\' .. brackets_for_filetype[1] .. ']') ~= nil then
5959
return 'skipped', text_edit, 0
6060
end
6161

@@ -64,12 +64,16 @@ function brackets.add_brackets(filetype, item)
6464
if item.insertTextFormat == vim.lsp.protocol.InsertTextFormat.Snippet then
6565
local placeholders = brackets.snippets_extract_placeholders(text_edit.newText)
6666
local last_placeholder_index = math.max(0, unpack(placeholders))
67-
text_edit.newText = text_edit.newText .. brackets[1] .. '$' .. tostring(last_placeholder_index + 1) .. brackets[2]
67+
text_edit.newText = text_edit.newText
68+
.. brackets_for_filetype[1]
69+
.. '$'
70+
.. tostring(last_placeholder_index + 1)
71+
.. brackets_for_filetype[2]
6872
-- Otherwise, we add as usual
6973
else
70-
text_edit.newText = text_edit.newText .. brackets[1] .. brackets[2]
74+
text_edit.newText = text_edit.newText .. brackets_for_filetype[1] .. brackets_for_filetype[2]
7175
end
72-
return 'added', text_edit, -#brackets[2]
76+
return 'added', text_edit, -#brackets_for_filetype[2]
7377
end
7478

7579
--- @param snippet string
@@ -89,7 +93,6 @@ end
8993
--- @param item blink.cmp.CompletionItem
9094
--- @param callback fun()
9195
function brackets.add_brackets_via_semantic_token(filetype, item, callback)
92-
vim.print('yo', brackets.should_run_resolution(filetype, 'semantic_token'))
9396
if not brackets.should_run_resolution(filetype, 'semantic_token') then return callback() end
9497

9598
local text_edit = item.textEdit
@@ -164,7 +167,6 @@ function brackets.should_run_resolution(filetype, resolution_method)
164167
if vim.tbl_contains(resolution_blocked_filetypes, filetype) then return false end
165168

166169
-- global
167-
vim.print(config)
168170
if not config.enabled then return false end
169171
if vim.tbl_contains(config.force_allow_filetypes, filetype) then return true end
170172
return not vim.tbl_contains(config.blocked_filetypes, filetype)

lua/blink/cmp/accept/init.lua

+14-16
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,39 @@ local brackets_lib = require('blink.cmp.accept.brackets')
66
local function accept(item)
77
item = vim.deepcopy(item)
88

9-
local text_edit = item.textEdit
10-
if text_edit ~= nil then
11-
-- Adjust the position of the text edit to be the current cursor position
12-
-- since the data might be outdated. We compare the cursor column position
13-
-- from when the items were fetched versus the current.
14-
-- hack: figure out a better way
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
1514
local offset = vim.api.nvim_win_get_cursor(0)[2] - item.cursor_column
16-
text_edit.range['end'].character = text_edit.range['end'].character + offset
15+
item.textEdit.range['end'].character = item.textEdit.range['end'].character + offset
16+
-- No text edit so we fallback to our own resolution
1717
else
18-
-- No text edit so we fallback to our own resolution
19-
text_edit = text_edits_lib.guess_text_edit(vim.api.nvim_get_current_buf(), item)
18+
item.textEdit = text_edits_lib.guess_text_edit(vim.api.nvim_get_current_buf(), item)
2019
end
21-
item.textEdit = text_edit
2220

2321
-- Add brackets to the text edit if needed
2422
local brackets_status, text_edit_with_brackets, offset = brackets_lib.add_brackets(vim.bo.filetype, item)
25-
text_edit = text_edit_with_brackets
23+
item.textEdit = text_edit_with_brackets
2624

2725
-- Snippet
2826
if item.insertTextFormat == vim.lsp.protocol.InsertTextFormat.Snippet then
2927
-- We want to handle offset_encoding and the text edit api can do this for us
3028
-- so we empty the newText and apply
31-
local temp_text_edit = vim.deepcopy(text_edit)
29+
local temp_text_edit = vim.deepcopy(item.textEdit)
3230
temp_text_edit.newText = ''
3331
text_edits_lib.apply_text_edits(item.client_id, { temp_text_edit })
3432

3533
-- Expand the snippet
36-
vim.snippet.expand(text_edit.newText)
34+
vim.snippet.expand(item.textEdit.newText)
3735

3836
-- OR Normal: Apply the text edit and move the cursor
3937
else
40-
text_edits_lib.apply_text_edits(item.client_id, { text_edit })
38+
text_edits_lib.apply_text_edits(item.client_id, { item.textEdit })
4139
vim.api.nvim_win_set_cursor(0, {
42-
text_edit.range.start.line + 1,
43-
text_edit.range.start.character + #text_edit.newText + offset,
40+
item.textEdit.range.start.line + 1,
41+
item.textEdit.range.start.character + #item.textEdit.newText + offset,
4442
})
4543
end
4644

0 commit comments

Comments
 (0)