Saturday, February 13, 2010

Code Snippets in Visual Studio 2010

One of the cool features I love in Visual Studio is the Code Snippets which makes development easier in many ways. :) Let’s see how to create a code snippet and add to the snippets gallery in Visual Studio 2010. Just looking back at VS 2008, though it supports creation of code snippets it’s not an easy process without the use of 3rd party applications.
There are two snippet types…
Expansion – This type of snippet is inserted at the curser.
SurroundsWith – This type wraps around the selected code block.
Lets create  a code file header snippet.
Add a XML file to your project and name it as verHeader.snippet. (.snippet is the extension of snippet files)
image Right click on the code editor window and select the menu item Insert Snippet or Press Ctrl + K + X  and click the item Snippet.
image Remove the tag below, as we are creating a Expansion snippet.
<SnippetType>SurroundsWith</SnippetType>
Modify the Title tag to read “Code File Header”.
Next put your name within the Author tags, so are the author of your code snippet. ;)
Modify the shortcut tag with “codehead” which is the rigger word that will activate the snippet.
”Adds a header to a code file” can be the text between the description tags. And yes I love C#, so I will choose the language attribute of the code as C#.
Now we need to alter the Literal section. Literals allow you to define editable values that are inserted into your snippet. I want the you to enter your own name in the author section, so change the ID value to Author and enter your name in as the Default tag.
Code section contains what will be added when you add the snippet.
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>Code File Header</Title>
<Author>Prabath Fonseka</Author>
<Shortcut>codeHead</Shortcut>
<Description>Adds a header to a code file.</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>Author</ID>
<Default>Prabath Fonseka</Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[
/**************** Code File Header ******************
Author: $Author$
Date: 
Version:
Description:
****************************************************/
]]>
</Code>
</Snippet>
</CodeSnippet>

Above is how your final snippet should look like.
Next step is to load the snippet in to Visual Studio which we have to do through Code Snippet  Manager under the Tools menu. But by default (in VS 2010 RC) Code Snippets Manger does not comes under Tools menu so our next task is to add it under tools menu.

Go to Tools > Customize > Commands > Tools > Add Command…

By default Menu bar: radio button is selected, select Tools from the dropdown.

image Under categories select Tools and under commands select Code Snippets Manager, click OK.

image Create a folder where you can keep all your personal/custom code snippets. Then copy the verHeader.snippet to that folder. Open Code Snippets Manager, select Visual C# as language and a folder to place your custom snippets, select Imports.

image Ok, if you followed me, you have successfully created and added a code snippet to VS 2010. Wanna test? :P

Add a class file in to your project. Move the curser to the first line Press Ctrl + K + X

image
Happy Coding.

2 comments:

ලහිරු said...

Thanks! It's Working. But still i couldn't understand what is the use of it. Please tell me about?

cmujica said...

code reuse, for example to insert into the database can be a code that called for 10 lines in a word