Create Code Snippet in Visual Studio 2010

IntelliSense Code Snippets provide a way for you to insert ready-made snippets of code into your projects.IntelliSense Code Snippets are XML files with a .snippet file name extension that adhere to the  IntelliSense Code Snippet XML schema (see Code Snippets Schema Reference).

Creating a Snippet File

1) To create a snippet file, you must create an XML file with .snippent extension
2) Open the file
3) Right-click on the editor window and select "Insert Snippet" to create a basic snippet template.
4) In the header section, like web page we need to give meta information for the snippet. We have the following elementes in header section
  a) Title - Title of the snippet.
  b) Author - Author of the snippet.
  c) Description - Description for the snippet
  d) Shortcut - It is the trigger word that activates your snippet (for example "gopisnippet")
  e) SnippetType
        The text value must one of the folllowing values.
         SurroundsWith: Allows the code snippet to be placed around a selected piece of code.
         Expansion: Allows the code snippet to be inserted at the cursor.
         Refactoring: Specifies that the code snippet is used during Visual C# refactoring. Refactoring cannot           be used in custom code snippets.
      Since we are creating an Expansion Snippet, remove the tag
SnippetType >SurroundsWith < /SnippetType>
5) Snippet section:
    a) Code :Provides a container the short code blocks of IntelliSense Code Snippets."Language" is the required attribute for this. Since we are creating a snippet for C# so change it to CSharp.
     <Code Language=”CSharp>
    b) The Code section contains the code block to be added.


The final version of the snippet is

<xml version="1.0" encoding="utf-8" ?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    < Header>
      < Title >Test Snippet  <  /Title>
      < Author  > Gopi  < /Author>
     < Shortcut  > gopisnippet  < /Shortcut>
      < Description  > Gopi Snippet description  < /Description>
     < SnippetTypes>
        < SnippetType > Expansion  < /SnippetType>
     </SnippetTypes>
    </Header>
    < Snippet>
      <Declarations>
        <Literal>
          <ID >  name  </ID>
          <Default >  value  < /Default>
        </Literal>
      < /Declarations>
      < Code Language="CSharp">
        < ![CDATA[using (SqlConnection oCon = new SqlConnection(DBHelper.CONN_STRING_SQL))
            {
             
            }]]>
      < /Code>
    < /Snippet>
</CodeSnippet>

Note: Inside the Snippet element, add the References element and all of the required child elements that add a reference to the project when the snippet is inserted. But it is now supports only for Visual Basic Code Snippets.

Load Snippets into Visual Studio:

To use a code snippet, first it will need to be loaded in Visual Studio. Follow the below steps to load your snippets in Visual studio.
1) Naviagate to "Tools -> Code Snippets Manager" in visual studio.
2) Select Import
3) Locate your snippet file
4) Visual Studio will then prompt you to where you want to place your snippet. By default "My Code Snippets" folder is checked. Click on Finish.

Now your snippet is ready for use.

To use code snippets through the Insert Code Snippet Menu:

1) In the Code Editor, put the cursor where you want to insert the code snippet.
2) Launch the Insert Code Snippet menu in one of three ways:
    a)Press CTRL+K, CTRL+X.
    b) On the Edit menu, point to IntelliSense, and then click Insert Snippet.
    c) Right-click the mouse and then select the Insert Snippet command on the shortcut menu.
3)Select the code snippet from the code snippet inserter and then press TAB or ENTER, or double-click the snippet.

To use code snippets through the snippet’s shortcut:

1)In the Code Editor, put the cursor where you want to insert the code snippet.
2) Type the shortcut for the code snippet that you want to add to your code.

OR

Type the first few letters of the shortcut and then press either CTRL+SPACE (C#) or a question mark followed by the TAB key (VB) to view the completion list, and then select the snippet shortcut from the list.

3) Press the TAB key once for Visual Basic or twice for C Sharp to invoke the code snippet.

Gopikrishna

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment