fix: changes for file paths

This commit is contained in:
2024-12-12 20:25:26 +01:00
parent 78b0172772
commit 30b119e5f9
5 changed files with 24 additions and 63 deletions

View File

@@ -1,15 +1,7 @@
-- lua/chatgpt_nvim/context.lua
-- Modified to:
-- 1) Use directories from config to build project structure.
-- 2) Include file contents from those directories.
-- 3) Skip files listed in .gitignore.
--
local M = {}
local uv = vim.loop
-- Returns a set of files mentioned in .gitignore patterns.
local function load_gitignore_patterns(root)
local gitignore_path = root .. "/.gitignore"
local fd = uv.fs_open(gitignore_path, "r", 438)
@@ -32,8 +24,6 @@ end
local function should_ignore_file(file, ignore_patterns)
for _, pattern in ipairs(ignore_patterns) do
-- Simple pattern matching. For more complex patterns, consider using lua patterns.
-- This is a basic implementation. Adjust as needed.
if file:find(pattern, 1, true) then
return true
end
@@ -74,20 +64,19 @@ function M.get_project_files(directories)
end
scandir(abs_dir, ignore_patterns, all_files)
end
return all_files
end
function M.get_project_structure(directories)
local files = M.get_project_files(directories)
-- Create a listing of files only (relative to root)
local root = vim.fn.getcwd()
local rel_files = {}
for _, f in ipairs(files) do
for _, f in ipairs(all_files) do
local rel = f:gsub("^" .. root .. "/", "")
table.insert(rel_files, rel)
end
local structure = "Files:\n" .. table.concat(rel_files, "\n")
return rel_files
end
function M.get_project_structure(directories)
local files = M.get_project_files(directories)
local structure = "Files:\n" .. table.concat(files, "\n")
return structure
end
@@ -95,13 +84,18 @@ function M.get_file_contents(files)
local root = vim.fn.getcwd()
local sections = {}
for _, f in ipairs(files) do
local fd = uv.fs_open(root .. "/" .. f, "r", 438)
local path = root .. "/" .. f
local fd = uv.fs_open(path, "r", 438)
if fd then
local stat = uv.fs_fstat(fd)
local data = uv.fs_read(fd, stat.size, 0)
uv.fs_close(fd)
if data then
table.insert(sections, "\n<<<CGPT Current File\n" .. (root .. "/" .. f) .. "\n" .. data .. "\n<<<CGPT Current File END\n")
if stat then
local data = uv.fs_read(fd, stat.size, 0)
uv.fs_close(fd)
if data then
table.insert(sections, "\n<<<CGPT Current File\n" .. path .. "\n" .. data .. "\n<<<CGPT Current File END\n")
end
else
uv.fs_close(fd)
end
end
end