jens-schaller.de

Code Snippets

Adding a Code Snippets path located on a network drive

I just got an e-mail referring to my Code Snippets - the whole enchilada blog post. The question was how you can add a Code Snippets path which is located on a network drive.

Here is how it works:

  • Open up regedit.exe
  • Go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Languages\CodeExpansions\CSharp\Paths
  • Edit the Microsoft Visual CSharp entry and add the desired path to where the Code Snippets are located separated by a semicolon.
    You can either use a mapped network drive or use the network path directly.

You can of course use a .reg file to import the new path. Here is an example for that:

<p>Windows Registry Editor Version 5.00  <p>[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Languages\CodeExpansions\CSharp\Paths]<br>"Microsoft Visual CSharp"="%InstallRoot%\\VC#\\Snippets\\%LCID%\\Visual C#\\;%InstallRoot%\\VC#\\Snippets\\%LCID%\\Refactoring\\;%MyDocs%\\Code Snippets\\Visual C#\\My Code Snippets;<strong>&lt;my new Snippets path&gt;</strong>"

Replace the <my new Snippets path> placeholder with your desired path and save this to a file with the .reg extension. Remember to double the number of backslashes for escaping purposes. So for a network folder like \\MyServer\MySnippetsFolder you have to use \\\\MyServer\\MySnippetsFolder in the .reg file.

After doing so, every Code Snippet located in the added folder will automagically be added to the list of available Code Snippets. You don't even have to restart Visual Studio after adding new Code Snippets. You can also add subfolders and place your Snippets there. The subfolders will also be added automaticaly.

Here comes the "but" as nearly always: The strange thing is, that you have to add this path to the "LOCAL_MACHINE" section to the registry. Adding the same path to the "CURRENT_USER" section had no effect.

Folien zum Vortrag "Visual Studio anpassen und erweitern"

Nachdem Roland Weigelt und ich am letzten Montag bei der .NET User Group Paderborn einen Vortrag zu den Anpassungs- und Erweiterungsmöglichkeiten von Visual Studio 2005 gehalten haben, sind die Powerpoint Folien nun auch als Download verfügbar.

Es hat riesigen Spaß gemacht vor den 15 anwesenden Zuhörern zu referieren. Vor allem wenn man bedenkt, dass angesichts des prima Wetters die meisten Paderborner den Abend in den Wiesen an der Pader genossen.

Visual Studio anpassen und erweitern - in Paderborn

Wahrscheinlich habt Ihr es bereits in Rolands Blog-Eintrag gelesen:

Am 02.04.2007 halten wir von 18:00 bis 21:00 einen Vortag bei der .NET User Group Paderborn zum Thema "Visual Studio anpassen und erweitern".

Wir reden dabei über:

  • Code Snippets
  • Project und Item Templates
  • Makros
  • Add-ins
  • VSPackages

Ob sämtliche Themen behandelt werden, hängt ganz und gar vom Publikum ab. Wir werden uns den Wünschen, den Interessen bzw. dem Kenntnisstand anpassen.

Wir freuen uns auf Euch!

Code Snippet for releasing a COM object

After writing about Code Snippets, I thought it is time to publish one myself.

Description: It's a Code Snippet for releasing a COM object. It first checks, if the object really is a COM object then releases it.
Shortcut: rco
Result:

if ( (MyObject !=null) && (Marshal.IsComObject(MyObject)) )
{
Marshal.ReleaseComObject(MyObject);
}

Download: ReleaseComObject.snippet

Code Snippets revisited

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.

MultiCodeSnippet

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.

SurroundWithExample

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.

SnippetType

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.

  • GenerateSwitchCases(EnumerationLiteral)
  • ClassName()
  • SimpleTypeName(TypeName)
  • CallBase(Parameter)

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

Scott Bellware published a great set of Code Snippets for working with NUnit. You can find them here.

Code Snippets - the whole enchilada

If you are using Visual Studio 2005, you sure have encountered a new nifty feature called Code Snippets.

There are a lot of articles and blog posts surrounding this topic, so why another post? Well I'll try to give give you a (hopefully) complete roundup on

  • what Code Snippets are
  • how you can use them
  • how to create your own
  • where to find some already done by others
  • and how to install and manage them

I'll describe the handling of Code Snippets in C#. In VB.NET Code Snippets behave a bit different, but since I am only using C#, I can't tell you much about that.

Introduction to Code Snippets

Code Snippets are short pieces of reusable code for everyday use, which can be used in a very handy way.

Well, since Visual Studio 2003 you can write own pieces of code, which you can drag to the Toolbox for reuse, but using Code Snippets is A LOT sweeter.

You don't have to use the mouse to drag them to your workplace anymore. Just type in the defined shortcut and press the TAB-key two times to insert a snippet. For browsing available Code Snippets you just press Ctrl+K,X or select Edit - IntelliSense - Insert Snippet... in the Visual Studio menu.

Also, you can define placeholders inside a snippet, which are replaced during insertion. You can take a look at a nice demo and how to by Jeff Atwood.

Create your own Code Snippets

Code Snippets are simply plain XML files, so you could write them in notepad with a little help of the Code Snippets Schema Reference. I would recommend taking a look at Snippy. Snippy is a free editor for creating and editing Code Snippets. Since the tools hosted on gotdotnet aren't usually well documented, you might want to take a look at the overview at Visual Studio Hacks.

Ready made Code Snippets by other develolpers

There are some good locations on the web, where you can find ready to use snippets made by other developers.

First I'd like to point you to the snippets done by my colleague Roland. He did some for inserting Properties with Prefix Notation, Events, NUnit Test Methods, etc.

After that you should take a look at GotCodeSnippets.NET, where many user defined Code Snippets are hosted.

In VB.NET you find a big bunch of already installed Code Snippets for any circumstance. Microsoft just published the same snippets for C# as a free download. Stupidly the installation isn't really that, what you want. In the C# Snippet Parity post by Jeff Atwood, you find a better way of installing, which brings me to the last section of this article.

Managing Code Snippets

Since Jeff Atwood wrote nearly everything I planned to write in his C# Snippet Parity post ;), I try to make this short.

Jeff wrote about a way to install the additional C# Code Snippets by Microsoft using a system wide approach and a registry file. I prefer a per user installation by the following reasons

  • Normally I am the only one on my PC coding
  • I don't like to install "personal" things in the Program Files folder, because they are gone after a clean system installation. (I use a different partition to store my files on, so I never have to worry when reinstalling my system.)
  • The snippet paths are stored in the HKCU path in the registry anyway
  • I don't have to add new snippet paths to the registry

So, what to do? When installing Visual Studio 2005, the path Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets will automatically be created inside the My Documents folder.

You can simply download the repackaged C# Code Snippets by Jeff and unpack them into the My Code Snippets folder. When calling the Visual Studio 2005 Code Snippets Manager using Ctrl+K,B or Tools - Code Snippets Manager..., you should find the new snippets inside the My Code Snippets folder.

CodeSnippetsManager

InsertCodeSnippetDialog

You can simply reorganize your Code Snippets using the Windows Explorer. Now, how easy is that?

UPDATE: Jeff just wrote a nice macro for enumerating all installed Code Snippets.

Just a few installation hints, since some guys had problems with it:

  • Download it from here
  • Unpack and rename it to KeyboardShortcuts.vb
  • Start the Macros IDE (Alt+F11 out of Visual Studio)
  • Import it by right-clicking on MyMacros -> Add - Add Existing Item

The rest should be clear. If not, just take a look at Jeff's post.

Links in this post (in order of appearance)

This article is also filed here.