name: rust-code-navigator description: “使用LSP导航Rust代码。触发词:/navigate, go to definition, find references, where is defined, 跳转定义, 查找引用, 定义在哪, 谁用了这个” argument-hint: “<符号> [在 文件.rs:行号]” allowed-tools: [“LSP”, “Read”, “Glob”]
Rust 代码导航器
使用语言服务器协议高效导航大型Rust代码库。
使用方法
/rust-code-navigator <符号> [在 文件.rs:行号]
示例:
/rust-code-navigator parse_config- 查找parse_config的定义/rust-code-navigator MyStruct 在 src/lib.rs:42- 从特定位置导航
LSP操作
1. 跳转到定义
查找符号定义的位置。
LSP(
operation: "goToDefinition",
filePath: "src/main.rs",
line: 25,
character: 10
)
使用时机:
- 用户询问“X定义在哪里?”
- 用户想了解类型/函数
- 相当于Ctrl+点击
2. 查找引用
查找符号的所有使用位置。
LSP(
operation: "findReferences",
filePath: "src/lib.rs",
line: 15,
character: 8
)
使用时机:
- 用户询问“谁使用了X?”
- 重构/重命名之前
- 理解变更影响
3. 悬停信息
获取符号的类型和文档。
LSP(
operation: "hover",
filePath: "src/main.rs",
line: 30,
character: 15
)
使用时机:
- 用户询问“X是什么类型?”
- 用户需要文档
- 快速类型检查
工作流程
用户: “Config结构体定义在哪里?”
│
▼
[1] 在工作区搜索“Config”
LSP(operation: "workspaceSymbol", ...)
│
▼
[2] 如果多个结果,请用户澄清
│
▼
[3] 跳转到定义
LSP(operation: "goToDefinition", ...)
│
▼
[4] 显示文件路径和上下文
读取周围代码获取上下文
输出格式
找到定义
## Config (结构体)
**定义于:** `src/config.rs:15`
```rust
#[derive(Debug, Clone)]
pub struct Config {
pub name: String,
pub port: u16,
pub debug: bool,
}
```
**文档:** 应用程序服务器的配置。
找到引用
## 对 `Config` 的引用 (找到5个)
| 位置 | 上下文 |
|----------|---------|
| src/main.rs:10 | `let config = Config::load()?;` |
| src/server.rs:25 | `fn new(config: Config) -> Self` |
| src/server.rs:42 | `self.config.port` |
| src/tests.rs:15 | `Config::default()` |
| src/cli.rs:8 | `config: Option<Config>` |
常见模式
| 用户说 | LSP操作 |
|---|---|
| “X定义在哪里?” | goToDefinition |
| “谁使用了X?” | findReferences |
| “X是什么类型?” | hover |
| “查找所有结构体” | workspaceSymbol |
| “这个文件里有什么?” | documentSymbol |
错误处理
| 错误 | 原因 | 解决方案 |
|---|---|---|
| “没有LSP服务器” | rust-analyzer未运行 | 建议: rustup component add rust-analyzer |
| “未找到符号” | 拼写错误或不在作用域内 | 先用workspaceSymbol搜索 |
| “多个定义” | 泛型或宏 | 显示所有并让用户选择 |
相关技能
| 时机 | 参见 |
|---|---|
| 调用关系 | rust-call-graph |
| 项目结构 | rust-symbol-analyzer |
| 特质实现 | rust-trait-explorer |
| 安全重构 | rust-refactor-helper |