One of my favorite new features in Whidbey is Code Snippets.  Code snippets are xml files stored on your machine that allow you to insert chunks of predefined code and then move quickly between common places that need to be filled in with values relevant to the current method or class, and then continue editing your file as usual.  For example, in a C# file you can type the word “for” and then hit the tab key.  This will insert a code snippet.  You’ll notice that you have inserted code similar to the following:

 

for (int i = 0 ; i < length ; i++)

{

 

}

 

The first “i” is highlighted in yellow.  You can now change that value “i” to something more meaningful, say iCount.  Hit the tab key again and your caret will move to the next yellow field “length”.  As you did this, all the other “i” values updated to be “iCount”.  You can hit tab again to go back to iCount and then hit enter to complete your task.  Your caret will now be placed in the middle of the loop ready for you to enter content.

 

To see all the code snippets that you have on your machine you can either go to the Tools menu and select “Code Snippet Manager…”, or you can use the insertion UI to browse through them and insert one.  To get to the insertion UI, right click in a C# file, choose the “Intellisense” menu and then choose “Insert Expansion”.  Selecting one of the snippets from the intellisense list here will insert that snippet into your file like it did when you typed “for” and tab.  To modify your snippets and customize them, all you need to do is go to the Code Snippet Manager, find the snippet you want, and look at the location field for the path to the snippet file.  Simply open this file in the xml editor and modify it.  As soon as you save it, you can go ahead and use that snippet again and your modifications will be there.

 

Here is an example of a snippet file:

 

<?xml version="1.0" encoding="utf-8" ?>

<CodeSnippets>

<CodeSnippet Format="1.0.0">

    <Header>

        <Title>for-iteration by index</Title>

        <Shortcut>for</Shortcut>

        <Description>Expansion snippet for 'for' loop</Description>

        <SnippetTypes>

            <SnippetType>Expansion</SnippetType>

            <SnippetType>SurroundsWith</SnippetType>

        </SnippetTypes>

    </Header>

    <Snippet>

        <Declarations>

            <Literal>

                <ID>index</ID>

                <Default>i</Default>

            <ToolTip>Index</ToolTip>

            </Literal>

            <Literal>

                <ID>max</ID>

                <Default>length</Default>

            <ToolTip>Max Length</ToolTip>

            </Literal>

        </Declarations>

        <Code Language="csharp" Format="CData"><![CDATA[for (int $index$ = 0; $index$ < $max$; $index$++)

                  {

                  $selected$ $end$

                  }]]>

            </Code>

    </Snippet>

</CodeSnippet>

</CodeSnippets> 

 

The most interesting things that you might want to change at first are the shortcut tag or the code block.  Changing or adding anything in the CDATA block will change what appears in your file when inserting it.  Changing the shortcut value changes what you type before hitting tab to insert the snippet.  The interesting portions of how to create your own snippet comes in with the literal tags.  Each literal corresponds to a value that you will tab between (the yellow highlighted portions in the editor).  You’ll notice that the first literal “index” is shown down in the code block with $ on each side of it.  The value that is inserted into the editor by default for this is “i” which is found in the Default tag for this literal.  The “max” literal is the next field in this for loop.  You’ll see two values in the code block that look like literals but aren’t defined anywhere.  $selected$ and $end$ are reserved literals.  $selected$ tells us that if you highlight a block of code and insert the for loop, we will insert the snippet around that selected text so that your selected text becomes the body of the loop.  The $end$ tag tells us where to put the caret when you hit the enter key after tabbing through the snippet fields.

 

If you are using VB, you can also insert these snippets by right clicking and choosing “Insert Snippet…”.  In Beta2 you should be able to use the shortcut like we did in C# by typing into the editor and hitting the tab key.  Also in Beta2 we will be adding xml snippets.

 

In my next post, I’ll talk a little more about other aspects of the snippet schema including how functions play into the literals.

 

There is a tool snippy that a member of the C# team (my husband Gus) wrote to create code snippets.  Once you create a snippet, save it to disc and then in the Code Snippet Manager you can tell us to look in that location for snippets or you can save the snippet with the other ones you already have on disc and we will automatically pick it up.

 

Let me know what you think of this feature…

 

-Meghan