@@ -120,14 +120,16 @@ function! copilot#Clear() abort
120120 return ' '
121121endfunction
122122
123- function ! s: Reject () abort
124- if exists (' s:uuid' )
125- call copilot#Request (' notifyRejected' , {' uuids' : [remove (s: , ' uuid' )]})
123+ function ! s: Reject (bufnr ) abort
124+ let uuid = getbufvar (a: bufnr , ' _copilot_uuid' )
125+ if ! empty (uuid)
126+ call setbufvar (a: bufnr , ' _copilot_uuid' , ' ' )
127+ call copilot#Request (' notifyRejected' , {' uuids' : [uuid]})
126128 endif
127129endfunction
128130
129131function ! copilot#Dismiss () abort
130- call s: Reject ()
132+ call s: Reject (' % ' )
131133 call copilot#Clear ()
132134 call s: UpdatePreview ()
133135 return ' '
@@ -145,6 +147,9 @@ let s:filetype_defaults = {
145147 \ ' .' : 0 }
146148
147149function ! s: BufferDisabled () abort
150+ if &buftype = ~# ' ^\%(help\|prompt\|quickfix\|terminal\)$'
151+ return 5
152+ endif
148153 if exists (' b:copilot_disabled' )
149154 return empty (b: copilot_disabled ) ? 0 : 3
150155 endif
@@ -197,11 +202,11 @@ endfunction
197202
198203function ! s: SuggestionTextWithAdjustments () abort
199204 try
200- if mode () !~# ' ^[iR]' || (s: HideDuringCompletion () && pumvisible ()) || ! s: dest || ! exists (' b:_copilot.suggestions' )
205+ if mode () !~# ' ^[iR]' || (s: HideDuringCompletion () && pumvisible ()) || ! exists (' b:_copilot.suggestions' )
201206 return [' ' , 0 , 0 , ' ' ]
202207 endif
203208 let choice = get (b: _copilot .suggestions, b: _copilot .choice, {})
204- if ! has_key (choice, ' range' ) || choice.range .start .line != line (' .' ) - 1
209+ if ! has_key (choice, ' range' ) || choice.range .start .line != line (' .' ) - 1 || type (choice.text) !=# v: t_string
205210 return [' ' , 0 , 0 , ' ' ]
206211 endif
207212 let line = getline (' .' )
@@ -294,49 +299,6 @@ function! copilot#GetDisplayedSuggestion() abort
294299 \ ' deleteSize' : delete }
295300endfunction
296301
297- let s: dest = 0
298- function ! s: WindowPreview (lines , outdent, delete , ... ) abort
299- try
300- if ! bufloaded (s: dest )
301- let s: dest = - s: has_ghost_text
302- return
303- endif
304- let buf = s: dest
305- let winid = bufwinid (buf )
306- call setbufvar (buf , ' &modifiable' , 1 )
307- let old_lines = getbufline (buf , 1 , ' $' )
308- if len (a: lines ) < len (old_lines) && old_lines !=# [' ' ]
309- silent call deletebufline (buf , 1 , ' $' )
310- endif
311- if empty (a: lines )
312- call setbufvar (buf , ' &modifiable' , 0 )
313- if winid > 0
314- call setmatches ([], winid)
315- endif
316- return
317- endif
318- let col = col (' .' ) - a: outdent - 1
319- let text = [strpart (getline (' .' ), 0 , col ) . a: lines [0 ]] + a: lines [1 :-1 ]
320- if old_lines !=# text
321- silent call setbufline (buf , 1 , text)
322- endif
323- call setbufvar (buf , ' &tabstop' , &tabstop )
324- if getbufvar (buf , ' &filetype' ) !=# ' copilot.' . &filetype
325- silent ! call setbufvar (buf , ' &filetype' , ' copilot.' . &filetype )
326- endif
327- call setbufvar (buf , ' &modifiable' , 0 )
328- if winid > 0
329- if col > 0
330- call setmatches ([{' group' : s: hlgroup , ' id' : 4 , ' priority' : 10 , ' pos1' : [1 , 1 , col ]}] , winid)
331- else
332- call setmatches ([] , winid)
333- endif
334- endif
335- catch
336- call copilot#logger#Exception ()
337- endtry
338- endfunction
339-
340302function ! s: ClearPreview () abort
341303 if s: has_nvim_ghost_text
342304 call nvim_buf_del_extmark (0 , copilot#NvimNs (), 1 )
@@ -353,10 +315,7 @@ function! s:UpdatePreview() abort
353315 if empty (text[-1 ])
354316 call remove (text, -1 )
355317 endif
356- if s: dest > 0
357- call s: WindowPreview (text, outdent, delete )
358- endif
359- if empty (text) || s: dest >= 0
318+ if empty (text) || ! s: has_ghost_text
360319 return s: ClearPreview ()
361320 endif
362321 if exists (' b:_copilot.cycling_callbacks' )
@@ -391,9 +350,9 @@ function! s:UpdatePreview() abort
391350 call prop_add (line (' .' ), col (' $' ), {' type' : s: annot_hlgroup , ' text' : ' ' . annot})
392351 endif
393352 endif
394- if uuid !=# get (s : , ' uuid ' , ' ' )
395- call s: Reject ()
396- let s: uuid = uuid
353+ if uuid !=# get (b : , ' _copilot_uuid ' , ' ' )
354+ call s: Reject (' % ' )
355+ let b: _copilot_uuid = uuid
397356 call copilot#Request (' notifyShown' , {' uuid' : uuid})
398357 endif
399358 catch
@@ -436,7 +395,7 @@ let s:is_mapped = copilot#IsMapped()
436395
437396function ! copilot#Schedule (... ) abort
438397 call copilot#Clear ()
439- if ! s: is_mapped || ! s: dest || ! copilot#Enabled ()
398+ if ! s: is_mapped || ! s: has_ghost_text || ! copilot#Enabled ()
440399 return
441400 endif
442401 let delay = a: 0 ? a: 1 : get (g: , ' copilot_idle_delay' , 75 )
@@ -448,14 +407,6 @@ function! copilot#OnInsertLeave() abort
448407endfunction
449408
450409function ! copilot#OnInsertEnter () abort
451- let s: is_mapped = copilot#IsMapped ()
452- let s: dest = bufnr (' ^copilot://$' )
453- if s: dest > 0 && bufwinnr (s: dest ) < 0
454- let s: dest = -1
455- endif
456- if s: dest < 0 && ! s: has_ghost_text
457- let s: dest = 0
458- endif
459410 return copilot#Schedule ()
460411endfunction
461412
@@ -471,8 +422,11 @@ function! copilot#OnCursorMovedI() abort
471422 return copilot#Schedule ()
472423endfunction
473424
425+ function ! copilot#OnBufUnload () abort
426+ call s: Reject (+ expand (' <abuf>' ))
427+ endfunction
428+
474429function ! copilot#OnVimLeavePre () abort
475- return s: Reject ()
476430endfunction
477431
478432function ! copilot#TextQueuedForInsertion () abort
@@ -487,8 +441,8 @@ function! copilot#Accept(...) abort
487441 let s = copilot#GetDisplayedSuggestion ()
488442 if ! empty (s .text)
489443 unlet ! b: _copilot
444+ let b: _copilot_uuid = ' '
490445 call copilot#Request (' notifyAccepted' , {' uuid' : s .uuid})
491- unlet ! s: uuid
492446 call s: ClearPreview ()
493447 let s: suggestion_text = s .text
494448 return repeat (" \<Left>\<Del> " , s .outdentSize) . repeat (" \<Del> " , s .deleteSize) .
@@ -503,7 +457,6 @@ function! copilot#Accept(...) abort
503457 try
504458 return call (a: 1 , [])
505459 catch
506- call copilot#logger#Exception ()
507460 return default
508461 endtry
509462 else
@@ -535,7 +488,7 @@ let s:commands = {}
535488
536489function ! s: EnabledStatusMessage () abort
537490 let buf_disabled = s: BufferDisabled ()
538- if ! s: has_ghost_text && bufwinid ( ' copilot:// ' ) == -1
491+ if ! s: has_ghost_text
539492 if has (' nvim' )
540493 return " Neovim 0.6 required to support ghost text"
541494 else
@@ -545,6 +498,8 @@ function! s:EnabledStatusMessage() abort
545498 return ' <Tab> map has been disabled or is claimed by another plugin'
546499 elseif ! get (g: , ' copilot_enabled' , 1 )
547500 return ' Disabled globally by :Copilot disable'
501+ elseif buf_disabled is # 5
502+ return ' Disabled for current buffer by buftype=' . &buftype
548503 elseif buf_disabled is # 4
549504 return ' Disabled for current buffer by b:copilot_enabled'
550505 elseif buf_disabled is # 3
@@ -753,24 +708,6 @@ function! s:commands.panel(opts) abort
753708 endif
754709endfunction
755710
756- function ! s: commands .split (opts) abort
757- let mods = a: opts .mods
758- if mods !~# ' \<\%(aboveleft\|belowright\|leftabove\|rightbelow\|topleft\|botright\|tab\)\>'
759- let mods = ' topleft ' . mods
760- endif
761- if a: opts .bang && getwinvar (bufwinid (' copilot://' ), ' &previewwindow' )
762- if mode () = ~# ' ^[iR]'
763- " called from <Cmd> map
764- return mods . ' pclose|sil! call copilot#OnInsertEnter()'
765- else
766- return mods . ' pclose'
767- endif
768- endif
769- return mods . ' pedit copilot://'
770- endfunction
771-
772- let s: commands .open = s: commands .split
773-
774711function ! copilot#CommandComplete (arg, lead, pos) abort
775712 let args = matchstr (strpart (a: lead , 0 , a: pos ), ' C\%[opilot][! ] *\zs.*' )
776713 if args !~# ' '
0 commit comments