Last week, I gave a presentation at the GSP Developers’ Guild’s monthly meeting where I talked about some of the cool (maybe hidden) things that ReSharper can do for you. In this article I’m going to summarize my talk and give links to where you can get the Custom Live Templates, and Extensions. I realize that this post is lessened by not having some of the visuals (no projector, etc).

 

ReSharper Intro

 

ReSharper can do some really cool things. 

 

ReSharper can bulk fix code style issues like variable names conventions.

 

 

 

Code completion is made easier and code simplification is highlighted.

 

It  can also help with if statement nesting or showing you extra code that can be reduced.

 

New to Resharper 9 is finding references on NuGet. I referenced a type from MomentSharp - a DateTime library that I wrote - but I don’t have it referenced in the project. I can use the light blub or Alt + Enter and “Find this type on nuget.org”.

 

Also new to ReSharper 9 is support for C# 6 features like string interpolation.

ReSharper can automatically import namespaces.

 

It can apply auto refactoring. I can move a block of code to the CommonStuff class. ReSharper will not only import the namespaces but it will change the references to the function in both classes. But since the code I moved is private, ReSharper tells us that the access will be broken by moving this. No problem; we’ll just Alt + Enter over the now broken code and change the access modifier.

 

Resharper can generate variables or even classes that doesn’t exist based on code.

 

We can add new properties by using prop and pressing tab.

 

I have an undefined type of Album and undefined object _albums. With the Alt + Enter or light bulb shortcuts ReSharper will help us create our model class and our list object.

 

We can easily move string values to a resource file, again with the Ctrl + Shift + R command.

 

ReSharper can be used to quickly create interface members.

 

We can even convert a class into an Interface. Press Ctrl + Shift + R on a class name and select “Refactor this.” From there we can extract class into an interface.

 

ReSharper can refactor method parameters into a class. Place the cursor on method name and use  Refactor This and Extract Class from Parameters.

Custom Live Templates:

I’m all about the shortest path when coding: How can I make the toolset do more for me so I can do more for me?

 

Here’s a couple of examples of live templates I’ve created. You can download these templates from here. To import these settings, save the file, open VS, ReSharper Menu, Manage Options, Import from File. You can also import from the "Raw" Gist.

 

AR - MVC Action Result

dbs - DBSet for CodeFirst Application DB Context e.g. DBSet<Type> Name { get; set; }

jreadys - jQuery Ready with a script tag

jready - must be in a script tag

fa - Font Awesome shortcut

glyphicon - Bootstrap Glyphicon shortcut

collapse - BS Collapse/Accordion

collapsepanel - BS Collapse Panel (meant to go inside above)

row - BS row div

col - BS column, includes col-sm-* and col-md-*

modal - BS modal window

bsip - BS input code block, includes form-group wrapper and an input tag with form-control

id - jquery id wrapper e.g. $("#IDGOESHERE")

 

You can create your own templates from highlighting code. Click the ReSharper Menu -> Tools -> Create Live Template form Selection. You can then setup your own variables as needed. Also take note of the Availability options. You can restrict your templates to be available only under certain conditions. Check out the download link for an examples.

 

Structural Search and Replace

With SSR, ReSharper allows you to find and replace entire blocks of code at a time. For instance, we can search for .Dispose method inside of a finally block instead of using a Using statement. We can highlight this code -> Right Click -> Search Pattern. 

 

You can store often used Patterns to your ReSharper options (ReSharper -> Options -> Code Inspection -> Custom Patterns).

 

You can download my exported SSR templates from this gits (same file as above)

 

Custom Code Inspection

 

You can also use these saved patterns to highlight coding errors based on custom business rules.

For instance, with our Disposable to Using example, we can go to the Code Inspection setting and mark this as a ReSharper hint or error even.

 

On the web side we can mark insecure resource loading as a bad practice. For example, loading Font Awesome over http is fine as long as page or site is loaded that way, but if you were using this in a ecomm solution or a site behind an SSL, this line would throw an error in the browser for mixed content.

 

You can download my exported Code Inspection templates from this gits (same file as above)

 

Value Tracking

 

ReSharper provides inspectors for Value Origin and Value Destination. We can use Ctrl + Shift + Alt + A to bring up “Inspect This”. From this window we can walk through the changes and see where and what happens to our value.

 

Extensions

Who doesn’t love Extensions? If you’re not satisfied with the amount of RAM VS takes up, here’s some can’t-live-without extensions to add to ReSharper (which is an extension).

 

Postfix Templates:

Update: As of Resharper 10 Postfix Templates is now built into Resharper and does not require the additional plugin.

 

Postfix templates adds a couple of extremely useful templates. We can create a variable in a sort of backwards way.

 

 

We can do a quick foreach on a list.

 

We can wrap a disposable in a using.

 

One of the really cool features is refactoring code to use a variable by copy and paste.

 

ZenSharp

 

 

This extension adds a lot of templates to creating properties, classes and the like.

pps

ppsAge

 

ReSpeller

Handy spell checker for your code and comments.

 

 

Presentation Assistant

While not required for day-in/day-out programming, this plugin will display keyboard commands in a small modal at the bottom of Visual Studio when turned on.

Sweet Shortcuts

Ctrl + T: Navigate to symbol

Ctrl + R, R: Rename all the things

Ctrl + Enter on Class Move To ClassName.cs

Ctrl + Shift + R: Extract To Variable

F12: Navigate to

Shift + F12 Find usage

Ctrl + R, M: Extract Method 

Alt + End: Go to interface implementors

Alt + Home: Go to base class

Alt + Page Up/Page Down go to Next/Previous warning

Ctrl + R, S - Change signature. Add new parameters, create an overload

Ctrl + E, U - Surround with (like try,catch)

Other Cool things

Resharper also provides a Stack Trace Viewer (Ctrl + E, Ctrl + T). You can copy a stack trace from Elmah or a YSOD into the stack trace view. 

 

What else should be included in this section? You tell me. Leave me a comment below with your favorite ReSharper magic trick. What makes the other programmers envious of your ReSharper know-how?