90 lines
3.5 KiB
Lua
90 lines
3.5 KiB
Lua
local M = {}
|
|
|
|
local context = require('chatgpt_nvim.context')
|
|
local handler = require('chatgpt_nvim.handler')
|
|
|
|
function M.run_chatgpt_command()
|
|
-- 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
|
|
local another = vim.fn.input("Do you want to paste another file? (y/n): ")
|
|
if another:lower() ~= "y" then
|
|
handler.finish()
|
|
break
|
|
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 <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
|
|
|
|
-- 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
|
|
|
|
handler.write_file(filepath, filecontent)
|
|
print("Wrote file: " .. filepath)
|
|
end
|
|
end
|
|
|
|
return M
|