How to create code snippets using Snippet Designer?

I am working with my friend on creating a demo to show TDD. The whole demo took more than 30 minute, we were asked to reduce the time. When we write tests, we tend to write method names as long as possible so that when someone reads it, the name make perfect sense. So one thing I noticed, I spend most of the time typing large method names. One suggestion was not to type rather paste them and explain what the test does. Which led to the question, why not we use snippets? A good excuse for blog.

To start with go to snippet designer codeplex page and download the snippet designer. There is another way you can do it by creating XML etc., I like snippet designer very much and once you start using it, you may not go to XML format. Once it is installed, when you right click on the code, you should see an option to Export as Snippet

image

Select the option and export a snippet, it will open up a tab with the content you selected like the following

image

Two points are important here. Make sure you name the snippet (green circle) and then give name for shortcut (black circle). This is the name you are going to use to bring the snippet when you are in a code. Now save this file. There are two places you can save the snippet, one place is default visual studio snippet area. I will not save my snippet in there since it is specific to a project. If you are creating a snippet that you want to use a whole a lot, then you put it in Visual Studio directory. In my case I put it in the solution folder itself.  By default, snippet manager will show all the snippets stored in default visual studio directory so you need to tell snippet manager to look it up not only in visual studio default directory but also additional directories. To do that, go to tools –> snippet manager like the following

image

When you choose code snippet manager, it will show a dialog like the following

image

Change the language to C# and select Visual C# and select add to add the directory where you placed your snippets. Now that all pieces in place, go to your program and start typing ‘finalkata’ (Remember, this is the short cut name we gave?), as you type the intellisense should find your snippet and show up.

image

select it and double tab and bring the code. That’s about it. Very simple and easy to use. What I did not show is how to create the replaceable content inside the snippet, for what I did, it was not required. When I come around to use it, I will do another blog.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s