源编辑器Source editor

可靠的源编辑器对简单高效地编写代码至关重要。A reliable source editor is essential for writing code succinctly and efficiently. Visual Studio for Mac 提供了成熟的源编辑器,它是与 IDE 进行的交互的中心。Visual Studio for Mac provides a sophisticated source editor that is at the center of your interactions with the IDE. 源编辑器提供了轻松工作所需的功能:从语法突出显示、代码片段和代码折叠等基础功能,到其 Roslyn 编译集成(如全功能 IntelliSense 代码完成)带来的优势,功能全面。The source editor provides features that you might expect and need to do your work with ease: From the basics such a syntax highlighting, code snippets, and code folding, to the benefits of its Roslyn compiler integration, such as fully functional IntelliSense code completion.

Visual Studio for Mac 中的源编辑器支持与 IDE 中的所有其他功能(如调试、重构和版本控制集成)的无缝体验。The source editor in Visual Studio for Mac allows for a seamless experience with all the other functionality in the IDE such as debugging, refactoring, and version control integration.

本文介绍源编辑器的一些主要功能,并探讨如何使用 Visual Studio for Mac 来尽可能提高生产力。This article introduces some of the key features of the source editor and explores how you can use Visual Studio for Mac to be as productive as possible.

源编辑器体验The source editor experience

快速查看代码并在其中高效移动是开发工作流的重要组成部分。Viewing and moving efficiently throughout code is an integral part of the development workflow. 查看和维护代码的方式由自己确定,这可能因开发者而异,并且经常因项目而异。Exactly how you decide to view and maintain code is a personal decision, which varies between developers - and often between projects.

Visual Studio for Mac 提供许多强大的功能,最大限度提高了跨平台开发的易用性和实用性。Visual Studio for Mac offers many powerful features to make cross-platform development as accessible and as useful as possible. 以下部分介绍了一些重要功能。The following sections describe some of the highlights.

代码折叠Code folding

代码折叠支持开发者显示或隐藏完成的代码部分(如 using 指令、样板代码和注释以及 #region 语句),简化了大型源代码文件的管理。Code folding makes it easier to manage large source code files by allowing developers to show or hide complete sections of code, such as using directives, boilerplate code and comments, and #region statements. 在 Visual Studio for Mac 中,代码折叠默认为关闭状态Code folding is turned off by default in Visual Studio for Mac

要打开代码折叠,请导航到“Visual Studio”>“首选项”>“文本编辑器”>“常规”>“代码折叠”:To turn on code folding, navigate to Visual Studio > Preferences > Text Editor > General > Code Folding:

代码折叠选项

此菜单还提供默认折叠 #regions 和注释、显示命名提示、替代代码的选项。This menu also includes the option to fold #regions and comments by default, displaying a named hint, in place of code.

要显示或隐藏部分代码,请使用行号旁边的公开小组件:To show or hide sections, use the disclosure widget next to the line number:

显示或隐藏部分代码

也可通过使用“视图”>“折叠”>“切换折叠”/“切换所有折叠”菜单项,在显示和隐藏折叠间切换:You can also switch between showing and hiding the folds by using the View > Folding > Toggle Fold / Toggle All Folds menu item:

折叠菜单项

此菜单项也可用于启用或禁用代码折叠。This menu item can also be used to enable or disable code folding.

自动换行Word wrap

当处理长代码行或视图空间有限时,自动换行可以帮助你管理空间。Word wrap can assist you in managing space when working on long lines of code or with limited view space. 自动换行还可以确保你的代码视图包含源文件的全部内容,即使在打开可能会遮挡视图或缩小源视图宽度的窗格时也是如此。Word wrap can also ensure that your code view contains the full contents of your source file even when opening panes that may obscure your view or reduce the width of your source view.

自动换行默认禁用,但可以通过 Visual Studio for Mac 中的“首选项”启用。Word wrap is disabled by default but can be enabled via Preferences in Visual Studio for Mac.

要启用自动换行,请导航到“Visual Studio”>“首选项”>“文本编辑器”>“自动换行”:To enable word wrap, navigate to Visual Studio > Preferences > Text Editor > Word Wrap:

自动换行选项

启用自动换行后,源文件中超出源编辑器视图宽度的行将自动换行到下一行。With word wrap enabled, lines which exceed the width of your source editor view will automatically wrap to the next line within your source file. 还可以启用一个选项,以在换行的行旁边显示一个可见字形。You can also enable an option which will display a visible glyph next to wrapped lines. 它可以帮助你区分已自动换行的行和已手动换行的行。This will allow you to differentiate between lines which have been wrapped automatically and those which you've wrapped manually.

启用了自动换行的已换行文本

标尺Ruler

列标尺对于确定行长度很有帮助,对于具有行长度准则的工作团队尤其如此。The column ruler is useful for determining line lengths, particularly when working on a team that has line length guidelines. 要打开或关闭列标尺,请导航到“Visual Studio”>“首选项”>“文本编辑器”>“标记和标尺”,并选中(或取消选中)“显示列标尺”,如下图中所示 :The column ruler can be turned on or off by navigating to Visual Studio > Preferences > Text Editor > Markers and Rulers and selecting (or deselecting) Show Column ruler, as illustrated in the following image:

首选项对话框,其中突出显示了“显示列标尺”

在源编辑器中,它显示为垂直的浅灰色直线。This displays as a vertical light gray line in the source editor.

突出显示标识符引用Highlight identifier references

启用“突出显示标识符引用”选项后,可以选择源代码中的任何符号,且源编辑器会向该文件中的所有其他引用提供一个直观的指南。With the "Highlight identifier references" option is enabled, you can select any symbol in the source code and the source editor will provide a visual guide to all other references in that file. 要启用此选项,请导航到“Visual Studio”>“首选项”>“文本编辑器”>“标记和标尺”,并选中“突出显示标识符引用”,如下图所示:To turn on this option, go to Visual Studio > Preferences > Text Editor > Markers and Rulers and select Highlight identifier references, as illustrated in the following image:

首选项对话框,其中突出显示了“突出显示标识符引用”

突出显示的颜色还有助于指示正在分配或引用的某些内容。The color of the highlight is also useful for denoting that something is being assigned or referenced. 分配的内容以红色突出显示;引用的内容以蓝色突出显示:If something is assigned, it is highlighted in red; if it is referenced, it is highlighted in blue:

显示突出显示颜色的示例

请参阅See also