Skip to content

Commit

Permalink
feat: respect git ignore for autosuggestions (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanh20009 authored Jan 11, 2025
1 parent 5c1b9d5 commit c3e7ba0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions lua/avante/suggestion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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 })
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/avante/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit c3e7ba0

Please sign in to comment.