First off, an apology. It’s been an age since I last posted. No excuses, I’ve just been busy lazy.
So, I happened to be working with my colleague Josh Twist on a customer gig about Prism (that’s a different story) and I spotted during his demos that Josh had ReSharper installed. Now I’m a big fan of ReSharper, as both readers of this blog might have guessed and certainly my customers and colleagues know very well! Well, to cut a long story short I got him excited about some ReSharper shortcuts he hadn’t come across before, and hence he challenged me to blog about some more. So here goes. Before I forget, I’m using the ReSharper Visual Studio key bindings, not the IntelliJ ones.
This one saves a whole lot of typing, even with the introduction of the controversial var keyword, but it does take a little getting used to. Let’s say that I want to new up a StringBuilder. Instead of thinking type (or var), variable, equals, new StringBuilder, I just think new StringBuilder. Leave the cursor at the end of the line and the light bulb appears. Hit Alt+Enter to show the hint menu:
Select Introduce variable and ReSharper will complete the type and the declaration, first giving the the choice of type:
Press Tab and it’ll move on to giving me a choice of variable name:
And finally Tab once more and the statement is complete.
I use this one quite a bit when I navigating around an unfamiliar and large solution, and in particular when the code hasn’t kept to the best practice guideline of one class per file. Pressing Ctrl+T brings up the Find Type search box:
As I start typing type names it suggests types that match:
What’s really nice it is supports what ReSharper calls CamelHumps. Essentially this means you only need to type the capitalized letters of a type, which is really useful if, like me, you like quite descriptive type names:
By the way, there’s a similar version of this shortcut, Ctrl+Shift+T, that does the same thing but finds files instead:
Move Up & Down
This is one for those who are picky about code layout. After all, the compiler doesn’t care about where stuff appears in the code, but I do! Take the case of this Person class:
It’s bugging me that LastName appears in the code before FirstName; that’s just plain wrong. So I move my cursor to the property name LastName and press Ctrl+Alt+Shift+Down:
Sorted! Of course I could have moved my cursor to the property name FirstName and pressed Ctrl+Alt+Shift+Up, which would have achieved the same result.
What’s really nice is this key combination is context aware. So if you’re on a class name it’ll move the whole class up or down in the file, and if you’re on a statement block within a method it’ll move the whole statement block up or down too.
Navigate To Base Or Inheritor
This is another really useful set of shortcuts when navigating around unfamiliar code, and I use it a great deal when doing code reviews and trying to understand the flow of the code. Let’s say I’m looking at the following InvoiceProcessor class:
And I’m trying to work out what happens in the SubmitInvoices method when SendConfirmation gets called on an IEmailSender. Well, if I move the cursor to the call to SendConfirmation and press Alt+End, it’ll try and find an inheritor of IEmailSender that implements SendConfirmation:
In this case I’ve got two classes that implement IEmailSender, so it gives me a list. Selecting one of these classes takes me straight to the method. However, if there was only a single class, it would have skipped the list and taken me straight to the only implementing method. In this case I chose the SpecialEmailSender:
Now I’m here I see that SpecialEmailSender derives from EmailSender, not IEmailSender. My cursor is still over the SendConfirmation method name, so I press Alt+Home to take be to the base, and it takes me straight to EmailSender’s SendConfirmation method.
There’s also a slightly more generic navigation shortcut that’s useful too, and that’s Alt+` (yes, that’s a back quote character). This pops up a navigation menu based on the current context:
Well, that’s pretty much all my favourites covered for now. Any other ReSharper fans out there got any more?