Skip to content

Commit 05e1298

Browse files
committed
Copilot.vim 1.2.0
1 parent 47eb231 commit 05e1298

File tree

9 files changed

+326
-208
lines changed

9 files changed

+326
-208
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,3 @@ Service](https://docs.github.com/en/github/site-policy/github-terms-of-service#j
4242

4343
Suggestions are displayed inline and can be accepted by pressing the tab key.
4444
See `:help copilot` for more information.
45-
46-
## Limitations
47-
48-
Copilot.vim does not yet support opening the GitHub Copilot panel on Ctrl+Enter.

autoload/copilot.vim

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function! s:JsonBody(response) abort
3333
endfunction
3434

3535
function! copilot#HttpRequest(url, options, ...) abort
36-
return call('copilot#agent#Call', ['httpRequest', extend({'url': a:url, 'timeout': 30000}, a:options)] + a:000)
36+
return call('copilot#Call', ['httpRequest', extend({'url': a:url, 'timeout': 30000}, a:options)] + a:000)
3737
endfunction
3838

3939
unlet! s:github
@@ -93,7 +93,44 @@ if !exists('s:github') && $CODESPACES ==# 'true' && len($GITHUB_TOKEN)
9393
endif
9494

9595
function! copilot#Init(...) abort
96-
call timer_start(0, { _ -> copilot#agent#Start() })
96+
call timer_start(0, { _ -> s:Start() })
97+
endfunction
98+
99+
function! s:Start() abort
100+
if exists('s:agent.job')
101+
return
102+
endif
103+
let s:agent = copilot#agent#New({'notifications': {
104+
\ 'PanelSolution': function('copilot#panel#Solution'),
105+
\ 'PanelSolutionsDone': function('copilot#panel#SolutionsDone'),
106+
\ }})
107+
endfunction
108+
109+
function! s:Stop() abort
110+
if exists('s:agent')
111+
let agent = remove(s:, 'agent')
112+
call agent.Close()
113+
endif
114+
endfunction
115+
116+
function! copilot#Agent() abort
117+
call s:Start()
118+
return s:agent
119+
endfunction
120+
121+
function! copilot#Request(method, params, ...) abort
122+
let agent = copilot#Agent()
123+
return call(agent.Request, [a:method, a:params] + a:000)
124+
endfunction
125+
126+
function! copilot#Call(method, params, ...) abort
127+
let agent = copilot#Agent()
128+
return call(agent.Call, [a:method, a:params] + a:000)
129+
endfunction
130+
131+
function! copilot#Notify(method, params, ...) abort
132+
let agent = copilot#Agent()
133+
return call(agent.Notify, [a:method, a:params] + a:000)
97134
endfunction
98135

