@@ -88,20 +88,12 @@ function! s:OAuthUserCallback(token, response) abort
8888 endtry
8989endfunction
9090
91- function ! s: InitCodespaces (async) abort
92- if $CODESPACES == # ' true' && len ($GITHUB_TOKEN )
93- let request = copilot#HttpRequest (' https://api.github.com/user' ,
94- \ {' timeout' : 5000 , ' headers' : {' Authorization' : ' Bearer ' . $GITHUB_TOKEN }},
95- \ function (' s:OAuthUserCallback' , [$GITHUB_TOKEN ]))
96- if ! a: async
97- call copilot#agent#Wait (request)
98- endif
99- endif
100- endfunction
91+ if ! exists (' s:github' ) && $CODESPACES == # ' true' && len ($GITHUB_TOKEN )
92+ let s: github = {' oauth_token' : $GITHUB_TOKEN , ' user' : empty ($GITHUB_USER ) ? ' codespace-user' : $GITHUB_USER }
93+ endif
10194
10295function ! copilot#Init (... ) abort
103- call copilot#agent#Start ()
104- call s: InitCodespaces (1 )
96+ call timer_start (0 , { _ - > copilot#agent#Start () })
10597endfunction
10698
10799let s: terms_version = ' 2021-10-14'
@@ -131,6 +123,7 @@ function! s:TermsAccepted(force_reload) abort
131123 try
132124 let s: terms_accepted = s: ReadTerms ()[s: github .user ].version >= s: terms_version
133125 return s: terms_accepted
126+ catch
134127 endtry
135128 endif
136129 let s: terms_accepted = 0
@@ -178,7 +171,7 @@ endfunction
178171
179172function ! s: Auth () abort
180173 if s: AuthFetch ()
181- call copilot#agent#Wait ( s: auth_request )
174+ call s: auth_request. Wait ( )
182175 endif
183176 if get (get (s: , ' auth_data' , {}), ' expires_at' ) > localtime () + 600
184177 return s: auth_data
@@ -203,6 +196,13 @@ function! copilot#Clear() abort
203196 call copilot#agent#Cancel (remove (g: , ' _copilot_completion' ))
204197 endif
205198 call s: UpdatePreview ()
199+ return ' '
200+ endfunction
201+
202+ function ! copilot#Dismiss () abort
203+ unlet ! b: _copilot_suggestion b: _copilot_completion
204+ call copilot#Clear ()
205+ return ' '
206206endfunction
207207
208208let s: filetype_defaults = {
@@ -243,13 +243,18 @@ function! copilot#Enabled() abort
243243 \ && empty (copilot#agent#StartupError ())
244244endfunction
245245
246- function ! copilot#Call (method, params, ... ) abort
246+ function ! copilot#Request (method, params, ... ) abort
247247 let params = copy (a: params )
248248 let auth = s: Auth ()
249249 if ! empty (auth) && ! has_key (params, ' token' )
250250 let params.token = auth.token
251251 endif
252- return call (' copilot#agent#Call' , [a: method , params] + a: 000 )
252+ return call (' copilot#agent#Request' , [a: method , params] + a: 000 )
253+ endfunction
254+
255+ function ! copilot#Call (method, params, ... ) abort
256+ let request = call (' copilot#Request' , [a: method , a: params ] + a: 000 )
257+ return a: 0 ? request : request.Await ()
253258endfunction
254259
255260function ! copilot#Complete (... ) abort
@@ -266,12 +271,12 @@ function! copilot#Complete(...) abort
266271 return {}
267272 endif
268273 let g: _copilot_completion =
269- \ copilot#agent#Send (' getCompletions' , {' doc' : doc, ' options' : {}, ' token' : auth.token})
274+ \ copilot#agent#Request (' getCompletions' , {' doc' : doc, ' options' : {}, ' token' : auth.token})
270275 let g: _copilot_last_completion = g: _copilot_completion
271276 endif
272277 let completion = g: _copilot_completion
273278 if ! a: 0
274- return copilot#agent# Await (completion )
279+ return completion. Await ()
275280 else
276281 call copilot#agent#Result (completion, a: 1 )
277282 if a: 0 > 1
@@ -280,12 +285,12 @@ function! copilot#Complete(...) abort
280285 endif
281286endfunction
282287
283- function ! s: CompletionTextWithAdjustments () abort
288+ function ! s: SuggestionTextWithAdjustments () abort
284289 try
285290 if mode () !~# ' ^[iR]' || pumvisible () || ! s: dest
286291 return [' ' , 0 , 0 ]
287292 endif
288- let choice = get (b: , ' _copilot_completion ' , {})
293+ let choice = get (b: , ' _copilot_suggestion ' , {})
289294 if ! has_key (choice, ' range' ) || choice.range .start .line != line (' .' ) - 1
290295 return [' ' , 0 , 0 ]
291296 endif
@@ -362,7 +367,7 @@ endfunction
362367
363368function ! s: UpdatePreview () abort
364369 try
365- let [text, outdent, delete ] = s: CompletionTextWithAdjustments ()
370+ let [text, outdent, delete ] = s: SuggestionTextWithAdjustments ()
366371 let text = split (text, " \n " , 1 )
367372 if empty (text[-1 ])
368373 call remove (text, -1 )
@@ -386,12 +391,13 @@ function! s:UpdatePreview() abort
386391 endtry
387392endfunction
388393
389- function ! s: AfterComplete (result) abort
394+ function ! s: HandleTriggerResult (result) abort
390395 if exists (' a:result.completions' )
391- let b: _copilot_completion = get (a: result .completions, 0 , {})
396+ let b: _copilot_suggestion = get (a: result .completions, 0 , {})
392397 else
393- let b: _copilot_completion = {}
398+ let b: _copilot_suggestion = {}
394399 endif
400+ let b: _copilot_completion = b: _copilot_suggestion
395401 call s: UpdatePreview ()
396402endfunction
397403
@@ -405,7 +411,7 @@ function! s:Trigger(bufnr, timer) abort
405411 let g: _copilot_timer = timer_start (100 , function (' s:Trigger' , [a: bufnr ]))
406412 return
407413 endif
408- call copilot#Complete (function (' s:AfterComplete ' ), function (' s:AfterComplete ' ))
414+ call copilot#Complete (function (' s:HandleTriggerResult ' ), function (' s:HandleTriggerResult ' ))
409415endfunction
410416
411417function ! copilot#IsMapped () abort
@@ -425,7 +431,7 @@ function! copilot#Schedule(...) abort
425431endfunction
426432
427433function ! copilot#OnInsertLeave () abort
428- unlet ! b: _copilot_completion
434+ unlet ! b: _copilot_suggestion b: _copilot_completion
429435 return copilot#Clear ()
430436endfunction
431437
@@ -455,9 +461,9 @@ function! copilot#SuggestionText() abort
455461endfunction
456462
457463function ! copilot#Accept (... ) abort
458- let [text, outdent, delete ] = s: CompletionTextWithAdjustments ()
464+ let [text, outdent, delete ] = s: SuggestionTextWithAdjustments ()
459465 if ! empty (text)
460- silent ! call remove ( b: , ' _copilot_completion' )
466+ unlet ! b: _copilot_suggestion b: _copilot_completion
461467 call s: ClearPreview ()
462468 let s: suggestion_text = text
463469 return repeat (" \<Left>\<Del> " , outdent) . repeat (" \<Del> " , delete ) .
@@ -626,9 +632,6 @@ function! s:commands.setup(opts) abort
626632
627633 let browser = copilot#Browser ()
628634
629- if ! exists (' s:github' )
630- call s: InitCodespaces (0 )
631- endif
632635 if empty (s: OAuthToken ()) || empty (s: Auth ()) || a: opts .bang
633636 let response = copilot#HttpRequest (' https://github.com/login/device/code' , {
634637 \ ' method' : ' POST' ,
@@ -639,42 +642,44 @@ function! s:commands.setup(opts) abort
639642 let @+ = data.user_code
640643 let @* = data.user_code
641644 echo " First copy your one-time code: " . data.user_code
642- if len (browser)
643- echo " Press ENTER to open " . data.verification_uri . " in your browser "
644- try
645- if len (& mouse )
646- let mouse = & mouse
647- set mouse =
648- endif
645+ try
646+ if len (& mouse )
647+ let mouse = & mouse
648+ set mouse =
649+ endif
650+ if len (browser)
651+ echo " Press ENTER to open GitHub your browser "
649652 let c = getchar ()
650653 while c isnot # 13 && c isnot # 10 && c isnot # 0
651654 let c = getchar ()
652655 endwhile
656+ let exit_status = copilot#job#Stream (browser + [data.verification_uri], v: null , v: null )
657+ if exit_status
658+ echo " Failed to open browser. Visit " . data.verification_uri
659+ else
660+ echo " Opened " . data.verification_uri
661+ endif
662+ else
663+ echo " Could not find browser. Visit " . data.verification_uri
664+ endif
665+ echo " Waiting (could take up to 5 seconds)"
666+ let result = {}
667+ call timer_start ((data.interval+ 1 ) * 1000 , function (' s:DevicePoll' , [result, data]))
668+ try
669+ while ! has_key (result, ' success' )
670+ sleep 100 m
671+ endwhile
653672 finally
654- if exists (' mouse' )
655- let &mouse = mouse
673+ if ! has_key (result, ' success' )
674+ let result.success = 0
675+ let result.error = " Interrupt"
656676 endif
677+ redraw
657678 endtry
658- let exit_status = copilot#job#Stream (browser + [data.verification_uri], v: null , v: null )
659- if exit_status
660- echo " Failed to open browser. Visit " . data.verification_uri
661- endif
662- else
663- echo " Could not find browser. Visit " . data.verification_uri
664- endif
665- echo " Waiting (could take up to 5 seconds)"
666- let result = {}
667- call timer_start ((data.interval+ 1 ) * 1000 , function (' s:DevicePoll' , [result, data]))
668- try
669- while ! has_key (result, ' success' )
670- sleep 100 m
671- endwhile
672679 finally
673- if ! has_key (result, ' success' )
674- let result.success = 0
675- let result.error = " Interrupt"
680+ if exists (' mouse' )
681+ let &mouse = mouse
676682 endif
677- redraw
678683 endtry
679684 if ! result.success
680685 return ' echoerr ' . string (' Copilot: Authentication failure: ' . result.error )
0 commit comments