原始檔編輯器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.

空白字元White space

您可能需要檢視原始程式碼中的不可見字元。It may be necessary for you to view invisible characters in source code. 這是能確保您符合編碼標準且不會浪費空間的可見方法。It's a visible way to make sure that you're adhering to coding standards and not needlessly wasting space. 此方法在撰寫 F# 時也十分有用,因為撰寫 F# 需仰賴針對評估程式碼的精確縮排行。It's also useful when writing F#, which depends on precisely indented lines for evaluating code.

巡覽至 [Visual Studio] > [喜好設定] > [文字編輯器] > [標記與尺規],來設定顯示空白的選項。Set options to show whitespace by navigating to Visual Studio > Preferences > Text Editor > Markers and Rulers. 選取此選項允許設定將顯示不可見字元的「時機」 :[永不]、[選取時] 或 [一律]:Selecting this option allows setting when invisible characters will be shown: Never, On Selection, or Always:

顯示不可見字元選項

也會提供顯示定位點、空格和行尾結束符號的選項:The option to show tabs, spaces, and line endings is also available:

顯示索引標籤和空格

不可見字元會顯示為灰色點,如下圖所示:Invisible characters are displayed as gray dots, as illustrated in the following image:

顯示的空白字元

尺規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)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