程式碼片段Code snippets

程式碼片段通常稱為「程式碼範本」,適用於具效率的程式設計,因為它們允許插入和編輯預先撰寫的程式碼區塊。Code snippets, often referred to as code templates , are useful for efficient programming as they allow the insertion and editing of pre-written blocks of code. 使用程式碼片段可便於迅速新增常見模式,或甚至在您是開發人員但不確定語法時學習新的模式。Using code snippets can be convenient for quickly adding common patterns, or even for learning new patterns when as the developer you are unsure of syntax. 有針對 C#、F#、HTML、XML、Python 和 Razor 提供的範本。There are templates provided for C#, F#, HTML, XML, Python, and Razor.

本節說明如何在程式碼中建立、插入和使用程式碼片段。This section explains how to create, insert, and use snippets in code.

插入程式碼片段Inserting a snippet

有一些不同的方式可以新增程式碼片段,而下面說明其中一部分:There are some different ways to add code snippets, some of which are described below:

  • 定位點擴充 – 開始輸入範本名稱,從清單中選取它,然後按 TabTab 來予以新增:Tab Expansion – Start typing the template name, select it from the list and press Tab , Tab to add it:

    程式碼中的定位點擴充

  • 工具箱 – 使用 [工具箱] 視窗來顯示所有程式碼片段的清單。Toolbox – Use the Toolbox Window to display a list of all code snippets. 將任何範本從工具箱拖曳至原始程式碼中的正確位置:Drag any template from the toolbox into the correct position in the source code:

    工具箱中的程式碼片段Code snippets in Toolbox

  • 插入範本命令 – 目前未設定任何用於插入範本的預設按鍵繫結關係。Insert Templates command – There is currently no default key binding set for inserting a template. 若要建立一個,請流覽至 Visual Studio > 喜好設定 > 按鍵 系結,並搜尋 templateTo create one, browse to Visual Studio > Preferences > Key Bindings and search for template. 這允許將想要的按鍵繫結關係新增至 [編輯繫結] 欄位,然後按一下 [套用]:This allows adding the desired key binding into the Edit Binding field, then click Apply :

    插入範本命令

建立新範本Creating a new template

雖然您可以使用和編輯多個各種語言的現有範本,但也可以新增範本,方法是巡覽至 [Visual Studio] > [喜好設定] > [文字編輯器] > [程式碼片段]:While there are many existing templates in a variety of languages that you can use and edit, new templates can also be added by navigating to Visual Studio > Preferences > Text Editor > Code Snippets :

插入新範本

按 [新增] 或 [編輯] 按鈕以建立或編輯程式碼片段。Press the Add or Edit buttons to create or edit snippets.

程式碼片段中的關鍵字Keywords in code snippets

將程式碼片段插入編輯器中之後,所有已定義的關鍵字都會以醒目提示方式顯示,您可以藉由使用 Tab 鍵在它們之間移動來編輯它們。After a code snippet is inserted into the editor, any keywords defined are highlighted and can be edited by tabbing between them. 關鍵字的作用就像程式碼片段中的「變數」,若要定義關鍵字,請在關鍵字名稱的前後加上一個貨幣符號 $Keywords behave like a "variable" in the code snippet and are defined by placing a dollar-sign $ before and after the name of the keyword.

以下顯示 [編輯範本] 視窗,其中正在編輯內建的 prop 程式碼片段。The Edit template window is shown below, editing the built-in prop snippet. 此程式碼片段包含兩個關鍵字 – $type$ ,且 $name$ – 可以設定進一步的屬性 (例如視窗右邊的預設值和工具提示) :The snippet contains two keywords – $type$ and $name$ – which can have further properties set (such as a default value and tooltip) on the right side of the window:

[編輯範本] 視窗

下列欄位可用來定義程式碼片段:The following fields are used to define a snippet:

  • 捷徑 – 使用者所輸入來插入程式碼片段的文字。Shortcut – The text the user types to insert the snippet.
  • 群組 – 程式碼片段會使用此值在程式碼片段內容功能表中一起組成群組。Group – Snippets are grouped together in the snippet content menu, using this value.
  • 描述 – 程式碼片段用途的說明。Description – Explanation of the snippet's purpose.
  • MIME – 控制可用程式碼片段的檔案類型。Mime – Controls what file types the snippet is available in.
  • Is expandable template (是可擴充的範本) – 請務必選取此選項,如此才可藉由輸入捷徑,在游標位置插入程式碼片段。Is expandable template – Ensure this is checked so that the snippet can be inserted at the cursor by typing the shortcut.
  • Is surround with template (是範圍陳述式範本) – 若要在編輯器的 [範圍陳述式] 內容功能表中列出此捷徑,請選取此選項。Is surround with template – Check this option to list this shortcut in the Surround with... content menu in the editor.
  • 範本文字 – 將插入到編輯器中的實際程式碼片段。Template text – The actual snippet that will be inserted into the editor. 定義關鍵字預留位置時,可藉由以貨幣符號括住語彙基元來定義,例如:Keyword placeholders can be defined by surrounding a token with dollar signs eg. $type$.$type$.
  • 關鍵字屬性面板 – 在視窗右側,使用頂端的下拉式清單來選擇關鍵字 (例如 type),然後編輯預設值和工具提示等屬性。Keyword property panel – On the right-side of the window, use the drop-down list at the top to choose a keyword (eg type) and edit properties like default value and tooltip.

在編輯器中使用關鍵字Using keywords in the editor

若要使用程式碼片段搭配關鍵字 (例如上方所定義的關鍵字),請輸入捷徑並按兩次 Tab 鍵,就會在游標位置插入程式碼片段內容:To use a snippet with keywords, such as the one defined above, type the shortcut and press Tab twice, and the snippet contents will be inserted at the cursor:

顯示關鍵字的已插入程式碼片段

Tab 鍵以在 objectMyProperty 之間移動,以自訂您類別的程式碼片段。Press the Tab key to move between object and MyProperty to customize the snippet for your class.

關鍵字可以在程式碼片段中重複出現,例如這個 for 範例,請注意 $i$ 關鍵字出現 3 次:A keyword can be repeated in a snippet, such as this for example, notice the $i$ keyword appears 3 times:

含有重複關鍵字的程式碼片段

在編輯器中使用時, Tab 鍵會在第一個 imax 之間切換。When used in the editor, the Tab key will switch between the first i and max. 如果您將 i 取代成不同的變數名稱,三個執行個體都會更新:If you overtype the i with a different variable name, all three instances will be updated:

顯示多個關鍵字的已插入程式碼片段

保留關鍵字Reserved keywords

您可以在程式碼片段中使用兩個保留關鍵字:There are two reserved keywords that you can use in a snippet:

  • $selected$ – 如果已為程式碼片段選取 [Is surround with template] (是範圍陳述式範本),就會以選擇程式碼片段時在編輯器中以醒目提示方式顯示的文字取代此關鍵字。$selected$ – If the snippet has Is surround with template checked, this keyword will be replaced by the text that was highlighted in the editor when the snippet was chosen.
  • $end$–當使用者完成編輯程式碼片段中的關鍵字時,游標將放置於關鍵字的位置 $end$$end$ – When the user has finished editing the keywords in a snippet, the cursor will be placed at the location of the $end$ keyword.

上一節中的 for 程式碼片段即是這兩個保留關鍵字的範例。The for snippet in the previous section is an example of both these reserved keywords.

請參閱See also