searching-sourcegraph

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Searching Sourcegraph

使用Sourcegraph进行搜索

Search before you build. Existing patterns reduce tokens, ensure consistency, and surface tested solutions.
在开发前先搜索。已有的代码模式可以减少token消耗、确保一致性,还能找到经过测试的解决方案。

Tool Selection Logic

工具选择逻辑

Start here:
  1. Know the exact symbol or pattern?
    sg_keyword_search
  2. Know the concept, not the code?
    sg_nls_search
  3. Need to understand how/why?
    sg_deepsearch_read
  4. Tracing a symbol's usage?
    sg_find_references
  5. Need full implementation?
    sg_go_to_definition
    sg_read_file
GoalTool
Concepts/semantic search
sg_nls_search
Exact code patterns
sg_keyword_search
Trace usage
sg_find_references
See implementation
sg_go_to_definition
Understand systems
sg_deepsearch_read
Read files
sg_read_file
Browse structure
sg_list_files
Find repos
sg_list_repos
Search commits
sg_commit_search
Track changes
sg_diff_search
Compare versions
sg_compare_revisions
从这里开始:
  1. 知道确切的符号或模式?
    sg_keyword_search
  2. 知道概念但不清楚具体代码?
    sg_nls_search
  3. 需要了解运作方式/原理?
    sg_deepsearch_read
  4. 追踪符号的使用场景?
    sg_find_references
  5. 需要完整实现代码?
    sg_go_to_definition
    sg_read_file
目标工具
概念/语义搜索
sg_nls_search
精确代码模式
sg_keyword_search
追踪使用场景
sg_find_references
查看实现代码
sg_go_to_definition
理解系统架构
sg_deepsearch_read
读取文件内容
sg_read_file
浏览文件结构
sg_list_files
查找代码仓库
sg_list_repos
搜索提交记录
sg_commit_search
追踪代码变更
sg_diff_search
对比版本差异
sg_compare_revisions

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 mocks
Start narrow. Expand only if results are empty.
Combine filters:
repo:^github.com/myorg/backend$ file:src/handlers lang:typescript
repo:^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:typescript

Context-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
    sg_deepsearch_read
    for architectural understanding
  • Follow up with
    sg_read_file
    on key files mentioned in the response
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:
  • workflows/implementing-feature.md
    — when building new features
  • workflows/understanding-code.md
    — when exploring unfamiliar systems
  • workflows/debugging-issue.md
    — when tracking down bugs
如需详细的分步工作流,请查看:
  • workflows/implementing-feature.md
    — 开发新功能时
  • workflows/understanding-code.md
    — 探索陌生系统时
  • workflows/debugging-issue.md
    — 排查Bug时

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
    sg_keyword_search
    over
    sg_nls_search
    when you have exact terms (faster, more precise)
Batch your understanding:
  • Read 2-3 related files before synthesising, rather than reading one and asking questions
  • Use
    sg_deepsearch_read
    for "how does X work" instead of multiple keyword searches
Avoid common token waste:
  • Don't search all repos when you know the target repo
  • Don't use
    sg_deepsearch_read
    for simple "find all" queries
  • 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

查询模式

IntentQuery
React hooks
file:.*\.tsx$ use[A-Z].*= \(
API routes
file:src/api app\.(get|post|put|delete)
Error handling
catch.*Error|\.catch\(
Type definitions
file:types/ export (interface|type)
Test setup
file:.*\.test\. beforeEach|beforeAll
Config files
file:(webpack|vite|rollup)\.config
CI/CD
file:\.github/workflows deploy
For more patterns, see
query-patterns.md
.
意图查询语句
React钩子
file:.*\.tsx$ use[A-Z].*= \(
API路由
file:src/api app\.(get|post|put|delete)
错误处理
catch.*Error|\.catch\(
类型定义
file:types/ export (interface|type)
测试初始化
file:.*\.test\. beforeEach|beforeAll
配置文件
file:(webpack|vite|rollup)\.config
CI/CD流程
file:\.github/workflows deploy
更多模式请查看
query-patterns.md

Output 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

常见错误

MistakeFix
Searching all reposAdd
repo:^github.com/org/repo$
Too many resultsAdd
file:
pattern or keywords
Missing relevant codeTry
sg_nls_search
for semantic matching
Not understanding contextUse
sg_deepsearch_read
Guessing patternsRead implementations with
sg_read_file
错误修复方案
搜索所有仓库添加
repo:^github.com/org/repo$
限定
结果过多添加
file:
模式或关键词
找不到相关代码尝试使用
sg_nls_search
进行语义匹配
无法理解上下文使用
sg_deepsearch_read
猜测代码模式使用
sg_read_file
查看实现代码

Principles

原则

  • Start narrow, expand if needed
  • Chain tools: search → read → find references → definition
  • Check tests for usage examples
  • Read before generating
  • 从窄范围开始,必要时再扩大
  • 串联工具使用:搜索 → 读取文件 → 查找引用 → 查看定义
  • 查看测试文件了解使用示例
  • 先阅读理解,再生成内容