Snippy
Written by: Jim Holmes at 3/1/2008 9:07:27 PMCode Snippets are a handy way to reuse pieces of code to avoid repeating repetitious work repeatedly. Visual Studio 2003 and 2005 both enable developers to drag snippets of code onto the General Toolbox, but stuffing snippets on the Toolbox isn’t very organized and doesn’t allow any form of keyboard access. Readers of my Confessions of a Keyboard Junkie article may have a feel for how much that would irritate me…
Visual Studio 2005 includes Intellisense support for adding in code snippets, making it much easier to work with snippets while developing your code. What’s cool is that the implementation enables you write your own snippets and drop them into Visual Studio 2005. Microsoft even established an XML schema detailing the layout of snippets. What’s not cool is that you’ll have to write your snippets inside a CDATA block inside lots of various XML elements. That gets rather odious rather quickly. The following is the destructor snippet included with Visual Studio 2005:
<?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>Code snippet for destructor</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>classname</ID>
<ToolTip>Class name</ToolTip>
<Default>ClassNamePlaceholder</Default>
<Function>ClassName()</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[~$classname$()
{
$end$
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Writing raw XML isn’t one of my favorite things, so I’m happy when a tool can put the right bits into the right elements for me. The Snippy Power Tool does just that, plus it helps you get the snippets registered and in the appropriate directory for Visual Studio to use them. Instead of writing the XML above, you use Snippy’s handy GUI and just fill in fields.

The overview graphic is a bit hard to decypher at its small size, so let’s take a zoomed-in look at the major fields

These fields all correspond to <Header> elements in the XML shown above. Writing the actual snippet is much simpler as well. Simply write your code in the field, adding markers for literals or objects as desired.

Adding and editing literals or objects (text that’s replaced in the snippet) is accomplished via a simple dialog box:

You’ll need to save snippets in a location where Visual Studio 2005 can find them. Snippets are stored in either a user’s profile folder or in Visual Studio’s snippets folder. You can find these paths by pulling open the Code Snippet Manager dialog from Visual Studio’s Tools menu and looking at the Location field.
Overall, Snippy is a very handy tool for creating and maintaining snippets. It’s not integrated into the Visual Studio environment like Dave Donaldson’s CodeKeep — but CodeKeep hasn’t yet been updated for Visual Studio 2005. (Editors Note: Dave promises it will be out soon)
You can learn more about creating snippets from the following sites:
- Creating Code Snippets — Microsoft’s How-To guide on creating basic and advanced snippets, including psuedo-clear explanations for Literal and Object replacements
- Creating and Using Intellisense Code Snippets — another Microsoft How-To for using and managing snippets
blog comments powered by Disqus