@@ -43,7 +43,7 @@ function! s:OAuthToken() abort
4343 endif
4444 if getfsize (s: config_hosts ) > 0
4545 try
46- let s: github = get (json_decode (join (readfile (s: config_hosts ))), ' github.com' )
46+ let s: github = get (json_decode (join (readfile (s: config_hosts ))), ' github.com' , {} )
4747 catch
4848 let s: github = {}
4949 endtry
@@ -130,60 +130,6 @@ function! s:TermsAccepted(force_reload) abort
130130 return s: terms_accepted
131131endfunction
132132
133- function ! s: AuthException (response, ... ) abort
134- unlet ! s: auth_request
135- endfunction
136-
137- function ! s: AuthCallback (response, ... ) abort
138- unlet ! s: auth_request
139- let data = s: JsonBody (a: response )
140- if a: response .status == 404
141- call s: OAuthSave (' ' , ' ' )
142- elseif has_key (data, ' token' )
143- let s: auth_data = data
144- endif
145- endfunction
146-
147- function ! s: AuthRefresh () abort
148- let token = s: OAuthToken ()
149- if ! empty (token)
150- if exists (' s:auth_request' )
151- return
152- endif
153- let s: auth_request = copilot#HttpRequest (
154- \ ' https://api.github.com/copilot_internal/token' ,
155- \ {' headers' : {' Authorization' : ' Bearer ' . token}},
156- \ function (' s:AuthCallback' ),
157- \ function (' s:AuthException' ))
158- endif
159- endfunction
160-
161- function ! s: AuthFetch () abort
162- let auth = get (s: , ' auth_data' , {})
163- if get (auth, ' expires_at' ) < localtime () - 1800
164- call s: AuthRefresh ()
165- return exists (' s:auth_request' )
166- elseif get (auth, ' expires_at' ) < localtime () - 7200
167- call s: AuthRefresh ()
168- return 0
169- endif
170- endfunction
171-
172- function ! s: Auth () abort
173- if s: AuthFetch ()
174- call s: auth_request .Wait ()
175- endif
176- if get (get (s: , ' auth_data' , {}), ' expires_at' ) > localtime () + 600
177- return s: auth_data
178- else
179- unlet ! s: auth_data
180- return {}
181- endif
182- endfunction
183-
184- unlet ! s: auth_data
185- unlet ! s: auth_request
186-
187133function ! copilot#NvimNs () abort
188134 return nvim_create_namespace (' github-copilot' )
189135endfunction
@@ -244,17 +190,11 @@ function! copilot#Enabled() abort
244190endfunction
245191
246192function ! copilot#Request (method, params, ... ) abort
247- let params = copy (a: params )
248- let auth = s: Auth ()
249- if ! empty (auth) && ! has_key (params, ' token' )
250- let params.token = auth.token
251- endif
252- return call (' copilot#agent#Request' , [a: method , params] + a: 000 )
193+ return call (' copilot#agent#Request' , [a: method , a: params ] + a: 000 )
253194endfunction
254195
255196function ! copilot#Call (method, params, ... ) abort
256- let request = call (' copilot#Request' , [a: method , a: params ] + a: 000 )
257- return a: 0 ? request : request.Await ()
197+ return call (' copilot#agent#Call' , [a: method , a: params ] + a: 000 )
258198endfunction
259199
260200function ! copilot#Complete (... ) abort
@@ -266,12 +206,8 @@ function! copilot#Complete(...) abort
266206 endif
267207 let doc = copilot#doc#Get ()
268208 if ! exists (' g:_copilot_completion.params.doc' ) || g: _copilot_completion .params.doc !=# doc
269- let auth = s: Auth ()
270- if empty (auth)
271- return {}
272- endif
273209 let g: _copilot_completion =
274- \ copilot#agent#Request (' getCompletions' , {' doc' : doc, ' options' : {}, ' token ' : auth.token })
210+ \ copilot#agent#Request (' getCompletions' , {' doc' : doc, ' options' : {}})
275211 let g: _copilot_last_completion = g: _copilot_completion
276212 endif
277213 let completion = g: _copilot_completion
@@ -316,6 +252,14 @@ function! s:SuggestionTextWithAdjustments() abort
316252 return [' ' , 0 , 0 ]
317253endfunction
318254
255+ function ! copilot#GetDisplayedSuggestion () abort
256+ let [text, outdent, delete ] = s: SuggestionTextWithAdjustments ()
257+ return {
258+ \ ' text' : text,
259+ \ ' outdentSize' : outdent,
260+ \ ' deleteSize' : delete }
261+ endfunction
262+
319263let s: dest = 0
320264function ! s: WindowPreview (lines , outdent, delete , ... ) abort
321265 try
@@ -425,7 +369,6 @@ function! copilot#Schedule(...) abort
425369 if ! s: is_mapped || ! s: dest || ! copilot#Enabled ()
426370 return
427371 endif
428- call s: AuthFetch ()
429372 let delay = a: 0 ? a: 1 : get (g: , ' copilot_idle_delay' , 75 )
430373 let g: _copilot_timer = timer_start (delay, function (' s:Trigger' , [bufnr (' ' )]))
431374endfunction
@@ -452,7 +395,7 @@ function! copilot#OnCursorMovedI() abort
452395 return copilot#Schedule ()
453396endfunction
454397
455- function ! copilot#SuggestionText () abort
398+ function ! copilot#TextQueuedForInsertion () abort
456399 try
457400 return remove (s: , ' suggestion_text' )
458401 catch
@@ -461,13 +404,13 @@ function! copilot#SuggestionText() abort
461404endfunction
462405
463406function ! copilot#Accept (... ) abort
464- let [text, outdent, delete ] = s: SuggestionTextWithAdjustments ()
465- if ! empty (text)
407+ let s = copilot#GetDisplayedSuggestion ()
408+ if ! empty (s . text)
466409 unlet ! b: _copilot_suggestion b: _copilot_completion
467410 call s: ClearPreview ()
468- let s: suggestion_text = text
469- return repeat (" \<Left>\<Del> " , outdent ) . repeat (" \<Del> " , delete ) .
470- \ " \<C-R>\<C-O> =copilot#SuggestionText ()\<CR> "
411+ let s: suggestion_text = s . text
412+ return repeat (" \<Left>\<Del> " , s .outdentSize ) . repeat (" \<Del> " , s .deleteSize ) .
413+ \ " \<C-R>\<C-O> =copilot#TextQueuedForInsertion ()\<CR> "
471414 endif
472415 let default = get (g: , ' copilot_tab_fallback' , pumvisible () ? " \<C-N> " : " \t " )
473416 if ! a: 0
@@ -621,7 +564,20 @@ function! s:commands.status(opts) abort
621564 return
622565 endif
623566
624- echo ' Copilot: Enabled and engaged'
567+ echo ' Copilot: Enabled and online'
568+ endfunction
569+
570+ function ! s: commands .signout (opts) abort
571+ unlet ! s: github
572+ if empty (s: OAuthToken ())
573+ echo ' Copilot: Not signed in'
574+ else
575+ let user = get (s: github , ' user' , ' <unknown>' )
576+ let s: github = {}
577+ echo ' Copilot: Signed out as GitHub user ' . user
578+ endif
579+ call delete (s: config_hosts )
580+ call copilot#Call (' signOut' , {})
625581endfunction
626582
627583function ! s: commands .setup (opts) abort
@@ -720,6 +676,7 @@ function! s:commands.setup(opts) abort
720676 unlet ! s: terms_accepted
721677 endif
722678
679+ call copilot#Call (' checkStatus' , {})
723680 echo ' Copilot: Authenticated as GitHub user ' . s: github .user
724681endfunction
725682
0 commit comments