Jeff Key of NetPing and SnippetCompiler fame,
wrote a nice expansion for using a generic
list, so I thought I would share some useful information on expansions.
NOTE - This is all based on Whidbey Tech Preview, so things can and
Expansions are fill-in-the-blank snippets of code that have several benefits.
They help automate boiler-plate code constructs like looping through a collection
using a foreach statement, they help reduce syntax errors and they're a
perfect example of code-focused RAD. One of the things you'll notice with C#
Whidbey is the concept of code-focused RAD, meaning RAD isn't just limited to drag-and-drop
wizards, we're going to add productivity features directly into the
code editor where C# developers spend the majority of their time.
Note - You can find the list of expansions that come with Whidbey in the "C:\Program
Files\Microsoft Visual Studio .NET Whidbey\VC#\ExStencil\" directory.
To help explain expansions, I've created two simple custom expansions in an xml file
named custom1.xml and placed the file in the ExStensil directory mentioned above.
The two expansion are explained below:
The code for these expansions is below:
<?xml version="1.0" encoding="utf-8"?>
<description>Expansion snippet for Console.ReadLine</description>
<description>Expansion snippet for Console.WriteLine</description>
<tooltip>Value to write to the Console</tooltip>
The title and description values are used to provide information to the developer
using the expansion. For example, when a user sees the list of available expansions,
if they highlight an expansion, they will see the expansion title and description
in this format: title(description). In the case of Console.ReadLine,
the user will see "Console ReadLine (Expansion snippet for Console.ReadLine)".
The category value determines which IDE context menu will list the expansion.
For example, when a user invokes the Console WriteLine expansion, the cursor is placed
directly on the value literal, which by default will have the value "hello world".
Visual Studio will automatically select the text, so if a user starts typing, the
default value is automatically replaced.
The next part is the actual code for the expansion, which is rendered in a CDATA XML
tag so that XML parsers ignore the contents and you can use reserved XML escape characters
like "?", "<", ">", in the code section.
In the Console WriteLine expansion, the code section is declared such that the $value$
variable will be directly inside the Writeline method call. After completing
the expansion, the cursor will then be positioned on the $selected$ section.
As you can see, writing expansions is easy, and they're one of my favorite features in
Whidbey. If you're into experimentation, I suggest you open up the expansions.xml
file located in the ExStencils directory and begin playing. I'll be
providing more information on expansions in future posts, but if you have feedback
on expansions (both the good and the bad) please let us know.