Skip to content

Commit 0668308

Browse files
committed
Copilot.vim 1.37.0
1 parent 88d08cf commit 0668308

File tree

6 files changed

+323
-321
lines changed

6 files changed

+323
-321
lines changed

autoload/copilot.vim

+20-24
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,8 @@ endfunction
780780

781781
function! s:commands.restart(opts) abort
782782
call s:Stop()
783-
let err = copilot#Client().StartupError()
784-
if !empty(err)
785-
return 'echoerr ' . string('Copilot: ' . err)
786-
endif
787-
echo 'Copilot: Restarting language server.'
783+
echo 'Copilot: Restarting language server'
784+
call s:Start()
788785
endfunction
789786

790787
function! s:commands.disable(opts) abort
@@ -803,6 +800,10 @@ function! s:commands.panel(opts) abort
803800
endif
804801
endfunction
805802

803+
function! s:commands.log(opts) abort
804+
return a:opts.mods . ' split +$ copilot:///log'
805+
endfunction
806+
806807
function! copilot#CommandComplete(arg, lead, pos) abort
807808
let args = matchstr(strpart(a:lead, 0, a:pos), 'C\%[opilot][! ] *\zs.*')
808809
if args !~# ' '
@@ -816,33 +817,28 @@ endfunction
816817
function! copilot#Command(line1, line2, range, bang, mods, arg) abort
817818
let cmd = matchstr(a:arg, '^\%(\\.\|\S\)\+')
818819
let arg = matchstr(a:arg, '\s\zs\S.*')
819-
if cmd ==# 'log'
820-
return a:mods . ' split +$ copilot:///log'
821-
endif
822820
if !empty(cmd) && !has_key(s:commands, tr(cmd, '-', '_'))
823821
return 'echoerr ' . string('Copilot: unknown command ' . string(cmd))
824822
endif
825823
try
826-
let err = copilot#Client().StartupError()
827-
if !empty(err)
828-
return 'echo ' . string('Copilot: ' . err)
829-
endif
830-
try
831-
let opts = copilot#Call('checkStatus', {'options': {'localChecksOnly': v:true}})
832-
catch
833-
call copilot#logger#Exception()
834-
let opts = {'status': 'VimException'}
835-
endtry
836824
if empty(cmd)
837-
if opts.status ==# 'VimException'
838-
return a:mods . ' split +$ copilot:///log'
839-
elseif opts.status !=# 'OK' && opts.status !=# 'MaybeOK'
840-
let cmd = 'setup'
825+
if !s:Running()
826+
let cmd = 'restart'
841827
else
842-
let cmd = 'panel'
828+
try
829+
let opts = copilot#Call('checkStatus', {'options': {'localChecksOnly': v:true}})
830+
if opts.status !=# 'OK' && opts.status !=# 'MaybeOK'
831+
let cmd = 'setup'
832+
else
833+
let cmd = 'panel'
834+
endif
835+
catch
836+
call copilot#logger#Exception()
837+
let cmd = 'log'
838+
endtry
843839
endif
844840
endif
845-
call extend(opts, {'line1': a:line1, 'line2': a:line2, 'range': a:range, 'bang': a:bang, 'mods': a:mods, 'arg': arg})
841+
let opts = {'line1': a:line1, 'line2': a:line2, 'range': a:range, 'bang': a:bang, 'mods': a:mods, 'arg': arg}
846842
let retval = s:commands[tr(cmd, '-', '_')](opts)
847843
if type(retval) == v:t_string
848844
return retval

autoload/copilot/client.vim

+46-43
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,23 @@ function! s:RejectRequest(request, error) abort
7878
endif
7979
endfunction
8080

81+
function! s:AfterInitialized(fn, ...) dict abort
82+
call add(self.after_initialized, function(a:fn, a:000))
83+
endfunction
84+
8185
function! s:Send(instance, request) abort
86+
if !has_key(a:instance, 'job')
87+
return v:false
88+
endif
8289
try
8390
call ch_sendexpr(a:instance.job, a:request)
8491
return v:true
8592
catch /^Vim\%((\a\+)\)\=:E906:/
8693
let a:instance.kill = v:true
87-
let job = a:instance.job
88-
call copilot#logger#Warn('Terminating process after failed write')
94+
let job = remove(a:instance, 'job')
8995
call job_stop(job)
9096
call timer_start(2000, { _ -> job_stop(job, 'kill') })
97+
call copilot#logger#Warn('Terminating process after failed write')
9198
return v:false
9299
catch /^Vim\%((\a\+)\)\=:E631:/
93100
return v:false
@@ -96,11 +103,7 @@ endfunction
96103

