ReSharper 2016.1 Help

Creating a Live Template

Live templates represent code fragments that can be quickly inserted into your code. The code of the template can be a short expression, a complete construct, or even an entire class or method. Each live template is identified by a Shortcut - a short string of characters such as foreach - that you can type right in the editor to invoke the template. For more information, see Creating Source Code Using Live Templates.

Live templates can be used for a variety of things, from creating classes, DTO’s or skeletons for unit tests, among others. To illustrate a live template, let's say we like to create entities that usually have an Id field prefixed with the name of the class, along with some other properties. The type of Id is not necessarily known beforehand. It can be of type string, GUID, etc. for example:

public class Customer { public string CustomerId { get; private set; } public string Name { get; private set; } public string Email { get; private set; } }

The body of a live template consists of literal strings and parameters. The idea is to combine these to maximize efficiency when invoking the template.

To create a new live template

  1. On the main menu, choose ReSharper | Tools | Templates Explorer.
  2. In the Templates Explorer Window that appears, click the Live Templates tab.
  3. Optionally, if you want to create your template in a specific settings layer, choose the desired layer in the Layer drop-down list. To learn more, see Storage of Templates.
  4. Click New Template ThemedIcon.AddLiveTemplate.Screen.[Gray]. The newly created template is opened in the Template Editor.
  5. Specify template shortcut in the Shortcut field. It 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 our example, we specify be as a shortcut and Business entry as a description.
  6. To change the default scope of the template, 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):
  7. Create the body of the template by typing or pasting the desired code.
    In our example, the template body would look as follows:
    Live template body
    As you can see, there are 3 types of elements that have different highlighting:
    • text literal - anything written as a plain text in the template body will appear as is when we invoke the template.
    • predefined parameter - in our example, the $END$ parameter tells ReSharper where we want the cursor to be positioned once we have finished creating the template.
    • user-defined parameter - as soon as you type a parameter (prefixed and suffixed with '$'), it appears in the Parameters section to the right of the text area. Yuo can configure all defined parameters as described in the Declaring Template Parameters section.
      In our case, since we want to establish the classname during its invocation, we make the $className$$ a variable. The same occurs with the $idType since we do not know what type we want the Id to be. Finally comes the Id property. As we want it to resemble the classname suffixed with 'Id', we just use the corresponding parameter suffixed with 'Id'.

    The different highlighting of the $className$$ parameter (red and black frame) means that the red entry will be used as the initial input position when in the Hot Spot Session when the template is invoked.

  8. Configure template formatting options:
    • Select the Reformat check box to make ReSharper automatically reformats 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.
  9. To save the template, choose File | Save Selected Items on the Visual Studio menu or press Ctrl+S.

See Also

Last modified: 19 August 2016