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,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 M = {}
local uv = vim.loop 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 ok_yaml, lyaml = pcall(require, "lyaml")
local function get_config_path() local function get_config_path()
@@ -18,9 +11,8 @@ end
function M.load() function M.load()
local path = get_config_path() 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 if not fd then
-- Return some default configuration if no file found
return { return {
initial_prompt = "", initial_prompt = "",
directories = { "." }, directories = { "." },
@@ -41,7 +33,6 @@ function M.load()
end end
end end
end end
-- Fallback if decode fails
return { return {
initial_prompt = "", initial_prompt = "",
directories = { "." }, directories = { "." },

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 M = {}
local uv = vim.loop local uv = vim.loop
-- Returns a set of files mentioned in .gitignore patterns.
local function load_gitignore_patterns(root) local function load_gitignore_patterns(root)
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)
@@ -32,8 +24,6 @@ end
local function should_ignore_file(file, ignore_patterns) local function should_ignore_file(file, ignore_patterns)
for _, pattern in ipairs(ignore_patterns) do 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 if file:find(pattern, 1, true) then
return true return true
end end
@@ -74,20 +64,19 @@ function M.get_project_files(directories)
end end
scandir(abs_dir, ignore_patterns, all_files) scandir(abs_dir, ignore_patterns, all_files)
end 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 = {} local rel_files = {}
for _, f in ipairs(files) do for _, f in ipairs(all_files) do
local rel = f:gsub("^" .. root .. "/", "") local rel = f:gsub("^" .. root .. "/", "")
table.insert(rel_files, rel) table.insert(rel_files, rel)
end 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 return structure
end end
@@ -95,13 +84,18 @@ function M.get_file_contents(files)
local root = vim.fn.getcwd() local root = vim.fn.getcwd()
local sections = {} local sections = {}
for _, f in ipairs(files) do 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 if fd then
local stat = uv.fs_fstat(fd) local stat = uv.fs_fstat(fd)
if stat then
local data = uv.fs_read(fd, stat.size, 0) local data = uv.fs_read(fd, stat.size, 0)
uv.fs_close(fd) uv.fs_close(fd)
if data then if data then
table.insert(sections, "\n<<<CGPT Current File\n" .. (root .. "/" .. f) .. "\n" .. data .. "\n<<<CGPT Current File END\n") 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 end
end end

View File

@@ -1,7 +1,3 @@
-- lua/chatgpt_nvim/handler.lua
-- No major changes needed, just ensure it can be reused for the new command.
-- Ensuring we have get_clipboard_content and write_file as is.
local M = {} local M = {}
function M.get_clipboard_content() function M.get_clipboard_content()
@@ -11,7 +7,7 @@ end
function M.write_file(filepath, content) function M.write_file(filepath, content)
local fd = vim.loop.fs_open(filepath, "w", 438) local fd = vim.loop.fs_open(filepath, "w", 438)
if not fd then if not fd then
vim.api.nvim_err_writeln("Could not open file: " .. filepath) vim.api.nvim_err_writeln("Could not open file for writing: " .. filepath)
return return
end end
vim.loop.fs_write(fd, content, -1) vim.loop.fs_write(fd, content, -1)

View File

@@ -1,14 +1,3 @@
-- lua/chatgpt_nvim/init.lua
-- Modified to:
-- 1) Use YAML for config and response parsing.
-- 2) Assume ChatGPT response is YAML of form:
-- files:
-- - path: "somefile.lua"
-- content: |
-- multi line
-- content
--
local M = {} local M = {}
local context = require('chatgpt_nvim.context') local context = require('chatgpt_nvim.context')
@@ -21,10 +10,9 @@ local function copy_to_clipboard(text)
vim.fn.setreg('+', text) vim.fn.setreg('+', text)
end end
-- Parse the response from ChatGPT in YAML.
local function parse_response(raw) local function parse_response(raw)
if not ok_yaml then if not ok_yaml then
vim.api.nvim_err_writeln("lyaml is not available. Please install lyaml for YAML parsing.") vim.api.nvim_err_writeln("lyaml not available. Install with `luarocks install lyaml`.")
return nil return nil
end end
local ok, data = pcall(lyaml.load, raw) local ok, data = pcall(lyaml.load, raw)
@@ -32,7 +20,6 @@ local function parse_response(raw)
vim.api.nvim_err_writeln("Failed to parse YAML response.") vim.api.nvim_err_writeln("Failed to parse YAML response.")
return nil return nil
end end
-- lyaml.load returns a list of documents. We assume only one document is given.
data = data[1] data = data[1]
return data return data
end end
@@ -53,21 +40,17 @@ function M.run_chatgpt_command()
local readme_content = context.get_readme_content() local readme_content = context.get_readme_content()
local sections = { local sections = {
conf.initial_prompt .. "\n", conf.initial_prompt .. "\n" .. user_input,
user_input, "\n\nProject Structure:\n",
"\n\nproject structure:\n",
project_structure, project_structure,
"\n\nproject files:\n" "\n\nFiels:\n",
} }
-- Add all other files in configured directories
table.insert(sections, file_sections) table.insert(sections, file_sections)
local prompt = table.concat(sections, "\n") local prompt = table.concat(sections, "\n")
-- Copy prompt to clipboard
copy_to_clipboard(prompt) copy_to_clipboard(prompt)
print("Prompt copied to clipboard! Please paste it into the ChatGPT O1 model and get the YAML response.") print("Prompt copied to clipboard! Paste it into the ChatGPT O1 model.")
end end
function M.run_chatgpt_paste_command() function M.run_chatgpt_paste_command()

View File

@@ -1,5 +1,2 @@
" plugin/chatgpt.vim
" Add commands for ChatGPT and ChatGPTPaste
command! ChatGPT lua require('chatgpt_nvim').run_chatgpt_command() command! ChatGPT lua require('chatgpt_nvim').run_chatgpt_command()
command! ChatGPTPaste lua require('chatgpt_nvim').run_chatgpt_paste_command() command! ChatGPTPaste lua require('chatgpt_nvim').run_chatgpt_paste_command()