feat: respect git ignore for autosuggestions (#994)

This commit is contained in:
Ethan Howard 2025-01-11 15:00:19 +00:00 committed by GitHub
parent 5c1b9d5463
commit c3e7ba0be0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 2 deletions

View File

@ -124,6 +124,7 @@ M._defaults = {
behaviour = { behaviour = {
auto_focus_sidebar = true, auto_focus_sidebar = true,
auto_suggestions = false, -- Experimental stage auto_suggestions = false, -- Experimental stage
auto_suggestions_respect_ignore = false,
auto_set_highlight_group = true, auto_set_highlight_group = true,
auto_set_keymaps = true, auto_set_keymaps = true,
auto_apply_diff_after_generation = false, auto_apply_diff_after_generation = false,

View File

@ -22,6 +22,8 @@ local SUGGESTION_NS = api.nvim_create_namespace("avante_suggestion")
---@field id number ---@field id number
---@field augroup integer ---@field augroup integer
---@field extmark_id integer ---@field extmark_id integer
---@field ignore_patterns table
---@field negate_patterns table
---@field _timer? table ---@field _timer? table
---@field _contexts table ---@field _contexts table
local Suggestion = {} local Suggestion = {}
@ -31,10 +33,15 @@ Suggestion.__index = Suggestion
---@return avante.Suggestion ---@return avante.Suggestion
function Suggestion:new(id) function Suggestion:new(id)
local instance = setmetatable({}, self) 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.id = id
instance.extmark_id = 1 instance.extmark_id = 1
instance._timer = nil instance._timer = nil
instance._contexts = {} instance._contexts = {}
instance.ignore_patterns = gitignore_patterns
instance.negate_patterns = gitignore_negate_patterns
if Config.behaviour.auto_suggestions then if Config.behaviour.auto_suggestions then
if not vim.g.avante_login or vim.g.avante_login == false 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 }) 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 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() local ctx = self:ctx()
if ctx.prev_doc and vim.deep_equal(ctx.prev_doc, Utils.get_doc()) then return end if ctx.prev_doc and vim.deep_equal(ctx.prev_doc, Utils.get_doc()) then return end

View File

@ -633,7 +633,7 @@ function M.parse_gitignore(gitignore_path)
return ignore_patterns, negate_patterns return ignore_patterns, negate_patterns
end 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 for _, pattern in ipairs(negate_patterns) do
if file:match(pattern) then return false end if file:match(pattern) then return false end
end end
@ -657,7 +657,7 @@ function M.scan_directory(directory, ignore_patterns, negate_patterns)
if type == "directory" then if type == "directory" then
vim.list_extend(files, M.scan_directory(full_path, ignore_patterns, negate_patterns)) vim.list_extend(files, M.scan_directory(full_path, ignore_patterns, negate_patterns))
elseif type == "file" then 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
end end