@@ -4,6 +4,7 @@ local config = require("copilot.config")
4
4
local hl_group = require (" copilot.highlight" ).group
5
5
local util = require (" copilot.util" )
6
6
local logger = require (" copilot.logger" )
7
+ local suggestion_util = require (" copilot.suggestion.utils" )
7
8
8
9
local M = {}
9
10
@@ -56,6 +57,27 @@ local function get_ctx(bufnr)
56
57
return ctx
57
58
end
58
59
60
+ --- @param idx integer
61
+ --- @param text string
62
+ --- @param bufnr ? integer
63
+ local function set_ctx_suggestion_text (idx , text , bufnr )
64
+ bufnr = bufnr or vim .api .nvim_get_current_buf ()
65
+
66
+ if not copilot .context [bufnr ] then
67
+ return
68
+ end
69
+
70
+ if not copilot .context [bufnr ].suggestions [idx ] then
71
+ return
72
+ end
73
+
74
+ local suggestion = copilot .context [bufnr ].suggestions [idx ]
75
+ local end_offset = # suggestion .text - # text
76
+ suggestion .text = text
77
+ suggestion .range [" end" ].character = suggestion .range [" end" ].character - end_offset
78
+ copilot .context [bufnr ].suggestions [idx ] = suggestion
79
+ end
80
+
59
81
--- @param ctx copilot_suggestion_context
60
82
local function reset_ctx (ctx )
61
83
logger .trace (" suggestion reset context" , ctx )
@@ -255,8 +277,6 @@ local function update_preview(ctx)
255
277
return
256
278
end
257
279
258
- --- @todo support popup preview
259
-
260
280
local annot = " "
261
281
if ctx .cycling_callbacks then
262
282
annot = " (1/…)"
@@ -265,14 +285,25 @@ local function update_preview(ctx)
265
285
end
266
286
267
287
local cursor_col = vim .fn .col (" ." )
288
+ local cursor_line = vim .fn .line (" ." ) - 1
289
+ local current_line = vim .api .nvim_buf_get_lines (0 , cursor_line , cursor_line + 1 , false )[1 ]
290
+ local text_after_cursor = string.sub (current_line , cursor_col )
268
291
269
292
displayLines [1 ] =
270
293
string.sub (string.sub (suggestion .text , 1 , (string.find (suggestion .text , " \n " , 1 , true ) or 0 ) - 1 ), cursor_col )
271
294
295
+ local suggestion_line1 = displayLines [1 ]
296
+
297
+ if # displayLines == 1 then
298
+ suggestion_line1 = suggestion_util .remove_common_suffix (text_after_cursor , suggestion_line1 )
299
+ local suggest_text = suggestion_util .remove_common_suffix (text_after_cursor , suggestion .text )
300
+ set_ctx_suggestion_text (ctx .choice , suggest_text )
301
+ end
302
+
272
303
local extmark = {
273
304
id = copilot .extmark_id ,
274
- virt_text_win_col = vim . fn . virtcol ( " . " ) - 1 ,
275
- virt_text = { { displayLines [ 1 ], hl_group . CopilotSuggestion } } ,
305
+ virt_text = { { suggestion_line1 , hl_group . CopilotSuggestion } } ,
306
+ virt_text_pos = " inline " ,
276
307
}
277
308
278
309
if # displayLines > 1 then
@@ -289,7 +320,6 @@ local function update_preview(ctx)
289
320
end
290
321
291
322
extmark .hl_mode = " combine"
292
-
293
323
vim .api .nvim_buf_set_extmark (0 , copilot .ns_id , vim .fn .line (" ." ) - 1 , cursor_col - 1 , extmark )
294
324
295
325
if config .suggestion .suggestion_notification then
@@ -540,14 +570,17 @@ function M.accept(modifier)
540
570
and vim .api .nvim_get_option_value (" fileencoding" , { buf = bufnr })
541
571
or vim .api .nvim_get_option_value (" encoding" , { scope = " global" })
542
572
vim .lsp .util .apply_text_edits ({ { range = range , newText = newText } }, bufnr , encoding )
543
- -- Put cursor at the end of current line.
544
- local cursor_keys = " <End>"
545
-
546
- -- TODO: Move to util and check only once
547
- if vim .fn .has (" nvim-0.10" ) == 1 then
548
- cursor_keys = string.rep (" <Down>" , # vim .split (newText , " \n " , { plain = true }) - 1 ) .. cursor_keys
549
- end
550
- vim .api .nvim_feedkeys (vim .api .nvim_replace_termcodes (cursor_keys , true , false , true ), " n" , false )
573
+ print (range [" end" ].line )
574
+ print (range [" end" ].character )
575
+
576
+ -- instead of calling <End>, go to the pos of the row after the last \n of inserted text
577
+ -- local cursor_keys = string.rep("<Down>", #vim.split(newText, "\n", { plain = true }) - 1) .. "<End>"
578
+ -- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(cursor_keys, true, false, true), "n", false)
579
+ local lines = vim .split (newText , " \n " , { plain = true })
580
+ local last_line = lines [# lines ]
581
+ local cursor_keys = string.rep (" <Down>" , # lines - 1 )
582
+ -- Position cursor at the end of the last inserted line
583
+ vim .api .nvim_win_set_cursor (0 , { range [" start" ].line + # lines , # last_line })
551
584
end )()
552
585
end
553
586
0 commit comments