mirror of
https://github.com/hanzoai/python-sdk.git
synced 2026-10-23 20:23:00 +00:00
Add detailed documentation with examples for: - hanzo-tools-fs: read, write, edit, tree, find, search, ast - hanzo-tools-shell: cmd, ps, zsh, bash, auto-backgrounding - hanzo-tools-browser: 70+ Playwright actions, device emulation - hanzo-tools-memory: memories, facts, knowledge bases, scopes - hanzo-tools-reasoning: think and critic tools - hanzo-tools-lsp: go-to-definition, references, rename, hover - hanzo-tools-refactor: rename, extract, inline, batch operations - hanzo-tools-agent: multi-agent orchestration, consensus, swarm - hanzo-tools-llm: unified LLM interface with 100+ models Each doc includes: - Installation instructions - Quick start examples - Full API reference - Usage examples - Best practices
6.9 KiB
6.9 KiB
hanzo-tools-refactor
Advanced refactoring with LSP/AST support. FAST parallel processing for large codebases.
Installation
pip install hanzo-tools-refactor
Or as part of the full toolkit:
pip install hanzo-mcp[tools-all]
Overview
hanzo-tools-refactor provides intelligent code refactoring:
- rename - Rename symbol across entire codebase
- rename_batch - Batch rename multiple symbols
- extract_function - Extract code into a new function
- extract_variable - Extract expression into a variable
- inline - Inline a function or variable
- move - Move symbol to another file
- change_signature - Modify function parameters
- find_references - Find all references to a symbol
- organize_imports - Clean up and sort imports
Quick Start
# Rename a symbol
refactor(action="rename", file="main.py", line=10, column=5, new_name="newName")
# Batch rename multiple symbols
refactor(
action="rename_batch",
renames=[
{"old": "foo", "new": "bar"},
{"old": "baz", "new": "qux"}
],
path="./src"
)
# Change function signature
refactor(
action="change_signature",
file="utils.py",
line=15,
add_parameter={"name": "timeout", "default": "30"}
)
# Find references
refactor(action="find_references", file="models.py", line=25, column=8)
Actions Reference
rename
Rename a symbol at a specific location.
refactor(
action="rename",
file="/path/to/file.py",
line=42,
column=10,
new_name="betterName"
)
Parameters:
file(required): File containing the symbolline(required): Line number (1-based)column(required): Column number (1-based)new_name(required): New name for the symbol
rename_batch
Rename multiple symbols across the codebase in parallel.
refactor(
action="rename_batch",
renames=[
{"old": "getUserData", "new": "fetchUserData"},
{"old": "setUserData", "new": "updateUserData"},
{"old": "deleteUserData", "new": "removeUserData"}
],
path="./src",
parallel=True # Default: true
)
Parameters:
renames(required): List of {old, new} pairspath: Directory to search (default: current directory)parallel: Process in parallel (default: true)
extract_function
Extract selected code into a new function.
refactor(
action="extract_function",
file="/path/to/file.py",
start_line=20,
end_line=30,
new_name="processData"
)
Parameters:
file(required): Source filestart_line(required): First line of code to extractend_line(required): Last line of code to extractnew_name: Name for the new function
extract_variable
Extract an expression into a variable.
refactor(
action="extract_variable",
file="/path/to/file.py",
line=15,
start_column=10,
end_column=45,
new_name="calculatedValue"
)
Parameters:
file(required): Source fileline(required): Line containing the expressionstart_column: Start of expressionend_column: End of expressionnew_name: Variable name
inline
Inline a function or variable at its call sites.
refactor(
action="inline",
file="/path/to/file.py",
line=10,
column=5
)
Parameters:
file(required): Source fileline(required): Line of the symbolcolumn(required): Column of the symbol
move
Move a symbol to a different file.
refactor(
action="move",
file="/path/to/source.py",
line=15,
column=6,
target_file="/path/to/destination.py"
)
Parameters:
file(required): Source fileline(required): Line of the symbolcolumn(required): Column of the symboltarget_file(required): Destination file
change_signature
Modify a function's parameters.
# Add a parameter
refactor(
action="change_signature",
file="api.py",
line=20,
add_parameter={"name": "timeout", "default": "30", "type": "int"}
)
# Remove a parameter
refactor(
action="change_signature",
file="api.py",
line=20,
remove_parameter="deprecated_param"
)
# Reorder parameters
refactor(
action="change_signature",
file="api.py",
line=20,
reorder_parameters=["user_id", "data", "options"]
)
find_references
Find all references to a symbol.
refactor(action="find_references", file="models.py", line=10, column=6)
Response:
{
"symbol": "UserModel",
"references": [
{"file": "views.py", "line": 15, "column": 8},
{"file": "tests/test_models.py", "line": 22, "column": 12}
],
"count": 2
}
organize_imports
Clean up and sort imports in a file.
refactor(action="organize_imports", file="main.py")
Parallel Processing
The refactor tool uses parallel processing for large-scale operations:
# Parallel is enabled by default
refactor(
action="rename_batch",
renames=[...], # Many renames
path="./large_codebase",
parallel=True # Uses all CPU cores
)
# Disable for sequential processing
refactor(
action="rename_batch",
renames=[...],
parallel=False
)
Preview Mode
Preview changes before applying them:
# Preview only - don't apply changes
refactor(
action="rename",
file="main.py",
line=10,
column=5,
new_name="newName",
preview=True
)
Response:
{
"preview": true,
"changes": [
{"file": "main.py", "line": 10, "old": "oldName", "new": "newName"},
{"file": "utils.py", "line": 25, "old": "oldName", "new": "newName"}
],
"files_affected": 2
}
Examples
Safe Symbol Rename
# 1. Find all usages first
refs = refactor(action="find_references", file="models.py", line=10, column=5)
# 2. Preview the rename
preview = refactor(
action="rename",
file="models.py",
line=10,
column=5,
new_name="BetterName",
preview=True
)
# 3. Apply the rename
refactor(
action="rename",
file="models.py",
line=10,
column=5,
new_name="BetterName"
)
Large-Scale Refactoring
# Rename multiple patterns across codebase
refactor(
action="rename_batch",
renames=[
{"old": "v1_api", "new": "legacy_api"},
{"old": "v2_api", "new": "current_api"},
{"old": "v3_api", "new": "api"}
],
path="./src",
parallel=True
)
Extract Common Code
# Extract duplicated logic into a function
refactor(
action="extract_function",
file="handlers.py",
start_line=45,
end_line=60,
new_name="validate_and_process"
)
Best Practices
- Always preview first - Use
preview=Truefor important refactors - Run tests after - Verify refactoring didn't break functionality
- Use batch operations -
rename_batchis faster than individual renames - Commit before refactoring - Have a clean git state to easily revert
- Combine with LSP - Use
lsptool for initial symbol discovery