fix: just load conf once per command

This commit is contained in:
2025-01-04 21:37:48 +01:00
parent 3505a295a9
commit 6950c542ad

View File

@@ -12,8 +12,8 @@ local function copy_to_clipboard(text)
vim.fn.setreg('+', text) vim.fn.setreg('+', text)
end end
local function parse_response(raw) -- We now expect 'conf' as a parameter, instead of loading config inside parse_response.
local conf = config.load() local function parse_response(raw, conf)
if not ok_yaml then if not ok_yaml then
vim.api.nvim_err_writeln("lyaml not available. Install with `luarocks install lyaml`.") vim.api.nvim_err_writeln("lyaml not available. Install with `luarocks install lyaml`.")
return nil return nil
@@ -54,8 +54,8 @@ local function is_directory(path)
return stat and stat.type == "directory" return stat and stat.type == "directory"
end end
local function handle_step_by_step_if_needed(prompt) -- We now expect 'conf' as a parameter, instead of loading config inside handle_step_by_step_if_needed.
local conf = config.load() local function handle_step_by_step_if_needed(prompt, conf)
local length = #prompt local length = #prompt
if not conf.enable_step_by_step or length <= (conf.prompt_char_limit or 8000) then if not conf.enable_step_by_step or length <= (conf.prompt_char_limit or 8000) then
return { prompt } return { prompt }
@@ -293,6 +293,7 @@ local function execute_debug_command(cmd)
end end
function M.run_chatgpt_command() function M.run_chatgpt_command()
-- Load the config once here for the entire command.
local conf = config.load() local conf = config.load()
ui.debug_log("Running :ChatGPT command.") ui.debug_log("Running :ChatGPT command.")
local dirs = conf.directories or {"."} local dirs = conf.directories or {"."}
@@ -372,7 +373,7 @@ function M.run_chatgpt_command()
local prompt = table.concat(initial_sections, "\n") local prompt = table.concat(initial_sections, "\n")
store_prompt_for_reference(prompt) store_prompt_for_reference(prompt)
local chunks = handle_step_by_step_if_needed(prompt) local chunks = handle_step_by_step_if_needed(prompt, conf)
copy_to_clipboard(chunks[1]) copy_to_clipboard(chunks[1])
if #chunks == 1 then if #chunks == 1 then
vim.api.nvim_out_write("Prompt copied to clipboard! Paste into ChatGPT.\n") vim.api.nvim_out_write("Prompt copied to clipboard! Paste into ChatGPT.\n")
@@ -397,7 +398,8 @@ function M.run_chatgpt_paste_command()
return return
end end
local data = parse_response(raw) -- Pass the loaded config into parse_response, so we dont load config.lua again.
local data = parse_response(raw, conf)
if not data then if not data then
return return
end end
@@ -504,7 +506,7 @@ function M.run_chatgpt_paste_command()
end end
local sections = { local sections = {
config.load().initial_prompt, conf.initial_prompt,
"\n\nProject name: " .. (conf.project_name or ""), "\n\nProject name: " .. (conf.project_name or ""),
"\n\nBelow are the requested files from the project, each preceded by its filename in backticks and enclosed in triple backticks.\n", "\n\nBelow are the requested files from the project, each preceded by its filename in backticks and enclosed in triple backticks.\n",
table.concat(file_sections, "\n"), table.concat(file_sections, "\n"),
@@ -631,7 +633,7 @@ function M.run_chatgpt_current_buffer_command()
store_prompt_for_reference(prompt) store_prompt_for_reference(prompt)
local chunks = handle_step_by_step_if_needed(prompt) local chunks = handle_step_by_step_if_needed(prompt, conf)
copy_to_clipboard(chunks[1]) copy_to_clipboard(chunks[1])
if #chunks == 1 then if #chunks == 1 then
vim.api.nvim_out_write("Prompt (from current buffer) copied to clipboard! Paste into ChatGPT.\n") vim.api.nvim_out_write("Prompt (from current buffer) copied to clipboard! Paste into ChatGPT.\n")
@@ -640,4 +642,4 @@ function M.run_chatgpt_current_buffer_command()
end end
end end
return M return M