@@ -39,22 +39,28 @@ local brackets = {
39
39
--- @param item blink.cmp.CompletionItem
40
40
--- @return ' added' | ' check_semantic_token' | ' skipped' , lsp.TextEdit | lsp.InsertReplaceEdit , number
41
41
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 )
43
45
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
44
53
-- not a function, skip
45
54
if
46
55
item .kind ~= vim .lsp .protocol .CompletionItemKind .Function
47
56
and item .kind ~= vim .lsp .protocol .CompletionItemKind .Method
48
57
then
49
- return ' check_semantic_token' , item . textEdit , 0
58
+ return ' check_semantic_token' , text_edit , 0
50
59
end
51
60
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
57
62
-- todo: won't work for snippets when the brackets_for_filetype is { '{', '}' }
63
+ -- I've never seen a language like that though
58
64
if brackets_for_filetype [1 ] ~= ' ' and text_edit .newText :match (' [\\ ' .. brackets_for_filetype [1 ] .. ' ]' ) ~= nil then
59
65
return ' skipped' , text_edit , 0
60
66
end
@@ -171,4 +177,13 @@ function brackets.should_run_resolution(filetype, resolution_method)
171
177
return not vim .tbl_contains (config .blocked_filetypes , filetype )
172
178
end
173
179
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
+
174
189
return brackets
0 commit comments