From 7d109faba7657140236c1c60a928ee8b5c0f6369 Mon Sep 17 00:00:00 2001 From: Ethan Howard Date: Mon, 23 Dec 2024 12:39:36 +0000 Subject: [PATCH] feat: respect git ignore for autosuggestions respect git ignore --- lua/avante/config.lua | 1 + lua/avante/suggestion.lua | 15 +++++++++++++++ lua/avante/utils/init.lua | 4 ++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lua/avante/config.lua b/lua/avante/config.lua index 5ed06db47..727466c0c 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -124,6 +124,7 @@ M._defaults = { behaviour = { auto_focus_sidebar = true, auto_suggestions = false, -- Experimental stage + auto_suggestions_respect_ignore = false, auto_set_highlight_group = true, auto_set_keymaps = true, auto_apply_diff_after_generation = false, diff --git a/lua/avante/suggestion.lua b/lua/avante/suggestion.lua index ac9c70dd4..8fd37abdc 100644 --- a/lua/avante/suggestion.lua +++ b/lua/avante/suggestion.lua @@ -22,6 +22,8 @@ local SUGGESTION_NS = api.nvim_create_namespace("avante_suggestion") ---@field id number ---@field augroup integer ---@field extmark_id integer +---@field ignore_patterns table +---@field negate_patterns table ---@field _timer? table ---@field _contexts table local Suggestion = {} @@ -31,10 +33,15 @@ Suggestion.__index = Suggestion ---@return avante.Suggestion function Suggestion:new(id) local instance = setmetatable({}, self) + local gitignore_path = Utils.get_project_root() .. "/.gitignore" + local gitignore_patterns, gitignore_negate_patterns = Utils.parse_gitignore(gitignore_path) + instance.id = id instance.extmark_id = 1 instance._timer = nil instance._contexts = {} + instance.ignore_patterns = gitignore_patterns + instance.negate_patterns = gitignore_negate_patterns if Config.behaviour.auto_suggestions then if not vim.g.avante_login or vim.g.avante_login == false then api.nvim_exec_autocmds("User", { pattern = Providers.env.REQUEST_LOGIN_PATTERN }) @@ -290,6 +297,14 @@ function Suggestion:setup_autocmds() if vim.bo.buftype ~= "" then return end + local full_path = api.nvim_buf_get_name(0) + if + Config.behaviour.auto_suggestions_respect_ignore + and Utils.is_ignored(full_path, self.ignore_patterns, self.negate_patterns) + then + return + end + local ctx = self:ctx() if ctx.prev_doc and vim.deep_equal(ctx.prev_doc, Utils.get_doc()) then return end diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index 92b8be237..a5ec39471 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -633,7 +633,7 @@ function M.parse_gitignore(gitignore_path) return ignore_patterns, negate_patterns end -local function is_ignored(file, ignore_patterns, negate_patterns) +function M.is_ignored(file, ignore_patterns, negate_patterns) for _, pattern in ipairs(negate_patterns) do if file:match(pattern) then return false end end @@ -657,7 +657,7 @@ function M.scan_directory(directory, ignore_patterns, negate_patterns) if type == "directory" then vim.list_extend(files, M.scan_directory(full_path, ignore_patterns, negate_patterns)) elseif type == "file" then - if not is_ignored(full_path, ignore_patterns, negate_patterns) then table.insert(files, full_path) end + if not M.is_ignored(full_path, ignore_patterns, negate_patterns) then table.insert(files, full_path) end end end