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? →
keyword_search - Know the concept, not the code? →
nls_search - Need to understand how/why? → →
deepsearchdeepsearch_read - Tracing a symbol's usage? →
find_references - Need full implementation? → →
go_to_definitionread_file - Need to know what repos a user has worked on? →
get_contributor_repos
| Goal | Tool |
|---|---|
| Concepts/semantic search | |
| Exact code patterns | |
| Trace usage | |
| See implementation | |
| Initiate a deep search | |
| Read deep search results | |
| Read files | |
| Browse structure | |
| Find repos | |
| Search commits | |
| Track changes | |
| Compare versions | |
| Find repos a user has worked on | |
从这里开始:
- 知道确切的符号或模式? →
keyword_search - 知道概念但不知道具体代码? →
nls_search - 需要了解运作方式/原因? → →
deepsearchdeepsearch_read - 追踪符号的使用情况? →
find_references - 需要完整实现代码? → →
go_to_definitionread_file - 需要了解用户参与过哪些仓库? →
get_contributor_repos
| 目标 | 工具 |
|---|---|
| 概念/语义搜索 | |
| 精确代码模式 | |
| 追踪使用情况 | |
| 查看实现代码 | |
| 启动深度搜索 | |
| 读取深度搜索结果 | |
| 读取文件 | |
| 浏览结构 | |
| 查找仓库 | |
| 搜索提交记录 | |
| 追踪变更 | |
| 对比版本 | |
| 查找用户参与过的仓库 | |
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":
- Use to initiate the search, then
deepsearchto retrieve resultsdeepsearch_read - Follow up with on key files mentioned in the response
read_file
When the user asks who worked on something or what repos a contributor has touched:
- Use with one or more usernames to discover their active repositories
get_contributor_repos - Then scope subsequent searches to those repos
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 troubleshooting an error, build failure, or runtime exception:
- Extract exact symbols, error codes, or log lines from the stack trace or build output
- Search for the error site, then trace the full call chain with
find_references - Check recent changes with and
diff_searchearly — regressions are commoncommit_search - Identify all affected code paths and services before proposing a fix
When fixing a bug:
- Extract exact symbols from the error message or stack trace
- Search for the error site, then trace the full call chain with
find_references - Check recent changes with and
diff_searchearly — regressions are commoncommit_search - Find all affected code paths before writing the fix
- Read existing tests to understand intended behaviour
当用户提供文件路径或错误信息时:
- 提取符号、函数名或错误代码
- 先搜索这些精确术语
- 如果错误涉及已知符号,追踪其引用
当用户询问“X是如何工作的”时:
- 使用启动搜索,再用
deepsearch获取结果deepsearch_read - 对响应中提到的关键文件,后续使用查看
read_file
当用户询问谁参与过某项工作,或贡献者接触过哪些仓库时:
- 使用并传入一个或多个用户名,发现他们活跃的仓库
get_contributor_repos - 之后将后续搜索范围限定在这些仓库内
当用户正在实现新功能时:
- 先搜索类似的现有实现
- 阅读测试用例获取使用示例
- 在创建新工具前先检查是否有可复用的共享工具
当排查错误、构建失败或运行时异常时:
- 从堆栈跟踪或构建输出中提取精确的符号、错误代码或日志行
- 搜索错误发生位置,再用追踪完整调用链
find_references - 尽早用和
diff_search检查近期变更——回归问题很常见commit_search - 在提出修复方案前,确定所有受影响的代码路径和服务
当修复bug时:
- 从错误信息或堆栈跟踪中提取精确符号
- 搜索错误发生位置,再用追踪完整调用链
find_references - 尽早用和
diff_search检查近期变更——回归问题很常见commit_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 troubleshooting errors, build failures, stack traces, support issues, or runtime exceptions
workflows/debugging-issue.md - — when fixing bugs with extensive Sourcegraph search
workflows/fixing-bug.md - — when reviewing a pull request or changeset
workflows/code-review.md
如需详细的分步工作流,请查看:
- —— 构建新功能时
workflows/implementing-feature.md - —— 探索不熟悉的系统时
workflows/understanding-code.md - —— 排查错误、构建失败、堆栈跟踪、支持问题或运行时异常时
workflows/debugging-issue.md - —— 使用Sourcegraph深度搜索修复bug时
workflows/fixing-bug.md - —— 审查拉取请求或变更集时
workflows/code-review.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
keyword_searchwhen you have exact terms (faster, more precise)nls_search
Batch your understanding:
- Read 2-3 related files before synthesising, rather than reading one and asking questions
- Use +
deepsearchfor "how does X work" instead of multiple keyword searchesdeepsearch_read
Avoid common token waste:
- Don't search all repos when you know the target repo
- Don't use for simple "find all" queries —
deepsearchis fasterkeyword_search - Don't re-read files you've already seen in this conversation
减少工具调用次数:
- 按逻辑链式调用工具:搜索 → 读取 → 查找引用 → 查看定义
- 不要重复搜索相同模式;使用之前调用的结果
- 当有精确术语时,优先使用而非
keyword_search(更快、更精准)nls_search
批量理解内容:
- 先阅读2-3个相关文件再进行总结,而非读一个文件就提问
- 对于“X是如何工作的”这类问题,使用+
deepsearch,而非多次关键词搜索deepsearch_read
避免常见的token浪费:
- 当知道目标仓库时,不要搜索所有仓库
- 简单的“查找全部”查询不要使用——
deepsearch更快keyword_search - 不要重复阅读本次对话中已经看过的文件
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 hooks | |
| 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
- 从窄范围开始,必要时再扩大
- 链式调用工具:搜索 → 读取 → 查找引用 → 查看定义
- 查看测试用例获取使用示例
- 先阅读再生成内容