Michael Van Cleave
Traveling the technical world, learning the language

Snippet... Snippet good!

Monday, August 22, 2005 5:47 PM

Okay, so while I had decided that I wasn't going to read more tonight about VS2005, I didn't say I wasn't going to play with it. 

I started to work on a project that seems like it would be a good one to try out all of the new features of C# 2.0 and the new IDE.  While creating tests for a business component that I was coding, I noticed that in the test file you don't have a snippet for just creating a “Test” method.  I was a little annoyed since creating test methods are pretty mindless, repetitive, and boring to type.  In fact, I remembered that in Resharper our team had created a template for creating the NUnit test fixture setup and tests. 

This reminded me of a feature that VS2005 has...  Yep, that is right, Snippets!  I decided to that since I had a good situation to create one that it would be a good time to get knees deep in it. 

Anyhow, I started down the help path to find more info on it.  The help seemed pretty complete, but after reading around a bit, I decided to just check out the ones the MSFT had created.  (Who knows better than the people that created the IDE, right?)  I found some good examples and was able to create my snippet. 

It is amazingly simple.  Really you just create a new XML file with the extension of .snippet (I like the fact that it uses an extension that defines it purpose in life..), and save it to 'My Documents\Visual Studio Projects\Code Snippets\<language of choice>\My Code Snippets'. 

Now just because you decided to get fancy and create a new fangled snippet doesn't mean that your IDE will see it.  You have to import it.  So, you just go to Tools -> Code Snippets Manager.  This will throw up a dialog for you to import, configure, and view your snippets.  Click Import, and browse your way to the file you created, and select OK.  Now you are off and running.

Just type in the shortcut you provided in your xml file and you will be all set it will popup in intellisense, you tab, it will emit the templated code for you.

Since I have created the TEST template, I figure that I can add it so everyone (that is everyone that hasn't created one already), can use it. 

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="
http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
 <CodeSnippet Format="1.0.0">
  <Header>
   <Author>RogueCoder</Author>
   <Description>Snippet for creating test methods.</Description>
   <Title>Test</Title>
   <Shortcut>Test</Shortcut>
   <Keywords>
    <Keyword>Test</Keyword>
    <Keyword>Test Method</Keyword>
    <Keyword>Test Method Snippet</Keyword>
   </Keywords>
   <SnippetTypes>
    <SnippetType>Expansion</SnippetType>
   </SnippetTypes>
  </Header>
  <Snippet>
   <Declarations>
    <Literal Editable="true">
     <ID>MethodName</ID>
     <Default>TestMethodName</Default>
     <ToolTip>Replace with test method name.</ToolTip>
    </Literal>
   </Declarations>
   <Code Language="CSharp">
    <![CDATA[
    [TestMethod]
    public void $MethodName$()
    {
     /*TODO: Add test statements here.*/
    }]]>
   </Code>
  </Snippet>
 </CodeSnippet>
</CodeSnippets>

Just cut and paste in an .snippet file, then import and you should be ready to go.  Feel free to edit it in any way you desire, but if you do something really cool, please send me a copy.  You never know, I might really like having it too.. :)

Until next time..

Michael

 


Feedback

No comments posted yet.


Post a comment





 

Please add 4 and 1 and type the answer here:




Archives

Post Categories

Great Links

Other Blogs

Pod Casts

Syndication: