code-refactor

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Code 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
    code-execution
    skill
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+个文件:自动使用
    code-execution
    skill
执行示例(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

undefined
undefined

When 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 context
Grep(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 none
Grep(pattern="getUserData", output_mode="files_with_matches")  # 应无返回结果

Workflow Examples

工作流示例

Rename Function

重命名函数

  1. Find:
    Grep(pattern="getUserData", output_mode="files_with_matches")
  2. Count: "Found 15 occurrences in 5 files"
  3. Replace in each file with
    replace_all=true
  4. Verify: Re-run Grep
  5. Suggest: Run tests
  1. 查找:
    Grep(pattern="getUserData", output_mode="files_with_matches")
  2. 统计:"在5个文件中找到15处匹配"
  3. 在每个文件中使用
    replace_all=true
    进行替换
  4. 验证:重新运行Grep
  5. 建议:运行测试

Replace Deprecated Pattern

替换已废弃模式

  1. Find:
    Grep(pattern="\\bvar\\s+\\w+", output_mode="content", -n=true)
  2. Analyze: Check if reassigned (let) or constant (const)
  3. Replace:
    Edit(old_string="var count = 0", new_string="let count = 0")
  4. Verify:
    npm run lint
  1. 查找:
    Grep(pattern="\\bvar\\s+\\w+", output_mode="content", -n=true)
  2. 分析:检查变量是否会被重新赋值(用let)或为常量(用const)
  3. 替换:
    Edit(old_string="var count = 0", new_string="let count = 0")
  4. 验证:
    npm run lint

Update API Calls

更新API调用

  1. Find:
    Grep(pattern="/api/auth/login", output_mode="content", -n=true)
  2. Replace:
    Edit(old_string="'/api/auth/login'", new_string="'/api/v2/authentication/login'", replace_all=true)
  3. Test: Recommend integration tests
  1. 查找:
    Grep(pattern="/api/auth/login", output_mode="content", -n=true)
  2. 替换:
    Edit(old_string="'/api/auth/login'", new_string="'/api/v2/authentication/login'", replace_all=true)
  3. 测试:建议运行集成测试

Best Practices

最佳实践

Planning:
  • Find all instances first
  • Review context of each match
  • Inform user of scope
  • Consider edge cases (strings, comments)
Safe Process:
  1. Search → Find all
  2. Analyze → Verify appropriate
  3. Inform → Tell user scope
  4. Execute → Make changes
  5. Verify → Confirm applied
  6. Test → Suggest running tests
Edge Cases:
  • Strings/comments: Ask if should update
  • Exported APIs: Warn of breaking changes
  • Case sensitivity: Be explicit
规划阶段:
  • 先查找所有实例
  • 查看每个匹配项的上下文
  • 告知用户变更范围
  • 考虑边缘情况(字符串、注释)
安全流程:
  1. 搜索 → 查找所有匹配项
  2. 分析 → 验证变更的合理性
  3. 告知 → 向用户说明变更范围
  4. 执行 → 进行变更
  5. 验证 → 确认变更已应用
  6. 测试 → 建议运行测试
边缘情况:
  • 字符串/注释:询问是否需要更新
  • 已导出的API:警告可能存在破坏性变更
  • 大小写敏感性:明确说明

Tool Reference

工具参考

Edit with replace_all:
  • replace_all=true
    : Replace all occurrences
  • replace_all=false
    : Replace only first (or fail if multiple)
  • Must match EXACTLY (whitespace, quotes)
Grep patterns:
  • -n=true
    : Show line numbers
  • -B=N, -A=N
    : Context lines
  • -i=true
    : Case-insensitive
  • type="py"
    : Filter by file type
带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:规划大型重构任务