Welcome to day 30 of my 31 Days of ReSharper.
Today I’m going to talk about that mother of all refactorings, Rename, available via F2. It is, of course, also available through the Ctrl+Shift+R menu.
Now, there’s only so much you can say about a Rename refactoring. Generally, either it works or it doesn’t; there’s a limit to how cool it can be. But ReSharper does manage to pull off a few nice tricks.
Note: Due to a combination of technical difficulties, a lack of planning, and a couple of big snowstorms, I’m currently unable to use ReSharper at home. So today’s post (and perhaps tomorrow’s too) will not have screenshots, and the details will be from memory. Please correct me if I get anything wrong. I’ll try to come back later and clean these up.
Renaming local variables: SyncEdit and suggestions
When you rename a local variable, ReSharper uses SyncEdit (my name for it, not theirs) to let you make the change right there in the editor. It also pops up its list of suggested variable names, so you can select a name from the list.
I’m used to Delphi, where I have to press Esc to end SyncEdit mode and apply my changes. Turns out this is poor preparation for ReSharper, because when you hit Esc in ReSharper’s SyncEdit, it reverts your changes. In ReSharper, you have to hit Enter to apply the change. (Yes, it would’ve made more sense if Delphi did it that way too.)
Now, using Enter to save your changes does make sense, but it has a downside: if the variable-name list is showing, Enter will select the current item in that list. So if you want to name your exception object, say,
ex, and you type
ex, pressing Enter will not rename your variable to
ex — it will rename it to
exception, because that’s the first variable-name suggestion that starts with
Here’s the workaround for entering shorter variable names: if you press Esc once, it will dismiss the variable-name suggestions, but will not end SyncEdit. (You need to press Esc again, with the suggestion list already gone, before it will revert your rename.) So you need to press Esc to get rid of the suggestions, and then Enter to apply the rename.
Renaming classes: Viral rename
I have long wished for a viral rename refactoring — one that infects all the code around it. For example, if I had a class called
SongFile, chances are I would have a lot of variables called
file, a lot of properties called
SongFile, etc. I want a rename that, when I rename
SongDataStore, infects all those variable names and property names, and renames them too.
ReSharper has it.
Well, mostly, anyway. When you rename a class, you get a wizard, and the first page asks you for the new name; nothing too exciting there. But then it looks for variables and properties that (1) match the type you’re renaming, and (2) have names similar to part of the name that you’re changing. And it will show a second page listing them, and suggesting new names for them all. You can uncheck any that you don’t want to rename, but why would you want to do that?
The only downside is that it doesn’t do the same for descendant classes. But I’ve filed a feature request, so we’ll see…