@@ -145,16 +145,6 @@ function! s:BufferText(bufnr) abort
145145 return join (getbufline (a: bufnr , 1 , ' $' ), " \n " ) . " \n "
146146endfunction
147147
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-
158148function ! s: SendRequest (agent, request) abort
159149 if empty (s: Send (a: agent , a: request )) && has_key (a: agent .requests, a: request .id)
160150 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
198188 endif
199189 let doc.version = doc_version
200190 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
202196 return call (' s:SetUpRequest' , [self , s: id , a: method , a: params ] + a: 000 )
203197endfunction
204198
@@ -255,8 +249,10 @@ function! s:OnMessage(agent, body, ...) abort
255249 let params = get (request, ' params' , v: null )
256250 if has_key (a: agent .methods, request.method)
257251 return s: DispatchMessage (a: agent , request.method, a: agent .methods[request.method], id, params)
258- elseif ! empty (id)
252+ elseif id isnot # v: null
259253 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))
260256 endif
261257endfunction
262258
@@ -289,7 +285,7 @@ function! s:OnResponse(agent, response, ...) abort
289285endfunction
290286
291287function ! s: OnErr (agent, line , ... ) abort
292- if ! has_key (a: agent , ' capabilities ' )
288+ if ! has_key (a: agent , ' serverInfo ' )
293289 call copilot#logger#Bare (' <-! ' . a: line )
294290 endif
295291endfunction
@@ -467,12 +463,22 @@ function! copilot#agent#Settings() abort
467463endfunction
468464
469465function ! 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
471470 let info = {
472471 \ ' editorInfo' : copilot#agent#EditorInfo (),
473472 \ ' editorPluginInfo' : copilot#agent#EditorPluginInfo (),
474473 \ ' 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
475478 call a: agent .Request (' setEditorInfo' , info)
479+ for request in pending
480+ call timer_start (0 , { _ - > s: SendRequest (a: agent , request) })
481+ endfor
476482endfunction
477483
478484function ! s: InitializeError (error , agent) abort
@@ -485,16 +491,26 @@ function! s:InitializeError(error, agent) abort
485491endfunction
486492
487493function ! 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 ' )
489495 sleep 10 m
490496 endwhile
491- if has_key (self , ' capabilities ' )
497+ if has_key (self , ' serverInfo ' )
492498 return ' '
493499 else
494500 return get (self , ' startup_error' , ' Something unexpected went wrong spawning the agent' )
495501 endif
496502endfunction
497503
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+
498514function ! copilot#agent#New (... ) abort
499515 let opts = a: 0 ? a: 1 : {}
500516 let instance = {' requests' : {},
@@ -507,7 +523,7 @@ function! copilot#agent#New(...) abort
507523 \ ' StartupError' : function (' s:AgentStartupError' ),
508524 \ }
509525 let instance.methods = extend ({
510- \ ' window/logMessage' : function (' s:LogMessage ' ),
526+ \ ' window/logMessage' : function (' copilot#handlers#window_logMessage ' ),
511527 \ }, get (opts, ' methods' , {}))
512528 let [command , node_version, command_error] = s: Command ()
513529 if len (command_error)
@@ -522,23 +538,18 @@ function! copilot#agent#New(...) abort
522538 if ! empty (node_version)
523539 let instance.node_version = node_version
524540 endif
525- let initializationOptions = {' copilotCapabilities' : {}}
526- for name in keys (instance.methods)
527- if name = ~# ' ^copilot/'
528- let initializationOptions.copilotCapabilities[matchstr (name, ' /\zs.*' )] = v: true
529- endif
530- endfor
541+ let opts = {' initializationOptions' : {}}
531542 if has (' nvim' )
532543 call extend (instance, {
533544 \ ' Close' : function (' s:LspClose' ),
534545 \ ' Notify' : function (' s:LspNotify' ),
535546 \ ' 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 )" )
537548 let instance.id = instance.client_id
538549 else
539550 let state = {' headers' : {}, ' mode' : ' headers' , ' buffer' : ' ' }
540551 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)
542553 let instance.job = job_start (command , {
543554 \ ' cwd' : copilot#job#Cwd (),
544555 \ ' in_mode' : ' lsp' ,
@@ -548,12 +559,10 @@ function! copilot#agent#New(...) abort
548559 \ ' exit_cb' : { j , d - > timer_start (0 , function (' s:OnExit' , [instance, d ])) },
549560 \ })
550561 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 ()
556564 let request = instance.Request (' initialize' , opts, function (' s:InitializeResult' ), function (' s:InitializeError' ), instance)
565+ let instance.initialization_pending = []
557566 endif
558567 let s: instances [instance.id] = instance
559568 return instance
0 commit comments