@@ -5,9 +5,19 @@ let g:autoloaded_copilot = 1
55
66scriptencoding utf- 8
77
8- let s: has_ghost_text = has (' nvim-0.6' ) && exists (' *nvim_buf_get_mark' )
8+ let s: has_nvim_ghost_text = has (' nvim-0.6' ) && exists (' *nvim_buf_get_mark' )
9+ let s: has_vim_ghost_text = has (' patch-9.0.0162' ) && has (' textprop' )
10+ let s: has_ghost_text = s: has_nvim_ghost_text || s: has_vim_ghost_text
911
1012let s: hlgroup = ' CopilotSuggestion'
13+ let s: annot_hlgroup = ' CopilotAnnotation'
14+
15+ if s: has_vim_ghost_text && empty (prop_type_get (s: hlgroup ))
16+ call prop_type_add (s: hlgroup , {' highlight' : s: hlgroup })
17+ endif
18+ if s: has_vim_ghost_text && empty (prop_type_get (s: annot_hlgroup ))
19+ call prop_type_add (s: annot_hlgroup , {' highlight' : s: annot_hlgroup })
20+ endif
1121
1222if len ($XDG_CONFIG_HOME )
1323 let s: config_root = $XDG_CONFIG_HOME
@@ -205,15 +215,16 @@ function! s:SuggestionTextWithAdjustments() abort
205215 return [' ' , 0 , 0 , ' ' ]
206216 endif
207217 let typed = strpart (line , 0 , offset)
208- let delete = strchars ( strpart (line , offset) )
218+ let delete = strpart (line , offset)
209219 let uuid = get (choice, ' uuid' , ' ' )
210- if typed == # strpart (choice.text, 0 , offset)
211- return [strpart (choice.text, offset), 0 , delete , uuid]
212- elseif typed = ~# ' ^\s*$'
220+ if typed = ~# ' ^\s*$'
213221 let leading = matchstr (choice.text, ' ^\s\+' )
214- if strpart (typed, 0 , len (leading)) == leading
215- return [strpart (choice.text, len (leading)), len (typed) - len (leading), delete , uuid]
222+ let unindented = strpart (choice.text, len (leading))
223+ if strpart (typed, 0 , len (leading)) == leading && unindented !=# delete
224+ return [unindented, len (typed) - len (leading), strchars (delete ), uuid]
216225 endif
226+ elseif typed == # strpart (choice.text, 0 , offset)
227+ return [strpart (choice.text, offset), 0 , strchars (delete ), uuid]
217228 endif
218229 catch
219230 call copilot#logger#Exception ()
@@ -330,8 +341,11 @@ function! s:WindowPreview(lines, outdent, delete, ...) abort
330341endfunction
331342
332343function ! s: ClearPreview () abort
333- if exists ( ' *nvim_buf_del_extmark ' )
344+ if s: has_nvim_ghost_text
334345 call nvim_buf_del_extmark (0 , copilot#NvimNs (), 1 )
346+ elseif s: has_vim_ghost_text
347+ call prop_remove ({' type' : s: hlgroup , ' all' : v: true })
348+ call prop_remove ({' type' : s: annot_hlgroup , ' all' : v: true })
335349 endif
336350endfunction
337351
@@ -349,24 +363,36 @@ function! s:UpdatePreview() abort
349363 return s: ClearPreview ()
350364 endif
351365 if exists (' b:_copilot.cycling_callbacks' )
352- let annot = [[ ' ' ], [ ' (1/…)' , ' CopilotAnnotation ' ]]
366+ let annot = ' (1/…)'
353367 elseif exists (' b:_copilot.cycling' )
354- let annot = [[ ' ' ], [ ' (' . (b: _copilot .choice + 1 ) . ' /' . len (b: _copilot .suggestions) . ' )' , ' CopilotAnnotation ' ]]
368+ let annot = ' (' . (b: _copilot .choice + 1 ) . ' /' . len (b: _copilot .suggestions) . ' )'
355369 else
356- let annot = []
370+ let annot = ' '
357371 endif
358- let data = {' id' : 1 }
359- let data.virt_text_win_col = virtcol (' .' ) - 1
360- let data.virt_text = [[text[0 ] . repeat (' ' , delete - len (text[0 ])), s: hlgroup ]]
361- if len (text) > 1
362- let data.virt_lines = map (text[1 :-1 ], { _, l - > [[l , s: hlgroup ]] })
363- let data.virt_lines[-1 ] += annot
372+ call s: ClearPreview ()
373+ if s: has_nvim_ghost_text
374+ let data = {' id' : 1 }
375+ let data.virt_text_win_col = virtcol (' .' ) - 1
376+ let data.virt_text = [[text[0 ] . repeat (' ' , delete - len (text[0 ])), s: hlgroup ]]
377+ if len (text) > 1
378+ let data.virt_lines = map (text[1 :-1 ], { _, l - > [[l , s: hlgroup ]] })
379+ if ! empty (annot)
380+ let data.virt_lines[-1 ] += [[' ' ], [annot, s: annot_hlgroup ]]
381+ endif
382+ elseif len (annot)
383+ let data.virt_text += [[' ' ], [annot, s: annot_hlgroup ]]
384+ endif
385+ let data.hl_mode = ' combine'
386+ call nvim_buf_set_extmark (0 , copilot#NvimNs (), line (' .' )-1 , col (' .' )-1 , data)
364387 else
365- let data.virt_text += annot
388+ call prop_add (line (' .' ), col (' .' ), {' type' : s: hlgroup , ' text' : text[0 ]})
389+ for line in text[1 :]
390+ call prop_add (line (' .' ), col (' .' ), {' type' : s: hlgroup , ' text_align' : ' below' , ' text' : line })
391+ endfor
392+ if ! empty (annot)
393+ call prop_add (line (' .' ), col (' $' ), {' type' : s: annot_hlgroup , ' text' : ' ' . annot[1 ][0 ]})
394+ endif
366395 endif
367- let data.hl_mode = ' combine'
368- call nvim_buf_del_extmark (0 , copilot#NvimNs (), 1 )
369- call nvim_buf_set_extmark (0 , copilot#NvimNs (), line (' .' )-1 , col (' .' )-1 , data)
370396 if uuid !=# get (s: , ' uuid' , ' ' )
371397 let s: uuid = uuid
372398 call copilot#Request (' notifyShown' , {' uuid' : uuid})
0 commit comments