Code Element (IntelliSense Code Snippets)
Provides a container the short code blocks of IntelliSense Code Snippets.
<CodeSnippets>
<CodeSnippet>
<Snippet>
<Code>
<Code Language="Language"
Kind="method body/method decl/type decl/page/file/any"
Delimiter="Delimiter">
Code to insert
</Code>
Attributes and Elements
The following sections describe attributes, child elements, and parent elements.
Attributes
Attribute |
Description |
---|---|
Delimiter |
Optional attribute. Specifies the delimiter used to describe literals and objects in the code. By default, the delimiter is $. |
Kind |
Optional attribute. Specifies the kind of code that the snippet contains and, therefore, the location at which a code snippet must be inserted for the code snippet to compile. The values available are method body, method decl, type decl, file, and any. For more information, see the section "Kind Attribute" in the following table. |
Language |
Required attribute. Specifies the language of the code snippet. For more information, see the section "Language Attribute". |
Kind Attribute
Value |
Description |
---|---|
method body |
Specifies that the code snippet is a method body, and therefore, must be inserted inside a method declaration. |
method decl |
Specifies that the code snippet is a method, and therefore, must be inserted inside a class or module. |
type decl |
Specifies that the code snippet is a type, and therefore, must be inserted inside a class, module, or namespace. |
file |
Specifies that the snippet is a full code file. These code snippets can be inserted alone into a code file, or inside a namespace. |
any |
Specifies that the snippet can be inserted anywhere. This tag is used for code snippets that are context-independent, such as comments. |
Language Attribute
Value |
Description |
---|---|
VB |
Identifies a Visual Basic code snippet. |
CSharp |
Identifies a Visual C# code snippet. |
XML |
Identifies an XML code snippet. |
CPP |
Identifies a C++ code snippet. |
JavaScript |
Identifies a JavaScript code snippet. |
JScript |
Identifies a Jscript code snippet. |
SQL |
Identifies a SQL code snippet. |
HTML |
Identifies an HTML code snippet. |
Child Elements
None.
Parent Elements
Element |
Description |
---|---|
Contains the references, imports, declarations, and code for the code snippet. |
Text Value
A text value is required.
This text specifies the code, along with the literals and objects, that you can use when this code snippet is inserted into a project.
Remarks
Two reserved words are available for use in the text of the Code element: $end$ and $selected$. $end$ marks the location to place the cursor after the code snippet is inserted. $selected$ represents text selected in the document that is to be inserted into the snippet when it is invoked. For example, given a snippet that includes:
$selected$ is a great color.
If the word "Blue" is selected when the user invokes the template, the result is:
Blue is a great color.
You may not use either $end$ or $selected$ more than once in a code snippet. If you do, only the second instance is recognized. Given a snippet that includes:
$selected$ is a great color. I love $selected$.
If the word "Blue" is selected, the result is:
is a great color. I love Blue.
The initial space appears because there is a space between $selected$ and is.
All other $ keywords are dynamically defined in the <Literal> and <Object> tags.
Example
The Code element of the following code snippet shows how to write a Visual C# code snippet that creates a SqlDataAdapter. The literal and object defined in the Declarations element are referenced in the Code element by the value of their ID element. In this example, the literal is referenced as $SQL$ and the object is referenced as $Connection$.
<CodeSnippets xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Create a data adapter</Title>
<Description>Creates a SqlDataAdapter object.</Description>
<Author>Microsoft Corporation</Author>
<Shortcut>createadapter</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>SQL</ID>
<ToolTip>Replace with a SQL connection string.</ToolTip>
<Default>"SQL connection string"</Default>
</Literal>
<Object>
<ID>Connection</ID>
<Type>System.Data.SqlClient.SqlConnection</Type>
<ToolTip>Replace with a connection object in your application.</ToolTip>
<Default>dcConnection</Default>
</Object>
</Declarations>
<Code Language="CSharp">
<![CDATA[
daCustomers = new SqlClient.SqlDataAdapter();
selectCommand = new SqlClient.SqlCommand($SQL$);
daCustomers.SelectCommand = selectCommand;
daCustomers.SelectCommand.Connection = $Connection$;
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>