Code Refactor
Systematic code refactoring across files. Auto-switches to execution mode for 10+ files (90% token savings).
Mode Selection
- 1-9 files: Use native tools (Grep + Edit with replace_all)
- 10+ files: Automatically use
code-execution skill
Execution example (50 files):
from api.code_transform import rename_identifier
result = rename_identifier('.', 'oldName', 'newName', '**/*.py')
# Returns: {'files_modified': 50, 'total_replacements': 247}
# ~500 tokens vs ~25,000 tokens traditional
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]"
Core Workflow (Native Mode)
1. Find All Occurrences
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
2. Replace All Instances
Edit(
file_path="src/api.js",
old_string="getUserData",
new_string="fetchUserData",
replace_all=true
)
3. Verify Changes
Grep(pattern="getUserData", output_mode="files_with_matches") # Should return none
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
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
Update API Calls
- 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
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
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
Integration
- test-fixing: Fix broken tests after refactoring
- code-transfer: Move refactored code
- feature-planning: Plan large refactorings
1---2name: code-refactor-43description: Perform bulk code refactoring operations like renaming variables/functions across files, replacing patterns, and updating API calls. Use when users request renaming identifiers, replacing deprecated code patterns, updating method calls, or making consistent changes across multiple locations.4---5
6# Code Refactor
7
8Systematic code refactoring across files. **Auto-switches to execution mode** for 10+ files (90% token savings).
9
10## Mode Selection
11
12- **1-9 files**: Use native tools (Grep + Edit with replace_all)
13- **10+ files**: Automatically use `code-execution` skill
14
15**Execution example (50 files):**
16```python
17from api.code_transform import rename_identifier
18result = rename_identifier('.', 'oldName', 'newName', '**/*.py')
19# Returns: {'files_modified': 50, 'total_replacements': 247}
20# ~500 tokens vs ~25,000 tokens traditional
21```
22
23## When to Use
24
25- "rename [identifier] to [new_name]"
26- "replace all [pattern] with [replacement]"
27- "refactor to use [new_pattern]"
28- "update all calls to [function/API]"
29- "convert [old_pattern] to [new_pattern]"
30
31## Core Workflow (Native Mode)
32
33### 1. Find All Occurrences
34```
35Grep(pattern="getUserData", output_mode="files_with_matches") # Find files
36Grep(pattern="getUserData", output_mode="content", -n=true, -B=2, -A=2) # Verify with context
37```
38
39### 2. Replace All Instances
40```
41Edit(
42 file_path="src/api.js",
43 old_string="getUserData",
44 new_string="fetchUserData",
45 replace_all=true
46)
47```
48
49### 3. Verify Changes
50```
51Grep(pattern="getUserData", output_mode="files_with_matches") # Should return none
52```
53
54## Workflow Examples
55
56### Rename Function
571. Find: `Grep(pattern="getUserData", output_mode="files_with_matches")`
582. Count: "Found 15 occurrences in 5 files"
593. Replace in each file with `replace_all=true`
604. Verify: Re-run Grep
615. Suggest: Run tests
62
63### Replace Deprecated Pattern
641. Find: `Grep(pattern="\\bvar\\s+\\w+", output_mode="content", -n=true)`
652. Analyze: Check if reassigned (let) or constant (const)
663. Replace: `Edit(old_string="var count = 0", new_string="let count = 0")`
674. Verify: `npm run lint`
68
69### Update API Calls
701. Find: `Grep(pattern="/api/auth/login", output_mode="content", -n=true)`
712. Replace: `Edit(old_string="'/api/auth/login'", new_string="'/api/v2/authentication/login'", replace_all=true)`
723. Test: Recommend integration tests
73
74## Best Practices
75
76**Planning:**
77- Find all instances first
78- Review context of each match
79- Inform user of scope
80- Consider edge cases (strings, comments)
81
82**Safe Process:**
831. Search → Find all
842. Analyze → Verify appropriate
853. Inform → Tell user scope
864. Execute → Make changes
875. Verify → Confirm applied
886. Test → Suggest running tests
89
90**Edge Cases:**
91- Strings/comments: Ask if should update
92- Exported APIs: Warn of breaking changes
93- Case sensitivity: Be explicit
94
95## Tool Reference
96
97**Edit with replace_all:**
98- `replace_all=true`: Replace all occurrences
99- `replace_all=false`: Replace only first (or fail if multiple)
100- Must match EXACTLY (whitespace, quotes)
101
102**Grep patterns:**
103- `-n=true`: Show line numbers
104- `-B=N, -A=N`: Context lines
105- `-i=true`: Case-insensitive
106- `type="py"`: Filter by file type
107
108## Integration
109
110- **test-fixing**: Fix broken tests after refactoring
111- **code-transfer**: Move refactored code
112- **feature-planning**: Plan large refactorings