feat: change to create a buffer for the prompt

This commit is contained in:
2024-12-23 15:34:36 +01:00
parent d6dc98cd58
commit eb19c4144f

View File

@@ -62,17 +62,36 @@ local function is_directory(path)
return stat and stat.type == "directory" return stat and stat.type == "directory"
end end
-- Replaces the inline input() call with opening a new buffer for prompt input
function M.run_chatgpt_command() function M.run_chatgpt_command()
local conf = config.load() local conf = config.load()
if conf.debug then if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:init] Running :ChatGPT command.\n") vim.api.nvim_out_write("[chatgpt_nvim:init] Running :ChatGPT command.\n")
end end
local user_input = vim.fn.input("Message for O1 Model: ")
if user_input == "" then -- Create a new scratch buffer for user to type the message
print("No input provided.") local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, "ChatGPT_Prompt")
vim.api.nvim_buf_set_option(bufnr, 'bufhidden', 'wipe')
vim.api.nvim_buf_set_option(bufnr, 'filetype', 'markdown')
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {
"# Enter your prompt below.",
"",
"Save & close with :wq to finalize your prompt."
})
local function on_write_post()
-- Read buffer lines and join them into a single string
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
-- Skip the first lines that are placeholders, if desired
local user_input = table.concat(lines, "\n")
if user_input == "" or user_input:find("^# Enter your prompt below.") then
print("No valid input provided.")
return return
end end
-- Continue the same logic as originally, just using our new user_input
local dirs = conf.directories or {"."} local dirs = conf.directories or {"."}
local project_structure = context.get_project_structure(dirs) local project_structure = context.get_project_structure(dirs)
@@ -130,6 +149,17 @@ function M.run_chatgpt_command()
copy_to_clipboard(prompt) copy_to_clipboard(prompt)
print("Prompt (requesting needed files) copied to clipboard! Paste it into the ChatGPT O1 model.") print("Prompt (requesting needed files) copied to clipboard! Paste it into the ChatGPT O1 model.")
end
-- Create an autocmd that triggers once when user saves the buffer (BufWritePost)
vim.api.nvim_create_autocmd("BufWritePost", {
buffer = bufnr,
once = true,
callback = on_write_post
})
-- Switch to the newly created buffer
vim.cmd("buffer " .. bufnr)
end end
function M.run_chatgpt_paste_command() function M.run_chatgpt_paste_command()