diff --git a/lua/copilot/client/init.lua b/lua/copilot/client/init.lua index bff572a3..a7329f35 100644 --- a/lua/copilot/client/init.lua +++ b/lua/copilot/client/init.lua @@ -39,8 +39,12 @@ function M.buf_is_attached(bufnr) end ---@param force? boolean -function M.buf_attach(force) - local bufnr = vim.api.nvim_get_current_buf() +---@param bufnr? integer The buffer number of which will be attached. 0 or nil for current buffer +function M.buf_attach(force, bufnr) + bufnr = bufnr or vim.api.nvim_get_current_buf() + if bufnr == 0 then + bufnr = vim.api.nvim_get_current_buf() + end if lsp.initialization_failed() then logger.error("copilot-language-server failed to initialize") @@ -53,7 +57,7 @@ function M.buf_attach(force) return end - if not (force or util.should_attach()) then + if not (force or util.should_attach(bufnr)) then logger.debug("not attaching to buffer based on force and should_attach criteria") return end @@ -163,9 +167,11 @@ function M.setup() vim.api.nvim_create_autocmd("FileType", { group = M.augroup, - callback = vim.schedule_wrap(function() - M.buf_attach() - end), + callback = function(ev) + vim.schedule(function() + M.buf_attach(nil, ev.buf) + end) + end, }) vim.schedule(M.ensure_client_started) diff --git a/lua/copilot/command.lua b/lua/copilot/command.lua index 0ac76a1f..7a09782c 100644 --- a/lua/copilot/command.lua +++ b/lua/copilot/command.lua @@ -29,13 +29,13 @@ function M.version() end)() end ----@param opts? { force?: boolean } +---@param opts? { force?: boolean, bufnr?: integer} function M.attach(opts) logger.trace("attaching to buffer") opts = opts or {} if not opts.force then - local should_attach, no_attach_reason = u.should_attach() + local should_attach, no_attach_reason = u.should_attach(opts.bufnr) if not should_attach then logger.notify(no_attach_reason .. "\nto force attach, run ':Copilot! attach'") @@ -45,7 +45,7 @@ function M.attach(opts) opts.force = true end - c.buf_attach(opts.force) + c.buf_attach(opts.force, opts.bufnr) end function M.detach() diff --git a/lua/copilot/util.lua b/lua/copilot/util.lua index 2efa1af8..04085d94 100644 --- a/lua/copilot/util.lua +++ b/lua/copilot/util.lua @@ -34,15 +34,23 @@ end ---@return boolean should_attach ---@return string? no_attach_reason -function M.should_attach() +function M.should_attach(bufnr) + bufnr = bufnr or vim.api.nvim_get_current_buf() + if bufnr == 0 then + bufnr = vim.api.nvim_get_current_buf() + end + + if not vim.api.nvim_buf_is_valid(bufnr) then + return false, "Invalid buffer" + end + local ft = config.filetypes - local ft_disabled, ft_disabled_reason = require("copilot.client.filetypes").is_ft_disabled(vim.bo.filetype, ft) + local ft_disabled, ft_disabled_reason = require("copilot.client.filetypes").is_ft_disabled(vim.bo[bufnr].filetype, ft) if ft_disabled then return not ft_disabled, ft_disabled_reason end - local bufnr = vim.api.nvim_get_current_buf() local bufname = vim.api.nvim_buf_get_name(bufnr) local conf_attach = config.should_attach(bufnr, bufname)