Create custom Visual Studio 2005 code snippets

Visual Studio 2005's built-in code snippets are great, but you can extend the concept by creating custom code snippets to easily reuse common code blocks in your application. This tutorial walks you through the process of creating custom code snippets.

Creating the file

Code snippets are defined in XML files with the .snippet file extension. Microsoft created the Code Snippet XML Schema, which defines the structure of a snippet XML file and specifies the elements (and their attributes) that you may use in a code snippet file.

Since code snippets are defined as XML, you can use your favorite XML or text editor to create and maintain them. Visual Studio 2005 supports XML editing, so it may be used to remain in a common environment.

Code snippet example

Depending on the project or organization, many code snippet candidates may exist. This may be a discrete chunk of code that you may reuse throughout the code (this also lends itself to a code library). In addition, the code may or may not include replaceable parameters that allow you to enforce a coding technique.

To demonstrate, I will create a code snippet that declares a set of objects for working with SQL Server. This allows a developer to insert and use these objects while saving typing time. Here is the C# code snippet:

SqlConnection conn;
SqlCommand comm;
SqlDataAdapter sda;

Listing A contains the appropriate code snippet XML. Listing B contains the equivalent code snippet definition for VB.NET. Here's a closer look at the XML:

  • A code snippet is defined by the CodeSnippet element. The CodeSnippets element is the root node, and it may contain one or more CodeSnippet elements.
  • The Description, Author, and Shortcut elements appear in the Code Snippets Manager window when it is highlighted. For this reason, the text included in the Description element should give a good idea of what the snippet actually does. In addition, the Shortcut element is what is typed within Visual Studio 2005 to actually use the snippet (type the shortcut and hit [Tab]).
  • The Code element contains the actual code associated with the snippet. In this example, there are no replaceable parameters used. The code is placed within the CDATA attribute.
  • The Language attribute of the Code element defines the code used. The options include VB, CSharp, VJSharp, and XML.

You should create a folder to contain your custom code snippets and save this sample in the folder. The next step is to make it available in Visual Studio 2005.

Adding a snippet to Visual Studio 2005

The Code Snippets Manager allows you to easily add your own code snippets to the environment. To do so, you should follow these steps:

  1. Open the Manager window via the Tools | Code Snippets Manager menu selection.
  2. Select the appropriate language in the Language field.
  3. Click the Add button to add your snippet to the environment. This opens the Code Snippets Directory window that allows you to navigate to the folder containing the snippet file(s). You don't select the actual snippet file — the folder is selected and all snippets within it are added.
  4. Once the directory is added, you can select it and browse the files within it.

You can test it by typing the appropriate shortcut in Visual Studio 2005 and hitting [Tab] to insert the code, or you can right-click and select Insert Snippet from the context menu. This allows you to navigate the available code snippet directories to locate one if you don't know its shortcut. If you're using C#, you may also use the Surround With context menu option. This is just one way to tackle this simple example.

More examples

The Code Snippet XML Schema includes the optional Declarations element that you may use to specify the literals and objects for the code specified in the snippet. It might include zero or more Literal elements that you may use to define literals, and zero or more Object elements to define objects used in the code. Literal and Object elements contain ID, ToolTip, and Default elements to assign a name to the item, tooltip text, and its default value. In addition, the Object element contains the Type element to specify the type of object.

The Object and Literal elements are used in the actual code within the Code element. You reference the literals and objects you declared in the Declarations element by placing $ symbols at the beginning and end of the value in the literal or object's ID element. The VB snippet file in Listing C uses these elements to declare literals to be used in our code.

When you use this code snippet, green boxes appear around the literals enclosed in dollar signs ($connString$ and $sqlString$), which allow you to insert the appropriate text. The example code snippets in this column provide a peek at the creation process, but there is much more functionality available via the Code Snippet XML Schema.

Importing code snippets

While creating custom code snippets provides power and flexibility, you may also import snippets (created by you or others) to Visual Studio 2005 via the Import button in the Code Snippets Manager. This allows you to bring snippets into existing directories used by the Code Snippets Manager. You select the destination directories and select the Finish button. The process physically copies the snippet file to the destination directory.

Microsoft provides plenty of Visual Studio 2005 code snippets, which you can download and import into your environment.

Third-party tools

It can be a bit tedious to create and edit XML files. It is especially cumbersome when you must know the syntax and order of a valid element that you may use in the snippet file. While Visual Studio 2005 does not include a snippet editor, there are code snippet editor tools available:

Customize your environment

Creating custom code snippets allows you to customize and streamline your development environment by making it easy to reuse standard coding elements and save typing. Use your favorite XML or text editor to create your own snippets and add them to Visual Studio 2005 to save valuable time.


  • Date: April 18th, 2008
  • Author: Tony Patton

Comments

Popular posts from this blog

Use configSource attribute to manage Web.Config sections in ASP.Net 2.0

DevOps - Key Concepts - Roles & Responsibilities - Tools & Technologies