@@ -5,7 +5,7 @@ let g:autoloaded_copilot_agent = 1
55
66scriptencoding utf- 8
77
8- let s: plugin_version = ' 1.3.5 '
8+ let s: plugin_version = ' 1.4.0 '
99
1010let s: error_exit = -1
1111
@@ -15,6 +15,11 @@ if !exists('s:instances')
1515 let s: instances = {}
1616endif
1717
18+ " allow sourcing this file to reload the Lua file too
19+ if has (' nvim' )
20+ lua package.loaded._copilot = nil
21+ endif
22+
1823let s: jobstop = function (exists (' *jobstop' ) ? ' jobstop' : ' job_stop' )
1924function ! s: Kill (agent, ... ) abort
2025 if has_key (a: agent , ' job' )
@@ -49,7 +54,8 @@ function! s:Send(agent, request) abort
4954endfunction
5055
5156function ! s: AgentNotify (method, params) dict abort
52- return s: Send (self , {' method' : a: method , ' params' : a: params })
57+ call s: Send (self , {' method' : a: method , ' params' : a: params })
58+ return v: true
5359endfunction
5460
5561function ! s: RequestWait () dict abort
@@ -77,20 +83,21 @@ endfunction
7783if ! exists (' s:id' )
7884 let s: id = 0
7985endif
80- function ! s: AgentRequest (method, params, ... ) dict abort
81- let s: id += 1
82- let request = {' method' : a: method , ' params' : a: params , ' id' : s: id }
83- call s: Send (self , request)
84- call extend (request, {
85- \ ' agent_id' : self .id,
86+
87+ function s: SetUpRequest (agent, id, method, params, ... ) abort
88+ let request = {
89+ \ ' agent_id' : a: agent .id,
90+ \ ' id' : a: id ,
91+ \ ' method' : a: method ,
92+ \ ' params' : a: params ,
8693 \ ' Agent' : function (' s:RequestAgent' ),
8794 \ ' Wait' : function (' s:RequestWait' ),
8895 \ ' Await' : function (' s:RequestAwait' ),
8996 \ ' Cancel' : function (' s:RequestCancel' ),
9097 \ ' resolve' : [],
9198 \ ' reject' : [],
92- \ ' status' : ' running' })
93- let self .requests[s : id ] = request
99+ \ ' status' : ' running' }
100+ let a: agent .requests[a : id ] = request
94101 let args = a: 000 [2 :-1 ]
95102 if len (args )
96103 if ! empty (a: 1 )
@@ -110,6 +117,13 @@ function! s:AgentRequest(method, params, ...) dict abort
110117 return request
111118endfunction
112119
120+ function ! s: AgentRequest (method, params, ... ) dict abort
121+ let s: id += 1
122+ let request = {' method' : a: method , ' params' : a: params , ' id' : s: id }
123+ call s: Send (self , request)
124+ return call (' s:SetUpRequest' , [self , s: id , a: method , a: params ] + a: 000 )
125+ endfunction
126+
113127function ! s: AgentCall (method, params, ... ) dict abort
114128 let request = call (self .Request, [a: method , a: params ] + a: 000 )
115129 if a: 0
@@ -160,7 +174,11 @@ function! s:OnMessage(agent, body, ...) abort
160174 if type (response) != v: t_dict
161175 return
162176 endif
177+ return s: OnResponse (a: agent , response)
178+ endfunction
163179
180+ function ! s: OnResponse (agent, response, ... ) abort
181+ let response = a: response
164182 let id = get (response, ' id' , v: null )
165183 if has_key (response, ' method' )
166184 let params = get (response, ' params' , v: null )
@@ -241,7 +259,7 @@ endfunction
241259
242260function ! s: OnExit (agent, code) abort
243261 let a: agent .exit_status = a: code
244- call remove ( a: agent, ' job' )
262+ unlet ! a: agent. job
245263 for id in sort (keys (a: agent .requests), { a , b - > + a > + b })
246264 let request = remove (a: agent .requests, id)
247265 if request.status == # ' canceled'
@@ -261,6 +279,53 @@ function! s:OnExit(agent, code) abort
261279 call copilot#logger#Info (' agent exited with status ' . a: code )
262280endfunction
263281
282+ function ! copilot#agent#LspInit (agent_id, initialize_result) abort
283+ if ! has_key (s: instances , a: agent_id )
284+ return
285+ endif
286+ let instance = s: instances [a: agent_id ]
287+ call s: GetCapabilitiesResult (a: initialize_result , instance)
288+ endfunction
289+
290+ function ! copilot#agent#LspExit (agent_id, code, signal) abort
291+ if ! has_key (s: instances , a: agent_id )
292+ return
293+ endif
294+ let instance = remove (s: instances , a: agent_id )
295+ unlet ! instance.client_id
296+ call s: OnExit (instance, a: code )
297+ endfunction
298+
299+ function ! copilot#agent#LspResponse (agent_id, opts, ... ) abort
300+ if ! has_key (s: instances , a: agent_id )
301+ return
302+ endif
303+ call s: OnResponse (s: instances [a: agent_id ], a: opts )
304+ endfunction
305+
306+ function ! s: LspRequest (method, params, ... ) dict abort
307+ let id = v: lua .require' _copilot' .lsp_request (self .id, a: method , a: params )
308+ return call (' s:SetUpRequest' , [self , id, a: method , a: params ] + a: 000 )
309+ endfunction
310+
311+ function ! s: LspClose () dict abort
312+ if ! has_key (self , ' client_id' )
313+ return
314+ endif
315+ return luaeval (' vim.lsp.get_client_by_id(_A).stop()' , self .client_id)
316+ endfunction
317+
318+ function ! s: LspNotify (method, params) dict abort
319+ return v: lua .require' _copilot' .rpc_notify (self .id, a: method , a: params )
320+ endfunction
321+
322+ function copilot#agent#LspHandle (agent_id, response) abort
323+ if ! has_key (s: instances , a: agent_id )
324+ return
325+ endif
326+ call s: OnResponse (s: instances [a: agent_id ], a: response )
327+ endfunction
328+
264329unlet ! s: is_arm_macos
265330function ! s: IsArmMacOS () abort
266331 if exists (' s:is_arm_macos' )
@@ -351,7 +416,7 @@ function! s:AgentStartupError() dict abort
351416 while has_key (self , ' job' ) && ! has_key (self , ' startup_error' ) && ! has_key (self , ' capabilities' )
352417 sleep 10 m
353418 endwhile
354- if has_key (self , ' capabilities' )
419+ if has_key (self , ' capabilities' ) || has_key ( self , ' client_id ' )
355420 return ' '
356421 else
357422 return get (self , ' startup_error' , ' Something unexpected went wrong spawning the agent' )
@@ -376,13 +441,22 @@ function! copilot#agent#New(...) abort
376441 let instance.startup_error = command_error
377442 return instance
378443 endif
379- let state = {' headers' : {}, ' mode' : ' headers' , ' buffer' : ' ' }
380- let instance.job = copilot#job#Stream (command ,
381- \ function (' s:OnOut' , [instance, state ]),
382- \ function (' s:OnErr' , [instance]),
383- \ function (' s:OnExit' , [instance]))
384- let instance.id = exists (' *jobpid' ) ? jobpid (instance.job) : job_info (instance.job).process
385- let request = instance.Request (' initialize' , {' capabilities' : {}}, function (' s:GetCapabilitiesResult' ), function (' s:GetCapabilitiesError' ), instance)
444+ if has (' nvim' )
445+ call extend (instance, {
446+ \ ' Close' : function (' s:LspClose' ),
447+ \ ' Notify' : function (' s:LspNotify' ),
448+ \ ' Request' : function (' s:LspRequest' )})
449+ let instance.client_id = v: lua .require' _copilot' .lsp_start_client (command , keys (instance.notifications) + keys (instance.methods) + [' LogMessage' ])
450+ let instance.id = instance.client_id
451+ else
452+ let state = {' headers' : {}, ' mode' : ' headers' , ' buffer' : ' ' }
453+ let instance.job = copilot#job#Stream (command ,
454+ \ function (' s:OnOut' , [instance, state ]),
455+ \ function (' s:OnErr' , [instance]),
456+ \ function (' s:OnExit' , [instance]))
457+ let instance.id = exists (' *jobpid' ) ? jobpid (instance.job) : job_info (instance.job).process
458+ let request = instance.Request (' initialize' , {' capabilities' : {' workspace' : {' workspaceFolders' : v: true }}}, function (' s:GetCapabilitiesResult' ), function (' s:GetCapabilitiesError' ), instance)
459+ endif
386460 let s: instances [instance.id] = instance
387461 return instance
388462endfunction
0 commit comments