feat: changed to and interactive workflow
This commit is contained in:
@@ -21,7 +21,6 @@ function M.get_project_structure()
|
||||
end
|
||||
end
|
||||
|
||||
-- Fallback if not in a git repo
|
||||
local dhandle = io.popen("find . -type f")
|
||||
if dhandle then
|
||||
local dresult = dhandle:read("*a")
|
||||
@@ -32,26 +31,21 @@ function M.get_project_structure()
|
||||
return "No files found."
|
||||
end
|
||||
|
||||
-- Helper to find the git root directory
|
||||
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()
|
||||
if root and root ~= "" then
|
||||
return root
|
||||
end
|
||||
return root
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Attempt to read README.md from the root of the git repository
|
||||
function M.get_readme_content()
|
||||
local root = get_git_root()
|
||||
if not root then
|
||||
return nil
|
||||
end
|
||||
|
||||
local readme_path = root .. "/README.md"
|
||||
local f = io.open(readme_path, "r")
|
||||
if f then
|
||||
|
||||
@@ -1,39 +1,13 @@
|
||||
local M = {}
|
||||
|
||||
-- paste_code_from_clipboard:
|
||||
-- The user copies a code block from the website (which includes a first line with the file path),
|
||||
-- and then runs :ChatGPTPaste.
|
||||
-- We retrieve the clipboard content, extract the first line as a path, and the rest as code.
|
||||
function M.paste_code_from_clipboard()
|
||||
local clipboard_content = vim.fn.getreg('+')
|
||||
if clipboard_content == "" then
|
||||
vim.api.nvim_err_writeln("Clipboard is empty. Copy the code block from the website first.")
|
||||
return
|
||||
end
|
||||
|
||||
local lines = {}
|
||||
for line in clipboard_content:gmatch("[^\r\n]+") do
|
||||
table.insert(lines, line)
|
||||
end
|
||||
|
||||
if #lines == 0 then
|
||||
vim.api.nvim_err_writeln("No content in clipboard to parse.")
|
||||
return
|
||||
end
|
||||
|
||||
local filepath = lines[1]
|
||||
local code_lines = {}
|
||||
for i = 2, #lines do
|
||||
table.insert(code_lines, lines[i])
|
||||
end
|
||||
|
||||
local code = table.concat(code_lines, "\n")
|
||||
M.write_file(filepath, code)
|
||||
print("Wrote file: " .. filepath)
|
||||
-- Retrieves clipboard content and trims trailing whitespace/newlines
|
||||
function M.get_clipboard_content()
|
||||
local content = vim.fn.getreg('+')
|
||||
content = content:gsub("%s+$", "")
|
||||
return content
|
||||
end
|
||||
|
||||
function M.write_file(filepath, content)
|
||||
-- Create directories if needed
|
||||
local dir = filepath:match("(.*/)")
|
||||
if dir and dir ~= "" then
|
||||
vim.fn.mkdir(dir, "p")
|
||||
@@ -48,4 +22,8 @@ function M.write_file(filepath, content)
|
||||
end
|
||||
end
|
||||
|
||||
function M.finish()
|
||||
print("All files processed. You can now continue working.")
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -1,39 +1,89 @@
|
||||
local M = {}
|
||||
|
||||
local context = require('chatgpt_nvim.context')
|
||||
local handler = require('chatgpt_nvim.handler')
|
||||
|
||||
function M.run_chatgpt_command()
|
||||
vim.schedule(function()
|
||||
-- Prompt the user for input
|
||||
local user_input = vim.fn.input("Message for O1 Model: ")
|
||||
if user_input == "" then
|
||||
print("No input provided.")
|
||||
return
|
||||
-- Prompt the user for input
|
||||
local user_input = vim.fn.input("Message for O1 Model: ")
|
||||
if user_input == "" then
|
||||
print("No input provided.")
|
||||
return
|
||||
end
|
||||
|
||||
local project_structure = context.get_project_structure()
|
||||
local current_file_content, current_file_path = context.get_current_file()
|
||||
local readme_content = context.get_readme_content()
|
||||
|
||||
local sections = {
|
||||
"Following you will get instructions for a development project. First you get a prompt of the user which starts with \"<<<CGP User Message\" in a line and ends with \"<<<CGP User Message End\" in a line.",
|
||||
"\nThen you get the project structure which starts with \"<<<CGPT Project Structure\" in a line and ends with \"<<<CGPT Project Structure End\" in a line",
|
||||
"\nThen you get the current file for context. It contains at the first line the file path, followed by its content.It starts with \"<<<CGPT Current File\" in a line and ends with \"<<<CGPT Current File End\" in a line",
|
||||
"\nLastly you get the content of the README.md for context if it is present. It starts with \"<<<CGPT README\" in a line and ends with \"<<<CGPT README End\" in a line",
|
||||
"\nReturn code blocks for the changes.",
|
||||
"\nIf it makes sense, also update the README.md to reflect the changes made.",
|
||||
"\n\n<<<CGPT User Message\n",
|
||||
user_input,
|
||||
"\n<<<CGPT User Message END",
|
||||
"\n\n<<<CGPT Project Structure\n",
|
||||
project_structure,
|
||||
"\n<<<CGPT Project Structure END",
|
||||
"\n\n<<<CGPT Current File\n",
|
||||
current_file_path .. "\n" .. current_file_content,
|
||||
"\n\n<<<CGPT Current File END\n",
|
||||
}
|
||||
|
||||
if readme_content then
|
||||
table.insert(sections, "\n\n<<<CGPT README\n" .. readme_content .. "\n<<<CGPT README END")
|
||||
end
|
||||
|
||||
local prompt = table.concat(sections, "\n")
|
||||
|
||||
-- Copy prompt to clipboard so user can paste it into the website form.
|
||||
vim.fn.setreg('+', prompt)
|
||||
print("Prompt copied to clipboard! Please paste it into the website form and get the response from the ChatGPT O1 model.")
|
||||
print("Press <Enter> 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
|
||||
|
||||
while true do
|
||||
-- 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 <Enter> 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
|
||||
|
||||
local project_structure = context.get_project_structure()
|
||||
local current_file_content, current_file_path = context.get_current_file()
|
||||
local readme_content = context.get_readme_content()
|
||||
|
||||
local sections = {
|
||||
"User Message:\n" .. user_input,
|
||||
"\nProject Structure:\n" .. project_structure,
|
||||
"\nCurrent File:\nPath: " .. current_file_path .. "\n" .. current_file_content,
|
||||
"\nInstructions:\nPlease return code blocks for the changes. Each code block must start with the file path on the first line, followed by the code.",
|
||||
"Do not comment out the file path. Just plain text.",
|
||||
"If multiple files are needed, return multiple code blocks each starting with a file path."
|
||||
}
|
||||
|
||||
if readme_content then
|
||||
table.insert(sections, "\nREADME Content:\n" .. readme_content)
|
||||
-- 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 <Enter> 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
|
||||
|
||||
local prompt = table.concat(sections, "\n")
|
||||
handler.write_file(filepath, filecontent)
|
||||
print("Wrote file: " .. filepath)
|
||||
|
||||
-- Copy prompt to clipboard so user can paste it into the website form.
|
||||
vim.fn.setreg('+', prompt)
|
||||
print("Prompt (including README if found) copied to clipboard! Paste it into the website form.")
|
||||
end)
|
||||
local another = vim.fn.input("Do you want to paste another file? (y/n): ")
|
||||
if another:lower() ~= "y" then
|
||||
handler.finish()
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user