@@ -6,55 +6,63 @@ local brackets_lib = require('blink.cmp.accept.brackets')
6
6
local function accept (item )
7
7
require (' blink.cmp.trigger.completion' ).hide ()
8
8
9
- -- create an undo point
10
- if require (' blink.cmp.config' ).accept .create_undo_point then
11
- vim .api .nvim_feedkeys (vim .api .nvim_replace_termcodes (' <C-g>u' , true , true , true ), ' n' , true )
12
- end
9
+ -- start the resolve immediately since text changes can invalidate the item
10
+ -- with some LSPs (i.e. rust-analyzer) causing them to return the item as-is
11
+ -- without i.e. auto-imports
12
+ require (' blink.cmp.sources.lib' ).resolve (item ):map (function (resolved_item )
13
+ local all_text_edits =
14
+ vim .deepcopy (resolved_item and resolved_item .additionalTextEdits or item .additionalTextEdits or {})
13
15
14
- item = vim .deepcopy (item )
15
- item .textEdit = text_edits_lib .get_from_item (item )
16
+ -- create an undo point
17
+ if require (' blink.cmp.config' ).accept .create_undo_point then
18
+ vim .api .nvim_feedkeys (vim .api .nvim_replace_termcodes (' <C-g>u' , true , true , true ), ' n' , true )
19
+ end
16
20
17
- -- Add brackets to the text edit if needed
18
- local brackets_status , text_edit_with_brackets , offset = brackets_lib .add_brackets (vim .bo .filetype , item )
19
- item .textEdit = text_edit_with_brackets
21
+ item = vim .deepcopy (item )
22
+ item .textEdit = text_edits_lib .get_from_item (item )
20
23
21
- -- Snippet
22
- if item .insertTextFormat == vim .lsp .protocol .InsertTextFormat .Snippet then
23
- -- We want to handle offset_encoding and the text edit api can do this for us
24
- -- so we empty the newText and apply
25
- local temp_text_edit = vim .deepcopy (item .textEdit )
26
- temp_text_edit .newText = ' '
27
- text_edits_lib .apply_text_edits (item .client_id , { temp_text_edit })
24
+ -- Add brackets to the text edit if needed
25
+ local brackets_status , text_edit_with_brackets , offset = brackets_lib .add_brackets (vim .bo .filetype , item )
26
+ item .textEdit = text_edit_with_brackets
28
27
29
- -- Expand the snippet
30
- vim .snippet .expand (item .textEdit .newText )
28
+ -- Snippet
29
+ if item .insertTextFormat == vim .lsp .protocol .InsertTextFormat .Snippet then
30
+ -- We want to handle offset_encoding and the text edit api can do this for us
31
+ -- so we empty the newText and apply
32
+ local temp_text_edit = vim .deepcopy (item .textEdit )
33
+ temp_text_edit .newText = ' '
34
+ table.insert (all_text_edits , temp_text_edit )
35
+ text_edits_lib .apply_text_edits (item .client_id , all_text_edits )
36
+
37
+ -- Expand the snippet
38
+ vim .snippet .expand (item .textEdit .newText )
31
39
32
40
-- OR Normal: Apply the text edit and move the cursor
33
- else
34
- text_edits_lib .apply_text_edits (item .client_id , { item .textEdit })
35
- vim .api .nvim_win_set_cursor (0 , {
36
- item .textEdit .range .start .line + 1 ,
37
- item .textEdit .range .start .character + # item .textEdit .newText + offset ,
38
- })
39
- end
40
-
41
- -- Check semantic tokens for brackets, if needed, and apply additional text edits
42
- if brackets_status == ' check_semantic_token' then
43
- -- todo: since we apply the additional text edits after, auto imported functions will not
44
- -- get auto brackets. If we apply them before, we have to modify the textEdit to compensate
45
- brackets_lib .add_brackets_via_semantic_token (vim .bo .filetype , item , function ()
41
+ else
42
+ table.insert (all_text_edits , item .textEdit )
43
+ text_edits_lib .apply_text_edits (item .client_id , all_text_edits )
44
+ vim .api .nvim_win_set_cursor (0 , {
45
+ item .textEdit .range .start .line + 1 ,
46
+ item .textEdit .range .start .character + # item .textEdit .newText + offset ,
47
+ })
48
+ end
49
+
50
+ -- Check semantic tokens for brackets, if needed, and apply additional text edits
51
+ if brackets_status == ' check_semantic_token' then
52
+ -- todo: since we apply the additional text edits after, auto imported functions will not
53
+ -- get auto brackets. If we apply them before, we have to modify the textEdit to compensate
54
+ brackets_lib .add_brackets_via_semantic_token (vim .bo .filetype , item , function ()
55
+ require (' blink.cmp.trigger.completion' ).show_if_on_trigger_character ()
56
+ require (' blink.cmp.trigger.signature' ).show_if_on_trigger_character ()
57
+ end )
58
+ else
46
59
require (' blink.cmp.trigger.completion' ).show_if_on_trigger_character ()
47
60
require (' blink.cmp.trigger.signature' ).show_if_on_trigger_character ()
48
- text_edits_lib .apply_additional_text_edits (item )
49
- end )
50
- else
51
- require (' blink.cmp.trigger.completion' ).show_if_on_trigger_character ()
52
- require (' blink.cmp.trigger.signature' ).show_if_on_trigger_character ()
53
- text_edits_lib .apply_additional_text_edits (item )
54
- end
55
-
56
- -- Notify the rust module that the item was accessed
57
- require (' blink.cmp.fuzzy' ).access (item )
61
+ end
62
+
63
+ -- Notify the rust module that the item was accessed
64
+ require (' blink.cmp.fuzzy' ).access (item )
65
+ end )
58
66
end
59
67
60
68
return accept
0 commit comments