Geeks With Blogs

News INETA Community Speakers Program
GeeksWithBlogs.net: WTFNext's hosting!

View Stacy Vicknair's profile on LinkedIn


WTF Next? Dev ramblings from a master of nothing.

Hey guys,

Here we are, and I’m taking the time to finish up my first series at my new host geekswithblogs! For this part of the series we’ll be taking a look at a practical example of an XML snippet, namely the snippet to create snippets.

I’ve created my own version, but if you’ve toyed with XML snippets before you would know that Microsoft has already included a snippet snippet. This is a good alternative to the “Hard” way described in Part 1, but what if you want a little more of a custom feel to your snippet snippet?

 

Getting Started

This time around I’ve published the code on Codeplex, at the following address:

http://www.codeplex.com/wtfnextsnippetseries

You can download the premade snippet directly, and then we’ll be looking into the elements of the snippet briefly.

 

A look through the XML

This snippet is fairly straightforward, and many of the items you might recognize if you are familiar with snippets or have gone through the first part of this series. Our header section is straightforward, and we make good use of literals.

There is one literal that we need to note here: CDATAEND. There’s a bit of a quirk that requires a workaround when making an XML snippet, and that’s if we need to use a CDATA section within our snippet itself.

The problem is that a CDATA section cannot contain another CDATA section. As a result, making a snippet of another snippet becomes complicated. How do you do it? Simply put a literal in the CDATA of your snippet that represents the terminator for the internal CDATA in the snippet.

So, we see in the snippet itself this:

    <Snippet>
      <Code Language="$Language$">
        <![CDATA[$selected$$CDATAEND$
      </Code>
    </Snippet>

and we have the literal CDATAEND defined as this:

<Literal Editable="false">
  <ID>CDATAEND</ID>
  <Default>]]&gt;</Default>
</Literal>

The $selected$ is a placeholder for any selected text when we use the SurroundsWith datatype. This allows us to highlight some VB or whatever and when we insert the snippet it automatically knows we want the code selected to be surrounded by our snippet, and have the selection inserted at the $selected$ point.

 

Summary

So this concludes the brief look at snippets, and I want to tell you guys that I’ll appreciate any feedback. The “Hard” way isn’t really that bad, and all in all (if you’re like me) it gives you complete quick control over your snippet creation. The “Easy” way is an external application, and is designed with syntax highlighting for a language specific creation, so you won’t be popping out the XML or C# snippets from the VB snippet creator.

Please post comments if you have criticisms or anything to say, I’m looking forward to improving the quality of this blog and of myself, and feedback will help with both!

 

Posted on Tuesday, November 4, 2008 12:45 PM Microsoft , How-To | Back to top


Comments on this post: Custom Code Snippets Series Part 3: XML Example

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Stacy Vicknair | Powered by: GeeksWithBlogs.net