Visual Studio Hacks [Electronic resources]

Andrew Lockhart

نسخه متنی -صفحه : 172/ 88
نمايش فراداده

Hack 57. Insert Commonly Used Code Automatically

Discover an add-in that will provide code templates and code completion to enable you to write code faster.

Throughout this book, we have talked about different ways to handle code snippets, including storing them in the toolbox or using other add-ins to handle your code snippets. The Code<Template>.NET add-in allows you to create and manage code templates, which are code snippets that contain variables. Instead of just having a piece of text you can drag to your document, you will be able to specify variables that you will be prompted for. What differentiates this add-in from other code snippet management add-ins is the ability to define variables as well as the ability to automatically insert your code template using a predefined keyword and shortcut key.

Contrary to the misleading angle brackets, this add-in does not have anything to do with C++ templates or .NET 2.0 generics.

First you will need to download and install the Code<Template>.NET add-in from http://www.codeproject.com/useritems/CodeTemplateNET.asp. The installation is relatively painless, and the next time you start Visual Studio you will see the CodeTemplate toolbar, which is shown in Figure 6-18.

Figure 6-18. CodeTemplate toolbar

It is from this menu that you will work with the add-in. First, let's add a new code template to the tool. Click Open <codetmpl.cs> on the CodeTemplate menu. The codetmpl.cs file contains all of the code templates that this tool manages. It is to this file that you will need to add your own code templates. As you can see, the format of this file is a little confusinghere is a look at one of the standard code templates:

#{Subsection Header|subh
///////////////////////////////////////////////////
// <%?Subsection name%>
}#

Each snippet starts with #{ and then the name of the snippet, in this case Subsection Header. After the name of the template is a | character followed by a keyword, which will be used to autoinsert the code into your document. What follows next is the actual code that will be inserted into the document. This can also include variables. In this case you see <%?Subsection name%> in the text. When the template is inserted, the add-in will prompt you with a dialog like the one shown in Figure 6-19.

Figure 6-19. CodeTemplate variable dialog

This dialog allows you to specify the variable at the time of insertion. The last part of the code template is the ending }#. So let's add a new code snippet to this file:

First you need to open the template and create a name and shortcut key for it:

#{SQLConnection|sqlc

Next you need to specify the actual text that will be inserted, including any variables:

SqlConnection sqlConn = new SqlConnection(
ConfigurationSettings.AppSettings["<%?ConfigKey%>"]);
using(sqlConn)
{
}

Then you need to close the template:

}#

Next you simply need to save the file, and the menu will be updated.

Now that the code template is added to the file, it can be invoked in two different ways. The first is to select the code template from the menu; the second is to type sqlc into the document and then press Ctrl-Space (You may want to reassign this shortcut key, as it tends to interfere with the normal Ctrl-Space shortcut.) After performing either of these functions, you will be presented with the variable dialog shown in Figure 6-20.

Figure 6-20. SQLConnection variable dialog

First, you need to specify the variable for this snippet. After you press the OK button, the code shown here will be inserted into the document.

SQLConnection sqlConn = new SQLConnection(
ConfigurationSettings.AppSetting["connString"]);
using(sqlConn)
{
}

As you can see, this add-in can help quickly insert code snippets, including code snippets that include variables.

This add-in also includes a number of default keywords that can be used in your templates; these are used by enclosing the keyword between <% and %> characters. (Notice that, unlike ConfigKey in the preceding example, there is no question mark; those are used only for variables that will be specified by the user.) You can insert any of the keywords shown in Table 6-4 in the template, and the keyword will be replaced with the value specified in the table.

Table 6-4. Template keywords

Keyword

Value

SOLUTION

The name of the current solution

PROJECT

The name of the current project

FILE

The name of the current file

NOW

The current date and time

TODAY

The current date

GUID

A new GUID

TEMPLATE

Includes the text generated by another template

The TEMPLATE keyword is particularly interesting since it allows you to nest templates within each other. Here is an example of how to use this keyword:

<%template:sqlc%>

This would insert the output from the template defined earlier into another template. You will still be prompted for any variables when the parent template is executed.

The Code<Template>.NET add-in is a great way to decrease the amount of time you spend typing the same code over and over again.