Rust Refactor Helper

actionbook/rust-skills/skills/rust-refactor-helper

作者 actionbook5c40d3ad7851无许可证1.5K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库6周前更新

Safe Rust refactoring with LSP analysis. Triggers on: /refactor, rename symbol, move function, extract, 重构, 重命名, 提取函数, 安全重构

AI 生成的概览

借助 LSP 分析安全重构 Rust 代码,并在应用改动前预览影响范围。

功能
该技能引导智能体执行安全的 Rust 重构操作:重命名符号、提取函数、内联函数以及在模块间移动符号。它使用 findReferences、hover、goToDefinition、incomingCalls 等 LSP 操作,在编辑前生成影响分析。它会输出受影响文件、导入变更、依赖移动和潜在问题的报告,并可在应用改动前以 dry-run 模式预览。
适用场景
适用于重命名 Rust 符号、将代码块提取为函数、内联函数或将符号移动到其他模块的场景。尤其适合需要关注引用完整性、命名冲突、可见性和导入更新的改动。
运行要求
需要 LSP 工具以及 Read、Glob、Grep、Edit 工具;不附带脚本。

Rust Refactor Helper

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/

LSP Operations Used

Pre-Refactor Analysis

# Find all references before renamingLSP(  operation: "findReferences",  filePath: "src/lib.rs",  line: 25,  character: 8)
# Get symbol infoLSP(  operation: "hover",  filePath: "src/lib.rs",  line: 25,  character: 8)
# Check call hierarchy for move operationsLSP(  operation: "incomingCalls",  filePath: "src/lib.rs",  line: 25,  character: 8)

Refactoring Workflows

1. Rename Symbol

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:

## Rename: parse_config → load_config
### Impact Analysis
**Definition:** src/config.rs:25**References found:** 8
| File | Line | Context | Change ||------|------|---------|--------|| src/config.rs | 25 | `pub fn parse_config(` | Definition || src/config.rs | 45 | `parse_config(path)?` | Call || src/main.rs | 12 | `config::parse_config` | Import || src/main.rs | 30 | `let cfg = parse_config(` | Call || src/lib.rs | 8 | `pub use config::parse_config` | Re-export || tests/config_test.rs | 15 | `parse_config("test.toml")` | Test || tests/config_test.rs | 25 | `parse_config("")` | Test || docs/api.md | 42 | `parse_config` | Documentation |
### Potential Issues
⚠️ **Documentation reference:** docs/api.md:42 may need manual update⚠️ **Re-export:** src/lib.rs:8 - public API change
### Proceed?- [x] --dry-run (preview only)- [ ] Apply changes

2. Extract Function

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:

## Extract Function: src/main.rs:20-35
### Selected Code​```rustlet 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
### Extracted Function
​```rustfn 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
​```rustlet config = load_and_validate_config(&path)?;​```

3. Move Symbol

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:

## Move: UserService → src/services/user.rs
### Current Locationsrc/handlers/auth.rs:50-120
### Dependencies (will be moved together)- struct UserService (50-80)- impl UserService (82-120)- const DEFAULT_TIMEOUT (48)
### Import Changes Required
| File | Current | New ||------|---------|-----|| 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)​```
### Circular Dependency Check✅ No circular dependencies detected

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

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.

Related Skills

WhenSee
Navigate to symbolrust-code-navigator
Understand call flowrust-call-graph
Project structurerust-symbol-analyzer
Trait implementationsrust-trait-explorer

来源与署名

来源:actionbook/rust-skills位于skills/rust-refactor-helper提交5c40d3a

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架