searching-sourcegraph
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSearching Sourcegraph
使用Sourcegraph进行搜索
Search before you build. Existing patterns reduce tokens, ensure consistency, and surface tested solutions.
在开发前先搜索。已有的代码模式可以减少token消耗、确保一致性,还能找到经过测试的解决方案。
Tool Selection Logic
工具选择逻辑
Start here:
- Know the exact symbol or pattern? →
sg_keyword_search - Know the concept, not the code? →
sg_nls_search - Need to understand how/why? →
sg_deepsearch_read - Tracing a symbol's usage? →
sg_find_references - Need full implementation? → →
sg_go_to_definitionsg_read_file
| Goal | Tool |
|---|---|
| Concepts/semantic search | |
| Exact code patterns | |
| Trace usage | |
| See implementation | |
| Understand systems | |
| Read files | |
| Browse structure | |
| Find repos | |
| Search commits | |
| Track changes | |
| Compare versions | |
从这里开始:
- 知道确切的符号或模式? →
sg_keyword_search - 知道概念但不清楚具体代码? →
sg_nls_search - 需要了解运作方式/原理? →
sg_deepsearch_read - 追踪符号的使用场景? →
sg_find_references - 需要完整实现代码? → →
sg_go_to_definitionsg_read_file
| 目标 | 工具 |
|---|---|
| 概念/语义搜索 | |
| 精确代码模式 | |
| 追踪使用场景 | |
| 查看实现代码 | |
| 理解系统架构 | |
| 读取文件内容 | |
| 浏览文件结构 | |
| 查找代码仓库 | |
| 搜索提交记录 | |
| 追踪代码变更 | |
| 对比版本差异 | |
Scoping (Always Do This)
范围限定(务必执行)
repo:^github.com/ORG/REPO$ # Exact repo (preferred)
repo:github.com/ORG/ # All repos in org
file:.*\.ts$ # TypeScript only
file:src/api/ # Specific directory
file:.*\.test\.ts$ -file:__mocks__ # Tests, exclude mocksStart narrow. Expand only if results are empty.
Combine filters:
repo:^github.com/myorg/backend$ file:src/handlers lang:typescriptrepo:^github.com/ORG/REPO$ # 精确指定仓库(推荐)
repo:github.com/ORG/ # 组织下所有仓库
file:.*\.ts$ # 仅TypeScript文件
file:src/api/ # 特定目录
file:.*\.test\.ts$ -file:__mocks__ # 测试文件,排除模拟文件先从窄范围开始搜索。仅当无结果时再扩大范围。
组合筛选条件:
repo:^github.com/myorg/backend$ file:src/handlers lang:typescriptContext-Aware Behaviour
上下文感知行为
When the user provides a file path or error message:
- Extract symbols, function names, or error codes
- Search for those exact terms first
- Trace references if the error involves a known symbol
When the user asks "how does X work":
- Prefer for architectural understanding
sg_deepsearch_read - Follow up with on key files mentioned in the response
sg_read_file
When the user is implementing a new feature:
- Search for similar existing implementations first
- Read tests for usage examples
- Check for shared utilities before creating new ones
When debugging:
- Search for the exact error string
- Trace the symbol where the error is thrown
- Check recent changes with
sg_diff_search
当用户提供文件路径或错误信息时:
- 提取符号、函数名或错误码
- 优先搜索这些精确术语
- 如果错误涉及已知符号,追踪其引用关系
当用户询问“X是如何工作的”时:
- 优先使用来理解架构
sg_deepsearch_read - 根据返回结果,使用查看提到的关键文件
sg_read_file
当用户要实现新功能时:
- 先搜索类似的现有实现
- 查看测试文件了解使用示例
- 先检查是否有可复用的公共工具,再考虑新建
当调试时:
- 搜索精确的错误字符串
- 追踪错误抛出位置的符号
- 使用查看近期变更
sg_diff_search
Workflows
工作流
For detailed step-by-step workflows, see:
- — when building new features
workflows/implementing-feature.md - — when exploring unfamiliar systems
workflows/understanding-code.md - — when tracking down bugs
workflows/debugging-issue.md
如需详细的分步工作流,请查看:
- — 开发新功能时
workflows/implementing-feature.md - — 探索陌生系统时
workflows/understanding-code.md - — 排查Bug时
workflows/debugging-issue.md
Efficiency Rules
效率规则
Minimise tool calls:
- Chain searches logically: search → read → references → definition
- Don't re-search for the same pattern; use results from prior calls
- Prefer over
sg_keyword_searchwhen you have exact terms (faster, more precise)sg_nls_search
Batch your understanding:
- Read 2-3 related files before synthesising, rather than reading one and asking questions
- Use for "how does X work" instead of multiple keyword searches
sg_deepsearch_read
Avoid common token waste:
- Don't search all repos when you know the target repo
- Don't use for simple "find all" queries
sg_deepsearch_read - Don't re-read files you've already seen in this conversation
减少工具调用次数:
- 合理串联工具:搜索 → 读取文件 → 查找引用 → 查看定义
- 同一模式不要重复搜索,复用之前的结果
- 当有精确术语时,优先使用而非
sg_keyword_search(更快、更精准)sg_nls_search
批量整合理解:
- 先读取2-3个相关文件再总结,而非读一个就提问
- 对于“X是如何工作的”这类问题,使用替代多次关键词搜索
sg_deepsearch_read
避免不必要的token浪费:
- 已知目标仓库时,不要搜索所有仓库
- 简单的“查找全部”查询不要使用
sg_deepsearch_read - 不要重复读取本次对话中已经看过的文件
Query Patterns
查询模式
| Intent | Query |
|---|---|
| React hooks | |
| API routes | |
| Error handling | |
| Type definitions | |
| Test setup | |
| Config files | |
| CI/CD | |
For more patterns, see .
query-patterns.md| 意图 | 查询语句 |
|---|---|
| React钩子 | |
| API路由 | |
| 错误处理 | |
| 类型定义 | |
| 测试初始化 | |
| 配置文件 | |
| CI/CD流程 | |
更多模式请查看。
query-patterns.mdOutput Formatting
输出格式
Search results:
- Present as a brief summary, not raw tool output
- Highlight the most relevant file and line
- Include a code snippet only if it directly answers the question
Code explanations:
- Start with a one-sentence summary
- Use the codebase's own terminology
- Reference specific files and functions
Recommendations:
- Present as numbered steps if actionable
- Link to specific patterns found in the codebase
- Note any existing utilities that should be reused
搜索结果:
- 以简短摘要呈现,而非原始工具输出
- 高亮最相关的文件和代码行
- 仅当代码片段直接回答问题时才包含
代码解释:
- 先以一句话总结
- 使用代码库自身的术语
- 引用具体的文件和函数
建议:
- 如果是可执行步骤,以编号列表呈现
- 链接到代码库中找到的具体模式
- 标注应复用的现有工具
Common Mistakes
常见错误
| Mistake | Fix |
|---|---|
| Searching all repos | Add |
| Too many results | Add |
| Missing relevant code | Try |
| Not understanding context | Use |
| Guessing patterns | Read implementations with |
| 错误 | 修复方案 |
|---|---|
| 搜索所有仓库 | 添加 |
| 结果过多 | 添加 |
| 找不到相关代码 | 尝试使用 |
| 无法理解上下文 | 使用 |
| 猜测代码模式 | 使用 |
Principles
原则
- Start narrow, expand if needed
- Chain tools: search → read → find references → definition
- Check tests for usage examples
- Read before generating
- 从窄范围开始,必要时再扩大
- 串联工具使用:搜索 → 读取文件 → 查找引用 → 查看定义
- 查看测试文件了解使用示例
- 先阅读理解,再生成内容