Skip to content

Commit 4a557e7

Browse files
committed
feat: add should_attach function
Used to determine if Copilot should attach to a buffer or not. Fixes: #74
1 parent 826e468 commit 4a557e7

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ require('copilot').setup({
101101
root_dir = function()
102102
return vim.fs.dirname(vim.fs.find(".git", { upward = true })[1])
103103
end,
104+
should_attach = nil, -- type is fun(bufnr: integer, bufname: string): boolean
104105
server_opts_overrides = {},
105106
})
106107
```
@@ -126,7 +127,6 @@ require("copilot.panel").refresh()
126127
### suggestion
127128

128129
When `auto_trigger` is `true`, copilot starts suggesting as soon as you enter insert mode.
129-
130130
When `auto_trigger` is `false`, use the `next`, `prev` or `accept` keymap to trigger copilot suggestion.
131131

132132
To toggle auto trigger for the current buffer, use `require("copilot.suggestion").toggle_auto_trigger()`.
@@ -305,6 +305,26 @@ They can also be added runtime, using the command `:Copilot workspace add [folde
305305
This allows changing the function that gets the root folder, the default looks for a parent folder that contains the folder `.git`.
306306
If none is found, it will use the current working directory.
307307

308+
### should_attach
309+
310+
This function is called to determine if copilot should attach to the buffer or not.
311+
It is useful if you would like to go beyond the filetypes and have more control over when copilot should attach.
312+
Since this happens before attaching to the buffer, it is good to prevent Copilot from reading sensitive files.
313+
314+
An example of this would be:
315+
316+
```lua
317+
require("copilot").setup {
318+
should_attach = function(_, bufname)
319+
if string.match(bufname, "env") then
320+
return false
321+
end
322+
323+
return true
324+
end
325+
}
326+
```
327+
308328
## Commands
309329

310330
`copilot.lua` defines the `:Copilot` command that can perform various actions. It has completion support, so try it out.

lua/copilot/client.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ local M = {
1717
node_version_error = nil,
1818
startup_error = nil,
1919
initialized = false,
20+
---@type copilot_should_attach|nil
21+
should_attach = nil,
2022
}
2123

2224
---@param id integer
@@ -93,6 +95,16 @@ end
9395

9496
---@param force? boolean
9597
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+
96108
if is_disabled then
97109
logger.warn("copilot is disabled")
98110
return
@@ -348,6 +360,7 @@ end
348360

349361
function M.setup()
350362
M.config = prepare_client_config(config.get("server_opts_overrides"))
363+
M.should_attach = config.get("should_attach") --[[@as copilot_should_attach|nil]]
351364

352365
if not M.config then
353366
is_disabled = true

lua/copilot/config.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ local default_config = {
6161
root_dir = function()
6262
return vim.fs.dirname(vim.fs.find(".git", { upward = true })[1])
6363
end,
64+
---@alias copilot_should_attach fun(bufnr: integer, bufname: string): boolean
65+
---@type copilot_should_attach|nil
66+
should_attach = nil,
6467
}
6568

6669
local mod = {

0 commit comments

Comments
 (0)