File tree Expand file tree Collapse file tree 3 files changed +26
-12
lines changed
Expand file tree Collapse file tree 3 files changed +26
-12
lines changed Original file line number Diff line number Diff line change @@ -39,8 +39,12 @@ function M.buf_is_attached(bufnr)
3939end
4040
4141--- @param force ? boolean
42- function M .buf_attach (force )
43- local bufnr = vim .api .nvim_get_current_buf ()
42+ --- @param bufnr ? integer The buffer number of which will be attached. 0 or nil for current buffer
43+ function M .buf_attach (force , bufnr )
44+ bufnr = bufnr or vim .api .nvim_get_current_buf ()
45+ if bufnr == 0 then
46+ bufnr = vim .api .nvim_get_current_buf ()
47+ end
4448
4549 if lsp .initialization_failed () then
4650 logger .error (" copilot-language-server failed to initialize" )
@@ -53,7 +57,7 @@ function M.buf_attach(force)
5357 return
5458 end
5559
56- if not (force or util .should_attach ()) then
60+ if not (force or util .should_attach (bufnr )) then
5761 logger .debug (" not attaching to buffer based on force and should_attach criteria" )
5862 return
5963 end
@@ -163,9 +167,11 @@ function M.setup()
163167
164168 vim .api .nvim_create_autocmd (" FileType" , {
165169 group = M .augroup ,
166- callback = vim .schedule_wrap (function ()
167- M .buf_attach ()
168- end ),
170+ callback = function (ev )
171+ vim .schedule (function ()
172+ M .buf_attach (nil , ev .buf )
173+ end )
174+ end ,
169175 })
170176
171177 vim .schedule (M .ensure_client_started )
Original file line number Diff line number Diff line change @@ -29,13 +29,13 @@ function M.version()
2929 end )()
3030end
3131
32- --- @param opts ? { force ?: boolean }
32+ --- @param opts ? { force ?: boolean , bufnr ?: integer }
3333function M .attach (opts )
3434 logger .trace (" attaching to buffer" )
3535 opts = opts or {}
3636
3737 if not opts .force then
38- local should_attach , no_attach_reason = u .should_attach ()
38+ local should_attach , no_attach_reason = u .should_attach (opts . bufnr )
3939
4040 if not should_attach then
4141 logger .notify (no_attach_reason .. " \n to force attach, run ':Copilot! attach'" )
@@ -45,7 +45,7 @@ function M.attach(opts)
4545 opts .force = true
4646 end
4747
48- c .buf_attach (opts .force )
48+ c .buf_attach (opts .force , opts . bufnr )
4949end
5050
5151function M .detach ()
Original file line number Diff line number Diff line change 3434
3535--- @return boolean should_attach
3636--- @return string ? no_attach_reason
37- function M .should_attach ()
37+ function M .should_attach (bufnr )
38+ bufnr = bufnr or vim .api .nvim_get_current_buf ()
39+ if bufnr == 0 then
40+ bufnr = vim .api .nvim_get_current_buf ()
41+ end
42+
43+ if not vim .api .nvim_buf_is_valid (bufnr ) then
44+ return false , " Invalid buffer"
45+ end
46+
3847 local ft = config .filetypes
39- local ft_disabled , ft_disabled_reason = require (" copilot.client.filetypes" ).is_ft_disabled (vim .bo .filetype , ft )
48+ local ft_disabled , ft_disabled_reason = require (" copilot.client.filetypes" ).is_ft_disabled (vim .bo [ bufnr ] .filetype , ft )
4049
4150 if ft_disabled then
4251 return not ft_disabled , ft_disabled_reason
4352 end
4453
45- local bufnr = vim .api .nvim_get_current_buf ()
4654 local bufname = vim .api .nvim_buf_get_name (bufnr )
4755 local conf_attach = config .should_attach (bufnr , bufname )
4856
You can’t perform that action at this time.
0 commit comments