97104
function! s:VimNotify(method, params) dict abort
98105
let request = {'method': a:method, 'params': a:params}
99-
if has_key(self, 'initialization_pending')
100-
call add(self.initialization_pending, request)
101-
else
102-
return s:Send(self, request)
103-
endif
106+
call self.AfterInitialized(function('s:Send', [self, request]))
104107
endfunction
105108

106109
function! s:RequestWait() dict abort
@@ -197,7 +200,7 @@ endfunction
197200

198201
let s:valid_request_key = '^\%(id\|method\|params\)$'
199202
function! s:SendRequest(instance, request, ...) abort
200-
if !has_key(a:instance, 'job') || has_key(a:instance, 'kill')
203+
if !has_key(a:instance, 'job') || get(a:instance, 'shutdown', a:request) isnot# a:request
201204
return s:RejectRequest(a:request, s:error_connection_inactive)
202205
endif
203206
let json = filter(copy(a:request), 'v:key =~# s:valid_request_key')
@@ -275,11 +278,7 @@ function! s:VimRequest(method, params, ...) dict abort
275278
let params = deepcopy(a:params)
276279
let [_, progress] = s:PreprocessParams(self, params)
277280
let request = call('s:SetUpRequest', [self, s:id, a:method, params, progress] + a:000)
278-
if has_key(self, 'initialization_pending')
279-
call add(self.initialization_pending, request)
280-
else
281-
call copilot#util#Defer(function('s:SendRequest'), self, request)
282-
endif
281+
call self.AfterInitialized(function('s:SendRequest', [self, request]))
283282
let self.requests[s:id] = request
284283
return request
285284
endfunction
@@ -397,6 +396,12 @@ function! s:OnExit(instance, code, ...) abort
397396
for id in sort(keys(a:instance.requests), { a, b -> +a > +b })
398397
call s:RejectRequest(remove(a:instance.requests, id), s:error_exit)
399398
endfor
399+
if has_key(a:instance, 'after_initialized')
400+
let a:instance.AfterInitialized = function('copilot#util#Defer')
401+
for Fn in remove(a:instance, 'after_initialized')
402+
call copilot#util#Defer(Fn)
403+
endfor
404+
endif
400405
call copilot#util#Defer({ -> get(s:instances, a:instance.id) is# a:instance ? remove(s:instances, a:instance.id) : {} })
401406
if a:code == 0
402407
call copilot#logger#Info(message)
@@ -412,7 +417,7 @@ function! copilot#client#LspInit(id, initialize_result) abort
412417
if !has_key(s:instances, a:id)
413418
return
414419
endif
415-
call s:AfterInitialize(a:initialize_result, s:instances[a:id])
420+
call s:PostInit(a:initialize_result, s:instances[a:id])
416421
endfunction
417422

418423
function! copilot#client#LspExit(id, code, signal) abort
@@ -445,17 +450,21 @@ endfunction
445450
function! s:NvimRequest(method, params, ...) dict abort
446451
let params = deepcopy(a:params)
447452
let [bufnr, progress] = s:PreprocessParams(self, params)
448-
if !has_key(self, 'client_id') || has_key(self, 'kill')
449-
let id = v:null
450-
else
451-
let id = eval("v:lua.require'_copilot'.lsp_request(self.id, a:method, params, bufnr)")
453+
let request = call('s:SetUpRequest', [self, v:null, a:method, params, progress] + a:000)
454+
call self.AfterInitialized(function('s:NvimDoRequest', [self, request, bufnr]))
455+
return request
456+
endfunction
457+
458+
function! s:NvimDoRequest(client, request, bufnr) abort
459+
let request = a:request
460+
if has_key(a:client, 'client_id') && !has_key(a:client, 'kill')
461+
let request.id = eval("v:lua.require'_copilot'.lsp_request(a:client.id, a:request.method, a:request.params, a:bufnr)")
452462
endif
453-
let request = call('s:SetUpRequest', [self, id, a:method, params, progress] + a:000)
454-
if id isnot# v:null
455-
let self.requests[id] = request
463+
if request.id isnot# v:null
464+
let a:client.requests[request.id] = request
456465
else
457-
if has_key(self, 'client_id')
458-
call copilot#client#LspExit(self.client_id, -1, -1)
466+
if has_key(a:client, 'client_id')
467+
call copilot#client#LspExit(a:client.client_id, -1, -1)
459468
endif
460469
call copilot#util#Defer(function('s:RejectRequest'), request, s:error_connection_inactive)
461470
endif
@@ -471,7 +480,11 @@ function! s:NvimClose() dict abort
471480
endfunction
472481

