Skip to content

Commit a3f5f0d

Browse files
authored
Merge pull request #4814 from gewarren/snippet-wt
Fix up snippet walkthrough
2 parents 9286b1d + 94bc10a commit a3f5f0d

File tree

4 files changed

+113
-154
lines changed

4 files changed

+113
-154
lines changed

docs/ide/code-snippets-schema-reference.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,11 @@ Specifies the type of the object. The `Object` element is used to identify an it
626626
| - |-----------------|
627627
|[Object element](../ide/code-snippets-schema-reference.md#object-element)|Defines the object fields of the code snippet that you can edit.|
628628

629-
A text value is required. This text specifies the type of the object.
629+
A text value is required. This text specifies the type of the object. For example:
630+
631+
```xml
632+
<Type>System.Data.SqlClient.SqlConnection</Type>
633+
```
630634

631635
## Url element
632636

Loading
Loading

docs/ide/walkthrough-creating-a-code-snippet.md

Lines changed: 108 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Walkthrough: Create a code snippet"
3-
ms.date: 10/27/2017
3+
ms.date: 06/10/2019
44
ms.topic: conceptual
55
helpviewer_keywords:
66
- "code snippets, creating"
@@ -20,11 +20,11 @@ ms.workload:
2020
---
2121
# Walkthrough: Create a code snippet
2222

23-
You can create a code snippet with only a few steps. All you need to do is create an XML file, fill in the appropriate elements, and add your code to it. You can also add references and replacement parameters to your code. You can add the snippet to your Visual Studio installation by using the **Import** button on the **Code Snippets Manager** (**Tools** > **Code Snippets Manager**).
23+
You can create a code snippet with only a few steps. All you need to do is create an XML file, fill in the appropriate elements, and add your code to it. You can optionally make use of replacement parameters and project references. Import the snippet to your Visual Studio installation by using the **Import** button in the **Code Snippets Manager** (**Tools** > **Code Snippets Manager**).
2424

2525
## Snippet template
2626

27-
The following is the basic snippet template:
27+
The following XML is the basic snippet template:
2828

2929
```xml
3030
<?xml version="1.0" encoding="utf-8"?>
@@ -42,227 +42,182 @@ The following is the basic snippet template:
4242
</CodeSnippets>
4343
```
4444

45-
### Create a code snippet
45+
## Create a code snippet
4646

4747
1. Create a new XML file in Visual Studio and add the template shown above.
4848

49-
2. Fill in the title of the snippet, e.g. "Hello World VB", in the **Title** element.
49+
2. Fill in the title of the snippet in the **Title** element. Use the title **Square Root**.
5050

51-
3. Fill in the language of the snippet in the **Language** attribute of the **Code** element. For this example, use "VB".
51+
3. Fill in the language of the snippet in the **Language** attribute of the **Code** element. For C#, use **CSharp**, and for Visual Basic, use **VB**.
5252

53-
4. Add some code in the **CDATA** section inside the **Code** element, for example:
53+
> [!TIP]
54+
> To see all the available language values, browse the [Code element attributes section](code-snippets-schema-reference.md#attributes) on the [Code snippets schema reference](code-snippets-schema-reference.md) page.
5455
55-
```xml
56-
<Code Language="VB">
57-
<![CDATA[Console.WriteLine("Hello, World!")]]>
58-
</Code>
59-
```
56+
4. Add the snippet code in the **CDATA** section inside the **Code** element.
6057

61-
5. Save the snippet as *VBCodeSnippet.snippet*.
58+
For C#:
6259

63-
### Add a code snippet to Visual Studio
60+
```xml
61+
<Code Language="CSharp">
62+
<![CDATA[double root = Math.Sqrt(16);]]>
63+
</Code>
64+
```
6465

65-
1. You can add your own snippets to your Visual Studio installation by using the Code Snippets Manager. Open the **Code Snippets Manager** (**Tools** > **Code Snippets Manager**).
66+
Or for Visual Basic:
67+
68+
```xml
69+
<Code Language="VB">
70+
<![CDATA[Dim root = Math.Sqrt(16)]]>
71+
</Code>
72+
```
73+
74+
5. Save the snippet as *SquareRoot.snippet* (you can save it anywhere).
75+
76+
## Import a code snippet
77+
78+
1. You can import a snippet to your Visual Studio installation by using the **Code Snippets Manager**. Open it by choosing **Tools** > **Code Snippets Manager**.
6679

6780
2. Click the **Import** button.
6881

6982
3. Go to the location where you saved the code snippet in the previous procedure, select it, and click **Open**.
7083

7184
4. The **Import Code Snippet** dialog opens, asking you to choose where to add the snippet from the choices in the right pane. One of the choices should be **My Code Snippets**. Select it and click **Finish**, then **OK**.
7285

73-
5. The snippet is copied to the following location:
86+
5. The snippet is copied to one of the following locations, depending on the code language:
7487

7588
::: moniker range="vs-2017"
7689

90+
*%USERPROFILE%\Documents\Visual Studio 2017\Code Snippets\Visual C#\My Code Snippets*
7791
*%USERPROFILE%\Documents\Visual Studio 2017\Code Snippets\Visual Basic\My Code Snippets*
7892

7993
::: moniker-end
8094

8195
::: moniker range=">=vs-2019"
8296

97+
*%USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual C#\My Code Snippets*
8398
*%USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual Basic\My Code Snippets*
8499

85100
::: moniker-end
86101

87-
6. Test your snippet by opening a Visual Basic project and opening a code file. In the file choose **Snippets** > **Insert Snippet** from the right-click menu, then **My Code Snippets**. You should see a snippet named **My Visual Basic Code Snippet**. Double-click it.
102+
6. Test your snippet by opening a C# or Visual Basic project. With a code file open in the editor, choose **Snippets** > **Insert Snippet** from the right-click menu, then **My Code Snippets**. You should see a snippet named **Square Root**. Double-click it.
88103

89-
`Console.WriteLine("Hello, World!")` is inserted in the code file.
104+
The snippet code is inserted in the code file.
90105

91-
### Add description and shortcut fields
106+
## Description and shortcut fields
92107

93108
::: moniker range="vs-2017"
94109

95-
1. Description fields give more information about your code snippet when viewed in the Code Snippets Manager. The shortcut is a tag that users can type in order to insert your snippet. Edit the snippet you have added by opening the file *%USERPROFILE%\Documents\Visual Studio 2017\Code Snippets\Visual Basic\My Code Snippet\VBCodeSnippet.snippet*.
110+
1. Description fields give more information about your code snippet when viewed in the Code Snippets Manager. The shortcut is a tag that users can type in order to insert your snippet. Edit the snippet you have added by opening the file *%USERPROFILE%\Documents\Visual Studio 2017\Code Snippets\\[Visual C# or Visual Basic]\My Code Snippet\SquareRoot.snippet*.
96111

97112
::: moniker-end
98113

99114
::: moniker range=">=vs-2019"
100115

101-
1. Description fields give more information about your code snippet when viewed in the Code Snippets Manager. The shortcut is a tag that users can type in order to insert your snippet. Edit the snippet you have added by opening the file *%USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual Basic\My Code Snippet\VBCodeSnippet.snippet*.
116+
1. Description fields give more information about your code snippet when viewed in the Code Snippets Manager. The shortcut is a tag that users can type in order to insert your snippet. Edit the snippet you have added by opening the file *%USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\\[Visual C# or Visual Basic]\My Code Snippet\SquareRoot.snippet*.
102117

103118
::: moniker-end
104119

120+
> [!TIP]
121+
> Since you're editing the file in the directory where Visual Studio placed it, you don't need to reimport it to Visual Studio.
122+
105123
2. Add **Author** and **Description** elements to the **Header** element, and fill them in.
106124

107125
3. The **Header** element should look something like this:
108126

109-
```xml
110-
<Header>
111-
<Title>Hello World VB</Title>
112-
<Author>Myself</Author>
113-
<Description>Says Hello to the world.</Description>
114-
</Header>
115-
```
127+
```xml
128+
<Header>
129+
<Title>Square Root</Title>
130+
<Author>Myself</Author>
131+
<Description>Calculates the square root of 16.</Description>
132+
</Header>
133+
```
116134

117-
4. Open the **Code Snippets Manager** and select your code snippet. In the right pane you should see that the **Description** and **Author** fields are now populated.
135+
4. Open the **Code Snippets Manager** and select your code snippet. In the right pane, notice that the **Description** and **Author** fields are now populated.
118136

119-
5. To add a shortcut, add a **Shortcut** element alongside the **Author** and **Description** element:
137+
![Code snippet description in Code Snippet Manager](media/code-snippet-description-author.png)
120138

121-
```xml
122-
<Header>
123-
<Title>Hello World VB</Title>
124-
<Author>Myself</Author>
125-
<Description>Says Hello to the world.</Description>
126-
<Shortcut>hello</Shortcut>
139+
5. To add a shortcut, add a **Shortcut** element within the **Header** element:
140+
141+
```xml
142+
<Header>
143+
<Title>Square Root</Title>
144+
<Author>Myself</Author>
145+
<Description>Calculates the square root of 16.</Description>
146+
<Shortcut>sqrt</Shortcut>
127147
</Header>
128-
```
148+
```
129149

130150
6. Save the snippet file again.
131151

132-
7. To test the shortcut, open a Visual Basic project and open a code file. Type `hello` in the file and press **Tab** twice.
133-
134-
The snippet code is inserted.
135-
136-
### Add references and imports
137-
138-
1. You can add a reference to a project by using the **References** element, and add an Imports declaration by using the **Imports** element. (This works for C# as well.) For example, if you change `Console.WriteLine` in the code example to `MessageBox.Show`, you may need to add the *System.Windows.Forms.dll* assembly to the project.
152+
7. To test the shortcut, open the project you used previously, type **sqrt** in the editor and press **Tab** (once for Visual Basic, twice for C#).
139153

140-
2. Open your snippet.
154+
The snippet code is inserted.
141155

142-
3. Add the **References** element under the **Snippet** element:
156+
## Replacement parameters
143157

144-
```xml
145-
<References>
146-
<Reference>
147-
<Assembly>System.Windows.Forms.dll</Assembly>
148-
</Reference>
149-
</References>
150-
```
158+
You may want parts of a code snippet to be replaced by the user. For example, you might want the user to replace a variable name with one in their current project. You can provide two types of replacements: literals and objects. Use the [Literal element](code-snippets-schema-reference.md#literal-element) to identify a replacement for a piece of code that is entirely contained within the snippet but will likely be customized after it's inserted into the code (for example, a string or numeric value). Use the [Object element](code-snippets-schema-reference.md#object-element) to identify an item that's required by the code snippet but is likely to be defined outside of the snippet itself (for example, an object instance or a control).
151159

152-
4. Add the **Imports** element under the **Snippet** element:
160+
1. To enable the user to easily replace the number to calculate the square root of, modify the **Snippet** element of the *SquareRoot.snippet* file as follows:
153161

154-
```xml
155-
<Imports>
156-
<Import>
157-
<Namespace>System.Windows.Forms</Namespace>
158-
</Import>
159-
</Imports>
160-
```
161-
162-
5. Change the **CDATA** section to the following:
162+
```xml
163+
<Snippet>
164+
<Code Language="CSharp">
165+
<![CDATA[double root = Math.Sqrt($Number$);]]>
166+
</Code>
167+
<Declarations>
168+
<Literal>
169+
<ID>Number</ID>
170+
<ToolTip>Choose the number you want the square root of.</ToolTip>
171+
<Default>16</Default>
172+
</Literal>
173+
</Declarations>
174+
</Snippet>
175+
```
163176

164-
```xml
165-
<![CDATA[MessageBox.Show("Hello, World!")]]>
166-
```
177+
Notice that the literal replacement is given an ID (`Number`). That ID is referenced from within the code snippet by surrounding it with `$` characters:
167178

168-
6. Save the snippet.
179+
```xml
180+
<![CDATA[double root = Math.Sqrt($Number$);]]>
181+
```
169182

170-
7. Open a Visual Basic project and add the snippet.
183+
2. Save the snippet file.
171184

172-
8. You will see an `Imports` statement at the top of the code file:
185+
3. Open a project and insert the snippet.
173186

174-
```vb
175-
Imports System.Windows.Forms
176-
```
187+
The code snippet is inserted and the editable literal is highlighted for replacement. Hover over the replacement parameter to see the tooltip for the value.
177188

178-
9. Look at the project's properties. The **References** tab includes a reference to *System.Windows.Forms.dll*.
189+
![Code snippet replacement parameter tooltip in Visual Studio](media/snippet-replacement-parameter-tooltip.png)
179190

180-
### Add replacements
191+
> [!TIP]
192+
> If there's more than one replacable parameter in a snippet, you can press **Tab** to navigate from one to the other to change the values.
181193
182-
1. You may want parts of your code snippets to be replaced by the user, for example if you add a variable and want the user to replace the variable with one in the current project. You can provide two types of replacements: literals and objects. Literals are strings of some type (string literals, variable names, or string representations of numeric values). Objects are instances of some type other than a string. In this procedure you will declare a literal replacement and an object replacement, and change the code to reference these replacements.
194+
## Import a namespace
183195

184-
2. Open your snippet.
196+
You can use a code snippet to add a `using` directive (C#) or `Imports` statement (Visual Basic) by including the [Imports element](code-snippets-schema-reference.md#imports-element). For .NET Framework projects, you can also add a reference to the project by using the [References element](code-snippets-schema-reference.md#references-element).
185197

186-
3. This example uses a SQL connection string, so you need to change the **Imports** and **References** elements to add the appropriate references:
198+
The following XML shows a code snippet that uses the method `File.Exists` in the System.IO namespace and, therefore, defines the **Imports** element to import the System.IO namespace.
187199

188-
```xml
189-
<References>
190-
<Reference>
191-
<Assembly>System.Data.dll</Assembly>
192-
</Reference>
193-
<Reference>
194-
<Assembly>System.Xml.dll</Assembly>
195-
</Reference>
196-
</References>
197-
<Imports>
198-
<Import>
199-
<Namespace>System.Data</Namespace>
200-
</Import>
200+
```xml
201+
<?xml version="1.0" encoding="utf-8"?>
202+
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
203+
<CodeSnippet Format="1.0.0">
204+
<Header>
205+
<Title>File Exists</Title>
206+
<Shortcut>exists</Shortcut>
207+
</Header>
208+
<Snippet>
209+
<Code Language="CSharp">
210+
<![CDATA[var exists = File.Exists("C:\\Temp\\Notes.txt");]]>
211+
</Code>
212+
<Imports>
201213
<Import>
202-
<Namespace>System.Data.SqlClient</Namespace>
214+
<Namespace>System.IO</Namespace>
203215
</Import>
204-
</Imports>
205-
```
206-
207-
4. To declare a literal replacement for the SQL connection string, add a **Declarations** element under the **Snippet** element, and in it add a **Literal** element with sub-elements for the ID, the tooltip, and the default value for the replacement:
208-
209-
```xml
210-
<Declarations>
211-
<Literal>
212-
<ID>SqlConnString</ID>
213-
<ToolTip>Replace with a SQL connection string.</ToolTip>
214-
<Default>"SQL connection string"</Default>
215-
</Literal>
216-
</Declarations>
217-
```
218-
219-
5. To declare an object replacement for the SQL connection, add an **Object** element inside the **Declarations** element, and add sub-elements for the ID, the type of the object, the tooltip, and the default value. The resulting **Declarations** element should look like this:
220-
221-
```xml
222-
<Declarations>
223-
<Literal>
224-
<ID>SqlConnString</ID>
225-
<ToolTip>Replace with a SQL connection string.</ToolTip>
226-
<Default>"SQL connection string"</Default>
227-
</Literal>
228-
<Object>
229-
<ID>SqlConnection</ID>
230-
<Type>System.Data.SqlClient.SqlConnection</Type>
231-
<ToolTip>Replace with a connection object in your application.</ToolTip>
232-
<Default>dcConnection</Default>
233-
</Object>
234-
</Declarations>
235-
```
236-
237-
6. In the code section, you reference the replacements with surrounding $ signs, for example `$replacement$`:
238-
239-
```xml
240-
<Code Language="VB" Kind="method body">
241-
<![CDATA[Dim daCustomers As SqlDataAdapter
242-
Dim selectCommand As SqlCommand
243-
244-
daCustomers = New SqlClient.SqlDataAdapter()
245-
selectCommand = new SqlClient.SqlCommand($SqlConnString$)
246-
daCustomers.SelectCommand = selectCommand
247-
daCustomers.SelectCommand.Connection = $SqlConnection$]]>
248-
</Code>
249-
```
250-
251-
7. Save the snippet.
252-
253-
8. Open a Visual Basic project and add the snippet.
254-
255-
9. The code should look like the following, where the replacements `SQL connection string` and `dcConnection` are highlighted in light orange. Choose **Tab** to navigate from one to the other.
256-
257-
```vb
258-
Dim daCustomers As SqlDataAdapter
259-
Dim selectCommand As SqlCommand
260-
261-
daCustomers = New SqlClient.SqlDataAdapter()
262-
selectCommand = New SqlClient.SqlCommand("SQL connection string")
263-
daCustomers.SelectCommand = selectCommand
264-
daCustomers.SelectCommand.Connection = dcConnection
265-
```
216+
</Imports>
217+
</Snippet>
218+
</CodeSnippet>
219+
</CodeSnippets>
220+
```
266221

267222
## See also
268223

0 commit comments

Comments
 (0)