You can edit the template code of pre-existing Live Templates, 'Surround With' Templates and File Templates in the Edit Template dialog. The same dialog is used to create a new template from scratch. For details on how to start creating or editing each type of template, please refer to the corresponding help sections:
Edit Template Dialog
A sample Edit Template dialog featuring the "itdic" live template is shown below:
Each template must have a unique abbreviation (see Using Live Templates or Using 'Surround With' Templates for description of use). You will not be able to save the template until a valid unique abbreviation is entered.
The template description appears in templates lists to help you choose the right template.
Specifies the template's availability scope. Click the link to set or modify the template's availability options via the following dialog:
Several choices are available:
- Language-specific: makes the template available in code of a specific language (chose the language from the drop-down list). For language-specific scope you can also narrow down the template's availability by selecting any of the five checkboxes (the choices are self-explanatory).
- All text files: makes the template available in all text files.
- File mask: makes the template available only for files whose names match the given mask. This allows you to create custom language-specific templates, for example, for editing HTML, XML or even C++ files.
Template code is defined in this text area. A template may represent just plain text, but it may also contain variables. After the template is expanded, these variables appear in the editor as input fields. Their values can be either entered by the user or automatically calculated by ReSharper.
To declare a variable within the template, use the following format: $<variable_name>$, where <variable_name> represents an arbitrary identifier. When the variable format is correct, it is colored in the template text with dark red as follows: $<variable_name>$.
There are special predefined template variables in ReSharper:
- $END$: Denotes the final caret position after the template is expanded and the user exits the template editing mode.
- $SELECTION$: Denotes the text selected by the user before requesting the template expansion. This variable is useless within the context of Live Templates, but is central for 'Surround With' Templates.
- $SELSTART$: Denotes the starting point of the text block that will be selected after the template is expanded. The caret will be positioned at this point after expanding (even if the $END$ variable is present).
- $SELEND$: Denotes the ending point of the text block that will be selected after the template is expanded.
To make ReSharper automatically suggest values for variables in template input fields, you can supply your variables with predefined values or/and with the macro-rules for calculating them. To assign predefined values or/and calculation rules, use the Template variables table (see description below).
The table contains a list of all template variables (except the special variables). It has the following columns:
- Name: Contains names of the template variables.
- Value: Contains values and/or macro-rules that ReSharper uses to suggest values when expanding the template. See Specifying Values of Template Variables below.
- Editable occurrence: Determines whether each template variable will have an input field allocated for it when expanding the template, and where the input fields will be located. If a given variable appears only once in the template code, the possible values of this field are Yes (the default) and No. If a variable appears in the template code n times, the possible choices range from #1 (the default, allocate the input field at the first occurrence of the template variable) to #n (allocate the input filed at the nth occurrence of the template variable) or None (do not allocate the input field). Double-click the cell to alter the value from its default state to any possible alternative. Should the value No/None be chosen in this column, the values for the variables suggested by ReSharper (if any) will be inserted by default when expanding the template.
|Use these buttons Move Up/Move Down buttons to move the variable rows up and down within the table. The order of variables specified in the table denotes the order in which ReSharper will switch between the corresponding input fields when the template is expanded.
Note The buttons are enabled only if there is at least one variable used in the template.
The true power of Live Templates is in the intelligent substitution of template variables with correct values after expanding the template. To take advantage of this feature you must specify the macros that will be used by ReSharper to suggest or even to guess value(s) for every template variable. To specify a macro for the chosen template variable, click Change macro... in the Value column of the Template variables list in the Edit Template dialog. The following dialog will appear allowing you to choose from several predefined macros:
Available macros are described in the table below. Entities in bold (e.g. format or variable) can be specified further from the Variables list table.
||Lets the user assume full responsibility for specifying the value of the template variable after expanding the template. No guessing will be attempted by ReSharper thus leaving the suggestions list empty.
A macro that expands to a static text provided by the template developer. When you choose this macro, a Constant value link appears in the Value column in the corresponding line of the table:
Clicking this link forces the following dialog to appear allowing you to type the constant value to be returned by the macro:
Cast to the required type (if the cast is necessary)
Inserts (if necessary) a cast to the type that is expected at the left side of an assignment operator.
||Evaluates to the name of the containing namespace.
|Containing type name
||Evaluates to the name of the innermost containing type.
|Containing type member name
||Evaluates to the name of the innermost containing type member.
|Current date in specified format
||Evaluates to the current date. When you choose this macro, a Choose format link appears in the column Value in the corresponding line of the table. You can specify the desired date format using a simple dialog window that appears after you click that link.
|Current file name
||Evaluates to the current file name.
|Current file name without extension
||Evaluates to the current file name but with the extension removed.
|Current line number
||Expands to the current line number. Perfect for debugging/diagnostic messages.
|Current project name
||Evaluates to the current project name.
|Current project output assembly name
||Evaluates to the output assembly name for the current project.
|Current solution name
||Evaluates to the current solution name.
|Current time in specified format
||Evaluates to the current time. When you choose this macro, a Choose format link appears in the column Value in the corresponding line of the table. You can specify the desired time format using the simple dialog window that appears after you click the link.
|Full name of the current user
||Evaluates to the full name of the current user.
||Evaluates to the default namespace for the current project.
|Default namespace for current file
||Evaluates to the default namespace for the current file.
|Guess element type if in array is expected at this point
||ReSharper will try to guess the element type of the array if one is expected at the input field position.
|Guess element type of collection represented by variable
||ReSharper will try to guess the element type of the collection that is substituted as a value of another template variable at the template expansion. When you choose this macro, a Choose value link appears in the column Value in the corresponding line of the table. Clicking on this link presents the following dialog which allows you to choose a template variable among the available ones:
|Guess value type of dictionary represented by variable
||Similar to Guess element type of collection represented by variable, but forces ReSharper to guess the value type of the referred dictionary.
|Guess key type of dictionary represented by variable
||Similar to the previous one, but instead of the element type the key type is determined.
|Short user name
||Evaluates to the current user name.
|Suggest an array variable
When expanding the template ReSharper tries to suggest a list of array variables visible in the context where the template is expanded.
|Suggest name for a variable
||When expanding the template ReSharper tries to suggest a reasonable unique name for a variable that is also consistent with the variable type and Code Style/Naming options.
|Suggest name for an index variable
||Similar to the previous macro, this informs ReSharper that the variable is to be used as an index variable so that it gets appropriate name suggestions.
Suggest variable of type
Forces ReSharper to come up with a list of variables of the desired type visible in the context where the template is expanded. When you choose this macro, a Choose type link appears in the column Value in the corresponding row of the table. Clicking this link presents the following dialog to appear which allows you to choose from the available types (or to enter a type name if you prefer):
|Value of another variable with the first character in lower case
||As suggested by its name, the macro expands to a value of another template variable but with the first character in lower case. You will need to choose the reference variable using the Choose Variable to Reference dialog.
|Value of another variable with the first character in upper case
||As suggested by its name, the macro expands to a value of another template variable but with the first character in the upper case. As with the previous macro, you need to choose the reference variable using the Choose Variable to Reference dialog.
If there is at least one red link in the Value column of the table, then you are not done yet with choosing your macros. Click on any red links to further specify their values.
Note It is not recommended to edit predefined templates that come with ReSharper, since all changes could be lost when upgrading ReSharper. Instead, make a copy of any predefined template you wish to modify and work with the copy.
Live Templates | 'Surround With' Templates | File Templates