Resharper Templates – supercharge your coding

Resharper_AltInsertResharper can help with a lot of repetitive tasks which can take a bit of time everyday. For example, the templates provided by Resharper are quickly available by using Alt+Insert when a project/folder/class are selected in the Solution Explorer, an example shown on the right. This will give you options as per the item selected on the solution explorer. In the example given here, the entries for “MS Test class” and “NUnit Test class” are custom file templates, which will not be available out of the box. Their creation is explained below.

The options provided here are the Resharper templates which can be edited by selecting the Resharper –> Templates Explorer… menu. This gives the Templates Explorer screen where the Live, Surround and File Templates can be selected, as shown below. The New/Edit Template Icon can be used to create/edit the respective type of template.

Resharper_FileTemplates

To add standard repeated items like copyright to a class/enum, click on File Templates sub-tab in the Templates Explorer tab, select C# in the Scopes column on the left, and double click the Class/Enum item, which should be at the top of the ‘In quicklist’ items on the right.

This should open up the edit template tab where anything that’s needed for all class definitions can be added. So if the copyright is added as shown, it will come up for each class created and would replace the class name where the $CLASS$ item has been added.

Resharper_EditTemplate

Resharper_AddMacro

The $ enclosed words are parsed as macros by Resharper and can be selected by clicking the link for the respective macro shown on the right hand side of the edit template screen under Parameters sub-section, as in the picture above. Clicking on the link brings up the “Choose Macro” dialog box shown to the right.

The above process will have to be carried out for any other file templates e.g. enum to get the same copyright etc.

Live templates are templates available in the files and are used by typing their shortcut and pressing the tab key. This will print out the template with ‘hotspot’ (the place to type code) at the first of the macro location. The other locations can be accessed using the tab key.

To create a new live template, like for NUnit test class, select Live Templates tab in the Templates Explorer and click on the icon for New Template shown before. Here the Class template can be copied over and alterations made as needed for a NUnit test class. The macros will have to be selected under the Parameters sub-section above. The resulting dialog ‘Choose Macro’ dialog box is shown.

The $END$ is a Resharper macro where the cursor is placed after all the editable parts of the template are filled one by one by using the Tab key. This allows continuing the flow of typing while filling in a template.

Resharper_AddTemplate

Creating a template as explained above will create a new template as named under the ‘Not in quicklist’ section on the right when Templates Explorer is opened and C# selected. To show this template in the quicklist drag the template into the ‘In quicklist’ section. This will show the template when Alt+Insert is clicked after selecting an artefact in Solution Explorer.

Resharper_ImportExport

Sharing the templates: To share the templates, select Resharper –> Manage Options… and click on Import/Export Settings –> ‘Export to File’ option highlighted on the right.

Then the relevant settings to export can be highlighted, select OK and provide an appropriate name for the file to share.

To import the settings select the ‘Import from File’ option from Resharper –> Manage Options… –> Import/Export Settings and choose the file required.

This will bring the appropriate templates in the C# section but only in ‘Not in quicklist’ section in the Templates Explorer. To see them on pressing Alt+Enter at the right location, drag the template into the ‘In quicklist’ section from Resharper –> Templates Explorer –> File Templates –> C#.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s