-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to disable Copilot for certain directories / files whose path matches certain patterns #74
Comments
I'll think about making this an official feature where users provide a pattern to be matched, but I think that may be overkill for something that can be done in a relatively simple autocmd. This code works perfectly for me:
Obviously you will need to change the disable_dir path to be wherever it is you do your work as well as run the above before copilot is launched |
Might be worth adding:
|
As there's no documentation for #127, I'm wondering how it resolves this issue. |
That PR just adds the You can use that to completely disable copilot. But the logic of when to disable copilot is still up to you. You can manually execute that command, or use that with Something like: local function should_disable_copilot(dir)
return dir == "/some/specific/dir"
end
vim.api.nvim_create_autocmd("DirChanged", {
callback = function(params)
if should_disable_copilot(params.file) then
vim.cmd("Copilot disable")
end
end,
})
-- for initial directory neovim was started with
if should_disable_copilot(vim.loop.cwd()) then
vim.cmd("Copilot disable")
end It's up to you to implement your preferred logic. |
Or you can use the And for your specific repo, create a file called vim.cmd("Copilot disable") |
A working solution is already provided in @zbirenbaum's comment, and this issue is supposed to be tracking a built-in solution. I'm fine if the built-in solution is not implemented right now, but I don't think this issue should be closed as a result of BTW, the |
That comment is quite old and not really what you want. Even if you do On the other hand But you don't specifically need to use I don't want to add some super specific config option for disabling copilot for certain directories/files patterns. The @zbirenbaum what do you think? |
I don't want to completely disable it. I just want it to be disabled when the file is in some directory. If I open a file in another directory, Copilot should be working there. |
Not sure about the use-case. Can you explain a bit about how you're using neovim?
Or something else? |
Ideally, whether Copilot is working or not should depend only on the path of the file being edited (whether the path matches a pattern), not where I initially opened neovim, not which files I opened before, and not where the PWD is. I don't understand your example, because you didn't specify which directory is the directory where I want to disable Copilot. BTW, the "another directory" in my previous comment means a directory that doesn't match the pattern, not an arbitrary directory. |
Hmm... I think I misunderstood your problem. Now I understand exactly what you want. I'd suggest to modify the solution from #74 (comment) to this: local augroup = vim.api.nvim_create_augroup("copilot-disable-patterns", { clear = true })
for _, pattern in ipairs({ "/dir-a/dir-x/*", "/dir-b/*" }) do
vim.api.nvim_create_autocmd("LspAttach", {
group = augroup,
pattern = pattern,
callback = vim.schedule_wrap(function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client.name == "copilot" then
vim.cmd("Copilot detach")
end
end),
})
end
Whether this will be included in the plugin's config is up to @zbirenbaum . The downside of adding it to plugin's config is that the patterns will need to be hardcoded in config. And directory structure is different for different machines (i.e. config won't be portable between machines). Keeping it outside plugin's config let's you use it from I'm keeping the issue open for zbirenbaum's decision. |
I realized this but I thought it was not a big problem in my workflow, and I didn't notice the new |
I changed the callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client.name == 'copilot' then
vim.defer_fn(function()
vim.cmd("silent Copilot detach")
end, 0)
end
end, Without |
Came here looking for exactly this. I could see an option to ignore certain paths being compelling to users or companies who are nervous about snippets of code being released outside their network, even with GitHub's assurance that they are not retained. |
Based on the functions suggested by @MunifTanjim, @ouuan and @zbirenbaum I wrote this one to exclude a number of directories and empty buffers with cwd matching any of the disable_dirs: local augroup = vim.api.nvim_create_augroup("copilot-disable-patterns", { clear = true })
local disable_dirs = {
vim.fn.expand "$HOME/Documents" .. "/*",
}
for _, pattern in ipairs(disable_dirs) do
vim.api.nvim_create_autocmd("LspAttach", {
group = augroup,
pattern = "*", -- This pattern will match all files, including new buffers
callback = function(args)
-- Check if the buffer has a name (file associated) or if CWD starts with /home/user/Documents
local bufname = vim.api.nvim_buf_get_name(0)
local cwd = vim.fn.getcwd()
if bufname == "" and cwd:match("^" .. pattern) or bufname:match(pattern) then
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client.name == "copilot" then
vim.defer_fn(function()
vim.cmd "silent Copilot detach"
end, 0)
end
end
end,
})
end But the "silent" part seems not to work - I get 202 "Detached buffer(id:1) from client (id:1)" messages. |
Many of the recent approaches react to the LspAttach event. This means the LSP client was started and attached already. I am asking myself: Does this already provide information to the LSP we would rather not reveal? Also not knowing the overall architecture the If the goal is to prevent data be handled by copilot, I think we should cut it of as soon as possible. Scanning through the code the plugin, I see https://github.com/zbirenbaum/copilot.lua/blob/b03617a6dc4bc88b65ab5deac1631da9a9c2dcaf/lua/copilot/util.lua#L99C12-L99C25 which can prevent the LSP client from actually being attached. I personally would rather like to see a feature hooking in there. That would give me a way better feeling that the exclusion actually works as intended. Do you think it would be possible to provide a customizable API, something which allows configurations like this? require("copilot").setup({
-- ... other options ...
should_attach = function()
-- Custom conditions come here.
return true
end
}) It would be very handy to have access to the current buffers filename and the cwd. To enable filtering based on paths. |
Motivation
I want to disable Copilot for my university assignments to avoid the risk of cheating. So I want to disable Copilot for all files in a particular directory.
Feature Request
Add an option to disable Copilot for certain directories or files whose path matches certain patterns.
Or provide example config scripts if this can be done by the user without modifying the plugin. I tried running
setup
conditionally and usingautocmd
but with no luck.The text was updated successfully, but these errors were encountered: