Skip to content

Commit 6149088

Browse files
committed
Copilot.vim 1.0.4
1 parent cd79468 commit 6149088

6 files changed

Lines changed: 71 additions & 102 deletions

File tree

README.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ entire function bodies as you type. GitHub Copilot is powered by the OpenAI
55
Codex AI system, trained on public Internet text and billions of lines of
66
code.
77

8-
Copilot.vim is a Vim plugin for GitHub Copilot. For now, it requires a Neovim
9-
0.6 prerelease (for virtual lines support) and a Node.js installation.
8+
Copilot.vim is a Vim plugin for GitHub Copilot. For now, it requires Neovim
9+
0.6 (for virtual lines support) and a Node.js installation.
1010

1111
To learn more about GitHub Copilot, visit https://copilot.github.com.
1212

@@ -25,11 +25,9 @@ Service](https://docs.github.com/en/github/site-policy/github-terms-of-service#j
2525

2626
## Getting started
2727

28-
1. Install [Node.js][] v12 or newer.
28+
1. Install [Node.js][] 12 or newer.
2929

30-
2. Install a [Neovim prerelease build][]. (Note: On macOS, [extra steps][]
31-
are required to due to lack of notarization. Alternatively, Homebrew users
32-
can run `brew install neovim --HEAD`).
30+
2. Install [Neovim][] 0.6 or newer.
3331

3432
3. Install `github/copilot.vim` using vim-plug, packer.nvim, or any other
3533
plugin manager. Or to install directly:
@@ -40,8 +38,7 @@ Service](https://docs.github.com/en/github/site-policy/github-terms-of-service#j
4038
4. Start Neovim and invoke `:Copilot setup`.
4139

4240
[Node.js]: https://nodejs.org/en/download/
43-
[Neovim prerelease build]: https://github.com/github/copilot.vim/releases/tag/neovim-nightlies
44-
[extra steps]: https://github.com/neovim/neovim/issues/11011#issuecomment-786413100
41+
[Neovim]: https://github.com/neovim/neovim/releases/latest
4542

4643
Suggestions are displayed inline and can be accepted by pressing the tab key.
4744
See `:help copilot` for more information.

autoload/copilot.vim

Lines changed: 33 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -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
131131
endfunction
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-
187133
function! copilot#NvimNs() abort
188134
return nvim_create_namespace('github-copilot')
189135
endfunction
@@ -244,17 +190,11 @@ function! copilot#Enabled() abort
244190
endfunction
245191

246192
function! 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)
253194
endfunction
254195

255196
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()
197+
return call('copilot#agent#Call', [a:method, a:params] + a:000)
258198
endfunction
259199

260200
function! 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]
317253
endfunction
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+
319263
let s:dest = 0
320264
function! 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('')]))
431374
endfunction
@@ -452,7 +395,7 @@ function! copilot#OnCursorMovedI() abort
452395
return copilot#Schedule()
453396
endfunction
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
461404
endfunction
462405

463406
function! 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', {})
625581
endfunction
626582

627583
function! 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
724681
endfunction
725682

autoload/copilot/agent.vim

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@ function! s:AgentClose() dict abort
3030
endfunction
3131

3232
function! s:LogSend(request, line) abort
33-
if type(get(a:request, 'params')) == v:t_dict && has_key(a:request.params, 'token')
34-
let request = deepcopy(a:request)
35-
let request.params.token = 'REDACTED'
36-
let line = json_encode(request)
37-
else
38-
let line = a:line
39-
endif
40-
return '--> ' . line
33+
return '--> ' . a:line
4134
endfunction
4235

4336
let s:chansend = function(exists('*chansend') ? 'chansend' : 'ch_sendraw')
@@ -168,7 +161,7 @@ endfunction
168161
function! s:OnExit(agent, code) abort
169162
let a:agent.exit_status = a:code
170163
call remove(a:agent, 'job')
171-
for id in sort(keys(a:agent.requests), { a, b -> a > b })
164+
for id in sort(keys(a:agent.requests), { a, b -> +a > +b })
172165
let request = remove(a:agent.requests, id)
173166
if request.status ==# 'canceled'
174167
return
@@ -177,7 +170,8 @@ function! s:OnExit(agent, code) abort
177170
call remove(request, 'resolve')
178171
let reject = remove(request, 'reject')
179172
let request.status = 'error'
180-
let request.error = {'code': s:error_exit, 'message': 'Agent exited', 'data': {'status': a:code}}
173+
let code = a:code < 0 || a:code > 255 ? 256 : a:code
174+
let request.error = {'code': code, 'message': 'Agent exited', 'data': {'status': a:code}}
181175
for Cb in reject
182176
let request.waiting[timer_start(0, function('s:Callback', [request, 'error', Cb]))] = 1
183177
endfor
@@ -192,6 +186,20 @@ function! copilot#agent#Close() abort
192186
endif
193187
endfunction
194188

189+
unlet! s:is_arm_macos
190+
function! s:IsArmMacOS() abort
191+
if exists('s:is_arm_macos')
192+
return s:is_arm_macos
193+
elseif has('win32') || !isdirectory('/private')
194+
let s:is_arm_macos = 0
195+
else
196+
let out = []
197+
call copilot#job#Stream(['uname', '-s', '-p'], function('add', [out]), v:null)
198+
let s:is_arm_macos = get(out, 0, '') ==# 'Darwin arm'
199+
endif
200+
return s:is_arm_macos
201+
endfunction
202+
195203
function! s:Command() abort
196204
if !has('nvim-0.5') && v:version < 802
197205
return [v:null, 'Vim version too old']
@@ -214,8 +222,12 @@ function! s:Command() abort
214222
return [v:null, 'Node exited with status ' . status]
215223
endif
216224
let major = +matchstr(get(out, 0, ''), '^v\zs\d\+\ze\.')
217-
if major < 12
218-
return [v:null, 'Node v12+ required but found ' . get(out, 0, 'nothing')]
225+
if !get(g:, 'copilot_ignore_node_version')
226+
if major < 16 && s:IsArmMacOS()
227+
return [v:null, 'Node v16+ required on Apple Silicon but found ' . get(out, 0, 'nothing')]
228+
elseif major < 12
229+
return [v:null, 'Node v12+ required but found ' . get(out, 0, 'nothing')]
230+
endif
219231
endif
220232
let agent = s:root . '/copilot/dist/agent.js'
221233
if !filereadable(agent)

autoload/copilot/doc.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function! copilot#doc#Get() abort
4949
\ 'indentSize': shiftwidth(),
5050
\ }
5151
let line = getline('.')
52-
let col_byte = col('.') - (mode() !~# '^[iR]' || empty(line))
52+
let col_byte = col('.') - (mode() =~# '^[iR]' || empty(line))
5353
let col_utf16 = copilot#doc#UTF16Width(strpart(line, 0, col_byte))
5454
let doc.position = {'line': line('.') - 1, 'character': col_utf16}
5555
let lines = getline(1, '$')

copilot/dist/agent.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/copilot.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ COMMANDS *:Copilot*
2323
*:Copilot_setup*
2424
:Copilot setup Authenticate and enable GitHub Copilot.
2525

26+
*:Copilot_signout*
27+
:Copilot signout Sign out of GitHub Copilot.
28+
2629
*:Copilot_status*
2730
:Copilot status Check if GitHub Copilot is operational for the current
2831
buffer and report on any issues.

0 commit comments

Comments
 (0)