fix: load config just once per call
This commit is contained in:
@@ -12,7 +12,7 @@ local function copy_to_clipboard(text)
|
||||
vim.fn.setreg('+', text)
|
||||
end
|
||||
|
||||
-- We now expect 'conf' as a parameter, instead of loading config inside parse_response.
|
||||
-- Expecting 'conf' instead of loading config in parse_response:
|
||||
local function parse_response(raw, conf)
|
||||
if not ok_yaml then
|
||||
vim.api.nvim_err_writeln("lyaml not available. Install with `luarocks install lyaml`.")
|
||||
@@ -54,7 +54,7 @@ local function is_directory(path)
|
||||
return stat and stat.type == "directory"
|
||||
end
|
||||
|
||||
-- We now expect 'conf' as a parameter, instead of loading config inside handle_step_by_step_if_needed.
|
||||
-- Expect 'conf' instead of loading config in handle_step_by_step_if_needed:
|
||||
local function handle_step_by_step_if_needed(prompt, conf)
|
||||
local length = #prompt
|
||||
if not conf.enable_step_by_step or length <= (conf.prompt_char_limit or 8000) then
|
||||
@@ -72,7 +72,7 @@ local function close_existing_buffer_by_name(pattern)
|
||||
end
|
||||
end
|
||||
|
||||
local function preview_changes(changes)
|
||||
local function preview_changes(changes, conf)
|
||||
close_existing_buffer_by_name("ChatGPT_Changes_Preview$")
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr, "ChatGPT_Changes_Preview")
|
||||
@@ -100,7 +100,7 @@ local function preview_changes(changes)
|
||||
vim.cmd("buffer " .. bufnr)
|
||||
end
|
||||
|
||||
local function partial_accept(changes)
|
||||
local function partial_accept(changes, conf)
|
||||
close_existing_buffer_by_name("ChatGPT_Partial_Accept$")
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr, "ChatGPT_Partial_Accept")
|
||||
@@ -201,7 +201,7 @@ local function partial_accept(changes)
|
||||
return final_changes
|
||||
end
|
||||
|
||||
local function store_prompt_for_reference(prompt)
|
||||
local function store_prompt_for_reference(prompt, conf)
|
||||
close_existing_buffer_by_name("ChatGPT_Generated_Prompt$")
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr, "ChatGPT_Generated_Prompt")
|
||||
@@ -221,27 +221,7 @@ local function store_prompt_for_reference(prompt)
|
||||
vim.cmd("buffer " .. bufnr)
|
||||
end
|
||||
|
||||
local function grep_in_file(search_string, filepath)
|
||||
local content = read_file(filepath)
|
||||
if not content then
|
||||
return "Could not read file: " .. filepath
|
||||
end
|
||||
local results = {}
|
||||
local line_num = 0
|
||||
for line in content:gmatch("([^\n]*)\n?") do
|
||||
line_num = line_num + 1
|
||||
if line:find(search_string, 1, true) then
|
||||
table.insert(results, filepath .. ":" .. line_num .. ":" .. line)
|
||||
end
|
||||
end
|
||||
if #results == 0 then
|
||||
return "No matches in " .. filepath
|
||||
else
|
||||
return table.concat(results, "\n")
|
||||
end
|
||||
end
|
||||
|
||||
local function execute_debug_command(cmd)
|
||||
local function execute_debug_command(cmd, conf)
|
||||
if type(cmd) ~= "table" or not cmd.command then
|
||||
return "Invalid command object."
|
||||
end
|
||||
@@ -277,6 +257,21 @@ local function execute_debug_command(cmd)
|
||||
end
|
||||
handle:close()
|
||||
local results = {}
|
||||
local function grep_in_file(search_string, filepath)
|
||||
local content = read_file(filepath)
|
||||
if not content then
|
||||
return "Could not read file: " .. filepath
|
||||
end
|
||||
local lines = {}
|
||||
local line_num = 0
|
||||
for line in content:gmatch("([^\n]*)\n?") do
|
||||
line_num = line_num + 1
|
||||
if line:find(search_string, 1, true) then
|
||||
table.insert(lines, filepath .. ":" .. line_num .. ":" .. line)
|
||||
end
|
||||
end
|
||||
return (#lines == 0) and ("No matches in " .. filepath) or table.concat(lines, "\n")
|
||||
end
|
||||
for _, f in ipairs(all_files) do
|
||||
local fstat = vim.loop.fs_stat(f)
|
||||
if fstat and fstat.type == "file" then
|
||||
@@ -285,6 +280,21 @@ local function execute_debug_command(cmd)
|
||||
end
|
||||
return table.concat(results, "\n")
|
||||
else
|
||||
local function grep_in_file(search_string, filepath)
|
||||
local content = read_file(filepath)
|
||||
if not content then
|
||||
return "Could not read file: " .. filepath
|
||||
end
|
||||
local lines = {}
|
||||
local line_num = 0
|
||||
for line in content:gmatch("([^\n]*)\n?") do
|
||||
line_num = line_num + 1
|
||||
if line:find(search_string, 1, true) then
|
||||
table.insert(lines, filepath .. ":" .. line_num .. ":" .. line)
|
||||
end
|
||||
end
|
||||
return (#lines == 0) and ("No matches in " .. filepath) or table.concat(lines, "\n")
|
||||
end
|
||||
return grep_in_file(pattern, target)
|
||||
end
|
||||
else
|
||||
@@ -293,12 +303,12 @@ local function execute_debug_command(cmd)
|
||||
end
|
||||
|
||||
function M.run_chatgpt_command()
|
||||
-- Load the config once here for the entire command.
|
||||
local conf = config.load()
|
||||
ui.setup_ui(conf)
|
||||
ui.debug_log("Running :ChatGPT command.")
|
||||
local dirs = conf.directories or {"."}
|
||||
if conf.interactive_file_selection then
|
||||
dirs = ui.pick_directories(dirs)
|
||||
dirs = ui.pick_directories(dirs, conf)
|
||||
if #dirs == 0 then
|
||||
dirs = conf.directories
|
||||
end
|
||||
@@ -329,7 +339,7 @@ function M.run_chatgpt_command()
|
||||
return
|
||||
end
|
||||
|
||||
local project_structure = context.get_project_structure(dirs)
|
||||
local project_structure = context.get_project_structure(dirs, conf)
|
||||
local initial_files = conf.initial_files or {}
|
||||
local included_sections = {}
|
||||
|
||||
@@ -337,7 +347,7 @@ function M.run_chatgpt_command()
|
||||
local root = vim.fn.getcwd()
|
||||
local full_path = root .. "/" .. item
|
||||
if is_directory(full_path) then
|
||||
local dir_files = context.get_project_files({item})
|
||||
local dir_files = context.get_project_files({item}, conf)
|
||||
for _, f in ipairs(dir_files) do
|
||||
local path = root .. "/" .. f
|
||||
local data = read_file(path)
|
||||
@@ -371,7 +381,7 @@ function M.run_chatgpt_command()
|
||||
end
|
||||
|
||||
local prompt = table.concat(initial_sections, "\n")
|
||||
store_prompt_for_reference(prompt)
|
||||
store_prompt_for_reference(prompt, conf)
|
||||
|
||||
local chunks = handle_step_by_step_if_needed(prompt, conf)
|
||||
copy_to_clipboard(chunks[1])
|
||||
@@ -390,15 +400,15 @@ end
|
||||
|
||||
function M.run_chatgpt_paste_command()
|
||||
local conf = config.load()
|
||||
ui.setup_ui(conf)
|
||||
ui.debug_log("Running :ChatGPTPaste command.")
|
||||
print("Reading ChatGPT YAML response from clipboard...")
|
||||
local raw = handler.get_clipboard_content()
|
||||
local raw = handler.get_clipboard_content(conf)
|
||||
if raw == "" then
|
||||
vim.api.nvim_err_writeln("Clipboard is empty. Please copy the YAML response from ChatGPT first.")
|
||||
return
|
||||
end
|
||||
|
||||
-- Pass the loaded config into parse_response, so we don’t load config.lua again.
|
||||
local data = parse_response(raw, conf)
|
||||
if not data then
|
||||
return
|
||||
@@ -407,7 +417,7 @@ function M.run_chatgpt_paste_command()
|
||||
if data.commands and conf.enable_debug_commands then
|
||||
local results = {}
|
||||
for _, cmd in ipairs(data.commands) do
|
||||
table.insert(results, execute_debug_command(cmd))
|
||||
table.insert(results, execute_debug_command(cmd, conf))
|
||||
end
|
||||
local output = table.concat(results, "\n\n")
|
||||
copy_to_clipboard(output)
|
||||
@@ -432,7 +442,7 @@ function M.run_chatgpt_paste_command()
|
||||
|
||||
if is_final then
|
||||
if conf.preview_changes then
|
||||
preview_changes(data.files)
|
||||
preview_changes(data.files, conf)
|
||||
print("Close the preview window to apply changes, or use :q to cancel.")
|
||||
local closed = vim.wait(60000, function()
|
||||
local bufs = vim.api.nvim_list_bufs()
|
||||
@@ -452,7 +462,7 @@ function M.run_chatgpt_paste_command()
|
||||
|
||||
local final_files = data.files
|
||||
if conf.partial_acceptance then
|
||||
final_files = partial_accept(data.files)
|
||||
final_files = partial_accept(data.files, conf)
|
||||
if #final_files == 0 then
|
||||
vim.api.nvim_err_writeln("No changes remain after partial acceptance. Aborting.")
|
||||
return
|
||||
@@ -473,11 +483,11 @@ function M.run_chatgpt_paste_command()
|
||||
|
||||
if fileinfo.delete == true then
|
||||
ui.debug_log("Deleting file: " .. fileinfo.path)
|
||||
handler.delete_file(fileinfo.path)
|
||||
handler.delete_file(fileinfo.path, conf)
|
||||
print("Deleted: " .. fileinfo.path)
|
||||
elseif fileinfo.content then
|
||||
ui.debug_log("Writing file: " .. fileinfo.path)
|
||||
handler.write_file(fileinfo.path, fileinfo.content)
|
||||
handler.write_file(fileinfo.path, fileinfo.content, conf)
|
||||
print("Wrote: " .. fileinfo.path)
|
||||
else
|
||||
vim.api.nvim_err_writeln("Invalid file entry. Must have 'content' or 'delete'.")
|
||||
@@ -537,18 +547,19 @@ end
|
||||
|
||||
function M.run_chatgpt_current_buffer_command()
|
||||
local conf = config.load()
|
||||
ui.setup_ui(conf)
|
||||
ui.debug_log("Running :ChatGPTCurrentBuffer command.")
|
||||
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
|
||||
local user_input = table.concat(lines, "\n")
|
||||
local dirs = conf.directories or {"."}
|
||||
if conf.interactive_file_selection then
|
||||
dirs = ui.pick_directories(dirs)
|
||||
dirs = ui.pick_directories(dirs, conf)
|
||||
if #dirs == 0 then
|
||||
dirs = conf.directories
|
||||
end
|
||||
end
|
||||
|
||||
local project_structure = context.get_project_structure(dirs)
|
||||
local project_structure = context.get_project_structure(dirs, conf)
|
||||
local initial_files = conf.initial_files or {}
|
||||
local included_sections = {}
|
||||
|
||||
@@ -576,7 +587,7 @@ function M.run_chatgpt_current_buffer_command()
|
||||
local root = vim.fn.getcwd()
|
||||
local full_path = root .. "/" .. item
|
||||
if is_directory(full_path) then
|
||||
local dir_files = context.get_project_files({item})
|
||||
local dir_files = context.get_project_files({item}, conf)
|
||||
for _, f in ipairs(dir_files) do
|
||||
local path = root .. "/" .. f
|
||||
local data = read_file(path)
|
||||
@@ -631,7 +642,7 @@ function M.run_chatgpt_current_buffer_command()
|
||||
vim.cmd("buffer " .. bufnr_ref)
|
||||
end
|
||||
|
||||
store_prompt_for_reference(prompt)
|
||||
store_prompt_for_reference(prompt, conf)
|
||||
|
||||
local chunks = handle_step_by_step_if_needed(prompt, conf)
|
||||
copy_to_clipboard(chunks[1])
|
||||
|
||||
Reference in New Issue
Block a user