Twitter LinkedIn Github

JetBrains

One of the cool features Resharper has is to allow you to define live code templates, i.e. snippets that you can interact with. Say for example you continuously type:

 

   1: var customerServices = new Mock<ICustomerServices>();

 

each time you want to mock out a service. You could shorten this to only typing:

 

mock<TAB>customerServices<TAB><ENTER>

and here's how:

1. Resharper -> Live Templates

2. Under User Templates, create a new one

3. Type in: var $name$ = var Mock<I$interface$>();

image

4. Give it the ShortCut name of "mock" (or whatever you please)

5. On the right-side of the Window, click on the Choose Macro on the name entry

image

6. Pick Constant Value and close the dialog

image

7. Click on the Constant Value again on the right-hand side

image

8. Enter a value, for example mockObject

image

9. Repeat the same process (Choose Macro) for the interface entry, but this type choose the following option:

image

10. Set the variable by clicking on another variable link:

image

11. Choose the variable name

image

12. Close the editor and you're done. Now watch how it works

 

Obviously this is just a simple example, but you can see how powerful Live Templates can be.