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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,19 @@ require('copilot').setup({
root_dir = function()
return vim.fs.dirname(vim.fs.find(".git", { upward = true })[1])
end,
should_attach = nil, -- type is fun(bufnr: integer, bufname: string): boolean
should_attach = function(_, _)
if not vim.bo.buflisted then
logger.debug("not attaching, buffer is not 'buflisted'")
return false
end

if vim.bo.buftype ~= "" then
logger.debug("not attaching, buffer 'buftype' is " .. vim.bo.buftype)
return false
end

return true
end,
server_opts_overrides = {},
})
```
Expand Down Expand Up @@ -324,6 +336,7 @@ If none is found, it will use the current working directory.

This function is called to determine if copilot should attach to the buffer or not.
It is useful if you would like to go beyond the filetypes and have more control over when copilot should attach.
You can also use it to attach to buflisted buffers by simply omiting that portion from the function.
Since this happens before attaching to the buffer, it is good to prevent Copilot from reading sensitive files.

An example of this would be:
Expand Down
17 changes: 5 additions & 12 deletions lua/copilot/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local M = {
node_version_error = nil,
startup_error = nil,
initialized = false,
---@type copilot_should_attach|nil
---@type copilot_should_attach
should_attach = nil,
}

Expand Down Expand Up @@ -95,22 +95,15 @@ end

---@param force? boolean
function M.buf_attach(force)
if M.should_attach then
local bufnr = vim.api.nvim_get_current_buf()
local bufname = vim.api.nvim_buf_get_name(bufnr)

if not M.should_attach(bufnr, bufname) then
logger.debug("copilot is disabled by should_attach")
return
end
end

if is_disabled then
logger.warn("copilot is disabled")
return
end

if not force and not util.should_attach() then
local bufnr = vim.api.nvim_get_current_buf()
local bufname = vim.api.nvim_buf_get_name(bufnr)

if not force and not M.should_attach(bufnr, bufname) and not util.should_attach() then
return
end

Expand Down
16 changes: 14 additions & 2 deletions lua/copilot/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,20 @@ local default_config = {
return vim.fs.dirname(vim.fs.find(".git", { upward = true })[1])
end,
---@alias copilot_should_attach fun(bufnr: integer, bufname: string): boolean
---@type copilot_should_attach|nil
should_attach = nil,
---@type copilot_should_attach
should_attach = function(_, _)
if not vim.bo.buflisted then
logger.debug("not attaching, bugger is not 'buflisted'")
return false
end

if vim.bo.buftype ~= "" then
logger.debug("not attaching, buffer 'buftype' is " .. vim.bo.buftype)
return false
end

return true
end,
}

local mod = {
Expand Down
8 changes: 0 additions & 8 deletions lua/copilot/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,6 @@ function M.should_attach()
return not ft_disabled, ft_disabled_reason
end

if not vim.bo.buflisted then
return false, "buffer not 'buflisted'"
end

if vim.bo.buftype ~= "" then
return false, "buffer 'buftype' is " .. vim.bo.buftype
end

return true
end

Expand Down