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!
Comments
I'm pretty sure you could've used Structural Search & Replace for this.
Posted by: Keith Lea | October 27, 2006 02:45 PM
SSR would've renamed the variables where they were declared, but it wouldn't have renamed usages of those variables in other places. I needed the refactoring behaviour for that.
Posted by: Marty Andrews | October 27, 2006 02:47 PM
http://www.jetbrains.net/jira/browse/IDEA-12246
Posted by: Maxim Mossienko | April 6, 2007 12:21 AM
Hi. This is really interesting post. Thank You! I have just subscribed to Your rss!
Best regards
Posted by: Forexman | May 25, 2008 11:06 PM
Hi. This is really interesting post. Thank You! I have just subscribed to Your rss!
Best regards
Posted by: Forexman | May 26, 2008 08:36 PM
Hi. This is really interesting post. Thank You! I have just subscribed to Your rss!
Best regards
Posted by: Forexman | May 27, 2008 11:55 PM