I've recently joined an existing project that has about 200k lines of code already in it, with no consistent coding standard applied. We decided that we wanted to consistently apply an underscore prefix for names of instance variables in classes. A quick grep through the source code told me that there were about 12000 (yep - thats twelve thousand) cases that didn't conform to the standard. I didn't want to do all of them by hand. IntelliJ IDEA has a feature called "Structural Search and Replace", but that will leave me with broken code. I really want a "Structural Search and Refactor". That doesn't exist, so I figured I'd build one.
I used the "Stuctural Find" feature inside IntelliJ to find all instance fields of a class. The template for that exists already. I set the regular expression on the field name to be
[a-z][a-zA-Z0-9]* to make sure I didn't pick up things that already had the underscore, and ticked the box saying "This variable is target of the search". After running that, I get my 12000 or so results in the find results window, and I can use a hotkey to navigate back and forth between them. My cursor appears right on the variable I want to rename.
At this stage, I have a sequence of keypresses I can use to refactor each variable name,
|apple - alt - down||# go to next misnamed variable|
|shift - F6||# rename the variable|
|home||# go to the start of the variable name|
|_||# insert an underscore|
|enter||# complete the refactoring|
|apple - S||# save the file|
I tried recording an IntelliJ macro to record the events, but unfortunately the macros don't work across modal dialogs. The rename refactoring pops one up, so that didn't work. Instead, I downloaded an OS keyboard macro tool called Keyboard Maestro. I put that sequence of keys into it and had it repeat every second while IntelliJ was active. I left it running overnight, and when I got up in the morning I had 12000 renamed variables that all conformed to our coding standard. Excellent!