@@ -206,12 +206,17 @@ function! s:SuggestionTextWithAdjustments() abort
206206 return [' ' , 0 , 0 , ' ' ]
207207 endif
208208 let typed = strpart (line , 0 , offset)
209- let delete = strpart (line , offset)
210- if choice.range .end .line == line (' .' ) - 1 && choice.range .end .character < copilot#doc#UTF16Width (line )
211- let append = delete
209+ if exists (' *utf16idx' )
210+ let end_offset = byteidx (line , choice.range .end .character , 1 )
211+ elseif has (' nvim' )
212+ let end_offset = v: lua .vim .str_byteindex (line , choice.range .end .character , 1 )
212213 else
213- let append = ' '
214+ let end_offset = len (line )
215+ while copilot#doc#UTF16Width (strpart (line , 0 , end_offset)) > choice.range .end .character && end_offset > 0
216+ let end_offset -= 1
217+ endwhile
214218 endif
219+ let delete = strpart (line , offset, end_offset - offset)
215220 let uuid = get (choice, ' uuid' , ' ' )
216221 if typed = ~# ' ^\s*$'
217222 let leading = matchstr (choice.text, ' ^\s\+' )
@@ -220,7 +225,7 @@ function! s:SuggestionTextWithAdjustments() abort
220225 return [unindented, len (typed) - len (leading), strchars (delete ), uuid]
221226 endif
222227 elseif typed == # strpart (choice.text, 0 , offset)
223- return [strpart (choice.text . append , offset), 0 , strchars (delete ), uuid]
228+ return [strpart (choice.text, offset), 0 , strchars (delete ), uuid]
224229 endif
225230 catch
226231 call copilot#logger#Exception ()
@@ -369,7 +374,8 @@ function! s:UpdatePreview() abort
369374 if s: has_nvim_ghost_text
370375 let data = {' id' : 1 }
371376 let data.virt_text_win_col = virtcol (' .' ) - 1
372- let data.virt_text = [[text[0 ] . repeat (' ' , delete - len (text[0 ])), s: hlgroup ]]
377+ let append = strpart (getline (' .' ), col (' .' ) - 1 + delete )
378+ let data.virt_text = [[text[0 ] . append . repeat (' ' , delete - len (text[0 ])), s: hlgroup ]]
373379 if len (text) > 1
374380 let data.virt_lines = map (text[1 :-1 ], { _, l - > [[l , s: hlgroup ]] })
375381 if ! empty (annot)
@@ -381,11 +387,6 @@ function! s:UpdatePreview() abort
381387 let data.hl_mode = ' combine'
382388 call nvim_buf_set_extmark (0 , copilot#NvimNs (), line (' .' )-1 , col (' .' )-1 , data)
383389 else
384- let trail = strpart (getline (' .' ), col (' .' ) - 1 )
385- while ! empty (trail) && trail[-1 ] == # text[0 ][-1 ]
386- let trail = trail[:-2 ]
387- let text[0 ] = text[0 ][:-2 ]
388- endwhile
389390 call prop_add (line (' .' ), col (' .' ), {' type' : s: hlgroup , ' text' : text[0 ]})
390391 for line in text[1 :]
391392 call prop_add (line (' .' ), 0 , {' type' : s: hlgroup , ' text_align' : ' below' , ' text' : line })
@@ -540,7 +541,7 @@ function! copilot#AcceptAll(...) abort
540541 call s: ClearPreview ()
541542 let s: suggestion_text = s .text
542543 return repeat (" \<Left>\<Del> " , s .outdentSize) . repeat (" \<Del> " , s .deleteSize) .
543- \ " \<C-R>\<C-O> =copilot#TextQueuedForInsertion()\<CR> "
544+ \ " \<C-R>\<C-O> =copilot#TextQueuedForInsertion()\<CR>\<End> "
544545 endif
545546 let default = get (g: , ' copilot_tab_fallback' , pumvisible () ? " \<C-N> " : " \t " )
546547 if ! a: 0
0 commit comments