Skip to content

Commit 99654fe

Browse files
committed
feat: add a default should_attach function that includes the buffer logic
This allows attaching to non-buflisted buffers through custom logic. Fixes #279 Fixes #239
1 parent d296017 commit 99654fe

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,19 @@ require('copilot').setup({
116116
root_dir = function()
117117
return vim.fs.dirname(vim.fs.find(".git", { upward = true })[1])
118118
end,
119-
should_attach = nil, -- type is fun(bufnr: integer, bufname: string): boolean
119+
should_attach = function(_, _)
120+
if not vim.bo.buflisted then
121+
logger.debug("not attaching, buffer is not 'buflisted'")
122+
return false
123+
end
124+
125+
if vim.bo.buftype ~= "" then
126+
logger.debug("not attaching, buffer 'buftype' is " .. vim.bo.buftype)
127+
return false
128+
end
129+
130+
return true
131+
end,
120132
server_opts_overrides = {},
121133
})
122134
```
@@ -324,6 +336,7 @@ If none is found, it will use the current working directory.
324336

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

329342
An example of this would be:

lua/copilot/client.lua

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ local M = {
1717
node_version_error = nil,
1818
startup_error = nil,
1919
initialized = false,
20-
---@type copilot_should_attach|nil
20+
---@type copilot_should_attach
2121
should_attach = nil,
2222
}
2323

@@ -95,22 +95,15 @@ end
9595

9696
---@param force? boolean
9797
function M.buf_attach(force)
98-
if M.should_attach then
99-
local bufnr = vim.api.nvim_get_current_buf()
100-
local bufname = vim.api.nvim_buf_get_name(bufnr)
101-
102-
if not M.should_attach(bufnr, bufname) then
103-
logger.debug("copilot is disabled by should_attach")
104-
return
105-
end
106-
end
107-
10898
if is_disabled then
10999
logger.warn("copilot is disabled")
110100
return
111101
end
112102

113-
if not force and not util.should_attach() then
103+
local bufnr = vim.api.nvim_get_current_buf()
104+
local bufname = vim.api.nvim_buf_get_name(bufnr)
105+
106+
if not force and not M.should_attach(bufnr, bufname) and not util.should_attach() then
114107
return
115108
end
116109

lua/copilot/config.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,20 @@ local default_config = {
6262
return vim.fs.dirname(vim.fs.find(".git", { upward = true })[1])
6363
end,
6464
---@alias copilot_should_attach fun(bufnr: integer, bufname: string): boolean
65-
---@type copilot_should_attach|nil
66-
should_attach = nil,
65+
---@type copilot_should_attach
66+
should_attach = function(_, _)
67+
if not vim.bo.buflisted then
68+
logger.debug("not attaching, bugger is not 'buflisted'")
69+
return false
70+
end
71+
72+
if vim.bo.buftype ~= "" then
73+
logger.debug("not attaching, buffer 'buftype' is " .. vim.bo.buftype)
74+
return false
75+
end
76+
77+
return true
78+
end,
6779
}
6880

6981
local mod = {

lua/copilot/util.lua

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,6 @@ function M.should_attach()
104104
return not ft_disabled, ft_disabled_reason
105105
end
106106

107-
if not vim.bo.buflisted then
108-
return false, "buffer not 'buflisted'"
109-
end
110-
111-
if vim.bo.buftype ~= "" then
112-
return false, "buffer 'buftype' is " .. vim.bo.buftype
113-
end
114-
115107
return true
116108
end
117109

0 commit comments

Comments
 (0)