code-refactor
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCode Refactor
代码重构
Systematic code refactoring across files. Auto-switches to execution mode for 10+ files (90% token savings).
跨文件的系统化代码重构。当文件数量达10个以上时自动切换至执行模式(可节省90%的token)。
Mode Selection
模式选择
- 1-9 files: Use native tools (Grep + Edit with replace_all)
- 10+ files: Automatically use skill
code-execution
Execution example (50 files):
python
from api.code_transform import rename_identifier
result = rename_identifier('.', 'oldName', 'newName', '**/*.py')- 1-9个文件:使用原生工具(Grep + 带replace_all的Edit)
- 10+个文件:自动使用skill
code-execution
执行示例(50个文件):
python
from api.code_transform import rename_identifier
result = rename_identifier('.', 'oldName', 'newName', '**/*.py')Returns: {'files_modified': 50, 'total_replacements': 247}
Returns: {'files_modified': 50, 'total_replacements': 247}
~500 tokens vs ~25,000 tokens traditional
~500 tokens vs ~25,000 tokens traditional
undefinedundefinedWhen to Use
使用场景
- "rename [identifier] to [new_name]"
- "replace all [pattern] with [replacement]"
- "refactor to use [new_pattern]"
- "update all calls to [function/API]"
- "convert [old_pattern] to [new_pattern]"
- "将[标识符]重命名为[新名称]"
- "将所有[模式]替换为[新内容]"
- "重构为使用[新模式]"
- "更新所有对[函数/API]的调用"
- "将[旧模式]转换为[新模式]"
Core Workflow (Native Mode)
核心工作流(原生模式)
1. Find All Occurrences
1. 查找所有匹配项
Grep(pattern="getUserData", output_mode="files_with_matches") # Find files
Grep(pattern="getUserData", output_mode="content", -n=true, -B=2, -A=2) # Verify with contextGrep(pattern="getUserData", output_mode="files_with_matches") # 查找文件
Grep(pattern="getUserData", output_mode="content", -n=true, -B=2, -A=2) # 结合上下文验证2. Replace All Instances
2. 替换所有实例
Edit(
file_path="src/api.js",
old_string="getUserData",
new_string="fetchUserData",
replace_all=true
)Edit(
file_path="src/api.js",
old_string="getUserData",
new_string="fetchUserData",
replace_all=true
)3. Verify Changes
3. 验证变更
Grep(pattern="getUserData", output_mode="files_with_matches") # Should return noneGrep(pattern="getUserData", output_mode="files_with_matches") # 应无返回结果Workflow Examples
工作流示例
Rename Function
重命名函数
- Find:
Grep(pattern="getUserData", output_mode="files_with_matches") - Count: "Found 15 occurrences in 5 files"
- Replace in each file with
replace_all=true - Verify: Re-run Grep
- Suggest: Run tests
- 查找:
Grep(pattern="getUserData", output_mode="files_with_matches") - 统计:"在5个文件中找到15处匹配"
- 在每个文件中使用进行替换
replace_all=true - 验证:重新运行Grep
- 建议:运行测试
Replace Deprecated Pattern
替换已废弃模式
- Find:
Grep(pattern="\\bvar\\s+\\w+", output_mode="content", -n=true) - Analyze: Check if reassigned (let) or constant (const)
- Replace:
Edit(old_string="var count = 0", new_string="let count = 0") - Verify:
npm run lint
- 查找:
Grep(pattern="\\bvar\\s+\\w+", output_mode="content", -n=true) - 分析:检查变量是否会被重新赋值(用let)或为常量(用const)
- 替换:
Edit(old_string="var count = 0", new_string="let count = 0") - 验证:
npm run lint
Update API Calls
更新API调用
- Find:
Grep(pattern="/api/auth/login", output_mode="content", -n=true) - Replace:
Edit(old_string="'/api/auth/login'", new_string="'/api/v2/authentication/login'", replace_all=true) - Test: Recommend integration tests
- 查找:
Grep(pattern="/api/auth/login", output_mode="content", -n=true) - 替换:
Edit(old_string="'/api/auth/login'", new_string="'/api/v2/authentication/login'", replace_all=true) - 测试:建议运行集成测试
Best Practices
最佳实践
Planning:
- Find all instances first
- Review context of each match
- Inform user of scope
- Consider edge cases (strings, comments)
Safe Process:
- Search → Find all
- Analyze → Verify appropriate
- Inform → Tell user scope
- Execute → Make changes
- Verify → Confirm applied
- Test → Suggest running tests
Edge Cases:
- Strings/comments: Ask if should update
- Exported APIs: Warn of breaking changes
- Case sensitivity: Be explicit
规划阶段:
- 先查找所有实例
- 查看每个匹配项的上下文
- 告知用户变更范围
- 考虑边缘情况(字符串、注释)
安全流程:
- 搜索 → 查找所有匹配项
- 分析 → 验证变更的合理性
- 告知 → 向用户说明变更范围
- 执行 → 进行变更
- 验证 → 确认变更已应用
- 测试 → 建议运行测试
边缘情况:
- 字符串/注释:询问是否需要更新
- 已导出的API:警告可能存在破坏性变更
- 大小写敏感性:明确说明
Tool Reference
工具参考
Edit with replace_all:
- : Replace all occurrences
replace_all=true - : Replace only first (or fail if multiple)
replace_all=false - Must match EXACTLY (whitespace, quotes)
Grep patterns:
- : Show line numbers
-n=true - : Context lines
-B=N, -A=N - : Case-insensitive
-i=true - : Filter by file type
type="py"
带replace_all的Edit:
- :替换所有匹配项
replace_all=true - :仅替换第一个匹配项(若存在多个则失败)
replace_all=false - 必须完全匹配(包括空格、引号)
Grep模式:
- :显示行号
-n=true - :显示上下文行数
-B=N, -A=N - :忽略大小写
-i=true - :按文件类型过滤
type="py"
Integration
集成
- test-fixing: Fix broken tests after refactoring
- code-transfer: Move refactored code
- feature-planning: Plan large refactorings
- test-fixing:重构后修复损坏的测试
- code-transfer:迁移重构后的代码
- feature-planning:规划大型重构任务