ReSharper, day 17: Change All with Alt+Enter (and, Introducing the red light bulb)

Welcome to day 17 of my 31 Days of ReSharper.

The next few days will continue to cover Alt+Enter. Today I’ll cover the use of Alt+Enter > Change All.

Red light bulbs

I’ll start by noting that yesterday’s feature, Alt+Enter to import namespaces, is atypical, in that it works no matter where the cursor is. Namespaces are the only place Alt+Enter works like that. It’s usually very positional: it only operates on the code at the current cursor position. That will be true of all of the Alt+Enter features I cover from here on in.

When there’s a problem (red, gray, or squiggly) that ReSharper knows how to fix, and you put the cursor on the problem code, ReSharper will show a red light bulb next to that line:

ReSharper's red light bulb, showing a problem you can fix with Alt+Enter

The red lightbulb indicates something that you should fix. There are also yellow lightbulbs, which mean something that you can do, but that’s totally at your discretion; I’ll cover some of these in future posts.

You can either click the light bulb, or press Alt+Enter. Either way, you’ll get a dropdown menu giving you options on how to fix the error.

ReSharper's Alt+Enter dropdown menu, showing options such as Change All to fix a compiler error

Update: The commands in this dropdown menu are referred to as “quick fixes”. (I should’ve mentioned that when I first posted this article.)

About Change All

One of the quick fixes for a red symbol is Change All. It does what its name suggests: it changes all occurrences of the (red) symbol to reference another (already defined and non-red) symbol. This one is handy if, for example, you cut some code from one method and paste it into another, where the variable or parameter names are different.

There are two parts to Change All: the suggestion list, and the sync editing.

The suggestion list: mind-reading at work

The suggestion list from ReSharper's Change All command

When you select Change All, you’ll get a suggestion list. Often there’s only one suggestion, and it’s exactly the one I want. It’s so good it’s spooky.

Now that I’m writing about it, and pushing the boundaries to find out exactly how it works, I’m even more impressed. It’s basically Ctrl+Shift+Space turned inside-out.

What happens is this: it looks at all the places where the symbol appears in red (ignoring any other methods that have same-named symbols that aren’t red), figures out how you’re using it in each place, and suggests symbols that will fit those usages without errors.

So if the symbol appears as the condition in an if statement, ReSharper will only suggest variables of type bool. If the symbol appears with .PixelOffsetMode = after it, ReSharper will only suggest variables of type Graphics (unless you’ve defined another type that has a PixelOffsetMode property).

In a lot of cases, you’ll end up with just one parameter, or just one property, that satisfies the way you’re using that symbol. Which makes it look like ReSharper is reading your mind. (It pretty much is!)

Worth noting: the Change All suggestion list is targeted to variable-like things (variables, parameters, and properties). You can use Change All for method names or type names, but you won’t get those suggestions in the Change All list.

SyncEdit comes to Visual Studio

Here’s kind of a nice touch: instead of popping up a dialog box prompting for the new name, Change All lets you type the new name directly into the editor in one place, and it changes the others as you type.

The ReSharper docs don’t have a name for this rename-it-right-there-in-the-edit-window feature, but it’s almost exactly like the SyncEdit feature from Delphi 2005, so that’s what I call it. It’s nice to have this feature available in Visual Studio.

Leave a Reply

Your email address will not be published. Required fields are marked *