name: rust-call-graph description: “使用LSP可视化Rust函数调用图。触发词:/call-graph, call hierarchy, who calls, what calls, 调用图, 调用关系, 谁调用了, 调用了谁” argument-hint: “<函数名> [–depth N] [–direction in|out|both]” allowed-tools: [“LSP”, “Read”, “Glob”]
Rust 调用图
使用LSP调用层次结构可视化函数调用关系。
使用方法
/rust-call-graph <函数名> [--depth N] [--direction in|out|both]
选项:
--depth N: 遍历多少层(默认: 3)--direction:in(调用者),out(被调用者),both(双向)
示例:
/rust-call-graph process_request- 显示调用者和被调用者/rust-call-graph handle_error --direction in- 仅显示调用者/rust-call-graph main --direction out --depth 5- 深度被调用者分析
LSP 操作
1. 准备调用层次结构
获取函数的调用层次结构项。
LSP(
operation: "prepareCallHierarchy",
filePath: "src/handler.rs",
line: 45,
character: 8
)
2. 传入调用(谁调用了这个?)
LSP(
operation: "incomingCalls",
filePath: "src/handler.rs",
line: 45,
character: 8
)
3. 传出调用(这个调用了什么?)
LSP(
operation: "outgoingCalls",
filePath: "src/handler.rs",
line: 45,
character: 8
)
工作流程
用户: "显示 process_request 的调用图"
│
▼
[1] 查找函数位置
LSP(workspaceSymbol) 或 Grep
│
▼
[2] 准备调用层次结构
LSP(prepareCallHierarchy)
│
▼
[3] 获取传入调用(调用者)
LSP(incomingCalls)
│
▼
[4] 获取传出调用(被调用者)
LSP(outgoingCalls)
│
▼
[5] 递归展开到深度 N
│
▼
[6] 生成ASCII可视化
输出格式
传入调用(谁调用了这个?)
## `process_request` 的调用者
main
└── run_server
└── handle_connection
└── process_request ◄── 当前位置
传出调用(这个调用了什么?)
## `process_request` 的被调用者
process_request ◄── 当前位置
├── parse_headers
│ └── validate_header
├── authenticate
│ ├── check_token
│ └── load_user
├── execute_handler
│ └── [动态分发]
└── send_response
└── serialize_body
双向(两者)
## `process_request` 的调用图
┌─────────────────┐
│ main │
└────────┬────────┘
│
┌────────▼────────┐
│ run_server │
└────────┬────────┘
│
┌────────▼────────┐
│handle_connection│
└────────┬────────┘
│
┌────────────────────┼────────────────────┐
│ │ │
┌───────▼───────┐ ┌───────▼───────┐ ┌───────▼───────┐
│ parse_headers │ │ authenticate │ │send_response │
└───────────────┘ └───────┬───────┘ └───────────────┘
│
┌───────┴───────┐
│ │
┌──────▼──────┐ ┌──────▼──────┐
│ check_token │ │ load_user │
└─────────────┘ └─────────────┘
分析洞察
生成调用图后,提供洞察:
## 分析
**入口点:** main, test_process_request
**叶子函数:** validate_header, serialize_body
**热路径:** main → run_server → handle_connection → process_request
**复杂度:** 12个函数,3层深度
**潜在问题:**
- `authenticate` 有高扇出(4个被调用者)
- `process_request` 被3个地方调用(考虑是否是有意设计)
常见模式
| 用户说 | 方向 | 使用场景 |
|---|---|---|
| “谁调用了X?” | 传入 | 影响分析 |
| “X调用了什么?” | 传出 | 理解实现 |
| “显示调用图” | 双向 | 完整视图 |
| “从main追踪到X” | 传出 | 执行路径 |
可视化选项
| 样式 | 最适合 |
|---|---|
| 树状图(默认) | 简单层次结构 |
| 方框图 | 复杂关系 |
| 扁平列表 | 多连接 |
| Mermaid | 导出到文档 |
Mermaid 导出
graph TD
main --> run_server
run_server --> handle_connection
handle_connection --> process_request
process_request --> parse_headers
process_request --> authenticate
process_request --> send_response
相关技能
| 何时使用 | 查看 |
|---|---|
| 查找定义 | rust-code-navigator |
| 项目结构 | rust-symbol-analyzer |
| 特质实现 | rust-trait-explorer |
| 安全重构 | rust-refactor-helper |