@@ -145,16 +145,6 @@ function! s:BufferText(bufnr) abort
145
145
return join (getbufline (a: bufnr ,1 ,' $' )," \n " ) ." \n "
146
146
endfunction
147
147
148
- function ! s: LogMessage (params)abort
149
- call copilot#logger#Raw (get (a: params ,' type' ,6 ),get (a: params ,' message' ,' ' ))
150
- endfunction
151
-
152
- function ! s: ShowMessageRequest (params)abort
153
- let choice= inputlist ([a: params .message ." \n\n Request Actions:" ]+
154
- \ map (copy (get (a: params ,' actions' , [])), {i ,v - > (i + 1 ) .' .' .v .title }))
155
- return choice > 0 ?get (a: params .actions, choice- 1 ,v: null ) :v: null
156
- endfunction
157
-
158
148
function ! s: SendRequest (agent, request)abort
159
149
if empty (s: Send (a: agent ,a: request ))&& has_key (a: agent .requests,a: request .id)
160
150
call s: RejectRequest (remove (a: agent .requests,a: request .id), {' code' :257 ,' message' :' Write failed' })
@@ -198,7 +188,11 @@ function! s:AgentRequest(method, params, ...) dict abort
198
188
endif
199
189
let doc.version = doc_version
200
190
endfor
201
- call timer_start (0 , { _- >s: SendRequest (self , request) })
191
+ if has_key (self ,' initialization_pending' )
192
+ call add (self .initialization_pending, request)
193
+ else
194
+ call timer_start (0 , { _- >s: SendRequest (self , request) })
195
+ endif
202
196
return call (' s:SetUpRequest' , [self ,s: id ,a: method ,a: params ]+ a: 000 )
203
197
endfunction
204
198
@@ -255,8 +249,10 @@ function! s:OnMessage(agent, body, ...) abort
255
249
let params= get (request,' params' ,v: null )
256
250
if has_key (a: agent .methods, request.method)
257
251
return s: DispatchMessage (a: agent , request.method,a: agent .methods[request.method], id, params)
258
- elseif ! empty (id)
252
+ elseif id isnot # v: null
259
253
call s: Send (a: agent , {" id" : id," error" : {" code" :-32700 ," message" :" Method not found:" . request.method}})
254
+ elseif request.method!~# ' ^\$/'
255
+ call copilot#logger#Warn (' Unexpected notification' . request.method .' called with' .json_encode (params))
260
256
endif
261
257
endfunction
262
258
@@ -289,7 +285,7 @@ function! s:OnResponse(agent, response, ...) abort
289
285
endfunction
290
286
291
287
function ! s: OnErr (agent,line ,... )abort
292
- if ! has_key (a: agent ,' capabilities ' )
288
+ if ! has_key (a: agent ,' serverInfo ' )
293
289
call copilot#logger#Bare (' <-!' .a: line )
294
290
endif
295
291
endfunction
@@ -467,12 +463,22 @@ function! copilot#agent#Settings() abort
467
463
endfunction
468
464
469
465
function ! s: InitializeResult (result, agent)abort
470
- let a: agent .capabilities= get (a: result ,' capabilities' , {})
466
+ let a: agent .serverInfo= get (a: result ,' serverInfo' , {})
467
+ if ! has_key (a: agent ,' node_version' )&& has_key (a: result .serverInfo,' nodeVersion' )
468
+ let a: agent .node_version= a: result .serverInfo.nodeVersion
469
+ endif
471
470
let info= {
472
471
\ ' editorInfo' :copilot#agent#EditorInfo (),
473
472
\ ' editorPluginInfo' :copilot#agent#EditorPluginInfo (),
474
473
\ ' editorConfiguration' :extend (copilot#agent#Settings (),a: agent .editorConfiguration)}
474
+ let pending= get (a: agent ,' initialization_pending' , [])
475
+ if has_key (a: agent ,' initialization_pending' )
476
+ call remove (a: agent ,' initialization_pending' )
477
+ endif
475
478
call a: agent .Request (' setEditorInfo' , info)
479
+ for requestin pending
480
+ call timer_start (0 , { _- >s: SendRequest (a: agent , request) })
481
+ endfor
476
482
endfunction
477
483
478
484
function ! s: InitializeError (error , agent)abort
@@ -485,16 +491,26 @@ function! s:InitializeError(error, agent) abort
485
491
endfunction
486
492
487
493
function ! s: AgentStartupError ()dict abort
488
- while (has_key (self ,' job' )|| has_key (self ,' client_id' ))&& ! has_key (self ,' startup_error' )&& ! has_key (self ,' capabilities ' )
494
+ while (has_key (self ,' job' )|| has_key (self ,' client_id' ))&& ! has_key (self ,' startup_error' )&& ! has_key (self ,' serverInfo ' )
489
495
sleep 10 m
490
496
endwhile
491
- if has_key (self ,' capabilities ' )
497
+ if has_key (self ,' serverInfo ' )
492
498
return ' '
493
499
else
494
500
return get (self ,' startup_error' ,' Something unexpected went wrong spawning the agent' )
495
501
endif
496
502
endfunction
497
503
504
+ let s: vim_handlers= {
505
+ \ ' window/showMessageRequest' :function (' copilot#handlers#window_showMessageRequest' ),
506
+ \ ' window/showDocument' :function (' copilot#handlers#window_showDocument' ),
507
+ \ }
508
+
509
+ let s: vim_capabilities= {
510
+ \ ' workspace' : {' workspaceFolders' :v: true },
511
+ \ ' window' : {' showDocument' : {' support' :v: true }},
512
+ \ }
513
+
498
514
function ! copilot#agent#New (... )abort
499
515
let opts= a: 0 ?a: 1 : {}
500
516
let instance= {' requests' : {},
@@ -507,7 +523,7 @@ function! copilot#agent#New(...) abort
507
523
\ ' StartupError' :function (' s:AgentStartupError' ),
508
524
\ }
509
525
let instance.methods= extend ({
510
- \ ' window/logMessage' :function (' s:LogMessage ' ),
526
+ \ ' window/logMessage' :function (' copilot#handlers#window_logMessage ' ),
511
527
\ },get (opts,' methods' , {}))
512
528
let [command , node_version, command_error]= s: Command ()
513
529
if len (command_error)
@@ -522,23 +538,18 @@ function! copilot#agent#New(...) abort
522
538
if ! empty (node_version)
523
539
let instance.node_version= node_version
524
540
endif
525
- let initializationOptions= {' copilotCapabilities' : {}}
526
- for namein keys (instance.methods)
527
- if name= ~#' ^copilot/'
528
- let initializationOptions.copilotCapabilities[matchstr (name,' /\zs.*' )]= v: true
529
- endif
530
- endfor
541
+ let opts= {' initializationOptions' : {}}
531
542
if has (' nvim' )
532
543
call extend (instance, {
533
544
\ ' Close' :function (' s:LspClose' ),
534
545
\ ' Notify' :function (' s:LspNotify' ),
535
546
\ ' Request' :function (' s:LspRequest' )})
536
- let instance.client_id= eval (" v:lua.require'_copilot'.lsp_start_client(command, keys(instance.methods),initializationOptions )" )
547
+ let instance.client_id= eval (" v:lua.require'_copilot'.lsp_start_client(command, keys(instance.methods),opts )" )
537
548
let instance.id= instance.client_id
538
549
else
539
550
let state = {' headers' : {},' mode' :' headers' ,' buffer' :' ' }
540
551
let instance.open_buffers= {}
541
- let instance.methods= extend ({ ' window/showMessageRequest ' : function ( ' s:ShowMessageRequest ' )} , instance.methods)
552
+ let instance.methods= extend (s: vim_handlers , instance.methods)
542
553
let instance.job= job_start (command , {
543
554
\ ' cwd' :copilot#job#Cwd (),
544
555
\ ' in_mode' :' lsp' ,
@@ -548,12 +559,10 @@ function! copilot#agent#New(...) abort
548
559
\ ' exit_cb' : {j ,d - >timer_start (0 ,function (' s:OnExit' , [instance,d ])) },
549
560
\ })
550
561
let instance.id= job_info (instance.job).process
551
- let opts= {
552
- \ ' capabilities' : {' workspace' : {' workspaceFolders' :v: true }},
553
- \ ' initializationOptions' : initializationOptions,
554
- \ ' processId' :getpid (),
555
- \ }
562
+ let opts.capabilities= s: vim_capabilities
563
+ let opts.processId= getpid ()
556
564
let request= instance.Request (' initialize' , opts,function (' s:InitializeResult' ),function (' s:InitializeError' ), instance)
565
+ let instance.initialization_pending= []
557
566
endif
558
567
let s: instances [instance.id]= instance
559
568
return instance