Surrounding Code Fragments with Templates
Surround templates are used to quickly enclose an expression,
a single statement, or an arbitrary block of code with
or other code constructs.
As the template expands, ReSharper intelligently reformats the code,
adjusts the selection, and/or repositions the caret in the end so that you could continue typing from a
To apply a surround template
- In the editor, select a block of code that you want to surround with a template.
- The surround action indicator appears automatically to the left of the selected code block.
- Press Alt+Enter or click on the indicator and choose the desired surround template in the action list:
- Alternatively, choose , on the main menu or press Ctrl+Alt+J.
- In the Surround With menu, choose one of the predefined templates in the quick access list. If you want to use a custom surround template that was not added to the quick access list, select More.
- If you have chosen More, the Choose Template dialog box appears, where you can select a required template. To optionally introduce a template into the quick access list, select its entry in the Choose Template dialog box and tick the Add to quicklist check box in the lower part of the dialog box before clicking OK.
If the template has
(i.e. requires user input), ReSharper deploys a
Hot Spot Session in the editor and sets the input position at the first parameter.
Then you can do the following:
- If ReSharper suggests some values for the current parameter, use Up and Down arrow keys to navigate through the list of suggested values, or just type in a desired value.
- Press Tab or Enter to accept the value and move to the input position of the next parameter. If this is the last parameter, the hot spot session completes and the caret moves to the end position defined for the session.
- Press Shift+Tab to move the input focus to the input position of the previous parameter.
- Press Esc to exit the hot spot session. In this case, all session parameters will be initialized with default values.
Consider the following code fragment:
We would like to enclose the statement in a
block to take care of possible
ReSharper conveniently offers the corresponding predefined template to streamline this operation. Pressing
after selecting this code block
opens a drop-down list with available templates:
To choose the try..catch template, you can do one of the following:
- Click it with the mouse.
- Go down to the corresponding menu item using arrow keys and press Enter.
- Press 8.
We supply the desired exception type (
IOException) at the first parameter and press
to move on to the next parameter.
At the input position of the second parameter,
we change the name of the exception variable suggested by ReSharper to
ioExc, and press
again to complete the hot spot session.
The caret and selection are moved to the positions specified by the template designer.
The code in the editor now looks as follows:
Thus, if the basic exception handling code provided by ReSharper does not suit your needs, simply type your own catch block body in place of the suggested code.
This feature is supported in the following languages/technologies:
The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.