Geeks With Blogs

News INETA Community Speakers Program
GeeksWithBlogs.net: WTFNext's hosting!

View Stacy Vicknair's profile on LinkedIn


WTF Next? Dev ramblings from a master of nothing.

In this brief series I’m going to go over creating your own custom code snippets for use in Visual Studio 2005 or later. We’ll be using the XML Schema from Visual Studio 2005, but I assure you it works for both.

This series will contain three parts:

  • The “Hard” Way
  • The “Easy” Way
  • Practical Example of an XML Snippet

The reason we’re going to call this version the “Hard” way is because there are snippet generator apps available, including one made by the teams themselves.

So before we begin, I’d like to say that I prefer to use the “Hard” way. For me, it helps me have a greater understanding with limited reliance on other software. Also, if the schema changes (which is very very doubtful) I will have instant knowledge of what needs fixing via intellisense tools.

Step 1: Create a new .snippet file

A .snippet file is just like having a .vb or .c file. A glorified text document. To start off, I like to have a text file with the following already in it:

 

<?xml version="1.0" encoding="utf-8" ?> 
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title></Title>
      <Shortcut></Shortcut>
      <Description></Description>
      <Author></Author>
      <HelpUrl></HelpUrl>
      <SnippetTypes>
        <SnippetType></SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="">
        <![CDATA[]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

What this allows you to do is get intellisense from Visual Studio when creating the snippet, as well as you have a starting structure that is common to most snippets, and it is just a matter of filling in the blanks.

You can create this in notepad, and then save it out to where you want to store your snippets. If you want exclusive access to your snippets as well as to have them “installed” automatically, drop them in your My Documents > Visual Studio YYYY > Code Snippets > Language of Choice > My Code Snippets directory. For this example, I’ll be working with Visual Studio 2005 and a VB .NET snippet. I’ll be naming my output cmdWrite.snippet, and my snippet will write a string to the console and then wait for the user to hit enter.

Step 2: Open .snippet in Visual Studio and fill in the blanks

By using Visual Studio to edit the snippet, we’ll get the rich experience of intellisense and syntax highlighting to make this process a bit easier.

Now we’ll start adding the meat and potatoes, and for reference check out the Schema Reference from the MSDN.

The Header Section

The Header contains all of that important information to say what this snippet is all about. The Title contains what the snippet will be named in intellisense, the shortcut is the snippet shortcut, the description is what shows as a tooltip when selecting the snippet.

The SnippetTypes section goes over what your snippet plans to do. Does it just insert at the point? Does it wrap around the text highlighted? Could it do either?

The Snippet Section

The Snippet section is where you put the real code for the snippet into play.

To get our show started, we’ve got to let the snippet know our intentions. Setting the language to “VB” does just that.

Now, the real code goes into the CDATA[] brackets. This is everything you want exported into Visual Studio when printed.

For this example we’re just adding the following:

Console.Writeline($String$)
Console.ReadLine()

Now, huh? What’s that $String$ business? Well, we want to have some intelligence to our simple script, so we gave it a literal declaration. To add this into the snippet XML, add it before the code block. This will specify we want a string to replace our $String$ placeholder:

<Declarations>
    <Literal>
        <ID>String</ID>
        <ToolTip>Replace with string to write.</ToolTip>
        <Default>"Hello World!"</Default>
    </Literal>
</Declarations>

Now we’re ready to save this out and try it out! Go ahead and save and close Visual Studio. Reopen and start a new VB console application and insert the snippet you’ve just made!

For final comparison, here is my finished code:

<?xml version="1.0" encoding="utf-8" ?> 
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> 
    <CodeSnippet Format="1.0.0"> 
        <Header>
            <Title>Command Writeline</Title>
            <Shortcut>cmdWrite</Shortcut>
            <Description>Writes a string to the commandline and then waits for the user to hit enter</Description>
            <Author>Stacy Vicknair</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>String</ID>
                    <ToolTip>Replace with string to write.</ToolTip>
                    <Default>"Hello World!"</Default>
                </Literal>
            </Declarations>
            <Code Language="VB" Kind="method body">
                <![CDATA[
                    Console.Writeline($String$)
                    Console.ReadLine()
                ]]>
            </Code>
        </Snippet>
    </CodeSnippet> 
</CodeSnippets>

References:

http://msdn.microsoft.com/en-us/library/ms171418(VS.80).aspx

http://weblogs.asp.net/srkirkland/archive/2007/10/29/creating-a-custom-code-snippet.aspx

 

Posted on Tuesday, October 14, 2008 1:12 PM VB .NET , How-To | Back to top


Comments on this post: Custom Code Snippets Series Part 1 of 3: The “Hard” Way

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Stacy Vicknair | Powered by: GeeksWithBlogs.net