# 在GitHub上导航代码

你可以直接在 GitHub 中导航代码，来理解存储库内及存储库之间的关系。

<!-- If you make changes to this feature, check whether any of the changes affect languages listed in /get-started/learning-about-github/github-language-support. If so, please update the article accordingly. -->

## 关于在 GitHub 上导航代码

代码导航通过显示命名实体的定义与该实体引用的对应关系，以及显示实体定义与其所有引用之间的关系，帮助您阅读、导航和理解代码。

![显示带有突出显示函数的文件的屏幕截图。 弹出窗口在“Definition”和“Reference”两个选项卡上提供有关函数的信息。](/assets/images/help/repository/code-navigation-popover.png)

代码导航使用 open source [`tree-sitter`](https://github.com/tree-sitter/tree-sitter) 库。 以下语言支持代码导航。

* Bash
* C
* C#
* C++
* CodeQL
* Elixir
* Go
* JSX
* Java
* JavaScript
* Lua
* PHP
* 协议缓冲区
* Python
* R
* Ruby
* Rust
* Scala
* Starlark
* Swift
* Typescript

您无需在存储库中配置任何内容即可启用代码导航。 我们将在所有仓库中自动提取这些受支持语言的代码导航信息。

GitHub 基于开源 [`tree-sitter`](https://github.com/tree-sitter/tree-sitter) 库开发了一种代码导航方法，该库搜索整个存储库中的所有定义和引用，以查找具有给定名称的实体。

可以使用键盘快捷方式在代码文件中导航。 有关详细信息，请参阅“[键盘快捷方式](/zh/get-started/accessibility/keyboard-shortcuts#navigating-within-code-files)”。

## 使用符号窗格

现在，可以使用符号窗格在代码中的函数或类等符号之间快速查看和导航。 可以在单个文件中、存储库中的所有文件中搜索符号，甚至可以在 GitHub 上的所有公共存储库中搜索符号。

符号搜索是代码搜索的一项功能。 有关详细信息，请参阅“[了解GitHub代码搜索语法](/zh/search-github/github-code-search/understanding-github-code-search-syntax#symbol-qualifier)”。

1. 选择存储库，然后导航到包含符号的文件。

2. 若要打开符号窗格，请在文件内容上方单击 <svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-code-square" aria-label="The code square icon" role="img"><path d="M0 1.75C0 .784.784 0 1.75 0h12.5C15.216 0 16 .784 16 1.75v12.5A1.75 1.75 0 0 1 14.25 16H1.75A1.75 1.75 0 0 1 0 14.25Zm1.75-.25a.25.25 0 0 0-.25.25v12.5c0 .138.112.25.25.25h12.5a.25.25 0 0 0 .25-.25V1.75a.25.25 0 0 0-.25-.25Zm7.47 3.97a.75.75 0 0 1 1.06 0l2 2a.75.75 0 0 1 0 1.06l-2 2a.749.749 0 0 1-1.275-.326.749.749 0 0 1 .215-.734L10.69 8 9.22 6.53a.75.75 0 0 1 0-1.06ZM6.78 6.53 5.31 8l1.47 1.47a.749.749 0 0 1-.326 1.275.749.749 0 0 1-.734-.215l-2-2a.75.75 0 0 1 0-1.06l2-2a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042Z"></path></svg>。

   或者，可以通过单击文件中的合格符号来打开符号窗格。 将鼠标悬停在可单击符号上时，它们会以黄色突出显示。

3. 单击要从符号窗格或文件本身中查找的符号。

   * 若要在整个存储库中搜索符号，请在符号窗格单击“在此存储库中搜索此符号”。 若要在 GitHub 上的所有存储库中搜索符号，请单击“所有存储库”。

4. 若要在对符号的引用之间导航，请单击 <svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-chevron-down" aria-label="The downwards-facing chevron icon" role="img"><path d="M12.78 5.22a.749.749 0 0 1 0 1.06l-4.25 4.25a.749.749 0 0 1-1.06 0L3.22 6.28a.749.749 0 1 1 1.06-1.06L8 8.939l3.72-3.719a.749.749 0 0 1 1.06 0Z"></path></svg> 或 <svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-chevron-up" aria-label="The upwards-facing chevron icon" role="img"><path d="M3.22 10.53a.749.749 0 0 1 0-1.06l4.25-4.25a.749.749 0 0 1 1.06 0l4.25 4.25a.749.749 0 1 1-1.06 1.06L8 6.811 4.28 10.53a.749.749 0 0 1-1.06 0Z"></path></svg>。

5. 要导航到对符号的特定引用，请单击“<svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-chevron-down" aria-label="chevron-down" role="img"><path d="M12.78 5.22a.749.749 0 0 1 0 1.06l-4.25 4.25a.749.749 0 0 1-1.06 0L3.22 6.28a.749.749 0 1 1 1.06-1.06L8 8.939l3.72-3.719a.749.749 0 0 1 1.06 0Z"></path></svg> In this file”下的符号搜索结果\*\*\*\*。

6. 要退出对特定符号的搜索，请单击“<svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-arrow-left" aria-label="arrow-left" role="img"><path d="M7.78 12.53a.75.75 0 0 1-1.06 0L2.47 8.28a.75.75 0 0 1 0-1.06l4.25-4.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042L4.81 7h7.44a.75.75 0 0 1 0 1.5H4.81l2.97 2.97a.75.75 0 0 1 0 1.06Z"></path></svg> All Symbols”\*\*\*\*。

## 跳至功能或方法的定义

您可以单击文件中的函数或方法调用，跳转到同一代码库中该函数或方法的定义。

![函数窗口的屏幕截图。 标题为“定义”的部分以深橙色轮廓显示。](/assets/images/help/repository/jump-to-definition-tab.png)

## 查找函数或方法的所有引用

通过单击文件中的函数或方法调用，可以在同一存储库中找到函数或方法的所有引用。

![函数窗口的屏幕截图。 标题为“3 个引用”的部分使用深橙色描边。](/assets/images/help/repository/find-all-references-tab.png)

## 代码导航疑难解答

如果已为您启用代码导航，但您没有看到指向函数和方法定义的链接：

* 代码导航仅适用于活动的分支。 推送到分支，然后重试。
* 代码导航仅适用于文件少于 100,000 个的存储库。

## 其他阅读材料

* [关于GitHub代码搜索](/zh/search-github/github-code-search/about-github-code-search)