@@ -3,20 +3,30 @@ local function preview(item, previous_text_edit)
3
3
local text_edits_lib = require (' blink.cmp.accept.text-edits' )
4
4
local text_edit = text_edits_lib .get_from_item (item )
5
5
6
+ -- with auto_insert, we may have to undo the previous preview
7
+ if previous_text_edit ~= nil then text_edit .range = text_edits_lib .get_undo_text_edit_range (previous_text_edit ) end
8
+
6
9
-- for snippets, expand them with the default property names
10
+ local cursor_pos = {
11
+ text_edit .range .start .line + 1 ,
12
+ text_edit .range .start .character + # text_edit .newText ,
13
+ }
7
14
if item .insertTextFormat == vim .lsp .protocol .InsertTextFormat .Snippet then
8
15
local expanded_snippet = require (' blink.cmp.sources.snippets.utils' ).safe_parse (text_edit .newText )
9
16
text_edit .newText = expanded_snippet and tostring (expanded_snippet ) or text_edit .newText
10
- end
11
17
12
- if previous_text_edit ~= nil then text_edit .range = text_edits_lib .get_undo_text_edit_range (previous_text_edit ) end
18
+ -- place the cursor at the first tab stop
19
+ local tabstops = require (' blink.cmp.sources.snippets.utils' ).get_tab_stops (text_edit .newText )
20
+ if tabstops and # tabstops > 0 then
21
+ cursor_pos [1 ] = text_edit .range .start .line + tabstops [1 ].line
22
+ cursor_pos [2 ] = text_edit .range .start .character + tabstops [1 ].character
23
+ end
24
+ end
13
25
14
26
text_edits_lib .apply_text_edits (item .client_id , { text_edit })
15
- vim .api .nvim_win_set_cursor (0 , {
16
- text_edit .range .start .line + 1 ,
17
- text_edit .range .start .character + # text_edit .newText ,
18
- })
27
+ vim .api .nvim_win_set_cursor (0 , cursor_pos )
19
28
29
+ -- return so that it can be undone in the future
20
30
return text_edit
21
31
end
22
32
0 commit comments