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 share it later by copying to a team-shared settings layer or by exporting it into a file. It might be also useful to learn to study the way ReSharper stores templates.
- On the main menu, choose ReSharper | Templates Explorer.
- In the Templates Explorer window that appears, click one of the tabs to choose the type of the template: Live Templates, Surround Templates, or File Templates.
- Optionally, choose a particular scope for your template in the left part of the Templates Explorer. You will be able to change it later, if necessary.
- Optionally, if you want to create your template in a particular settings layer, choose the desired layer in the Layer drop-down list. To learn more, see Storage of Templates.
. The newly created template is opened in the
It opens in a new document tab so that you can switch between the template and the application
code 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. The shortcut is used to quickly invoke the template when creating code from template; it also appears in completion lists. In the Description field, type optional description for the template.
- For a surround template, specify a name in the Description field.
- For a file template, specify a name in the Description field. Optionally, in the Default file name specify a name that ReSharper suggests when applying the template, and tick the File name must be a valid identifier check box to make ReSharper check if the file name specified when applying the template is valid as a type identifier. This option is helpful if your template creates a file for a new type and the type name is initialized from the provided file name with the Current file name without extension macro. You can check the predefined file templates to see how it is implemented.
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 applying your custom code templates.
Template Scopes, click the
Availability hyperlink. In the
Select scopes dialog box that 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 versions):
- In the text area of the Template Editor, enter the template source code.
- The template code can contain plain text and parameters. As soon as you type a parameter (for example, $PARAM$), it appears in the Parameters section to the right of the text area. Configure all defined parameters as described in the Declaring Template Parameters section.
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 will not insert any namespace import directives.
- To save the template, choose File | Save Selected Items on the Visual Studio menu or press Ctrl+S.