Creating Code Snippets

Published Sun, Mar 26 2006 21:01 | Paul June

My previous blog entry discusses the essentials on using Code Snippets to speed up your development. But as you would have noticed, not all snippets that are available would cater to your needs.

In this scenario, you must create your own Code Snippets Library so that you can have snippets which are greatly customized for your development.

Here I will discuss creating custom snippets.

How does a snippet work? Well, this is the first question that we need to answer to fully understand the background processing that’s happening on code snippets. Code snippets are just xml’s. There is a proper format for you to follow. Here’s a sample code snippet:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippet Format="1.0.0">
  <Header>
    <Title>class</Title>
    <Shortcut>class</Shortcut>
    <Description>Expansion snippet for class</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
      <SnippetType>SurroundsWith</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal default="true">
        <ID>name</ID>
        <ToolTip>Class name</ToolTip>
        <Default>MyClass</Default>
      </Literal>
    </Declarations>
   <Code Language="csharp" Format="CData">
    <![CDATA[class $name$
    {
      $selected$$end$
    }]]>
   </Code>
  </Snippet>
</CodeSnippet>

Let’s digest this sample bit by bit. The root element of a code snippet is the <CodeSnippet> tag. All declarations on your snippet must reside between the <CodeSnippet> tag. The <CodeSnippet> tag has two-key sub-elements. These are the <Header> and the <Snippet> tag. The <Header> tag contains the information of the snippet that is needed by the IDE. In our example it contains four tags:

  • <Title> – the title of the code snippet.
  • <Shortcut> - defines the shortcut of the class.
    • I kinda forgot to mention that you can directly type a shortcut for the code snippet and press tab to automatically insert it in your code.
  • <Description> - A proper description of your code snippet. It will be displayed when you browse your snippet in the intellisense.
  • <SnippetTypes> - specifies which category your snippet would appear. It could have up to three values. Expansion, SurroundsWith or Refractoring. A snippet could be in all of the three categories.

The <Snippet> tag is a little bit more complicated. This is where you’ll define your parameters and the code snippet itself.

The <Snippet> tag has two sub-elements. These are:

  • <Declarations> - contains the editable or user-specified variables in the snippet. In the example:

    <Declarations>
      <Literal default="true">
        <ID>name</ID>
        <ToolTip>Class name</ToolTip>
        <Default>MyClass</Default>
      </Literal>
    </Declarations>
 
The snippet contains one parameter which is the “Class name”. You can specify as many parameters as you want.

  • <Code> - this tag contains the specific code to insert. Let’s see the example:

   <Code Language="csharp" Format="CData">
    <![CDATA[class $name$
    {
      $selected$$end$
    }]]>
   </Code>

By looking at the code, you’ll notice three different-looking directives. The $name$, $selected$ and $end$. The $name$ directive is the ID of the parameter that you declared in the <Declarations> tag. This will tell the IDE that this is the place where the user must specify a value. The $selected$ directive represents the code that was selected prior to the insertion of the code snippet. This is pretty useful if your snippet will supports “SurroundsWith”. Lastly the $end$ directive is where your cursor would be located after the insertion of the code snippet.

By now, you can already create your own code snippet file. But how would you load it in Visual Studio?

You can register your snippets by directly copying your file to the default location of all code snippets (C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#).

But, if you want to create you own library then you’ll have to use the Code Snippet Manager (Tools->Code Snippet Manager). You can specify which folder the IDE would look for code snippets.


Now that you have learned how to create code snippets, I would greatly advise to make use of this feature. It will definitely boost up your programming speed.

Comments

# Frank Kroondijk said on April 17, 2006 5:12 PM:

just did a post on snippet resources & tools:

http://dotnetpret.blogspot.com/2006/04/code-snippets-whole-enchilada.html

(extension on post http://jens-schaller.de)

# Max R. said on May 9, 2007 1:10 PM:

Hello! Very interesting. Thank you.

Leave a Comment

Name:  
Website: