There are plenty of predefined templates included in the ReSharper installation, and they cover main code constructs for different languages. However, you may want to use custom templates for your particular development needs. ReSharper provides Templates Explorer and Template Editor that allow you to quickly create new custom templates.
Your new custom templates can be readily available to other developers. Depending on your needs, you can create the new code template in different settings layers including team-shared layers. If necessary, you can later share it by copying to a team-shared settings layer or by exporting it into a file.
- On the main menu, choose ReSharper | Templates Explorer or press Alt+R+P.
- In the Templates Explorer window that appears, click one of three tabs corresponding to the required type of template: Live Templates, Surround Templates, or File Templates.
- Optionally, if you want to create your template in a particular settings layer, choose the desired layer in the Layer drop-down list. For more information on settings layers, see Managing and Sharing Options.
Click New Template .
The newly created template is opened in the
The Editor opens in a separate Visual Studio code pane and
allows you switching between your code and your template to quickly test the created template:
Identify your new template in one of the following ways:
- For a live template, specify a shortcut (abbreviation) in the Shortcut field. A template shortcut is used to quickly invoke the corresponding template from the code editor when you create source code using live template. In the Description field, type optional description for the template.
- For a surround template, specify the name of a new template in the Description field.
- For a file template, fill in two text fields: Description (name of template) and Default file name (the file name that ReSharper suggests by default when a user invokes this particular template). Optionally, select File name must be a valid identifier to make ReSharper validate file name when you apply the template.
ReSharper does not save each code template in a separate file. Therefore, their identifiers displayed to the user (Shortcut for a live template and Description for a surround template or a file template) do not have to be unique. However, try to provide comprehensible identifiers so as not to be confused when you apply the created code templates.
To specify Template Scopes, click the Available hyperlink.
When the Select scopes dialog box appears,
use check boxes to define where the new template can be applied.
You can expand some scopes to specify details (e.g. file masks or language version):
- In the text area of the Template Editor, enter the template source code that may contain plain text and variables. As soon as you type a variable (for example, $VAR$), it appears in the list to the right of the text area. See Template Variables for details.
- Configure all user-defined variables in your template as described in the Declaring Variables section. A well-designed set of variables is essential for Parameterized Templates.
Configure template formatting options:
- Select the Reformat check box to make ReSharper automatically reformat the expanded code fragment according to your code style settings.
- Select the Shorten qualified references check box to make ReSharper automatically insert namespace import directives or remove qualifiers that become redundant after the template is applied. If this check box is not selected, ReSharper won't insert any namespace import directives at all.
- To save the created template, choose File | Save Selected Items on the Visual Studio menu or press Ctrl+S.