Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

An Introduction to the New Visual Studio Snippet Format  : Page 2

Visual Studio 2005 ships with a pre-built—and extensible—personal code library that it makes available as "snippets" in the IDE so you can drop pre-tested chunks of code into your projects easily.


advertisement
Create Your Own Snippets
Now you know how to retrieve and insert snippets into your projects using the Visual Studio Snippets Manager, and you've probably explored the large number of built-in snippets that ship with Visual Studio. But you're probably eager to create your own snippets and use them the same way. Here's how to create a .snippet file, save it in the My Code Snippets folder and use it in your current project. Afterwards, you will see how to add other snippet folders to the Snippets Manager to help organize your code better or access snippets on a network share.

First, create a stub private function called replaceLastOcurr() that returns a string. Your code should look like this.

Private Function replaceLastOcurr() As String End Function

A snippet file is a well-formed and valid XML file with a .snippet extension. Start by creating a new XML file to hold the snippet using the standard document type declaration, including the encoding attribute. You can use Visual Studio, or NotePad, or any text editor; however, I use Altova's XML Spy for all my XML-based documents.

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

Snippet files contain the following nodes, some of which can contain child nodes. The root element is <CodeSnippets>, which contains one or more <CodeSnippet> elements. Each <CodeSnippet> element contains the snippet code, as well as nodes for ancillary information, such as the snippet title, author, description, and keywords. The <CodeSnippet> element also includes a shortcut phrase and help URL.



<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/ 2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title></Title> <Author></Author> <Description></Description> <Keywords> <Keyword></Keyword> <Keyword></Keyword> </Keywords> <Shortcut></Shortcut> <HelpUrl></HelpUrl> </Header>

The code structure description begins after the header, in a <Snippet> element. Each <Snippet> element has a child <Declarations> element, which contains a set of <Literal> nodes. A literal is the description of a replaceable parameter or variable.

Each <Literal> element contains:

  • An <ID> element, which is your replaceable parameter.
  • A <Type> element, which holds the data type of your replaceable parameter.
  • A <ToolTip> element, which displays information or instructions in the Snippets Manager for display on a <MouseOver> event.
  • A <Default> element, which is a default value for this literal.

<Snippet> <Declarations> <Literal> <ID></ID> <Type></Type> <ToolTip></ToolTip> <Default></Default> </Literal> <Literal> <ID></ID> <Type></Type> <ToolTip></ToolTip> <Default></Default> </Literal> <Literal> <ID></ID> <Type></Type> <ToolTip></ToolTip> <Default></Default> </Literal> </Declarations>

The following and final child element in the <Snippet> section is <Code>, which is where your actual code snippet goes. The <Code> node has two attributes; Language and Kind. The Language attribute defines the language used for the code and accepts VB, Csharp, JSharp or XML. The Kind attribute specifies the kind of code the snippet contains and filters out snippets based on your location in the code. This example uses "method body" and "function body."

You begin your code by containing it within a character data declaration.

<![CDATA[code_here]]>

The CDATA tag tells the XML interpreter to not parse the code—to treat it as literal content.

You write the code exactly the same as if it were in your project, except that you place the IDs from your Literal nodes where your replaceable parameters go, delimiting them within your code using dollar ($) signs.

 
Figure 4. An XML Snippet File: This example snippet file contains code converted from an existing DevX VB.NET tip.
For this example, I've converted an existing DevX VB Tip to a code snippet. Figure 4 shows an example snippet XML file and how the code for the VB Tip was transformed.

Save this file with a .snippet extension into the folder C:\Documents and Settings\\My Documents\Visual Studio 2005\Code Snippets\Visual Basic\My Code Snippets.

The VS.NET 2005 Beta 2 installation creates that folder during installation, and should already be included in your Snippets Manager. The Snippets Manager comes with a few options to add snippets or folders of snippets. The Add button gives you the ability to add a new folder and all its contents to the directory tree, whereas the Import button lets you import a single snippet into an existing directory within the Manager. There is a Remove button to remove entire folders from the directory tree, but note that the Remove feature does not let you remove individual snippets. There is a Search Online button, but with VS 2005 not yet in production, there were no snippets to be had online at the time of this writing. Give it time.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap