編輯器的行為Editor Behavior

可以設定編輯器的行為,讓程式碼在撰寫時即格式化。Editor behaviors can be set to allow code to be formatted as it is written. 這些動作設定於 [Visual Studio] > [喜好設定] > [文字編輯器] > [行為] 下,以下是一些較常用的函式:These actions are set under Visual Studio > Preferences... > Text Editor > Behavior, and some of the more commonly used functions are described below:

編輯器的行為選項

  • 建立新的類別、方法或屬性時,對稱的右大括弧可以自動新增到程式碼。Matching closing braces can be added automatically to code when creating new classes, methods, or properties. 選取此選項時,輸入 { 會自動新增 }When this option is selected, typing { will automatically add }.
  • 字元按壓會觸發即時程式碼格式化,例如分號或大括弧,這會模擬設定的格式化喜好設定。On-the-fly code formatting is triggered by character presses, such as semi-colon or braces, which will emulate the formatting preferences that are set.
  • 您也可以選擇在儲存檔案時將它格式化,如此可以視需要撰寫程式碼,讓 IDE 負責依現有喜好設定所設定地進行程式碼格式化。You can also choose to format the file when saving it, which allows the writing of code as desired and leaves the IDE responsible for formatting code as set by existing preferences.
  • 縮排可設定為無、自動或智慧型。Indentation can be set to None, Auto, or Smart. 這些會執行下列動作:These do the following:
    • 無 - 將插入號設定到下一行的開頭None - sets the caret to the start of the next line
    • 自動 - 將插入號設定到下一行的相同資料行Auto - sets the caret to the same column on the next line
    • 智慧型 - 根據程式碼在下一行進行縮排Smart - indents on the following line based on the code
  • 各個作業系統之間的斷詞行為不同,而為了瀏覽目的,文字編輯器必須知道單字開頭或結尾的位置。Word-breaking behavior differs between OSes, and for navigation purposes, the text editor needs to know where words begin or end. 格式化可以設定為 Unix 或 Windows。The formatting can be set to Unix or Windows.

您也可以設定 XML、CSS、HTML 和 JSON 的格式化規則。You can also set formatting rules for XML, CSS, HTML, and JSON.