重構Refactoring

重構程式碼是重新排列、重建和釐清現有程式碼的方法,並能同時確保程式碼的整體行為不會變更。Refactoring code is a way to rearrange, restructure, and clarify existing code while ensuring that the overall behavior of the code doesn't change.

重構會產生較健康的程式碼基底,以讓您、任何其他開發人員或可能參考程式碼的使用者更容易使用、讀取和維護它。Refactoring produces a healthier code base, making it more useable, readable, and maintainable for you or any other developer or user that might refer to the code.

Visual Studio for Mac 與 Roslyn (Microsoft 的開放原始碼 .NET 編譯器平台) 的整合,可讓您進行更多重構作業。Visual Studio for Mac's integration with Roslyn, Microsoft's open-source .NET compiler platform, allows for more refactoring operations.

重新命名Renaming

[重新命名] 重構命令可以用於任何程式碼識別碼 (例如,類別名稱、屬性名稱等等),以尋找該識別碼的所有出現項目並進行變更。The Rename refactoring command can be used on any code identifier (for example, a class name, property name etc.) to find all occurrences of that identifier and change them. 若要重新命名符號,請以滑鼠右鍵按一下它,然後選擇 [重新命名],或使用 Cmd (⌘) + R 按鍵繫結關係:To rename a symbol, right-click on it and choose Rename..., or use the Cmd (⌘) + R key binding:

重新命名功能表項目

這將醒目提示符號和其任何參考。This highlights the symbol and any references to it. 當您開始鍵入新名稱時,您程式碼中的所有參考都會自動變更,您可以按 Enter 認可變更:When you start typing a new name it automatically changes all references in your code, and you can commit your changes by pressing Enter:

重新命名和識別碼

快速動作Quick actions

快速動作可讓您輕鬆地重構、產生或用其他方式以單一動作修改程式碼。Quick Actions let you easily refactor, generate, or otherwise modify code with a single action.

快速動作可用於:Quick Actions can be used to:

  • 針對程式碼分析器規則的違規情況套用程式碼修正Apply a code fix for a code analyzer rule violation
  • 隱藏程式碼分析器規則的違規情況Suppress a code analyzer rule violation
  • 套用重構作業 (例如,內嵌暫存變數)Apply a refactoring (for example, inline a temporary variable)
  • 產生程式碼 (例如,引進區域變數)Generate code (for example, introduce a local variable)

您可以使用燈泡 燈泡圖示 或螺絲起子 螺絲起子圖示 圖示,或當游標位於有可用動作的程式碼行時按 Option (⌥)+Enter 來套用快速動作。Quick Actions can be applied by using the light bulb light bulb icon or screwdriver screwdriver icon icons, or by pressing Option (⌥)+Enter when your cursor is on a line of code for which an action is available. 如果有紅色波浪線指出錯誤,而且 Visual Studio 有該錯誤可用的修正程式,您就會看到錯誤燈泡 錯誤燈泡圖示You'll see an error light bulb error light bulb icon if there is a red squiggle indicating an error, and Visual Studio has a fix available for that error.

好比說,協力廠商可以針對任何語言,在 SDK 當中提供自訂診斷和建議,而 Visual Studio 燈泡會依據這些規則亮燈。For any language, third parties can provide custom diagnostics and suggestions, for example as part of an SDK, and Visual Studio light bulbs light up based on those rules.

快速動作圖示Quick Action Icons

當有快速動作可用時,顯示的圖示會指出可用的修正或重構類型。The icon that appears when a Quick Action is available gives an indication of the type of fix or refactoring that's available. 「螺絲起子」螺絲起子圖示圖示表示有可變更程式碼的動作,但不一定要使用。The screwdriver screwdriver icon icon indicates just that there are actions available to change the code, but you shouldn't necessarily use them. 「黃色燈泡」燈泡圖示圖示表示有「應」執行的動作,以改善程式碼。The yellow light bulb light bulb icon icon indicates there are actions available that you should do to improve your code. 「錯誤燈泡」錯誤燈泡圖示圖示表示有動作可修正您程式碼中的錯誤。The error light bulb error light bulb icon icon indicates there's an action available that fixes an error in your code.

顯示燈泡或螺絲起子To see a light bulb or screwdriver

  • 如果有可用的修正,燈泡會在您將滑鼠暫留於錯誤位置的同時顯示。If a fix is available, light bulbs spontaneously appear when you hover the mouse at the location of an error.

    當滑鼠游標暫留時的燈泡

  • 燈泡和螺絲起子會在您將游標移到可使用快速動作的程式碼上時,顯示在編輯器的左側邊界。Light bulbs and screwdrivers appear in the left margin of the editor when you move the caret into a line of code for which a Quick Action is available.

  • 在程式碼行任意處按 Option (⌥)+Enter,即可看到可用快速動作與重構的清單。Press Option (⌥)+Enter anywhere on a line to see a list of available Quick Actions and refactorings.

顯示內容項目

將滑鼠游標移至任何內容動作上方,會提供從程式碼所新增或移除項目的預覽。Hovering over any of the context actions provides you with a preview of what will be added or removed from your code.

選項輸入內容項目

若要啟用這些選項,您必須選取 [Visual Studio for Mac] > [喜好設定] > [文字編輯器] > [來源分析] 選項中的 [啟用開啟檔案的來源分析]:To enable these options, you must select Enable source analysis of open files in the options Visual Studio for Mac > Preferences > Text Editor > Source Analysis:

啟用來源分析

可建議超過 100 個可能的動作,其啟用或停用方式是瀏覽至 [Visual Studio for Mac] > [喜好設定] > [來源分析] > [C#] > [程式碼動作],並選取或取消選取動作旁的方塊:There are over 100 possible actions that can be suggested, which are enabled or disabled by browsing to Visual Studio for Mac > Preferences > Source Analysis > C# > Code Actions and selecting or unselecting the box next to the action:

C# 來源分析動作

一般快速動作Common quick actions

您可以在一般快速動作一文章深入了解一般快速動作。You can learn more about common quick actions in the Common Quick Actions article.

來源分析Source analysis

來源分析透過將潛在錯誤和樣式違規加上底線,並將自動修正提供為內容動作,來即時分析您的程式碼。Source analysis analyzes your code on the fly by underlining potential errors and style violations, and providing auto fixes as context actions.

檢視文字編輯器右側的捲軸,即可隨時檢視任何檔案的所有來源分析結果:You can view all results of the source analysis for any file, at any time, by viewing the scroll bar on the right side of the text editor:

來源分析提要欄位

如果您按一下頂端的圓圈,則可以逐一查看每項建議,而最高嚴重性問題會顯示在最前面。If you click on the circle at the top, you can iterate through each suggestion, with the highest severity issues showing first. 將滑鼠游標移至個別結果或個別行上方即會顯示問題,這可透過內容動作進行修正:Hovering over an individual result or line displays the issue, which can be fixed through context actions:

來源分析項目

另請參閱See also