Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lua/copilot/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions lua/copilot/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
Expand All @@ -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()
Expand Down
14 changes: 11 additions & 3 deletions lua/copilot/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down