We will introduce you to a good tool,
A Visual Studio. NET addin that provides a mechanic
For inserting commonly used code snippets
Introduction
Code <template>. NET is an addin for Visual Studio. NET that provides
Machism 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
CalledCodetmpl .*(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
These menu items, the corresponding text fragment will be inserted in the active
Window's insertion point.
Configuration
The template files are completely available 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 Open menu ( #{
) And Close menu ( }# ) Tags.
The Open menu tag shoshould 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
Display name by a vertical bar ( | ); This menu ID is used
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
Access key by placing' & 'Character before the character
To be used as the access key. For example, to specify the "F" in "file" as
Access key, you wowould specify the caption for the menu name as "& File". You
Can use this feature to provide keyboard navigation for your templates.
Separator ( ## ) Tag can be used
Separate menu items and Exclamation mark (!) At the start
A line is used for single line comments.
Additional template files can be encoded by adding a line starting
"# 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
int main ()
{< br> STD: cout "Hello, New World! \ N ";
}< br> #}
# ########################
#{ Hello world-& GUI | hwg
#include
int Pascal winmain (handle hinstance,
handle hprevinstance,
lpstr lpszcommandline,
int cmdshow)
{< br> MessageBox (null, "Hello, world" , "example" , mb_ OK);
}< br> #}
If your template file contained the above text then clicking the codetmpl
Toolbar button wocould display a popup menu containing the optionsHello World
-COnsoleAndHello world-GUI, With a separator
Between them. SelectingHello world-ConsoleWocould paste the text shown
Between the '# {' and '#}' tags into your source; a speedier way to insert this
Template wocould be to typeHWCAnd then press
CTRL + <space>.
Keywords
The replaceable text between the menu open and menu close tags can contain
Replaceable keywords. These keywords are surrounded byKeyword
Open(<%) AndKeyword
Close(%>) Tags and are case-insensitive.
Insert these tags verbatim in the text (in ASP templates for example) escape
Percent('%') Symbol by preceding it with
Backslash('\').Less
Than('<') AndGreater
('>') 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:
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
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:
Logged-in user name: velasqueze
When using nested Templates usingTemplateKeyword,
Template that is being referenced must have a menuid. For example:
# {Template A | tpla
// This is template
}#
# {Template B | tplb
// <% Template: tpla %>
// This is template B
}#
Ah, and by the way... recursive template Nesting isBad
Thing... Don't do it!
If the keyword's name starts withQuestion 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!
TheCursor PositionTag (% $ %) Will
Reposition the caret after the text has been formatted and inserted in
Editor window.
Formatters
The value of a keyword can be modified by one or moreFormatters.
Formatters follow the keyword name and are separated
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 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 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 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.
}#