43 lines
1.4 KiB
Lua
43 lines
1.4 KiB
Lua
local handler = require("chatgpt_nvim.handler")
|
|
local lint = require("chatgpt_nvim.tools.lint")
|
|
|
|
local M = {}
|
|
|
|
M.run = function(tool_call, conf, prompt_user_tool_accept, is_subpath, read_file)
|
|
local path = tool_call.path
|
|
local new_content = tool_call.content
|
|
|
|
if not path or not new_content then
|
|
return "[edit_file] Missing 'path' or 'content'."
|
|
end
|
|
local root = vim.fn.getcwd()
|
|
|
|
if not is_subpath(root, path) then
|
|
return string.format("Tool [edit_file for '%s'] REJECTED. Path outside project root.", path)
|
|
end
|
|
|
|
-- 1) Write the new content
|
|
handler.write_file(path, new_content, conf)
|
|
|
|
local msg = {}
|
|
msg[#msg+1] = string.format("Tool [edit_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, new_content)
|
|
msg[#msg+1] = "\nIMPORTANT: For any future changes to this file, use the final_file_content shown above as your reference.\n"
|
|
|
|
-- 2) Lint check if enabled
|
|
if conf.auto_lint then
|
|
local output, err = lint.lint_file(path)
|
|
if output then
|
|
msg[#msg+1] = "\n--- Lint Results ---\n"
|
|
msg[#msg+1] = output
|
|
else
|
|
msg[#msg+1] = "\n(Lint) " .. (err or "Could not lint this file.")
|
|
end
|
|
end
|
|
|
|
return table.concat(msg, "")
|
|
end
|
|
|
|
return M
|