@@ -78,16 +78,23 @@ function! s:RejectRequest(request, error) abort
7878 endif
7979endfunction
8080
81+ function ! s: AfterInitialized (fn, ... ) dict abort
82+ call add (self .after_initialized, function (a: fn , a: 000 ))
83+ endfunction
84+
8185function ! 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
97104function ! 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]))
104107endfunction
105108
106109function ! s: RequestWait () dict abort
@@ -197,7 +200,7 @@ endfunction
197200
198201let s: valid_request_key = ' ^\%(id\|method\|params\)$'
199202function ! 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
285284endfunction
@@ -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 ])
416421endfunction
417422
418423function ! copilot#client#LspExit (id, code, signal) abort
@@ -445,17 +450,21 @@ endfunction
445450function ! 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
471480endfunction
472481
473482function ! 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)" )
475488endfunction
476489
477490function ! 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 )
482495endfunction
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-
497497let s: script_name = ' dist/language-server.js'
498498function ! s: Command () abort
499499 if ! has (' nvim-0.6' ) && v: version < 900
@@ -562,7 +562,7 @@ function! copilot#client#Settings() abort
562562 return settings
563563endfunction
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
574578endfunction
575579
576580function ! 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 )
582583endfunction
583584
584585function ! 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
0 commit comments