From 30b119e5f993967f1e6a5258b8ba9645d1995179 Mon Sep 17 00:00:00 2001 From: Dominik Polakovics Date: Thu, 12 Dec 2024 20:25:26 +0100 Subject: [PATCH] fix: changes for file paths --- lua/chatgpt_nvim/config.lua | 11 +--------- lua/chatgpt_nvim/context.lua | 40 +++++++++++++++--------------------- lua/chatgpt_nvim/handler.lua | 6 +----- lua/chatgpt_nvim/init.lua | 27 +++++------------------- plugin/chatgpt.vim | 3 --- 5 files changed, 24 insertions(+), 63 deletions(-) diff --git a/lua/chatgpt_nvim/config.lua b/lua/chatgpt_nvim/config.lua index 934a45f..d0cb85c 100644 --- a/lua/chatgpt_nvim/config.lua +++ b/lua/chatgpt_nvim/config.lua @@ -1,13 +1,6 @@ --- lua/chatgpt_nvim/config.lua --- Changed to use YAML for configuration instead of JSON. --- Reads from .chatgpt_config.yaml and uses lyaml to parse it. --- If no config file is found, uses default values. - local M = {} local uv = vim.loop --- Attempt to require lyaml for YAML parsing. --- Make sure lyaml is installed (e.g., via luarocks install lyaml) local ok_yaml, lyaml = pcall(require, "lyaml") local function get_config_path() @@ -18,9 +11,8 @@ end function M.load() local path = get_config_path() - local fd = uv.fs_open(path, "r", 438) -- 438 = 0o666 + local fd = uv.fs_open(path, "r", 438) if not fd then - -- Return some default configuration if no file found return { initial_prompt = "", directories = { "." }, @@ -41,7 +33,6 @@ function M.load() end end end - -- Fallback if decode fails return { initial_prompt = "", directories = { "." }, diff --git a/lua/chatgpt_nvim/context.lua b/lua/chatgpt_nvim/context.lua index 12c1069..757e906 100644 --- a/lua/chatgpt_nvim/context.lua +++ b/lua/chatgpt_nvim/context.lua @@ -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<<