The body of a code template may contain:
- Plain text that represents source code constructs and comments. It is inserted without changes when the template is applied.
template parameters that are replaced dynamically when the template is applied.
A template parameter has the following format:
$PARAM_NAME$ - where PARAM_NAME is an arbitrary identifier.
When the format is correct, Templates Explorer highlights predefined parameters in dark blue and other parameters in dark red:
Predefined template parameters
The following template parameters are predefined in ReSharper:
- $END$ - the caret position after the template is applied.
- $SELECTION$ - the text selected by the user before invoking the template. This parameter is only used in surround templates.
- $SELSTART$ - the starting position of the text block that will be selected after the template is applied.
- $SELEND$ - the ending position of the text block that will be selected after the template is applied.
If neither $END$ nor $SELSTART$ and $SELEND$ parameters are used in a template, the caret position is set after the last symbol of the template. If you use $SELSTART$ and $SELEND$ parameter together with the $END$ parameter, the latter will be overridden. Before creating new custom templates, we recommend you to study the predefined templates to learn how to use parameters.
User-defined template parameters
All other parameters can be defined with arbitrary identifiers when you create or edit a template. Normally you will need to supply your parameters with macros for calculating their values when the template is applied.
Also, you can make each parameter editable so that the user can change its value when the template is applied.
If a code template has no editable parameters, it is applied instantly without user interactions; all non-editable parameters, if any, are calculated automatically with macros (e.g. by inserting a fixed value, the current namespace, type name, or even the clipboard content).
After the template code is inserted into your source code, you resume typing at the position specified by template developer with the $END$ or $SELEND$ parameters, but if you need to change some parts of the inserted code, you will be able to do so only by going back and editing it.
In contrast, editable parameters will expose the fields that may need to be changed and, often, reasonable choices will be suggested for them by the underlying macros. As you are getting acquainted with ReSharper, you will see that many of its predefined templates have editable parameters. You can also study Creating and Editing Templates section to learn how to create and edit custom templates with editable parameters.
For example, a well-designed template for iteration over all elements of a container will use editable parameters to allow you choosing:
- A suitable name for the loop index variable (for example, i or j).
- A list of all variables of the specified type in the current scope (for example, Arrays or Lists) as an expression for the iterated container.
- A suitable name for the assigned variable that will hold the current container element during the iteration.
- The type of elements in the iterated container.
When a template with editable parameters is applied, ReSharper deploys a Hot Spot Session in the editor. This session lets you specify or choose values for all parameters, step by step. You can either replace the parameters with your input or accept one of the suggested values.