initial change to tools logic
This commit is contained in:
47
lua/chatgpt_nvim/tools/replace_in_file.lua
Normal file
47
lua/chatgpt_nvim/tools/replace_in_file.lua
Normal file
@@ -0,0 +1,47 @@
|
||||
local handler = require("chatgpt_nvim.handler")
|
||||
|
||||
local M = {}
|
||||
|
||||
local function search_and_replace(original, replacements)
|
||||
-- Basic approach: do a global plain text replace for each entry
|
||||
local updated = original
|
||||
for _, r in ipairs(replacements) do
|
||||
local search_str = r.search or ""
|
||||
local replace_str = r.replace or ""
|
||||
-- Here we do a global plain text replacement
|
||||
updated = updated:gsub(search_str, replace_str)
|
||||
end
|
||||
return updated
|
||||
end
|
||||
|
||||
M.run = function(tool_call, conf, prompt_user_tool_accept, is_subpath, read_file)
|
||||
local path = tool_call.path
|
||||
local replacements = tool_call.replacements or {}
|
||||
|
||||
if not path or #replacements == 0 then
|
||||
return "[replace_in_file] Missing 'path' or 'replacements'."
|
||||
end
|
||||
local root = vim.fn.getcwd()
|
||||
|
||||
if not is_subpath(root, path) then
|
||||
return string.format("Tool [replace_in_file for '%s'] REJECTED. Path outside project root.", path)
|
||||
end
|
||||
|
||||
local orig_data = read_file(path)
|
||||
if not orig_data then
|
||||
return string.format("[replace_in_file for '%s'] FAILED. Could not read file.", path)
|
||||
end
|
||||
|
||||
local updated_data = search_and_replace(orig_data, replacements)
|
||||
handler.write_file(path, updated_data, conf)
|
||||
|
||||
local msg = {}
|
||||
msg[#msg+1] = string.format("[replace_in_file for '%s'] Result:\nThe content was successfully saved to %s.", path, path)
|
||||
msg[#msg+1] = "\nHere is the full, updated content of the file that was saved:\n"
|
||||
msg[#msg+1] = string.format("<final_file_content path=\"%s\">\n%s\n</final_file_content>", path, updated_data)
|
||||
msg[#msg+1] = "\nIMPORTANT: For any future changes to this file, use the final_file_content shown above as your reference.\n"
|
||||
|
||||
return table.concat(msg, "")
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user