Code formatting: replacing with camelCase everywhere

I have a large java project and need to reformat all field names. They are currently written like this and need to be replaced with camel case:

 
protected SplendorAbility splendor_ability;


I could probably write a regexp for doing it but I'm not good at regexp.
Also I need to be careful because I'd like to keep underscores in ALL CAPS names (public final ones).

Is there a way apply this style via Intellij?
Thanks!

1 comment
Comment actions Permalink

There are inspections to locate such problems. But to the best of my knowledge, there is not a quick fix to fix the issue. You will manually need to rename them.

You can at least use the inspections to get a list of them all. Open the Settings (File > Settings) and go to Editor > Inspections. Copy an inspection profile then click the "Reset to empty" reset-to-empty--insoections.png button. Then in the "Naming Conventions" section of the list of inspections, select the desired inspections. Most likely you'll want "Instance field naming convention", "local variable naming convention", "method parameter naming convention" and possibly the "method naming convention." See if there are any others that you want. You may want to edit the specifications of each inspection, such as increasing the maxium length so it only finds snake case names and not ones that are > 20 characters. Once you have that set, close the settings and go to the menu and select Analyze > Inspect Code. Select the profile you just created and run it on the project. You will get a list of all the problems.

To make solving things quicker, I would expand all the results via the expand all expand-all.png button. then use the arrow key to cursor down to the first one. Type F4 to jump to the source. You can then immediately type Shift+F6 to launch the rename refactoring. After running the refactoring type F12 to return to the last tool window (the inspection results in this case). You could also (temporarily) map a shortcut to the inspection tool window in the keymap section of the settings.

The problem with using a regex to do this is that the Java regex syntax (to the best of my knowledge, and comfirmed by this StackOverflow post) does not support modifying the case of a capture group upon replacement. Since IDEA uses the Java Regex syntax, you cannot do it via the standard "Replace in Path" action. You would need to write a small program to travers the source code, use a regex to find the problems, and then use code change the first character of the second word to uppercase. The above StackOverflow post has some examples. (This could be a good exercise to help you learn Regexes :))

Finally, you can take a look at the Plug-in respository (http://plugins.jetbrains.com/) and see if someone has a plug-in that does this. The CamelCase plug-in does have the ability to switch between snake_case and camelCase. But it simnply changes the "word" at the current location. It is not a refactoring that will modify all the references. But there may be other plug-ins.

0

Please sign in to leave a comment.