給大家介紹一個好工具,
A Visual Studio.NET addin that provides a mechanism
for inserting commonly used code snippets
Introduction
Code<Template>.NET is an addin for Visual Studio.NET that provides a
mechanism for inserting commonly used text fragments into your source code. It
is based on the ideas presented in my additions to Michael
Taylor's extension to Darren
Richard's original CodeTmpl addin.
The main differences with the original CodeTmpl addin are:
- Support for Visual Studio.NET
- Rewritten from scratch in C#
- Cleaner file format
- Keyword, environment and prompted replaceable values
- Predefined and .NET standard formatters
The text fragments used by Code<Template>.NET are contained in files
called CodeTmpl.* (where * is a per-language extension) which
are located in the user's roaming profile. These files are originally copied
from sample files which are located same directory as the addin's executable.
The format of these text files is very simple: named blocks contain the text
fragments that will be inserted in your source code. Besides the directly
inserted text, these fragments can also contain tags which represent replaceable
keywords and prompted values. These named blocks will show up as menus and
submenus in Code<Template>.NET's toolbar button. When you click on one of
these menu items, the corresponding text fragment will be inserted in the active
window's insertion point.
Configuration
The template files are completely configurable so you can replace or change
the default text fragments to suit your own needs. The format of the template
files is extremely simple. Basically you paste your block of code into the file
and surround it with the open menu (#{
) and close menu (}#) tags.
The open menu tag should be followed by the name that will appear on the popup
menu. The open menu tag can also include a Menu ID that is separated from the
display name by a vertical bar (|); this Menu ID is used to
directly insert a template into the editor by pressing
Ctrl+Enter. Whatever text is placed between the open
and close menu tags will be copied verbatim into the text editor. Menu items can
be nested, creating a hierarchical view of templates. You can also specify an
access key by placing an '&' character before the character
to be used as the access key. For example, to specify the "F" in "File" as an
access key, you would specify the caption for the menu name as "&File". You
can use this feature to provide keyboard navigation for your templates. A
separator (##) tag can be used to
separate menu items and an exclamation mark (!) at the start of
a line is used for single line comments.
Additional template files can be included by adding a line starting with
"#include" followed by the file name. If the file name does not
have an absolute path, the file will be searched for relative to the base
template file in the user's roaming profile. Include statements can only be
inserted between menu and submenu definitions.
For example:
#{Hello World - &Console|hwc
#include <iostream>
int main()
{
std::cout << "Hello, new world!\n";
}
#}
##########################
#{Hello World - &GUI|hwg
#include <WinUser.h>
int PASCAL WinMain(HANDLE hInstance,
HANDLE hPrevInstance,
LPSTR lpszCommandLine,
int cmdShow)
{
MessageBox(NULL, "Hello, World", "Example", MB_OK);
}
#}
If your template file contained the above text then clicking the CodeTmpl
toolbar button would display a popup menu containing the options Hello World
- Console and Hello World - GUI, with a separator
between them. Selecting Hello World - Console would paste the text shown
between the '#{' and '#}' tags into your source; a speedier way to insert this
template would be to type hwc and then press
ctrl+<space> .
Keywords
The replaceable text between the menu open and menu close tags can contain
replaceable keywords. These keywords are surrounded by the keyword
open (<% ) and keyword
close (%>) tags and are case-insensitive. To
insert these tags verbatim in the text (in ASP templates for example) escape the
percent ('%') symbol by preceding it with a
backslash ('\'). The less
than ('<') and the greater than
('>') characters are escapable too.
Currently, the following keywords are predefined:
SOLUTION |
Returns the solution name |
PROJECT |
Returns the current project name |
FILE |
Returns the current file name |
NOW |
Returns the current date and time |
TODAY |
Returns the current date |
GUID |
Returns a GUID |
TEMPLATE |
Inserts the text from another template |
For example:
#{Sample
The solution's name is <%SOLUTION%>
and the current project is <%PROJECT%>
}#
Will be expanded to:
The solution's name is CodeTemplateNET
and the current project is CodeTemplateNET
If an undefined keyword is used, then it's value is searched for in the
system's environment; if the environment variable is not found the keyword is
replaced with the empty string. For example:
#{User name
Logged-in user name: <%USERNAME%>
}#
Will be expanded to:
Logged-in user name: velasqueze
When using nested templates using the template keyword, the
template that is being referenced must have a MenuID. For example:
#{Template A|tpla
// This is template A
}#
#{Template B|tplb
// <%template:tpla%>
// This is template B
}#
Ah, and by the way... recursive template nesting is a Bad
Thing... don't do it!
If the keyword's name starts with a question mark
(?) then the keyword represents a prompt value and it's name is
used as the prompt string in the input dialog. The prompt keyword name can
contain spaces. For example:
#{Ask me something
<%?Please answer the prompt%>
}#
Will be expanded to: (Supposing you typed "I just answered!" in the input
box)
I just answered!
The cursor position tag (%$%) will
reposition the caret after the text has been formatted and inserted in the
editor window.
Formatters
The value of a keyword can be modified by one or more formatters.
Formatters follow the keyword name and are separated by
colons (:) and can have parameters.
Currently, the following formatters are predefined:
U |
U[=start[,length]] |
Returns the key's value in uppercase. Start is zero-based. |
L |
L[=start[,length]] |
Returns the key's value in lowercase. Start is zero-based. |
W |
W=width |
Returns the key's value truncated to the parameter's width. |
R |
R=str1[,str2] |
If only str1 is present, returns the key's value replacing the first character with the second character in the parameter string. If str2 is also present, returns the key's value replacing str1 with str2. |
FMT |
FMT=str |
Returns the key's value formatted applying the parameter to System.String.Format(). e.g. String.Format("{0:str }", key) |
VALUES |
VALUES=v1{, vn}* |
Forces the prompt keyword to show up as a combobox with a predefined list of values. Other values can be typed in. |
FIXEDVALUES |
FIXEDVALUES=v1{, vn}* |
Forces the prompt keyword to show up as a combobox with a predefined list of values. Only the values in the list can be used. |
MULTILINE |
MULTILINE=int |
Forces the prompt keyword to show up as a multiline textbox. The provided value indicates the height (in lines) of the control. |
DRIVE |
DRIVE |
Returns the drive part of the key's value |
DIR |
DIR |
Returns the directory part of the key's value |
FNAME |
FNAME |
Returns the file name part of the key's value |
EXT |
EXT |
Returns the file extension part of the key's value |
PATH |
PATH |
Returns the drive and directory parts of the key's value |
BASENAME |
BASENAME |
Returns the file name and extension parts of the key's value. |
For example:
#{Sample
User name: <%USERNAME:U%> // User name uppercased
GUID: <%GUID:R=-_:U%> // GUID replacing all the '-' with '_' and uppercased
File's base name: <%FILE:BASENAME%>
m_a is a <%?Access:values= public,protected,private%> variable.
}#