关于 rust-call-graph
通过 LSP 调用层级操作展示 Rust 函数之间的调用关系,适合追踪代码执行路径的开发者。可按方向和深度查看调用方、被调用方或两者。
主要功能
- 01展示调用方和被调用方
- 02设置遍历方向
- 03配置图表深度
- 04导出 Mermaid 图
适用场景
- 01追踪某函数的调用方
- 02查看某函数调用的函数
- 03从 main 跟踪执行路径
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ actionbook/ rust-skills --skill rust-call-graph安装后在 Claude Code 中输入/rust-call-graph确认已加载。
详细介绍
Rust Call Graph
使用 LSP 调用层级可视化函数调用关系。
用法
/rust-call-graph <function_name> [--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
)
工作流
User: "Show call graph for process_request"
│
▼
[1] Find function location
LSP(workspaceSymbol) or Grep
│
▼
[2] Prepare call hierarchy
LSP(prepareCallHierarchy)
│
▼
[3] Get incoming calls (callers)
LSP(incomingCalls)
│
▼
[4] Get outgoing calls (callees)
LSP(outgoingCalls)
│
▼
[5] Recursively expand to depth N
│
▼
[6] Generate ASCII visualization
输出格式
传入调用(谁调用了它?)
## Callers of `process_request`
main
└── run_server
└── handle_connection
└── process_request ◄── YOU ARE HERE
传出调用(它调用了什么?)
## Callees of `process_request`
process_request ◄── YOU ARE HERE
├── parse_headers
│ └── validate_header
├── authenticate
│ ├── check_token
│ └── load_user
├── execute_handler
│ └── [dynamic dispatch]
└── send_response
└── serialize_body
双向(两者)
## Call Graph for `process_request`
┌─────────────────┐
│ main │
└────────┬────────┘
│
┌────────▼────────┐
│ run_server │
└────────┬────────┘
│
┌────────▼────────┐
│handle_connection│
└────────┬────────┘
│
┌────────────────────┼────────────────────┐
│ │ │
┌───────▼───────┐ ┌───────▼───────┐ ┌───────▼───────┐
│ parse_headers │ │ authenticate │ │send_response │
└───────────────┘ └───────┬───────┘ └───────────────┘
│
┌───────┴───────┐
│ │
┌──────▼──────┐ ┌──────▼──────┐
│ check_token │ │ load_user │
└─────────────┘ └─────────────┘
分析洞察
生成调用图后,提供以下洞察:
## Analysis
**Entry Points:** main, test_process_request
**Leaf Functions:** validate_header, serialize_body
**Hot Path:** main → run_server → handle_connection → process_request
**Complexity:** 12 functions, 3 levels deep
**Potential Issues:**
- `authenticate` has high fan-out (4 callees)
- `process_request` is called from 3 places (consider if this is intentional)
常见模式
| 用户说法 | 方向 | 用例 |
|---|---|---|
| “谁调用了 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 |
| Trait 实现 | rust-trait-explorer |
| 安全重构 | rust-refactor-helper |
评论
支持 Markdown