473482
function! s:NvimNotify(method, params) dict abort
474-
return eval("v:lua.require'_copilot'.rpc_notify(self.id, a:method, a:params)")
483+
call self.AfterInitialized(function('s:NvimDoNotify', [self.client_id, a:method, a:params]))
484+
endfunction
485+
486+
function! s:NvimDoNotify(client_id, method, params) abort
487+
return eval("v:lua.require'_copilot'.rpc_notify(a:client_id, a:method, a:params)")
475488
endfunction
476489

477490
function! copilot#client#LspHandle(id, request) abort
@@ -481,19 +494,6 @@ function! copilot#client#LspHandle(id, request) abort
481494
return s:OnMessage(s:instances[a:id], a:request)
482495
endfunction
483496

484-
function! s:GetNodeVersion(command) abort
485-
let out = []
486-
let err = []
487-
let status = copilot#job#Stream(a:command + ['--version'], function('add', [out]), function('add', [err]))
488-
let string = matchstr(join(out, ''), '^v\zs\d\+\.[^[:space:]]*')
489-
if status != 0
490-
let string = ''
491-
endif
492-
let major = str2nr(string)
493-
let minor = str2nr(matchstr(string, '\.\zs\d\+'))
494-
return {'status': status, 'string': string, 'major': major, 'minor': minor}
495-
endfunction
496-
497497
let s:script_name = 'dist/language-server.js'
498498
function! s:Command() abort
499499
if !has('nvim-0.6') && v:version < 900
@@ -562,7 +562,7 @@ function! copilot#client#Settings() abort
562562
return settings
563563
endfunction
564564

565-
function! s:AfterInitialize(result, instance) abort
565+
function! s:PostInit(result, instance) abort
566566
let a:instance.serverInfo = get(a:result, 'serverInfo', {})
567567
if !has_key(a:instance, 'node_version') && has_key(a:result.serverInfo, 'nodeVersion')
568568
let a:instance.node_version = a:result.serverInfo.nodeVersion
@@ -571,14 +571,15 @@ function! s:AfterInitialize(result, instance) abort
571571
call s:Warn(a:instance.node_version_warning)
572572
endif
573573
endif
574+
let a:instance.AfterInitialized = function('copilot#util#Defer')
575+
for Fn in remove(a:instance, 'after_initialized')
576+
call copilot#util#Defer(Fn)
577+
endfor
574578
endfunction
575579

576580
function! s:InitializeResult(result, instance) abort
577-
call s:AfterInitialize(a:result, a:instance)
578581
call s:Send(a:instance, {'method': 'initialized', 'params': {}})
579-
for request in remove(a:instance, 'initialization_pending')
580-
call copilot#util#Defer(function('s:SendRequest'), a:instance, request)
581-
endfor
582+
call s:PostInit(a:result, a:instance)
582583
endfunction
583584

584585
function! s:InitializeError(error, instance) abort
@@ -639,7 +640,9 @@ function! copilot#client#New(...) abort
639640
let instance = {'requests': {},
640641
\ 'progress': {},
641642
\ 'workspaceFolders': {},
643+
\ 'after_initialized': [],
642644
\ 'status': {'status': 'Starting', 'message': ''},
645+
\ 'AfterInitialized': function('s:AfterInitialized'),
643646
\ 'Close': function('s:Nop'),
644647
\ 'Notify': function('s:False'),
645648
\ 'Request': function('s:VimRequest'),
@@ -715,7 +718,7 @@ function! copilot#client#New(...) abort
715718
let opts.capabilities = s:vim_capabilities
716719
let opts.processId = getpid()
717720
let request = instance.Request('initialize', opts, function('s:InitializeResult'), function('s:InitializeError'), instance)
718-
let instance.initialization_pending = []
721+
call call(remove(instance.after_initialized, 0), [])
719722
call instance.Notify('workspace/didChangeConfiguration', {'settings': settings})
720723
endif
721724
let s:instances[instance.id] = instance

autoload/copilot/logger.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let s:logs = []
1111
function! copilot#logger#BufReadCmd() abort
1212
try
1313
setlocal modifiable noreadonly
14-
call deletebufline('', 1, '$')
14+
silent call deletebufline('', 1, '$')
1515
if !empty(s:logs)
1616
call setline(1, s:logs)
1717
endif

autoload/copilot/version.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
function! copilot#version#String() abort
2-
return '1.36.0'
2+
return '1.37.0'
33
endfunction

dist/language-server.js

+252-249
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/language-server.js.map

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)