grepai-trace-callers
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGrepAI Trace Callers
GrepAI Trace 调用方查找
This skill covers using to find all code locations that call a specific function or method.
grepai trace callers本技能介绍如何使用查找调用特定函数或方法的所有代码位置。
grepai trace callersWhen to Use This Skill
何时使用此技能
- Finding all usages of a function before refactoring
- Understanding function dependencies
- Impact analysis before changes
- Code navigation and exploration
- 重构前查找函数的所有用法
- 理解函数依赖关系
- 变更前的影响分析
- 代码导航与探索
What is Trace Callers?
什么是Trace Callers?
grepai trace callersfunc Login(user, pass) {...}
↑
│
┌───────┴───────────────────┐
│ Who calls Login()? │
├───────────────────────────┤
│ • HandleAuth (auth.go:42) │
│ • TestLogin (test.go:15) │
│ • CLI (main.go:88) │
└───────────────────────────┘grepai trace callersfunc Login(user, pass) {...}
↑
│
┌───────┴───────────────────┐
│ Who calls Login()? │
├───────────────────────────┤
│ • HandleAuth (auth.go:42) │
│ • TestLogin (test.go:15) │
│ • CLI (main.go:88) │
└───────────────────────────┘Basic Usage
基础用法
bash
grepai trace callers "FunctionName"bash
grepai trace callers "FunctionName"Example
示例
bash
grepai trace callers "Login"Output:
🔍 Callers of "Login"
Found 3 callers:
1. HandleAuth
File: handlers/auth.go:42
Context: user.Login(ctx, credentials)
2. TestLoginSuccess
File: handlers/auth_test.go:15
Context: result := Login(testUser, testPass)
3. RunCLI
File: cmd/main.go:88
Context: err := auth.Login(username, password)bash
grepai trace callers "Login"输出:
🔍 Callers of "Login"
Found 3 callers:
1. HandleAuth
File: handlers/auth.go:42
Context: user.Login(ctx, credentials)
2. TestLoginSuccess
File: handlers/auth_test.go:15
Context: result := Login(testUser, testPass)
3. RunCLI
File: cmd/main.go:88
Context: err := auth.Login(username, password)JSON Output
JSON输出
For programmatic use:
bash
grepai trace callers "Login" --jsonOutput:
json
{
"query": "Login",
"mode": "callers",
"count": 3,
"results": [
{
"file": "handlers/auth.go",
"line": 42,
"caller": "HandleAuth",
"context": "user.Login(ctx, credentials)"
},
{
"file": "handlers/auth_test.go",
"line": 15,
"caller": "TestLoginSuccess",
"context": "result := Login(testUser, testPass)"
},
{
"file": "cmd/main.go",
"line": 88,
"caller": "RunCLI",
"context": "err := auth.Login(username, password)"
}
]
}供编程使用:
bash
grepai trace callers "Login" --json输出:
json
{
"query": "Login",
"mode": "callers",
"count": 3,
"results": [
{
"file": "handlers/auth.go",
"line": 42,
"caller": "HandleAuth",
"context": "user.Login(ctx, credentials)"
},
{
"file": "handlers/auth_test.go",
"line": 15,
"caller": "TestLoginSuccess",
"context": "result := Login(testUser, testPass)"
},
{
"file": "cmd/main.go",
"line": 88,
"caller": "RunCLI",
"context": "err := auth.Login(username, password)"
}
]
}Compact JSON (AI Optimized)
紧凑JSON格式(AI优化)
bash
grepai trace callers "Login" --json --compactOutput:
json
{
"q": "Login",
"m": "callers",
"c": 3,
"r": [
{"f": "handlers/auth.go", "l": 42, "fn": "HandleAuth"},
{"f": "handlers/auth_test.go", "l": 15, "fn": "TestLoginSuccess"},
{"f": "cmd/main.go", "l": 88, "fn": "RunCLI"}
]
}bash
grepai trace callers "Login" --json --compact输出:
json
{
"q": "Login",
"m": "callers",
"c": 3,
"r": [
{"f": "handlers/auth.go", "l": 42, "fn": "HandleAuth"},
{"f": "handlers/auth_test.go", "l": 15, "fn": "TestLoginSuccess"},
{"f": "cmd/main.go", "l": 88, "fn": "RunCLI"}
]
}TOON Output (v0.26.0+)
TOON格式输出(v0.26.0及以上版本)
TOON format offers ~50% fewer tokens than JSON:
bash
grepai trace callers "Login" --toonOutput:
callers[3]:
- call_site:
context: "user.Login(ctx, credentials)"
file: handlers/auth.go
line: 42
symbol:
name: HandleAuth
...Note:and--jsonare mutually exclusive.--toon
TOON格式的令牌数比JSON少约50%:
bash
grepai trace callers "Login" --toon输出:
callers[3]:
- call_site:
context: "user.Login(ctx, credentials)"
file: handlers/auth.go
line: 42
symbol:
name: HandleAuth
...注意:和--json不能同时使用。--toon
Extraction Modes
提取模式
GrepAI offers two extraction modes:
GrepAI提供两种提取模式:
Fast Mode (Default)
快速模式(默认)
Uses regex patterns. Fast and dependency-free.
bash
grepai trace callers "Login" --mode fast使用正则表达式模式。速度快且无依赖。
bash
grepai trace callers "Login" --mode fastPrecise Mode
精确模式
Uses tree-sitter AST parsing. More accurate but requires tree-sitter.
bash
grepai trace callers "Login" --mode precise使用tree-sitter AST解析。准确率更高但需要tree-sitter依赖。
bash
grepai trace callers "Login" --mode preciseComparison
对比
| Mode | Speed | Accuracy | Dependencies |
|---|---|---|---|
| ⚡⚡⚡ | Good | None |
| ⚡⚡ | Excellent | tree-sitter |
| 模式 | 速度 | 准确率 | 依赖项 |
|---|---|---|---|
| ⚡⚡⚡ | 良好 | 无 |
| ⚡⚡ | 优秀 | tree-sitter |
Configuration
配置
Configure trace in :
.grepai/config.yamlyaml
trace:
mode: fast # fast or precise
enabled_languages:
- .go
- .js
- .ts
- .py
- .php
- .rs
exclude_patterns:
- "*_test.go"
- "*.spec.ts"在中配置trace:
.grepai/config.yamlyaml
trace:
mode: fast # fast or precise
enabled_languages:
- .go
- .js
- .ts
- .py
- .php
- .rs
exclude_patterns:
- "*_test.go"
- "*.spec.ts"Supported Languages
支持的语言
| Language | Extensions |
|---|---|
| Go | |
| JavaScript | |
| TypeScript | |
| Python | |
| PHP | |
| C/C++ | |
| Rust | |
| Zig | |
| C# | |
| Java | |
| Pascal/Delphi | |
| 语言 | 扩展名 |
|---|---|
| Go | |
| JavaScript | |
| TypeScript | |
| Python | |
| PHP | |
| C/C++ | |
| Rust | |
| Zig | |
| C# | |
| Java | |
| Pascal/Delphi | |
Use Cases
使用场景
Before Refactoring
重构前
bash
undefinedbash
undefinedFind all usages before renaming
重命名前查找所有用法
grepai trace callers "getUserById"
grepai trace callers "getUserById"
Check impact of changing signature
检查修改签名的影响
grepai trace callers "processPayment"
undefinedgrepai trace callers "processPayment"
undefinedUnderstanding Codebase
理解代码库
bash
undefinedbash
undefinedWho uses this core function?
谁在使用这个核心函数?
grepai trace callers "validateToken"
grepai trace callers "validateToken"
Find entry points to a module
查找模块的入口点
grepai trace callers "initialize"
undefinedgrepai trace callers "initialize"
undefinedDebugging
调试
bash
undefinedbash
undefinedWhere is this function called from?
这个函数从哪里被调用?
grepai trace callers "problematicFunction"
undefinedgrepai trace callers "problematicFunction"
undefinedCode Review
代码评审
bash
undefinedbash
undefinedVerify function usage before approving changes
批准变更前验证函数用法
grepai trace callers "deprecatedMethod"
undefinedgrepai trace callers "deprecatedMethod"
undefinedHandling Common Names
处理通用名称
If your function name is common, results may include unrelated code:
如果你的函数名称很通用,结果可能包含不相关的代码:
Problem
问题
bash
grepai trace callers "get" # Too common, many false positivesbash
grepai trace callers "get" # 过于通用,存在大量误报Solutions
解决方案
- Use more specific name:
bash
grepai trace callers "getUserProfile"- Filter results by path:
bash
grepai trace callers "get" --json | jq '.results[] | select(.file | contains("auth"))'- 使用更具体的名称:
bash
grepai trace callers "getUserProfile"- 按路径过滤结果:
bash
grepai trace callers "get" --json | jq '.results[] | select(.file | contains("auth"))'Combining with Semantic Search
与语义搜索结合使用
Use together for comprehensive understanding:
bash
undefined结合使用以全面理解:
bash
undefinedFind what Login does (semantic)
查找Login的功能(语义搜索)
grepai search "user login authentication"
grepai search "user login authentication"
Find who uses Login (trace)
查找谁在使用Login(追踪)
grepai trace callers "Login"
undefinedgrepai trace callers "Login"
undefinedScripting Examples
脚本示例
Bash
Bash
bash
undefinedbash
undefinedCount callers
统计调用方数量
grepai trace callers "MyFunction" --json | jq '.count'
grepai trace callers "MyFunction" --json | jq '.count'
Get caller function names
获取调用方函数名称
grepai trace callers "MyFunction" --json | jq -r '.results[].caller'
grepai trace callers "MyFunction" --json | jq -r '.results[].caller'
Get file paths only
仅获取文件路径
grepai trace callers "MyFunction" --json | jq -r '.results[].file' | sort -u
undefinedgrepai trace callers "MyFunction" --json | jq -r '.results[].file' | sort -u
undefinedPython
Python
python
import subprocess
import json
result = subprocess.run(
['grepai', 'trace', 'callers', 'Login', '--json'],
capture_output=True,
text=True
)
data = json.loads(result.stdout)
print(f"Found {data['count']} callers of Login:")
for r in data['results']:
print(f" - {r['caller']} in {r['file']}:{r['line']}")python
import subprocess
import json
result = subprocess.run(
['grepai', 'trace', 'callers', 'Login', '--json'],
capture_output=True,
text=True
)
data = json.loads(result.stdout)
print(f"Found {data['count']} callers of Login:")
for r in data['results']:
print(f" - {r['caller']} in {r['file']}:{r['line']}")Common Issues
常见问题
❌ Problem: No callers found
✅ Solutions:
- Check function name spelling (case-sensitive)
- Ensure file type is in
enabled_languages - Run to update symbol index
grepai watch
❌ Problem: Too many false positives
✅ Solutions:
- Use more specific function name
- Add exclude patterns in config
- Filter results with
jq
❌ Problem: Missing some callers
✅ Solutions:
- Try for better accuracy
--mode precise - Check if files are in ignore patterns
❌ 问题: 未找到调用方
✅ 解决方案:
- 检查函数名称拼写(区分大小写)
- 确保文件类型在中
enabled_languages - 运行更新符号索引
grepai watch
❌ 问题: 误报过多
✅ 解决方案:
- 使用更具体的函数名称
- 在配置中添加排除模式
- 使用过滤结果
jq
❌ 问题: 缺少部分调用方
✅ 解决方案:
- 尝试以提高准确率
--mode precise - 检查文件是否在忽略模式中
Best Practices
最佳实践
- Use exact function name: Case matters
- Check symbol index: Run first
grepai watch - Use JSON for scripts: Easier to parse
- Combine with search: Semantic + trace = full picture
- Filter large results: Use or grep
jq
- 使用精确的函数名称: 区分大小写
- 检查符号索引: 先运行
grepai watch - 脚本中使用JSON: 更易于解析
- 与搜索结合: 语义搜索+追踪=完整视图
- 过滤大量结果: 使用或grep
jq
Output Format
输出格式
Trace callers result:
🔍 Callers of "Login"
Mode: fast
Language files scanned: 245
Found 3 callers:
1. HandleAuth
File: handlers/auth.go:42
Context: user.Login(ctx, credentials)
2. TestLoginSuccess
File: handlers/auth_test.go:15
Context: result := Login(testUser, testPass)
3. RunCLI
File: cmd/main.go:88
Context: err := auth.Login(username, password)
Tip: Use --json for machine-readable output
Use --mode precise for more accurate results追踪调用方的结果:
🔍 Callers of "Login"
Mode: fast
Language files scanned: 245
Found 3 callers:
1. HandleAuth
File: handlers/auth.go:42
Context: user.Login(ctx, credentials)
2. TestLoginSuccess
File: handlers/auth_test.go:15
Context: result := Login(testUser, testPass)
3. RunCLI
File: cmd/main.go:88
Context: err := auth.Login(username, password)
Tip: Use --json for machine-readable output
Use --mode precise for more accurate results