feat: dont show files and folders in list which are in .gitignore

This commit is contained in:
2025-01-07 17:58:57 +01:00
parent 1520641a04
commit b9643952cb

View File

@@ -2,6 +2,37 @@ local M = {}
local uv = vim.loop local uv = vim.loop
-- Converts a .gitignore-style pattern to a Lua pattern
local function gitignore_to_lua_pattern(gip)
-- Trim spaces
gip = gip:gsub("^%s*(.-)%s*$", "%1")
-- Escape magic chars in Lua patterns
local magic_chars = "().^$+%-*?[]"
gip = gip:gsub("["..magic_chars.."]", "%%%1")
-- Convert ** to .- (match any path, including dirs)
gip = gip:gsub("%%%%%*%%%%%*", ".*")
-- Convert * to [^/]* (match anything except /)
gip = gip:gsub("%%%%%*", "[^/]*")
-- If pattern starts with /, ensure it matches start of string
if gip:sub(1,1) == "/" then
gip = "^" .. gip:sub(2)
else
-- Otherwise allow matching anywhere
gip = gip
end
-- If pattern ends with /, ensure it matches a directory
if gip:sub(-1) == "/" then
gip = gip .. ".*"
end
return gip
end
local function load_gitignore_patterns(root, conf) local function load_gitignore_patterns(root, conf)
local gitignore_path = root .. "/.gitignore" local gitignore_path = root .. "/.gitignore"
local fd = uv.fs_open(gitignore_path, "r", 438) local fd = uv.fs_open(gitignore_path, "r", 438)
@@ -19,7 +50,7 @@ local function load_gitignore_patterns(root, conf)
for line in data:gmatch("[^\r\n]+") do for line in data:gmatch("[^\r\n]+") do
line = line:match("^%s*(.-)%s*$") line = line:match("^%s*(.-)%s*$")
if line ~= "" and not line:match("^#") then if line ~= "" and not line:match("^#") then
patterns[#patterns+1] = line table.insert(patterns, gitignore_to_lua_pattern(line))
end end
end end
if conf.debug then if conf.debug then
@@ -30,7 +61,7 @@ end
local function should_ignore_file(file, ignore_patterns, conf) local function should_ignore_file(file, ignore_patterns, conf)
for _, pattern in ipairs(ignore_patterns) do for _, pattern in ipairs(ignore_patterns) do
if file:find(pattern, 1, true) then if file:match(pattern) then
if conf.debug then if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:context] Ignoring file/dir: " .. file .. " (matched pattern: " .. pattern .. ")\n") vim.api.nvim_out_write("[chatgpt_nvim:context] Ignoring file/dir: " .. file .. " (matched pattern: " .. pattern .. ")\n")
end end
@@ -154,4 +185,4 @@ function M.get_file_contents(files, conf)
return table.concat(sections, "\n") return table.concat(sections, "\n")
end end
return M return M