@@ -12,14 +12,41 @@ local function get_params(up)
12
12
return params
13
13
end
14
14
15
+ local function extract_cursor_position (text_edits )
16
+ local cursor = { text_edits [1 ].range .start .line }
17
+ local prev_te
18
+ for _ , te in ipairs (text_edits ) do
19
+ if te .newText and te .insertTextFormat == 2 then
20
+ if not cursor [2 ] then
21
+ if prev_te then
22
+ cursor [1 ] = cursor [1 ]
23
+ + math.max (0 , te .range .start .line - prev_te .range [' end' ].line - 1 )
24
+ - (prev_te .range .start .line == te .range .start .line and 1 or 0 )
25
+ end
26
+ local pos_start = string.find (te .newText , ' %$0' )
27
+ local lines = vim .split (string.sub (te .newText , 1 , pos_start ), ' \n ' )
28
+ local total_lines = # lines
29
+ cursor [1 ] = cursor [1 ] + total_lines
30
+ if pos_start then
31
+ cursor [2 ] = (total_lines == 1 and te .range .start .character or 0 ) + # lines [total_lines ] - 1
32
+ end
33
+ end
34
+ end
35
+ prev_te = te
36
+ end
37
+ return cursor
38
+ end
39
+
15
40
-- move it baby
16
41
local function handler (_ , result , ctx )
17
42
if result == nil or # result == 0 then
18
43
return
19
44
end
45
+ local cursor = extract_cursor_position (result )
20
46
local overrides = require (' rustaceanvim.overrides' )
21
47
overrides .snippet_text_edits_to_text_edits (result )
22
48
vim .lsp .util .apply_text_edits (result , ctx .bufnr , vim .lsp .get_client_by_id (ctx .client_id ).offset_encoding )
49
+ vim .api .nvim_win_set_cursor (0 , cursor )
23
50
end
24
51
25
52
local rl = require (' rustaceanvim.rust_analyzer' )
0 commit comments