From 99654fe9ad6cb2500c66b178a03326f75c95f176 Mon Sep 17 00:00:00 2001 From: Antoine Gaudreau Simard Date: Tue, 25 Mar 2025 19:43:56 -0400 Subject: [PATCH] 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 --- README.md | 15 ++++++++++++++- lua/copilot/client.lua | 17 +++++------------ lua/copilot/config.lua | 16 ++++++++++++++-- lua/copilot/util.lua | 8 -------- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 15a80c7c..3c134f14 100644 --- a/README.md +++ b/README.md @@ -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 = {}, }) ``` @@ -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: diff --git a/lua/copilot/client.lua b/lua/copilot/client.lua index 42fc2172..7a1cc4e3 100644 --- a/lua/copilot/client.lua +++ b/lua/copilot/client.lua @@ -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, } @@ -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 diff --git a/lua/copilot/config.lua b/lua/copilot/config.lua index f8c6ce2a..f67973db 100644 --- a/lua/copilot/config.lua +++ b/lua/copilot/config.lua @@ -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 = { diff --git a/lua/copilot/util.lua b/lua/copilot/util.lua index ab416966..4eb11981 100644 --- a/lua/copilot/util.lua +++ b/lua/copilot/util.lua @@ -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