code-refactor
npx machina-cli add skill mhattingpete/claude-skills-marketplace/code-refactor --openclawFiles (1)
SKILL.md
3.3 KB
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-executionskill
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 occurrencesreplace_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-insensitivetype="py": Filter by file type
Integration
- test-fixing: Fix broken tests after refactoring
- code-transfer: Move refactored code
- feature-planning: Plan large refactorings
Source
git clone https://github.com/mhattingpete/claude-skills-marketplace/blob/main/code-operations-plugin/skills/code-refactor/SKILL.mdView on GitHub Overview
Code Refactor enables bulk renaming, pattern replacement, and API-call updates across multiple files. It auto-switches to execution mode for 10+ files to save tokens and speed large refactors.
How This Skill Works
First, identify target occurrences using Grep. Then apply replacements with Edit and replace_all across the matched files. Finally, verify changes with Grep; for large scopes (10+ files), the skill automatically uses code-execution mode to expedite the process.
When to Use It
- 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]
Quick Start
- Step 1: Define scope and target patterns (rename, replace, or update)
- Step 2: Find occurrences with Grep (output_mode=files_with_matches) and review context
- Step 3: Apply edits with Edit(..., replace_all=true) and verify with Grep; run tests
Best Practices
- Find all instances first
- Review context of each match and consider edge cases (strings, comments)
- Inform user of scope and potential impact (e.g., breaking changes to exported APIs)
- Execute changes in controlled batches and verify after each batch
- Run tests or lint after refactoring to catch regressions
Example Use Cases
- Rename Function: getUserData -> fetchUserData across the codebase
- Replace Deprecated Pattern: var count = 0 -> let count = 0 in multiple files
- Update API Calls: '/api/auth/login' -> '/api/v2/authentication/login'
- Convert old_pattern to new_pattern: replace legacy getUserData calls with fetchUserData calls
- Bulk refactor across 50 files in a module using code-execution mode
Frequently Asked Questions
Add this skill to your agents