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)
39
39
end
40
40
41
41
--- @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
44
48
45
49
if lsp .initialization_failed () then
46
50
logger .error (" copilot-language-server failed to initialize" )
@@ -53,7 +57,7 @@ function M.buf_attach(force)
53
57
return
54
58
end
55
59
56
- if not (force or util .should_attach ()) then
60
+ if not (force or util .should_attach (bufnr )) then
57
61
logger .debug (" not attaching to buffer based on force and should_attach criteria" )
58
62
return
59
63
end
@@ -163,9 +167,11 @@ function M.setup()
163
167
164
168
vim .api .nvim_create_autocmd (" FileType" , {
165
169
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 ,
169
175
})
170
176
171
177
vim .schedule (M .ensure_client_started )
Original file line number Diff line number Diff line change @@ -29,13 +29,13 @@ function M.version()
29
29
end )()
30
30
end
31
31
32
- --- @param opts ? { force ?: boolean }
32
+ --- @param opts ? { force ?: boolean , bufnr ?: integer }
33
33
function M .attach (opts )
34
34
logger .trace (" attaching to buffer" )
35
35
opts = opts or {}
36
36
37
37
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 )
39
39
40
40
if not should_attach then
41
41
logger .notify (no_attach_reason .. " \n to force attach, run ':Copilot! attach'" )
@@ -45,7 +45,7 @@ function M.attach(opts)
45
45
opts .force = true
46
46
end
47
47
48
- c .buf_attach (opts .force )
48
+ c .buf_attach (opts .force , opts . bufnr )
49
49
end
50
50
51
51
function M .detach ()
Original file line number Diff line number Diff line change 34
34
35
35
--- @return boolean should_attach
36
36
--- @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
+
38
47
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 )
40
49
41
50
if ft_disabled then
42
51
return not ft_disabled , ft_disabled_reason
43
52
end
44
53
45
- local bufnr = vim .api .nvim_get_current_buf ()
46
54
local bufname = vim .api .nvim_buf_get_name (bufnr )
47
55
local conf_attach = config .should_attach (bufnr , bufname )
48
56
You can’t perform that action at this time.
0 commit comments