99136
let s:terms_version = '2021-10-14'
@@ -191,15 +228,7 @@ function! copilot#Enabled() abort
191228
return get(g:, 'copilot_enabled', 1)
192229
\ && s:TermsAccepted(0)
193230
\ && empty(s:BufferDisabled())
194-
\ && empty(copilot#agent#StartupError())
195-
endfunction
196-
197-
function! copilot#Request(method, params, ...) abort
198-
return call('copilot#agent#Request', [a:method, a:params] + a:000)
199-
endfunction
200-
201-
function! copilot#Call(method, params, ...) abort
202-
return call('copilot#agent#Call', [a:method, a:params] + a:000)
231+
\ && empty(copilot#Agent().StartupError())
203232
endfunction
204233

205234
function! copilot#Complete(...) abort
@@ -209,10 +238,10 @@ function! copilot#Complete(...) abort
209238
if exists('g:_copilot_timer')
210239
call timer_stop(remove(g:, '_copilot_timer'))
211240
endif
212-
let doc = copilot#doc#Get()
213-
if !exists('b:_copilot.doc') || b:_copilot.doc !=# doc
214-
let b:_copilot = {'doc': doc, 'first':
215-
\ copilot#agent#Request('getCompletions', {'doc': doc, 'options': {}})}
241+
let params = copilot#doc#Params()
242+
if !exists('b:_copilot.params') || b:_copilot.params !=# params
243+
let b:_copilot = {'params': params, 'first':
244+
\ copilot#Request('getCompletions', params)}
216245
let g:_copilot_last = b:_copilot
217246
endif
218247
let completion = b:_copilot.first
@@ -295,8 +324,8 @@ function! s:GetSuggestionsCycling(callback) abort
295324
call a:callback(b:_copilot)
296325
elseif exists('b:_copilot.suggestions')
297326
let b:_copilot.cycling_callbacks = [a:callback]
298-
let b:_copilot.cycling = copilot#agent#Request('getCompletionsCycling',
299-
\ {'doc': b:_copilot.first.params.doc, 'options': {}},
327+
let b:_copilot.cycling = copilot#Request('getCompletionsCycling',
328+
\ params,
300329
\ function('s:GetSuggestionsCyclingCallback', [b:_copilot]),
301330
\ function('s:GetSuggestionsCyclingCallback', [b:_copilot]),
302331
\ )
@@ -456,7 +485,7 @@ endfunction
456485

457486
function! copilot#OnInsertEnter() abort
458487
let s:is_mapped = copilot#IsMapped()
459-
let s:dest = bufnr('copilot://')
488+
let s:dest = bufnr('^copilot://$')
460489
if s:dest < 0 && !s:has_ghost_text
461490
let s:dest = 0
462491
endif
@@ -577,7 +606,7 @@ endfunction
577606
let s:commands = {}
578607

579608
function s:NetworkStatusMessage() abort
580-
let err = copilot#agent#StartupError()
609+
let err = copilot#Agent().StartupError()
581610
if !empty(err)
582611
return err
583612
endif
@@ -619,7 +648,13 @@ function! s:EnabledStatusMessage() abort
619648
endif
620649
endfunction
621650

622-
function! s:commands.status(opts) abort
651+
function! s:VerifySetup() abort
652+
let error = copilot#Agent().StartupError()
653+
if !empty(error)
654+
echo 'Copilot: ' . error
655+
return
656+
endif
657+
623658
if empty(s:OAuthToken())
624659
echo 'Copilot: Not authenticated. Invoke :Copilot setup'
625660
return
@@ -629,6 +664,13 @@ function! s:commands.status(opts) abort
629664
echo 'Copilot: Telemetry terms not accepted. Invoke :Copilot setup'
630665
return
631666
endif
667+
return 1
668+
endfunction
669+
670+
function! s:commands.status(opts) abort
671+
if !s:VerifySetup()
672+
return
673+
endif
632674

633675
let status = s:EnabledStatusMessage()
634676
if !empty(status)
@@ -681,7 +723,9 @@ function! s:commands.setup(opts) abort
681723
let mouse = &mouse
682724
set mouse=
683725
endif
684-
if len(browser)
726+
if get(a:opts, 'bang')
727+
echo "In your browser, visit " . data.verification_uri
728+
elseif len(browser)
685729
echo "Press ENTER to open GitHub in your browser"
686730
let c = getchar()
687731
while c isnot# 13 && c isnot# 10 && c isnot# 0
@@ -776,8 +820,8 @@ function! s:commands.log(opts) abort
776820
endfunction
777821

778822
function! s:commands.restart(opts) abort
779-
call copilot#agent#Close()
780-
let err = copilot#agent#StartupError()
823+
call s:Stop()
824+
let err = copilot#Agent().StartupError()
781825
if !empty(err)
782826
return 'echoerr ' . string('Copilot: ' . err)
783827
endif
@@ -792,6 +836,12 @@ function! s:commands.enable(opts) abort
792836
let g:copilot_enabled = 1
793837
endfunction
794838

839+
function! s:commands.panel(opts) abort
840+
if s:VerifySetup()
841+
return copilot#panel#Open(a:opts)
842+
endif
843+
endfunction
844+
795845
function! s:commands.split(opts) abort
796846
let mods = a:opts.mods
797847
if mods !~# '\<\%(aboveleft\|belowright\|leftabove\|rightbelow\|topleft\|botright\|tab\)\>'
@@ -826,7 +876,7 @@ function! copilot#Command(line1, line2, range, bang, mods, arg) abort
826876
if empty(s:OAuthToken()) || !s:TermsAccepted(1)
827877
let cmd = 'setup'
828878
else
829-
let cmd = 'status'
879+
let cmd = 'panel'
830880
endif
831881
elseif cmd ==# 'auth'
832882
let cmd = 'setup'

0 commit comments

Comments
 (0)