ReSharper, day 18: Adding things with Alt+Enter

Welcome to day 18 of my 31 Days of ReSharper.

Yesterday, I talked about the red light bulb and how you can use it to invoke Change All. Today I’ll talk about a couple more of the quick fixes in the red-light-bulb menu. As before, whenever you see the red light bulb, you can drop down its menu by pressing Alt+Enter.

Context sensitivity

When you have an undefined (red) symbol, ReSharper will give you options to define it. It’s smart about which options to show:

  • Create property (which really should say “create read-only property”, since that’s what it does) is shown if the symbol starts with a capital letter.

  • There’s also Create get/set property, but that’s only shown if this particular usage is the target of an assignment. (It’s not cool enough to look at all the usages and see if any of them are assigned to. Update: they’ve gotten other requests for this, and are already planning to implement it in the next version of ReSharper.)

  • Create field and Create local variable are shown if the symbol starts with a lowercase letter.

  • Create method is shown (instead of property/field/variable) if the symbol is followed by an open parenthesis.

  • Related is Create set accessor, which is shown when you press Alt+Enter where the code is trying to set a read-only property. This one is actually based on a red squiggly, not a red symbol.

These are all smart, but not as eerily smart as Change All:

Type this, and… Create Local Variable
if (a) bool a;
ArrayList.Adapter(b) IList b;
c.PixelOffsetMode = PixelOffsetMode.Half; object c;

If Create Local Variable was as smart as Change All, that third line would generate Graphics c; instead of just plain object. (I suspect the difference is because Change All has to see which already-existing symbols match the usage, so it’s got to look at perhaps a few dozen variables and properties. Create Local Variable doesn’t have the advantage of an already-filtered starting point, so to do the same thing, it would have to look at every one of the tens or hundreds of thousands of classes in the .NET Framework, which might well be pretty slow. But it’d be pretty cool. Update: Support agrees. They’ve added a feature request, and said it would be in the next version of ReSharper!)

Smarts about the type

When it’s building the Alt+Enter menu, ReSharper doesn’t look at how a symbol is used. But once you select an option, it does.

If you just use a symbol one place, like this:

ReSharper will simply declare the variable as the exact type, Bitmap in this case. (It takes the cursor straight to the newly-declared variable, though, with a temporary Code Template; so if you want a different type, you can type it in.)

But if you also use the symbol in a place that expects a base type, like this (DrawImage expects Bitmap’s parent type, Image, for its first parameter):

then Create Variable will give you a choice of which type you want to use, and will default to the base type:

Options for properties

When you create a property, ReSharper again creates a temporary Code Template. You start out at the return type, which you can accept or change. Then when you hit Tab, you get a choice of how to generate the getter/setter code:

ReSharper after using Alt+Enter to create a property, showing the options to use either a backing field or the default body template

  • Backing field will do what its name suggests: it will declare a field with the appropriate type and name (following the Naming Conventions settings you’ve specified in ReSharper Options), return the field from the getter, and (if applicable) set it in the setter. If the field already exists, it won’t be added again.

  • Default body template will throw an exception in the getter and the setter.

Options for methods

When you use Alt+Enter to create a method, the temporary Code Template will let you specify the return type, the parameter types, and the parameter names. Nothing too surprising here.

What’s pretty cool, though, is that ReSharper’s variable name suggestions kick in. If the call site was passing a variable or property, ReSharper will default to using that same variable name for the parameter name; beyond that, it makes the usual suggestions based on the type name.

When you Tab to a parameter that has more than one suggested name, the dropdown completion box will appear. Of course, you can always type a name that isn’t in that list.

ReSharper after using Alt+Enter to create a method, showing the parameter name suggestions

You can even type a prefix (e.g., “first”) and then press Ctrl+Space to suggest the rest of the name. (Unfortunately, this won’t catch the variable name from the call site, it’ll only suggest based on the type name. For example, in the above code, if I typed first and pressed Ctrl+Space, I would get firstInt, not firstCount.)

Limitation: Getting back

When you use any of these commands, ReSharper puts the cursor inside the new declaration, so you can use the temporary Code Template to change the type or the code. But when it’s done, you have to figure out how to get back to where you were.

ReSharper has a Go to last edit location command, accessed via Ctrl+Shift+Backspace. It seems to be able to get you back where you started, some of the time at least. It won’t do anything the first time you hit it, but if you’re lucky, the second time, it’ll take you back to the formerly-red symbol. (But if you then use Alt+Enter to complete something else, and try to get back again, it doesn’t seem to work as expected.)

Limitation: Not naming-convention savvy

These features are not smart enough to follow the “Naming Convention” page in ReSharper Options. Given the semi-standard of fields starting with _, one would expect that symbols that already start with _ wouldn’t get the option to create a local variable; and one would further expect that symbols without the _ would either not get the “Create field” option, or would add the _ when you create a field. ReSharper doesn’t do any of this; Create Field and Create Local Variable are both completely oblivious to Naming Convention settings. (I’ve entered a feature request for this.)

Leave a Reply

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