@@ -188,14 +188,14 @@ function! s:PreprocessParams(agent, params) abort
188188 for doc in filter ([get (a: params , ' doc' , {}), get (a: params , ' textDocument' , {})], ' type(get(v:val, "uri", "")) == v:t_number' )
189189 let bufnr = doc.uri
190190 call s: RegisterWorkspaceFolderForBuffer (a: agent , bufnr )
191- let synced = a: agent .SyncTextDocument (bufnr )
191+ let synced = a: agent .Attach (bufnr )
192192 let doc.uri = synced.uri
193- let doc.version = synced. version
193+ let doc.version = get ( synced, ' version' , 0 )
194194 endfor
195195 return bufnr
196196endfunction
197197
198- function ! s: VimSyncTextDocument (bufnr ) dict abort
198+ function ! s: VimAttach (bufnr ) dict abort
199199 if ! bufloaded (a: bufnr )
200200 return {' uri' : ' ' , ' version' : 0 }
201201 endif
@@ -223,6 +223,10 @@ function! s:VimSyncTextDocument(bufnr) dict abort
223223 return doc
224224endfunction
225225
226+ function ! s: VimIsAttached (bufnr ) dict abort
227+ return bufloaded (a: bufnr ) && has_key (self .open_buffers, a: bufnr ) ? v: true : v: false
228+ endfunction
229+
226230function ! s: AgentRequest (method, params, ... ) dict abort
227231 let s: id += 1
228232 let params = deepcopy (a: params )
@@ -365,14 +369,18 @@ function! copilot#agent#LspResponse(agent_id, opts, ...) abort
365369 call s: OnResponse (s: instances [a: agent_id ], a: opts )
366370endfunction
367371
368- function ! s: NvimSyncTextDocument (bufnr ) dict abort
372+ function ! s: NvimAttach (bufnr ) dict abort
369373 if ! bufloaded (a: bufnr )
370374 return {' uri' : ' ' , ' version' : 0 }
371375 endif
372376 call luaeval (' pcall(vim.lsp.buf_attach_client, _A[1], _A[2])' , [a: bufnr , self .id])
373377 return luaeval (' {uri = vim.uri_from_bufnr(_A), version = vim.lsp.util.buf_versions[_A]}' , a: bufnr )
374378endfunction
375379
380+ function ! s: NvimIsAttached (bufnr ) dict abort
381+ return bufloaded (a: bufnr ) ? luaeval (' vim.lsp.buf_is_attached(_A[1], _A[2])' , [a: bufnr , self .id]) : v: false
382+ endfunction
383+
376384function ! s: LspRequest (method, params, ... ) dict abort
377385 let params = deepcopy (a: params )
378386 let bufnr = s: PreprocessParams (self , params)
@@ -575,7 +583,8 @@ function! copilot#agent#New(...) abort
575583 \ ' Close' : function (' s:AgentClose' ),
576584 \ ' Notify' : function (' s:AgentNotify' ),
577585 \ ' Request' : function (' s:AgentRequest' ),
578- \ ' SyncTextDocument' : function (' s:VimSyncTextDocument' ),
586+ \ ' Attach' : function (' s:VimAttach' ),
587+ \ ' IsAttached' : function (' s:VimIsAttached' ),
579588 \ ' Call' : function (' s:AgentCall' ),
580589 \ ' Cancel' : function (' s:AgentCancel' ),
581590 \ ' StartupError' : function (' s:AgentStartupError' ),
@@ -622,7 +631,8 @@ function! copilot#agent#New(...) abort
622631 \ ' Close' : function (' s:LspClose' ),
623632 \ ' Notify' : function (' s:LspNotify' ),
624633 \ ' Request' : function (' s:LspRequest' ),
625- \ ' SyncTextDocument' : function (' s:NvimSyncTextDocument' ),
634+ \ ' Attach' : function (' s:NvimAttach' ),
635+ \ ' IsAttached' : function (' s:NvimIsAttached' ),
626636 \ })
627637 let instance.client_id = eval (" v:lua.require'_copilot'.lsp_start_client(command, keys(instance.methods), opts, instance.settings)" )
628638 let instance.id = instance.client_id
0 commit comments