Skip to content
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

feat (autosuggestions) respect gitignore for autosuggestions #994

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: respect git ignore for autosuggestions
respect git ignore
  • Loading branch information
ethanh20009 committed Jan 11, 2025
commit 7d109faba7657140236c1c60a928ee8b5c0f6369
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
Loading