Using Live Templates

Live templates are predefined code fragments that you can quickly insert into your source code. The code structure is laid out automatically and all that's left is to fill in the input positions (if any) specified by the template designer. The code of the template can be a short expression, a complete construct such as a for loop, or even an entire class or method.

Using Live Templates

There are several ways to invoke a Live Template.

Abbreviation and Expansion

Each template is identified by a unique abbreviation - a short string of characters such as itar (short for "Iterate an array"), thr (for throw new) and so on. Abbreviations work like shortcuts: you type the template abbreviation in the code and press Tab, the template invocation key, to expand it into full template code. Example:

  1. Type psvm where you want to place a Main function.

     

  2. Press Tab to expand the template.

     

Note   Templates use the indentation set by your code style settings (for help on style settings, refer to Code Style/Code Formatting).

Template Completion

Alternatively, you can bring up a list of available Live Templates by pressing Ctrl + J. A menu will be displayed with short descriptions for each template. Choose a template from the list to insert and expand it in the code. Example:

  1. Press Ctrl + J anywhere in the code.

     

  2. Select a template from the list and press Tab or Enter.

Note   Redundant references can be shortened automatically after template code is expanded. For details on this feature, refer to Editing Code Templates.

If you remember only the beginning of the abbreviation you want to use, type some of its letters and press Ctrl + J. The menu will be filtered to show all templates with suitable abbreviations. You can continue typing to narrow down the list.

For other template examples, please refer to Predefined Live Templates.

Menu

  1. In ReSharper menu, select Code | Insert Live Template.
  2. Select the Live Template you want to use.

Parametrized Templates

Simple templates described above are expanded into predefined text independently of the context in which they are used. After the template code is inserted into your code, you resume typing at the position specified by template designer. However, the most powerful feature of Live Templates lies in their ability to adapt to surrounding context.

When invoked and expanded, parametrized templates suggest reasonable choices at the input positions of the template parameters. For example, a cleverly designed parametrized template to perform an iteration over all elements of a container will suggest:

When a template is expanded (directly after the invocation key Tab is pressed), the code editor temporarily switches into the template editing mode. The template variables either can be replaced with your input or can be calculated by ReSharper based on the input or the surrounding context. In template editing mode, each template variable corresponds to an input position in the editor. You can move the input focus to the next input position in the expanded template using Tab or Enter . Pressing Shift + Tab moves the input focus to the previous input position. The Escape key switches the template editing mode off and moves the caret to the end position (that is specified by the template developer).

Parametrized templates are best learned by studying how Predefined Live Templates are used. One such example is given below.

Example

Assume you have the following code fragment in your source:

Fig.1

In Fig.1 above, itar is the abbreviation for a template that contains code for iterating over an array. When you press Tab (the template invocation key), the template expands replacing the "itar" abbreviation with template code, with suggested values for the template's "variables" already inserted, as in Fig.2:

Fig.2

Remember, the editor is now in its special template editing mode. You can see that by the presence of the red border around the index variable, which is the first input position in this template. Note that ReSharper automatically suggested i as the name for the index variable. If you want to change the suggested value, just type a new name. All references to the variable within the expanded template will be automatically changed as you type the new name. In Fig.3 below, i has been changed to idx:

Fig.3

Regardless of whether you have changed the suggestion at the current input position, pressing Tab or Enter moves the caret to the next input position defined by the template:

Fig.4

As you can see in Fig.4 above, ReSharper automatically detected all array variables in the scope, and suggested selecting one of them (namely, the last declared one). Had there been only one array element in the scope, ReSharper would have inserted its name automatically (without popping up a list).

Tip   If the list of suggestions is too long, you may want to enter the desired name manually. See Working with Suggestion Lists below.

Fig.5

ReSharper detects the changes of the type of the array elements. It automatically updates the type of the variable holding the current array element, and suggests a suitable name for it.

After filling up all input positions, press Tab or Enter to get the caret moved to the specified end position of the template:

Fig.6

In many cases, the default values suggested by ReSharper may be accepted, thus you can immediately proceed to writing the loop body after very few keystrokes.

Working with Suggestion Lists

The desired element name can be entered manually when the input position is highlighted and the list of suggestions is open. In this case, the suggestions list will be reduced with each entered character so that only matching element names will remain:

Fig.7

See Also

Predefined Live Templates | Managing and Sharing Live Templates