fix: change to a normal buffer

This commit is contained in:
2024-12-23 17:02:30 +01:00
parent eb19c4144f
commit a354ff080a

View File

@@ -62,105 +62,114 @@ 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 ---------------------------------------------------------------------------
-- Updated run_chatgpt_command using BufWriteCmd to avoid creating a file --
---------------------------------------------------------------------------
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
-- Create a new scratch buffer for user to type the message -- Create a normal, listed buffer so :w / :wq will work
local bufnr = vim.api.nvim_create_buf(false, true) local bufnr = vim.api.nvim_create_buf(false, false)
vim.api.nvim_buf_set_name(bufnr, "ChatGPT_Prompt") -- Assign a filename so Vim treats it like a normal file, but we intercept writes
vim.api.nvim_buf_set_option(bufnr, 'bufhidden', 'wipe') vim.api.nvim_buf_set_name(bufnr, "ChatGPT_Prompt.md")
vim.api.nvim_buf_set_option(bufnr, 'filetype', 'markdown') vim.api.nvim_buf_set_option(bufnr, "filetype", "markdown")
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
vim.api.nvim_buf_set_option(bufnr, "buftype", "")
vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
-- Set some initial placeholder lines
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {
"# Enter your prompt below.", "# Enter your prompt below.",
"", "",
"Save & close with :wq to finalize your prompt." "Save & close with :wq to finalize your prompt."
}) })
local function on_write_post() -- Intercept the write so that no file is actually created on disk
-- Read buffer lines and join them into a single string vim.api.nvim_create_autocmd("BufWriteCmd", {
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) buffer = bufnr,
-- Skip the first lines that are placeholders, if desired callback = function()
local user_input = table.concat(lines, "\n") -- Gather lines
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local user_input = table.concat(lines, "\n")
if user_input == "" or user_input:find("^# Enter your prompt below.") then -- Basic check to ensure user actually wrote something
print("No valid input provided.") if user_input == "" or user_input:find("^# Enter your prompt below.") then
return vim.api.nvim_out_write("No valid input provided.\n")
end -- Mark buffer as unmodified, so :wq can still exit
vim.api.nvim_buf_set_option(bufnr, "modified", false)
return
end
-- Continue the same logic as originally, just using our new user_input -- Build the prompt using the 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)
local initial_files = conf.initial_files or {} local initial_files = conf.initial_files or {}
local included_sections = {} local included_sections = {}
if #initial_files > 0 then if #initial_files > 0 then
table.insert(included_sections, "\n\nIncluded files and directories (pre-selected):\n") table.insert(included_sections, "\n\nIncluded files and directories (pre-selected):\n")
local root = vim.fn.getcwd() local root = vim.fn.getcwd()
for _, item in ipairs(initial_files) do for _, item in ipairs(initial_files) do
local full_path = root .. "/" .. item local full_path = root .. "/" .. item
if is_directory(full_path) then if is_directory(full_path) then
local dir_files = context.get_project_files({item}) local dir_files = context.get_project_files({item})
for _, f in ipairs(dir_files) do for _, f in ipairs(dir_files) do
local path = root .. "/" .. f local path = root .. "/" .. f
local data = read_file(path) local data = read_file(path)
if data then if data then
table.insert(included_sections, "\nFile: `" .. f .. "`\n```\n" .. data .. "\n```\n") table.insert(included_sections, "\nFile: `" .. f .. "`\n```\n" .. data .. "\n```\n")
end
end
else
local data = read_file(full_path)
if data then
table.insert(included_sections, "\nFile: `" .. item .. "`\n```\n" .. data .. "\n```\n")
end end
end
else
local data = read_file(full_path)
if data then
table.insert(included_sections, "\nFile: `" .. item .. "`\n```\n" .. data .. "\n```\n")
end end
end end
end end
end
local initial_sections = { local initial_sections = {
"### Basic Prompt Instructions:\n", "### Basic Prompt Instructions:\n",
conf.initial_prompt .. "\n\n\n", conf.initial_prompt .. "\n\n\n",
"### User Instructions:\n", "### User Instructions:\n",
user_input .. "\n\n\n", user_input .. "\n\n\n",
"### Context/Data:\n", "### Context/Data:\n",
"Project name: " .. (conf.project_name or "") .. "\n", "Project name: " .. (conf.project_name or "") .. "\n",
"Project Structure:\n", "Project Structure:\n",
project_structure, project_structure,
table.concat(included_sections, "\n") table.concat(included_sections, "\n")
} }
local prompt = table.concat(initial_sections, "\n") local prompt = table.concat(initial_sections, "\n")
local token_limit = conf.token_limit or 8000 local token_limit = conf.token_limit or 8000
local token_count = estimate_tokens(prompt) local token_count = estimate_tokens(prompt)
if conf.debug then if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:init] Prompt token count: " .. token_count .. "\n") vim.api.nvim_out_write("[chatgpt_nvim:init] Prompt token count: " .. token_count .. "\n")
end end
if token_count > token_limit then if token_count > token_limit then
print("Too many files in project structure. The request exceeds the O1 model limit of " .. token_limit .. " tokens.") vim.api.nvim_out_write("Too many files in project structure. The request exceeds the O1 model limit of " .. token_limit .. " tokens.\n")
return else
end copy_to_clipboard(prompt)
vim.api.nvim_out_write("Prompt (requesting needed files) copied to clipboard! Paste it into the ChatGPT O1 model.\n")
end
copy_to_clipboard(prompt) -- Mark as unmodified so :wq won't complain
print("Prompt (requesting needed files) copied to clipboard! Paste it into the ChatGPT O1 model.") vim.api.nvim_buf_set_option(bufnr, "modified", false)
end 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 -- Switch to the newly created buffer
vim.cmd("buffer " .. bufnr) vim.cmd("buffer " .. bufnr)
end end
---------------------------------------------------------------------------
function M.run_chatgpt_paste_command() function M.run_chatgpt_paste_command()
local conf = config.load() local conf = config.load()