通过精确字符串替换编辑文件(str_replace 模式)。
使用 old_string 定位文件中唯一的目标片段并替换为 new_string。
old_string 必须在文件中唯一出现;若有多处匹配则返回错误,要求提供更多上下文。
- 有 Sandbox 时走
sandbox.edit_file/4 - 无 Sandbox 时走
CMDC.Sandbox.Local
使用示例
# 替换特定代码片段
edit_file(path: "lib/app.ex",
old_string: "def hello, do: :world",
new_string: "def hello, do: :universe"
)
# 删除一段内容(new_string 为空字符串)
edit_file(path: "lib/app.ex",
old_string: "\n # TODO: remove this line",
new_string: ""
)