-
Notifications
You must be signed in to change notification settings - Fork 0
Refactoring Engine
Status: ✅ Complete
Phase: Phase 2 (Post-MVP)
Last Updated: December 5, 2025
The RiceCoder Refactoring Engine provides safe, automated code refactoring with comprehensive impact analysis and rollback capabilities. It enables developers to make systematic code changes with full understanding of dependencies and automatic safety checks.
- Safe Refactoring: Automatic backups and rollback on failure
- Impact Analysis: Understand scope and risk of changes
- Preview Mode: See changes before applying them
- Configuration-Driven: Language-specific rules via configuration files
- Multi-Language Support: Rust, TypeScript, Python, and generic fallback
- Validation: Comprehensive safety checks before and after refactoring
- Reusable Patterns: Store and apply refactoring patterns across projects
- Rename: Rename symbols (functions, variables, types, etc.)
- Extract: Extract code into new functions/methods
- Inline: Inline functions or variables
- Move: Move symbols to different locations
- ChangeSignature: Modify function signatures
- RemoveUnused: Remove unused code
- Simplify: Simplify code structures
use ricecoder_refactoring::{
RefactoringEngine, Refactoring, RefactoringType, RefactoringTarget,
RefactoringOptions, ConfigManager,
};
use std::path::PathBuf;
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create configuration manager
let config_manager = Arc::new(ConfigManager::new());
// Create refactoring engine
let engine = RefactoringEngine::new(config_manager);
// Create a refactoring
let refactoring = Refactoring {
id: "rename-function".to_string(),
refactoring_type: RefactoringType::Rename,
target: RefactoringTarget {
file: PathBuf::from("src/main.rs"),
symbol: "old_name".to_string(),
range: None,
},
options: RefactoringOptions::default(),
};
// Preview the refactoring
let preview = engine.preview(&refactoring).await?;
println!("Impact: {:?}", preview.impact);
// Execute the refactoring
let result = engine.execute(&refactoring).await?;
println!("Success: {}", result.success);
Ok(())
}Before applying a refactoring, analyze its impact:
let impact = engine.analyze_impact(&refactoring).await?;
println!("Affected files: {}", impact.affected_files.len());
println!("Risk level: {:?}", impact.risk_level);
println!("Estimated effort: {:?}", impact.estimated_effort);See what changes will be made:
let preview = engine.preview(&refactoring).await?;
for hunk in &preview.diff.hunks {
println!("File: {}", hunk.file);
println!("Changes:\n{}", hunk.content);
}The engine automatically handles backups and rollback:
let result = engine.execute(&refactoring).await?;
if result.success {
println!("Refactoring applied successfully");
} else {
println!("Refactoring failed and was rolled back");
}See Refactoring-Configuration.md for detailed configuration guide.
See Refactoring-Architecture.md for architecture documentation.
See Refactoring-Patterns.md for pattern guide.
The engine creates automatic backups before applying changes:
let backup = engine.create_backup(&refactoring).await?;
println!("Backup created at: {}", backup.path.display());Comprehensive validation ensures code integrity:
let validation = engine.validate(&refactoring).await?;
if validation.is_valid {
println!("Refactoring is safe to apply");
} else {
println!("Validation errors: {:?}", validation.errors);
}Automatic rollback on failure:
let result = engine.execute(&refactoring).await?;
if !result.success {
engine.rollback(&result.backup_info).await?;
println!("Refactoring rolled back");
}All operations return Result<T> with explicit error types:
match engine.execute(&refactoring).await {
Ok(result) => println!("Success: {}", result.success),
Err(e) => eprintln!("Error: {}", e),
}- Configuration loading: < 100ms
- Impact analysis: < 2s for typical projects
- Preview generation: < 1s
- Provider lookup: < 50ms
- Check the error message for details
- Review the impact analysis to understand scope
- Verify configuration is correct
- Check that the target symbol exists
- Verify backup files exist
- Check file permissions
- Ensure sufficient disk space
- Review rollback logs
- Profile the operation to identify bottlenecks
- Check configuration for unnecessary rules
- Consider breaking large refactorings into smaller ones
- Verify system resources are available
- Refactoring-Configuration.md - Configuration guide
- Refactoring-Architecture.md - Architecture documentation
- Refactoring-Patterns.md - Pattern guide
- API-Reference.md - API documentation
Last updated: December 5, 2025