fix: load config just once per call

This commit is contained in:
2025-01-05 01:45:43 +01:00
parent 6950c542ad
commit ca729140c3
4 changed files with 103 additions and 93 deletions

View File

@@ -1,10 +1,11 @@
local M = {}
local uv = vim.loop
local config = require('chatgpt_nvim.config')
-- Remove local config = require('chatgpt_nvim.config') so we don't load config each time
-- We'll accept a 'conf' parameter in our functions instead
-- local config = require('chatgpt_nvim.config')
local function load_gitignore_patterns(root)
local conf = config.load()
local function load_gitignore_patterns(root, conf)
local gitignore_path = root .. "/.gitignore"
local fd = uv.fs_open(gitignore_path, "r", 438)
if not fd then
@@ -19,7 +20,7 @@ local function load_gitignore_patterns(root)
if not data then return {} end
local patterns = {}
for line in data:gmatch("[^\r\n]+") do
line = line:match("^%s*(.-)%s*$") -- trim
line = line:match("^%s*(.-)%s*$")
if line ~= "" and not line:match("^#") then
patterns[#patterns+1] = line
end
@@ -30,8 +31,7 @@ local function load_gitignore_patterns(root)
return patterns
end
local function should_ignore_file(file, ignore_patterns)
local conf = config.load()
local function should_ignore_file(file, ignore_patterns, conf)
for _, pattern in ipairs(ignore_patterns) do
if file:find(pattern, 1, true) then
if conf.debug then
@@ -43,8 +43,7 @@ local function should_ignore_file(file, ignore_patterns)
return false
end
local function is_text_file(file)
local conf = config.load()
local function is_text_file(file, conf)
local fd = uv.fs_open(file, "r", 438)
if not fd then
if conf.debug then
@@ -54,7 +53,6 @@ local function is_text_file(file)
end
local chunk = uv.fs_read(fd, 1024, 0) or ""
uv.fs_close(fd)
-- Check for null bytes as a heuristic for binary files
if chunk:find("\0") then
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:context] File appears binary: " .. file .. "\n")
@@ -64,8 +62,7 @@ local function is_text_file(file)
return true
end
local function scandir(dir, ignore_patterns, files)
local conf = config.load()
local function scandir(dir, ignore_patterns, files, conf)
local fd = uv.fs_opendir(dir, nil, 50)
if not fd then
if conf.debug then
@@ -78,11 +75,11 @@ local function scandir(dir, ignore_patterns, files)
if not ents then break end
for _, ent in ipairs(ents) do
local fullpath = dir .. "/" .. ent.name
if not should_ignore_file(fullpath, ignore_patterns) then
if ent.type == "file" and is_text_file(fullpath) then
if not should_ignore_file(fullpath, ignore_patterns, conf) then
if ent.type == "file" and is_text_file(fullpath, conf) then
table.insert(files, fullpath)
elseif ent.type == "directory" and ent.name ~= ".git" then
scandir(fullpath, ignore_patterns, files)
scandir(fullpath, ignore_patterns, files, conf)
end
end
end
@@ -91,17 +88,17 @@ local function scandir(dir, ignore_patterns, files)
return files
end
function M.get_project_files(directories)
local conf = config.load()
function M.get_project_files(directories, conf)
-- conf is passed in from outside so we don't load config repeatedly
local root = vim.fn.getcwd()
local ignore_patterns = load_gitignore_patterns(root)
local ignore_patterns = load_gitignore_patterns(root, conf)
local all_files = {}
for _, dir in ipairs(directories) do
local abs_dir = dir
if not abs_dir:match("^/") then
abs_dir = root .. "/" .. dir
end
scandir(abs_dir, ignore_patterns, all_files)
scandir(abs_dir, ignore_patterns, all_files, conf)
end
local rel_files = {}
@@ -117,14 +114,13 @@ function M.get_project_files(directories)
return rel_files
end
function M.get_project_structure(directories)
local files = M.get_project_files(directories)
function M.get_project_structure(directories, conf)
local files = M.get_project_files(directories, conf)
local structure = "Files:\n" .. table.concat(files, "\n")
return structure
end
function M.get_file_contents(files)
local conf = config.load()
function M.get_file_contents(files, conf)
local root = vim.fn.getcwd()
local sections = {}
for _, f in ipairs(files) do
@@ -150,4 +146,4 @@ function M.get_file_contents(files)
return table.concat(sections, "\n")
end
return M
return M