From 78b017277208125a55a44202c459e59637f5481d Mon Sep 17 00:00:00 2001 From: Dominik Polakovics Date: Thu, 12 Dec 2024 18:57:35 +0100 Subject: [PATCH] changes for a better approach --- .chatgpt_config.yaml | 15 ++++ README.md | 39 ++++---- lua/chatgpt_nvim/config.lua | 51 +++++++++++ lua/chatgpt_nvim/context.lua | 167 +++++++++++++++++++++++++---------- lua/chatgpt_nvim/handler.lua | 28 +++--- lua/chatgpt_nvim/init.lua | 128 ++++++++++++++------------- plugin/chatgpt.vim | 5 +- 7 files changed, 285 insertions(+), 148 deletions(-) create mode 100644 .chatgpt_config.yaml create mode 100644 lua/chatgpt_nvim/config.lua diff --git a/.chatgpt_config.yaml b/.chatgpt_config.yaml new file mode 100644 index 0000000..78b7251 --- /dev/null +++ b/.chatgpt_config.yaml @@ -0,0 +1,15 @@ +initial_prompt: | + You are a coding assistant who receives a project’s context and user instructions. The user will provide a prompt, and you will return modifications to the project in a YAML structure. This YAML must have a top-level key named files, which should be a list of file changes. Each element in files must be a mapping with the keys path and content. The path should be the file path relative to the project’s root directory, and content should contain the new file content as a multiline string (using the YAML | literal style). Do not include additional commentary outside of the YAML. + Here is the structure expected in your final answer: + files: + - path: "relative/path/to/file1.ext" + content: | + + - path: "relative/path/to/file2.ext" + content: | + + Based on the prompt and project context provided, you must only return the YAML structure shown above (with actual file paths and contents substituted in). Any file that should be created or modified must appear as one of the files entries. The content should contain the complete and final code for that file, reflecting all changes requested by the user. + +directories: + - "lua" + - "plugin" diff --git a/README.md b/README.md index 21f42d7..babd30b 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,20 @@ -# ChatGPT NeoVim Plugin + +# ChatGPT NeoVim Plugin (Updated for YAML) -This plugin helps integrate the ChatGPT O1 model into the development workflow with Neovim. -It provides one main interactive command: `:ChatGPT`. +This plugin integrates a ChatGPT O1 model workflow into Neovim. It allows you to generate prompts containing: +- An **initial prompt** configured via a `.chatgpt_config.yaml` file in your project root. +- A list of directories (also specified in the `.chatgpt_config.yaml`) from which it gathers the complete project structure and file contents. +- The current file and the project's `README.md`, if present. +- It uses YAML for configuration and also expects the ChatGPT response in YAML. -## :ChatGPT +It also respects `.gitignore` entries, skipping those files from the prompt. -**Workflow:** +## Configuration -1. **Initial Prompt:** - When you run `:ChatGPT`, the plugin asks you for a prompt. After entering the prompt, it gathers context: - - The project structure based on the git repository of the currently open file. - - The `README.md` file content if it exists at the project root. - - The currently open file’s content. +Create a `.chatgpt_config.yaml` in your project root. For example: - It then constructs a combined prompt and automatically copies it to your system clipboard. You will be instructed to paste this prompt into the ChatGPT O1 model’s website. - -2. **Pasting Prompt into ChatGPT:** - Switch to your browser, paste the prompt into ChatGPT’s interface, and submit it. ChatGPT will return one or multiple code blocks. Each code block should start with the file path on the first line, followed by the code. - -3. **Interactive File Pasting:** - Return to Neovim. After pressing ``, the plugin enters an interactive mode: - - It will prompt you to copy the file path (the first line of the returned code block) into your clipboard and press ``. - - Once you do that, it will store that file path. - - Next, it will ask you to copy the file content (the rest of the returned code block) to your clipboard and press `` again. - - The plugin will then write the pasted content into the file specified by the previously stored path. - - After this file is processed, it asks if you want to paste another file. Press `y` to process another file (repeat the steps above) or `n` to finish. - -By following this interactive workflow, you can iteratively apply all changes suggested by ChatGPT O1 model directly into your project files without leaving Neovim. +```yaml +initial_prompt: "You are a helpful coding assistant that follows instructions meticulously." +directories: + - "lua" + - "plugin" diff --git a/lua/chatgpt_nvim/config.lua b/lua/chatgpt_nvim/config.lua new file mode 100644 index 0000000..934a45f --- /dev/null +++ b/lua/chatgpt_nvim/config.lua @@ -0,0 +1,51 @@ +-- 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() + local cwd = vim.fn.getcwd() + local config_path = cwd .. "/.chatgpt_config.yaml" + return config_path +end + +function M.load() + local path = get_config_path() + local fd = uv.fs_open(path, "r", 438) -- 438 = 0o666 + if not fd then + -- Return some default configuration if no file found + return { + initial_prompt = "", + directories = { "." }, + } + end + local stat = uv.fs_fstat(fd) + local data = uv.fs_read(fd, stat.size, 0) + uv.fs_close(fd) + if data and ok_yaml then + local ok, result = pcall(lyaml.load, data) + if ok and type(result) == "table" then + local config = result[1] + if type(config) == "table" then + return { + initial_prompt = config.initial_prompt or "", + directories = config.directories or { "." }, + } + end + end + end + -- Fallback if decode fails + return { + initial_prompt = "", + directories = { "." }, + } +end + +return M diff --git a/lua/chatgpt_nvim/context.lua b/lua/chatgpt_nvim/context.lua index 226d454..12c1069 100644 --- a/lua/chatgpt_nvim/context.lua +++ b/lua/chatgpt_nvim/context.lua @@ -1,61 +1,134 @@ +-- 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 = {} -function M.get_current_file() - local buf = vim.api.nvim_get_current_buf() - local path = vim.api.nvim_buf_get_name(buf) - if path == "" then - path = "untitled" - end - local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false) - local content = table.concat(lines, "\n") - return content, path -end +local uv = vim.loop -function M.get_project_structure() - local handle = io.popen("git ls-files 2>/dev/null") - if handle then - local result = handle:read("*a") - handle:close() - if result and result ~= "" then - return "Files:\n" .. result +-- 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) + if not fd then + return {} + end + local stat = uv.fs_fstat(fd) + local data = uv.fs_read(fd, stat.size, 0) + uv.fs_close(fd) + if not data then return {} end + local patterns = {} + for line in data:gmatch("[^\r\n]+") do + line = line:match("^%s*(.-)%s*$") -- trim + if line ~= "" and not line:match("^#") then + patterns[#patterns+1] = line end end - - local dhandle = io.popen("find . -type f") - if dhandle then - local dresult = dhandle:read("*a") - dhandle:close() - return "Files:\n" .. (dresult or "") - end - - return "No files found." + return patterns end -local function get_git_root() - local handle = io.popen("git rev-parse --show-toplevel 2>/dev/null") - if handle then - local root = handle:read("*l") - handle:close() - return root +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 end - return nil + return false +end + +local function scandir(dir, ignore_patterns, files) + files = files or {} + local fd = uv.fs_opendir(dir, nil, 50) + if not fd then return files end + while true do + local ents = uv.fs_readdir(fd) + 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" then + table.insert(files, fullpath) + elseif ent.type == "directory" and ent.name ~= ".git" then + scandir(fullpath, ignore_patterns, files) + end + end + end + end + uv.fs_closedir(fd) + return files +end + +function M.get_project_files(directories) + local root = vim.fn.getcwd() + local ignore_patterns = load_gitignore_patterns(root) + 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) + 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 + local rel = f:gsub("^" .. root .. "/", "") + table.insert(rel_files, rel) + end + + local structure = "Files:\n" .. table.concat(rel_files, "\n") + return structure +end + +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) + 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<< once you've pasted the prompt into the website and have the first file ready to copy.") - vim.fn.input("") -- wait for user to press Enter + -- Copy prompt to clipboard + copy_to_clipboard(prompt) + print("Prompt copied to clipboard! Please paste it into the ChatGPT O1 model and get the YAML response.") +end - while true do - local another = vim.fn.input("Do you want to paste another file? (y/n): ") - if another:lower() ~= "y" then - handler.finish() - break +function M.run_chatgpt_paste_command() + print("Reading ChatGPT YAML response from clipboard...") + local raw = handler.get_clipboard_content() + if raw == "" then + vim.api.nvim_err_writeln("Clipboard is empty. Please copy the YAML response from ChatGPT first.") + return + end + + local data = parse_response(raw) + if not data or not data.files then + vim.api.nvim_err_writeln("No 'files' field found in the YAML response.") + return + end + + for _, fileinfo in ipairs(data.files) do + if fileinfo.path and fileinfo.content then + handler.write_file(fileinfo.path, fileinfo.content) + print("Wrote file: " .. fileinfo.path) + else + vim.api.nvim_err_writeln("Invalid file entry. Must have 'path' and 'content'.") end - - -- Ask user for filepath - local filepath = "" - while true do - print("Please copy the FILE PATH (the first line of the code block) to your clipboard.") - print("Press when done.") - vim.fn.input("") -- Wait for user confirmation - filepath = handler.get_clipboard_content() - if filepath == "" then - vim.api.nvim_err_writeln("Clipboard is empty. Please copy the file path and try again.") - else - print("Got file path: " .. filepath) - break - end - end - - -- Ask user for file content - local filecontent = "" - while true do - print("Now copy the FILE CONTENT (everything after the first line) to your clipboard.") - print("Press when done.") - vim.fn.input("") -- Wait for user confirmation - filecontent = handler.get_clipboard_content() - if filecontent == "" then - vim.api.nvim_err_writeln("Clipboard is empty. Please copy the file content and try again.") - else - break - end - end - - handler.write_file(filepath, filecontent) - print("Wrote file: " .. filepath) end end diff --git a/plugin/chatgpt.vim b/plugin/chatgpt.vim index eec3a61..88e8563 100644 --- a/plugin/chatgpt.vim +++ b/plugin/chatgpt.vim @@ -1,2 +1,5 @@ -" Defines the :ChatGPT command +" plugin/chatgpt.vim +" Add commands for ChatGPT and ChatGPTPaste + command! ChatGPT lua require('chatgpt_nvim').run_chatgpt_command() +command! ChatGPTPaste lua require('chatgpt_nvim').run_chatgpt_paste_command()