rust-refactor-helper

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Rust Refactor Helper

Rust重构助手

Perform safe refactoring with comprehensive impact analysis.
通过全面的影响分析执行安全重构。

Usage

使用方法

/rust-refactor-helper <action> <target> [--dry-run]
Actions:
  • rename <old> <new>
    - Rename symbol
  • extract-fn <selection>
    - Extract to function
  • inline <fn>
    - Inline function
  • move <symbol> <dest>
    - Move to module
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

重构前分析

undefined
undefined

Find 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 )
undefined
LSP( operation: "incomingCalls", filePath: "src/lib.rs", line: 25, character: 8 )
undefined

Refactoring 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 tool
Output:
undefined
用户:"将parse_config重命名为load_config"
[1] 查找符号定义
    LSP(goToDefinition)
[2] 查找所有引用
    LSP(findReferences)
[3] 按文件分类
[4] 检查冲突
    - 'load_config'是否已被使用?
    - 是否存在宏生成的引用?
[5] 展示影响分析结果(--dry-run模式)
[6] 使用编辑工具应用更改
输出结果:
undefined

Rename: parse_config → load_config

重命名:parse_config → load_config

Impact Analysis

影响分析

Definition: src/config.rs:25 References found: 8
FileLineContextChange
src/config.rs25
pub fn parse_config(
Definition
src/config.rs45
parse_config(path)?
Call
src/main.rs12
config::parse_config
Import
src/main.rs30
let cfg = parse_config(
Call
src/lib.rs8
pub use config::parse_config
Re-export
tests/config_test.rs15
parse_config("test.toml")
Test
tests/config_test.rs25
parse_config("")
Test
docs/api.md42
parse_config
Documentation
定义位置: src/config.rs:25 找到的引用数: 8
文件行号上下文更改类型
src/config.rs25
pub fn parse_config(
定义
src/config.rs45
parse_config(path)?
调用
src/main.rs12
config::parse_config
导入
src/main.rs30
let cfg = parse_config(
调用
src/lib.rs8
pub use config::parse_config
重导出
tests/config_test.rs15
parse_config("test.toml")
测试
tests/config_test.rs25
parse_config("")
测试
docs/api.md42
parse_config
文档

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(仅预览)
  • 应用更改
undefined

2. 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 call
Output:
undefined
用户:"将main.rs中20-35行的代码提取为函数"
[1] 读取选中的代码块
[2] 分析变量
    - 哪些是输入变量?(在代码块外定义但被使用)
    - 哪些是输出变量?(在代码块内定义且在外部被使用)
    - 哪些是局部变量?(仅在代码块内定义和使用)
[3] 确定函数签名
[4] 检查是否存在提前返回、循环等结构
[5] 生成提取后的函数
[6] 用函数调用替换原代码
输出结果:
undefined

Extract 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)?; ​
undefined

3. 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 plan
Output:
undefined
用户:"将UserService移动到src/services/"
[1] 查找符号及其所有依赖项
[2] 查找所有引用(调用方)
    LSP(findReferences)
[3] 分析需要的导入变更
[4] 检查是否存在循环依赖
[5] 生成移动计划
输出结果:
undefined

Move: 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

需要的导入变更

FileCurrentNew
src/main.rs
use handlers::auth::UserService
use services::user::UserService
src/handlers/api.rs
use super::auth::UserService
use crate::services::user::UserService
tests/auth_test.rs
use crate::handlers::auth::UserService
use crate::services::user::UserService
文件当前导入新导入
src/main.rs
use handlers::auth::UserService
use services::user::UserService
src/handlers/api.rs
use super::auth::UserService
use crate::services::user::UserService
tests/auth_test.rs
use crate::handlers::auth::UserService
use crate::services::user::UserService

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
✅ 未检测到循环依赖
undefined

Safety Checks

安全检查

CheckPurpose
Reference completenessEnsure all uses are found
Name conflictsDetect existing symbols with same name
Visibility changesWarn if pub/private scope changes
Macro-generated codeWarn about code in macros
DocumentationFlag doc comments mentioning symbol
Test coverageShow affected tests
检查项目的
引用完整性确保找到所有使用场景
名称冲突检测是否存在同名符号
可见性变更警告pub/private作用域变更
宏生成代码警告宏中的代码引用
文档标记提及该符号的文档注释
测试覆盖展示受影响的测试用例

Dry Run Mode

试运行模式

Always use
--dry-run
first to preview changes:
/rust-refactor-helper rename old_name new_name --dry-run
This shows all changes without applying them.
请始终先使用
--dry-run
预览变更:
/rust-refactor-helper rename old_name new_name --dry-run
该模式会展示所有变更但不会实际应用。

Related Skills

相关技能

WhenSee
Navigate to symbolrust-code-navigator
Understand call flowrust-call-graph
Project structurerust-symbol-analyzer
Trait implementationsrust-trait-explorer
场景参考技能
跳转到符号rust-code-navigator
理解调用流程rust-call-graph
项目结构分析rust-symbol-analyzer
Trait实现查看rust-trait-explorer