Freigeben über


I can't type - Code Snippets in Visual Studio 2005 to the rescue

Visual Studio 2005 has a feature that will help me and hopefully you a great deal. See, I do not know how to touch type. However, many people who have seen me type think I am one of the fastest two finger typists around. This is probably not something to be proud of by the way. Back to the topic at hand, Code snippets. Code snippets are small templates of code that you can insert into your program with the touch of few keystrokes. For example, some of the snippets that ship with the June Community Preview of Visual Studio 2005 include “property” (which inserts a set and get property), “propertyg” (which inserts a get property), etc. The snippets include all of the boilerplate code so I don’t have to type it which improves my productivity. This feature is very similar to a feature that I have used in the past from IntelliJ (https://www.jetbrains.com) which was called Live Templates.

Not only can you use the ones out of the box you can also create your own code snippets. In keeping with the topic at hand let’s create a code snippet for inserting a test method using Visual Studio Team System. The existing C# snippets are in the following directory and I suggest studying them as examples for creating your own snippets:

”C:\Program Files\Microsoft Visual Studio 8\VC#\Expansions\1033\Expansions”

You can place your templates in this directory (not recommended, but easy) or you can place them in the My Code Snippets directory:

“\My Documents\Visual Studio\Whidbey\CodeSnippets\Code Snippets\VC#\My Code Snippets”

Now that we know where to put them, what does a snippet look like? A code snippet is an XML file, what else. Here is a code snippet for a test method:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0">
    <Header>
        <Title>testmethod</Title>
        <Shortcut>test</Shortcut>
        <Description>Generate TestMethod</Description>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
    <Declarations>
        <Literal default="true">
             <ID>name</ID>
             <ToolTip>Method Name</ToolTip>
             <Default>TestName</Default>
        </Literal>
    </Declarations>
        <Code Language="csharp" Format="CData"><![CDATA[
    [TestMethod]
      public void $name$()
      {
          $end$
      }]]>
        </Code>
    </Snippet>
</CodeSnippet>

Save this in a file named “testmethod.xml” in the appropriate snippet directory and you can now use the code snippet instead of typing test method boilerplate code over and over. In addition, I created another snippet to generate a test method that expects an exception, here it is:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0">
    <Header>
        <Title>testmethod - expected exception</Title>
        <Shortcut>expect exception</Shortcut>
        <Description>Generate TestMethod, ExpectedException</Description>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal default="true">
                <ID>name</ID>
                <ToolTip>Method Name</ToolTip>
                <Default>TestName</Default>
            </Literal>
            <Literal default="true">
                <ID>expectedException</ID>
                <ToolTip>ExpectedException</ToolTip>
                <Default>ExceptionType</Default>
            </Literal>
        </Declarations>
        <Code Language="csharp" Format="CData"><![CDATA[
     [TestMethod, ExpectedException(typeof($expectedException$))]
      public void $name$()
      {
          $end$
      }]]>
        </Code>
    </Snippet>
</CodeSnippet>

Save this to a file named “testmethodexpectexception.xml” in the appropriate code snippet directory and you will be freed from typing this again. I don’t know about you but it’s these little productivity enhancers that really help.

This posting is provided "AS IS" with no warranties, and confers no rights.

Comments

  • Anonymous
    June 02, 2004
    Is this comparable to QuickCode? ( http://www.dvxp.com/en/QuickCode.aspx )
  • Anonymous
    June 06, 2004
    They do seem comparable to QuickCode.
  • Anonymous
    June 14, 2004
    Here is another one: Autocode http://www.devprojects.net/autocode20.aspx
  • Anonymous
    February 01, 2005
    Cut & paste is so 1984 for your code. Grok the new marvel: Code Snippets. In Whidbey (aka Visual Studio 2005), reuse your old code like the object it was written to be. (That's a shot across the bow, o...