In my last post concerning Code Snippets I covered some of the basics. This post will cover some details i didn’t mention before.
Today I will talk about:
- If a single Code Snippet can support different programming languages
- How to write a SurroundsWith-Snippet
- Why the SnippetType should be set
- Code Snippets Functions
- NUnit Code Snippets for Visual Studio 2005
Is it possible to create Code Snippets supporting different programming languages?
When creating your own Snippets, you normally do this for a specific language e.g. C#. But what if you got a team where some guys code in C#, some in VB.NET and you would like to create only one set of snippets each supporting code for C# AND VB.NET in a single file?
Unfortunately I have to say, that it doesn’t work.
Even though the XML schema for defining Code Snippets allows you to define more than one Code-tag inside a Snippet definition, Visual Studio 2005 only parses a Snippet for the first existing Code-tag and ignores all others.
How to write a SurroundsWith-Snippet
When you select a part of your code and right-click, you’ll find a Surround With entry, which actually lists all Snippets supporting this mode.
Preparing a Snippet for surrounding the current selection is fairly simple: You just have to contain the $selected$ parameter at the designated position as you can see in the example of the #region Snippet, which is shipped with Visual Studio 2005.
The SnippetType should be set to SurroundsWith (see next chapter).
Why should the SnippetType be set?
The type of a Snippet can be defined using the SnippetType-tag inside the Snippet header.
It’s an optional tag, but it should always be set due to the following reasons.
There are three values, which can be used inside the SnippetType-tag:
- 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.
As you can see, only the first two types can be used for defining own Snippets. But those two are quite important.
The type Expansion is used for inserting the code of a Snippet at the current cursor position e.g. when using a snippet for inserting a property. It overwrites the current selection!
So, if you select a part of your code, without having set the SnippetType correctly, this Snippet will also be presented when you right-click in your code and select Surround with. If you then choose a snippet, which isn’t prepared for actually surrounding your selected code with something, your code will be overwritten on insertion of the Snippet code.
As you can see in the #region example, it is allowed to define multiple SnippetType-tags.
Code Snippets Functions
When defining a Snippet parameter (see Literal-tag) you can specify a Function-tag. You cannot define own functions, but there are 4 predefined functions you can use for C# Code Snippets.
For a description of those functions please take a look at the function definitions in the MSDN. Examples on how to use this functions can also be found there.
NUnit Code Snippets for Visual Studio 2005