rust-refactor-helper
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust Refactor Helper
Rust重构助手
Perform safe refactoring with comprehensive impact analysis.
通过全面的影响分析执行安全重构。
Usage
使用方法
/rust-refactor-helper <action> <target> [--dry-run]Actions:
- - Rename symbol
rename <old> <new> - - Extract to function
extract-fn <selection> - - Inline function
inline <fn> - - Move to module
move <symbol> <dest>
Examples:
/rust-refactor-helper rename parse_config load_config/rust-refactor-helper extract-fn src/main.rs:20-35/rust-refactor-helper move UserService src/services/
/rust-refactor-helper <action> <target> [--dry-run]操作类型:
- - 重命名符号
rename <旧名称> <新名称> - - 提取为函数
extract-fn <选中区域> - - 内联函数
inline <函数名> - - 移动到模块
move <符号> <目标路径>
示例:
/rust-refactor-helper rename parse_config load_config/rust-refactor-helper extract-fn src/main.rs:20-35/rust-refactor-helper move UserService src/services/
LSP Operations Used
使用的LSP操作
Pre-Refactor Analysis
重构前分析
undefinedundefinedFind all references before renaming
重命名前查找所有引用
LSP(
operation: "findReferences",
filePath: "src/lib.rs",
line: 25,
character: 8
)
LSP(
operation: "findReferences",
filePath: "src/lib.rs",
line: 25,
character: 8
)
Get symbol info
获取符号信息
LSP(
operation: "hover",
filePath: "src/lib.rs",
line: 25,
character: 8
)
LSP(
operation: "hover",
filePath: "src/lib.rs",
line: 25,
character: 8
)
Check call hierarchy for move operations
检查移动操作的调用层级
LSP(
operation: "incomingCalls",
filePath: "src/lib.rs",
line: 25,
character: 8
)
undefinedLSP(
operation: "incomingCalls",
filePath: "src/lib.rs",
line: 25,
character: 8
)
undefinedRefactoring Workflows
重构工作流
1. Rename Symbol
1. 重命名符号
User: "Rename parse_config to load_config"
│
▼
[1] Find symbol definition
LSP(goToDefinition)
│
▼
[2] Find ALL references
LSP(findReferences)
│
▼
[3] Categorize by file
│
▼
[4] Check for conflicts
- Is 'load_config' already used?
- Are there macro-generated uses?
│
▼
[5] Show impact analysis (--dry-run)
│
▼
[6] Apply changes with Edit toolOutput:
undefined用户:"将parse_config重命名为load_config"
│
▼
[1] 查找符号定义
LSP(goToDefinition)
│
▼
[2] 查找所有引用
LSP(findReferences)
│
▼
[3] 按文件分类
│
▼
[4] 检查冲突
- 'load_config'是否已被使用?
- 是否存在宏生成的引用?
│
▼
[5] 展示影响分析结果(--dry-run模式)
│
▼
[6] 使用编辑工具应用更改输出结果:
undefinedRename: parse_config → load_config
重命名:parse_config → load_config
Impact Analysis
影响分析
Definition: src/config.rs:25
References found: 8
| File | Line | Context | Change |
|---|---|---|---|
| src/config.rs | 25 | | Definition |
| src/config.rs | 45 | | Call |
| src/main.rs | 12 | | Import |
| src/main.rs | 30 | | Call |
| src/lib.rs | 8 | | Re-export |
| tests/config_test.rs | 15 | | Test |
| tests/config_test.rs | 25 | | Test |
| docs/api.md | 42 | | Documentation |
定义位置: src/config.rs:25
找到的引用数: 8
| 文件 | 行号 | 上下文 | 更改类型 |
|---|---|---|---|
| src/config.rs | 25 | | 定义 |
| src/config.rs | 45 | | 调用 |
| src/main.rs | 12 | | 导入 |
| src/main.rs | 30 | | 调用 |
| src/lib.rs | 8 | | 重导出 |
| tests/config_test.rs | 15 | | 测试 |
| tests/config_test.rs | 25 | | 测试 |
| docs/api.md | 42 | | 文档 |
Potential Issues
潜在问题
⚠️ Documentation reference: docs/api.md:42 may need manual update
⚠️ Re-export: src/lib.rs:8 - public API change
⚠️ 文档引用: docs/api.md:42 可能需要手动更新
⚠️ 重导出: src/lib.rs:8 - 公共API变更
Proceed?
是否继续?
- --dry-run (preview only)
- Apply changes
undefined- --dry-run(仅预览)
- 应用更改
undefined2. Extract Function
2. 提取函数
User: "Extract lines 20-35 in main.rs to a function"
│
▼
[1] Read the selected code block
│
▼
[2] Analyze variables
- Which are inputs? (used but not defined in block)
- Which are outputs? (defined and used after block)
- Which are local? (defined and used only in block)
│
▼
[3] Determine function signature
│
▼
[4] Check for early returns, loops, etc.
│
▼
[5] Generate extracted function
│
▼
[6] Replace original code with callOutput:
undefined用户:"将main.rs中20-35行的代码提取为函数"
│
▼
[1] 读取选中的代码块
│
▼
[2] 分析变量
- 哪些是输入变量?(在代码块外定义但被使用)
- 哪些是输出变量?(在代码块内定义且在外部被使用)
- 哪些是局部变量?(仅在代码块内定义和使用)
│
▼
[3] 确定函数签名
│
▼
[4] 检查是否存在提前返回、循环等结构
│
▼
[5] 生成提取后的函数
│
▼
[6] 用函数调用替换原代码输出结果:
undefinedExtract Function: src/main.rs:20-35
提取函数:src/main.rs:20-35
Selected Code
选中的代码
rust let file = File::open(&path)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; let config: Config = toml::from_str(&contents)?; validate_config(&config)?;
rust let file = File::open(&path)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; let config: Config = toml::from_str(&contents)?; validate_config(&config)?; Analysis
分析结果
Inputs: path: &Path
Outputs: config: Config
Side Effects: File I/O, may return error
输入: path: &Path
输出: config: Config
副作用: 文件I/O操作,可能返回错误
Extracted Function
提取后的函数
rust fn load_and_validate_config(path: &Path) -> Result<Config> { let file = File::open(path)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; let config: Config = toml::from_str(&contents)?; validate_config(&config)?; Ok(config) }
rust fn load_and_validate_config(path: &Path) -> Result<Config> { let file = File::open(path)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; let config: Config = toml::from_str(&contents)?; validate_config(&config)?; Ok(config) } Replacement
替换后的代码
rust let config = load_and_validate_config(&path)?; undefined
rust let config = load_and_validate_config(&path)?; undefined3. Move Symbol
3. 移动符号
User: "Move UserService to src/services/"
│
▼
[1] Find symbol and all its dependencies
│
▼
[2] Find all references (callers)
LSP(findReferences)
│
▼
[3] Analyze import changes needed
│
▼
[4] Check for circular dependencies
│
▼
[5] Generate move planOutput:
undefined用户:"将UserService移动到src/services/"
│
▼
[1] 查找符号及其所有依赖项
│
▼
[2] 查找所有引用(调用方)
LSP(findReferences)
│
▼
[3] 分析需要的导入变更
│
▼
[4] 检查是否存在循环依赖
│
▼
[5] 生成移动计划输出结果:
undefinedMove: UserService → src/services/user.rs
移动:UserService → src/services/user.rs
Current Location
当前位置
src/handlers/auth.rs:50-120
src/handlers/auth.rs:50-120
Dependencies (will be moved together)
依赖项(将一并移动)
- struct UserService (50-80)
- impl UserService (82-120)
- const DEFAULT_TIMEOUT (48)
- struct UserService (50-80)
- impl UserService (82-120)
- const DEFAULT_TIMEOUT (48)
Import Changes Required
需要的导入变更
| File | Current | New |
|---|---|---|
| src/main.rs | | |
| src/handlers/api.rs | | |
| tests/auth_test.rs | | |
| 文件 | 当前导入 | 新导入 |
|---|---|---|
| src/main.rs | | |
| src/handlers/api.rs | | |
| tests/auth_test.rs | | |
New File Structure
新文件结构
src/ ├── services/ │ ├── mod.rs (NEW - add `pub mod user;`) │ └── user.rs (NEW - UserService moved here) ├── handlers/ │ └── auth.rs (UserService removed)
src/ ├── services/ │ ├── mod.rs (新增 - 添加 `pub mod user;`) │ └── user.rs (新增 - UserService已移动至此) ├── handlers/ │ └── auth.rs (已移除UserService) Circular Dependency Check
循环依赖检查
✅ No circular dependencies detected
undefined✅ 未检测到循环依赖
undefinedSafety Checks
安全检查
| Check | Purpose |
|---|---|
| Reference completeness | Ensure all uses are found |
| Name conflicts | Detect existing symbols with same name |
| Visibility changes | Warn if pub/private scope changes |
| Macro-generated code | Warn about code in macros |
| Documentation | Flag doc comments mentioning symbol |
| Test coverage | Show affected tests |
| 检查项 | 目的 |
|---|---|
| 引用完整性 | 确保找到所有使用场景 |
| 名称冲突 | 检测是否存在同名符号 |
| 可见性变更 | 警告pub/private作用域变更 |
| 宏生成代码 | 警告宏中的代码引用 |
| 文档 | 标记提及该符号的文档注释 |
| 测试覆盖 | 展示受影响的测试用例 |
Dry Run Mode
试运行模式
Always use first to preview changes:
--dry-run/rust-refactor-helper rename old_name new_name --dry-runThis shows all changes without applying them.
请始终先使用预览变更:
--dry-run/rust-refactor-helper rename old_name new_name --dry-run该模式会展示所有变更但不会实际应用。
Related Skills
相关技能
| When | See |
|---|---|
| Navigate to symbol | rust-code-navigator |
| Understand call flow | rust-call-graph |
| Project structure | rust-symbol-analyzer |
| Trait implementations | rust-trait-explorer |
| 场景 | 参考技能 |
|---|---|
| 跳转到符号 | rust-code-navigator |
| 理解调用流程 | rust-call-graph |
| 项目结构分析 | rust-symbol-analyzer |
| Trait实现查看 | rust-trait-explorer |