@@ -78,16 +78,23 @@ function! s:RejectRequest(request, error) abort
78
78
endif
79
79
endfunction
80
80
81
+ function ! s: AfterInitialized (fn, ... ) dict abort
82
+ call add (self .after_initialized, function (a: fn , a: 000 ))
83
+ endfunction
84
+
81
85
function ! s: Send (instance, request) abort
86
+ if ! has_key (a: instance , ' job' )
87
+ return v: false
88
+ endif
82
89
try
83
90
call ch_sendexpr (a: instance .job, a: request )
84
91
return v: true
85
92
catch /^Vim\%((\a\+)\)\=:E906:/
86
93
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' )
89
95
call job_stop (job)
90
96
call timer_start (2000 , { _ - > job_stop (job, ' kill' ) })
97
+ call copilot#logger#Warn (' Terminating process after failed write' )
91
98
return v: false
92
99
catch /^Vim\%((\a\+)\)\=:E631:/
93
100
return v: false
@@ -96,11 +103,7 @@ endfunction
96
103
97
104
function ! s: VimNotify (method, params) dict abort
98
105
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]))
104
107
endfunction
105
108
106
109
function ! s: RequestWait () dict abort
@@ -197,7 +200,7 @@ endfunction
197
200
198
201
let s: valid_request_key = ' ^\%(id\|method\|params\)$'
199
202
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
201
204
return s: RejectRequest (a: request , s: error_connection_inactive )
202
205
endif
203
206
let json = filter (copy (a: request ), ' v:key =~# s:valid_request_key' )
@@ -275,11 +278,7 @@ function! s:VimRequest(method, params, ...) dict abort
275
278
let params = deepcopy (a: params )
276
279
let [_, progress] = s: PreprocessParams (self , params)
277
280
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]))
283
282
let self .requests[s: id ] = request
284
283
return request
285
284
endfunction
@@ -397,6 +396,12 @@ function! s:OnExit(instance, code, ...) abort
397
396
for id in sort (keys (a: instance .requests), { a , b - > + a > + b })
398
397
call s: RejectRequest (remove (a: instance .requests, id), s: error_exit )
399
398
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
400
405
call copilot#util#Defer ({ - > get (s: instances , a: instance .id) is # a: instance ? remove (s: instances , a: instance .id) : {} })
401
406
if a: code == 0
402
407
call copilot#logger#Info (message)
@@ -412,7 +417,7 @@ function! copilot#client#LspInit(id, initialize_result) abort
412
417
if ! has_key (s: instances , a: id )
413
418
return
414
419
endif
415
- call s: AfterInitialize (a: initialize_result , s: instances [a: id ])
420
+ call s: PostInit (a: initialize_result , s: instances [a: id ])
416
421
endfunction
417
422
418
423
function ! copilot#client#LspExit (id, code, signal) abort
@@ -445,17 +450,21 @@ endfunction
445
450
function ! s: NvimRequest (method, params, ... ) dict abort
446
451
let params = deepcopy (a: params )
447
452
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)" )
452
462
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
456
465
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 )
459
468
endif
460
469
call copilot#util#Defer (function (' s:RejectRequest' ), request, s: error_connection_inactive )
461
470
endif
@@ -471,7 +480,11 @@ function! s:NvimClose() dict abort
471
480
endfunction
472
481
473
482
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)" )
475
488
endfunction
476
489
477
490
function ! copilot#client#LspHandle (id, request) abort
@@ -481,19 +494,6 @@ function! copilot#client#LspHandle(id, request) abort
481
494
return s: OnMessage (s: instances [a: id ], a: request )
482
495
endfunction
483
496
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
-
497
497
let s: script_name = ' dist/language-server.js'
498
498
function ! s: Command () abort
499
499
if ! has (' nvim-0.6' ) && v: version < 900
@@ -562,7 +562,7 @@ function! copilot#client#Settings() abort
562
562
return settings
563
563
endfunction
564
564
565
- function ! s: AfterInitialize (result, instance) abort
565
+ function ! s: PostInit (result, instance) abort
566
566
let a: instance .serverInfo = get (a: result , ' serverInfo' , {})
567
567
if ! has_key (a: instance , ' node_version' ) && has_key (a: result .serverInfo, ' nodeVersion' )
568
568
let a: instance .node_version = a: result .serverInfo.nodeVersion
@@ -571,14 +571,15 @@ function! s:AfterInitialize(result, instance) abort
571
571
call s: Warn (a: instance .node_version_warning)
572
572
endif
573
573
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
574
578
endfunction
575
579
576
580
function ! s: InitializeResult (result, instance) abort
577
- call s: AfterInitialize (a: result , a: instance )
578
581
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 )
582
583
endfunction
583
584
584
585
function ! s: InitializeError (error , instance) abort
@@ -639,7 +640,9 @@ function! copilot#client#New(...) abort
639
640
let instance = {' requests' : {},
640
641
\ ' progress' : {},
641
642
\ ' workspaceFolders' : {},
643
+ \ ' after_initialized' : [],
642
644
\ ' status' : {' status' : ' Starting' , ' message' : ' ' },
645
+ \ ' AfterInitialized' : function (' s:AfterInitialized' ),
643
646
\ ' Close' : function (' s:Nop' ),
644
647
\ ' Notify' : function (' s:False' ),
645
648
\ ' Request' : function (' s:VimRequest' ),
@@ -715,7 +718,7 @@ function! copilot#client#New(...) abort
715
718
let opts.capabilities = s: vim_capabilities
716
719
let opts.processId = getpid ()
717
720
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 ), [])
719
722
call instance.Notify (' workspace/didChangeConfiguration' , {' settings' : settings})
720
723
endif
721
724
let s: instances [instance.id] = instance
0 commit comments