|
The first part of this article introduced Visual Studio code snippets and showed how they can save
you lots of time and typing by reducing the need for you to manually type repetitive code. It also
showed you how to use shortcuts to make using snippets even faster. This installment will examine
the .snippet file format and show you how to expand your snippet library by both downloading packaged
snippets from the Web and creating your own snippets from scratch.
What Does a .snippet File Look Like?
Before we start installing more snippets or creating our own, it's a good idea to have a basic
understanding of the .snippet file format.
If you played around with the Code Snippets Manager that we discussed at the end of the
last article, you may have noticed that when you select a snippet, it'll show you the filename
which contains the snippet's declaration.
Since I used the "If...Else...End If" snippet for
illustration last time, let's take a look at the actual .snippet file that makes it work.
I've been using Visual Web Developer 2008 Express for this article and it stores the
file in question at:
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\VBExpress\Snippets\1033\common code patterns\conditionals and loops\IfElseEndIfStatement.snippet
The file is just a regular XML-based text file so you can open it using Notepad, Visual Studio,
or any other text editor. The contents of the file are below.
IfElseEndIfStatement.snippet:
<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>If...Else...End If Statement </Title>
<Author>Microsoft Corporation</Author>
<Description>Inserts an If...Else...End If statement.</Description>
<Keywords>
</Keywords>
<Shortcut>IfElse</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>Condition</ID>
<Type>Boolean</Type>
<ToolTip>Replace with an expression that evaluates to either True or False.</ToolTip>
<Default>True</Default>
</Literal>
</Declarations>
<Code Language="VB" Kind="method body"><![CDATA[If $Condition$ Then
Else
End If]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
The file is split into two main sections: "Header" and "Snippet". The
"Header" section provides the basic information about the snippet. This is where you put the
snippet's title, description, shortcut key, and other stuff like that. The "Snippet"
section contains the actual code for the snippet as well as defining "Declarations"
which are the editable areas which get highlighted in green when the user inserts a snippet.
Downloading and Installing Additional Code Snippets
I was actually relatively surprised that I didn't find more sources for snippets online.
Everyone says they have code snippets, but very few actually have code available in
Visual Studio's snippet format. One site that actually does is
GotCodeSnippets.NET.
The thing that's a little weird about downloading snippets is that they are usually packaged
in the Visual Studio Community Content Installer (VSI) format. Simply save the .vsi file
somewhere you'll be able to find it. Once you've downloaded it, simply double-click
the file and you'll be greeted with a screen that looks something like this:
After you tell it where to install the snippet
(I recommend you install all your downloaded and custom snippets in the "My Code Snippets" folder),
it will copy the files to the appropriate places and display a status screen:
That's all there is to it. The downloaded snippet(s) are now installed and can be used
just like any of the ones that shipped with your version of Visual Studio.
Creating Your Own Snippets
Sure Visual Studio includes lots of great snippets, and sure you can download lots more from
the Web, but the real power of snippets is unleashed when you start creating your own. After
all, only you know how you like to work and what you spend most of your time doing.
Manually
The format of .snippet files is simple enough that you can easily create your own
snippets quite quickly by hand. Not wanting to type everything from scratch, I
simply opened up an existing snippet and replaced the values with ones appropriate
for what I wanted my snippet to do.
For the sake of illustration, I've chosen to share one of the snippets that
I find myself using quite a bit. It's short and I intentionally gave it a
shortcut name that is the same as the IsPostBack property of the page object.
I did it not only to show that you can, but also because it makes the
shortcut almost impossible to forget.
This snippet simply creates a conditional that checks to be sure the page is NOT
being posted back. I quite often write this conditional so that code to
initialize values and set the default state of objects on the page only runs
when the page is first called. It's pretty simple so I'll just show you the
complete .snippet file.
IsNotPostBackCheck.snippet:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>IsNotPostBack Check</Title>
<Author>15Seconds.com</Author>
<Description>Creates a conditional to check that a page is not being posted back. Useful for initialization.</Description>
<HelpUrl>http://www.15seconds.com/issue/080710.htm</HelpUrl>
<Keywords />
<Shortcut>IsPostBack</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>InitCode</ID>
<Type>String</Type>
<ToolTip>Replace with code to run if page is running for the first time.</ToolTip>
<Default>' Initialization Code Goes Here</Default>
</Literal>
</Declarations>
<Code Language="VB" Kind="method body"><![CDATA[If Not IsPostBack Then
$InitCode$
End If]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Once you've completed your changes to your new .snippet file all you need to do is place it
into the "My Code Snippets" folder located under the Visual Studio folder in
"My Documents". The full path should be something like:
C:\Documents and Settings\_Your_User_Name_\My Documents\Visual Studio 2008\Code Snippets\Visual Basic\My Code Snippets.
Once the file is there, if everything is working correctly Visual Studio should
automatically find it and make it available for you to use.
Here are a couple screen caps of the snippet in use.
Code Snippet Editor
If you're more of a GUI type of person, there are also some tools floating around to help you
create snippets without having to write the XML yourself. The Code Snippet Editor for Visual Basic 2008
is one such tool. You can find out more about it and download a copy from here:
http://msdn.microsoft.com/en-us/vbasic/bb973770.aspx
The interface is quite straight-forward and you shouldn't have any problems finding
your way around the tool. So you can get an idea what it looks like, here's a
screen shot of the tool with the "IsNotPostBack Check" sample snippet created
loaded for editing.
If you have any trouble with the way the tool manages snippets, don't forget you can always use the
import functionality of Visual Studio's Code Snippets Manager or simply copy your .snippet file to the
"My Code Snippets" folder located in your "My Documents" folder just like we did
with the one we created by hand.
Conclusion
The first part of this article introduced Visual Studio code snippets and showed how they can save
you lots of time and typing by reducing the need for you to manually type repetitive code. It also
showed you how to use shortcuts to make using snippets even faster. This part showed how you
can take the time savings to a whole new level by downloading and creating your own snippets.
Snippets are one of the greatest time savers to come along in a long while. If you haven't already
been using them I hope this article has helped open your eyes. If you have, hopefully you still
learned something new and useful about these powerful little coders.
Additional Information
For more information on Visual Studio Code Snippets you may find these links helpful.
|