I know I said earlier that I would talk about the features in the upcoming Visual Studio 2005, but a certain something has occupied my time for the last few months. Right before I left for Brazil I downloaded some of the express editions that microsoft posted. I’ve only installed the C# edition, so far. At first I was skeptical about these express editions, but it seems to be pretty good so far.
I started playing with the new framework and the IDE (can we call it that, still?). Right now, it’s just the equivalent of doodling, but I do have some project ideas. My framework play has so far only been with System.IO. There are some nifty improvements in that namespace, and they’re not even that flashy. I wanted to look at what some of the new properties spit out, so I whipped up a quick Console App. One of the things I type a lot when debugging(yeah, yeah unit tests, I know) is
Console.WriteLine(”Some string {0}”, someobject.Property);
My fingers can fly across the keyboard when I type that. Sometimes they fly too fast and I start off with “COnsole,” and then intellisense doesn’t show up, etc. Is there a way to reduce typing? With VS 2005, there is! Yay! The folks at MS have setup a lot of the “code-based RAD” features as code snippets: refactoring, expansions, surround with.
Code snippets are XML files that contain templates for the particular expansion you want. For instance, if you type foreach and then press Tab the expansion will show up like so:
So I tried my hand at creating a new one for Console.WriteLine(). If you click Tools > Code Snippet Manager, you get an almost useless dialog that shows you where the expansions can be found. It installs two directories by default:
- [Program Files]\Visual Studio 8\VC#\Expansions\1033\Expansions
- [My Documents]\Visual Studio\Whidbey\CodeSnippets\Code Snippets\VC#\My Code Snippets
Something like those anyway. The first is where the default ones are kept. The second is a path that isn’t created by VS, you have to do it. Once you create it, though you can dump your expansions in there, and the IDE should find them. The reason they don’t create it has something to do with this being beta stuff.
To create mine, I just copied one at random from the first path into the second path, renamed it and edited the xml. This is what I got:
<CodeSnippet Format="1.0.0"> <Header> <Title>Console.WriteLine</Title> <Shortcut>writeline</Shortcut> <Description>Expansion for Console.WriteLine</Description> <SnippetTypes> <SnippetType>Expansion</SnippetType> <SnippetType>SurroundsWith</SnippetType> </SnippetTypes> </Header> <Snippet> <Code Language="csharp" Format="CData">Console.WriteLine("{0}", $selected$ ); $end$ </Code> </Snippet> </CodeSnippet>
As you can see, it’s a very simple expansion. The real work is in Snippet tag, I think. (I did this without google or documentation, so I can only guess at meanings and that’s bad.) There you can see the code that I put in. The ‘$selected$’ placeholder is for any selected text, so you can surround a selection with this particular code. I’m not sure what ‘$end$’ does, but I speculate it’s telling the IDE that it’s reached the end of the CDATA section. One thing I found is that $selected$ has to be separated from the rest of text by whitespace: “$selected$)” would fail; but “$selected$ )” wouldn’t.
Easy, eh? Now in the IDE, I can type writeline (note the Shortcut tag above), hit Tab and out pops Console.WriteLine(). Very cool and very powerful. I have a feeling I’ll be using this a lot. I also have a hunch that templates for whole files are based on this (I’m pretty sure I read that somewhere). I’ll look into that and maybe come back with something more formal and more detailed. Here’s a link to the xml file for Console.WriteLine().
Very cool – I’ll have to download the beta and give it a whirl.
SharpTools is a freeware extensible VS.NET IDE add-in that includes a code snippet library that works nicely with the editor in current versions of the IDE – and will in Whidbey, of course. Other cool stuff too – the Code Library is only one plugin.
Yep, freeware.
Just a note about the $end$ tag in the Snippet Code. This tells the IDE where to place the caret when you complete the snippet. Press the ESC key to exit Code Snippet mode and it moves the caret to the location marked by the $end$ tag.