Is there any way to automatically rearrange modifier keywords in IntelliJ?
For example, if I have the following code:
private final static int x = 0;
final private static int y = 0;
static final private int z = 0;
Rearrange it to:
private final static int x = 0;
private final static int y = 0;
private final static int z = 0;
Go to Settings and enable the Editor | Inspections | Java | Code style issues | Missorted modifiers inspection. It has a quick fix to sort the modifiers. This inspection is also part of Analyze | Code Cleanup..., so another solution would be to invoke that on your code.
I had once looked up a similar formatting need. Explored "rearrange", "templates(command+shift+M)" etc, but this did not help. There is an "Arrangement" option under Editor->Code Style->Java->Arrangement
tab. It looks like a rule editor, but that also did not help. To my understanding it is for ordering(sorting) members with in a class rather than ordering key words. It would have been really robust to have this feature under Reformat/Rearrange.
Anyway, only way I could come up with was to use Find|Replace with "regular expressions" So, in the example you have given, we can use find/replace(⌘R in OS X) with regular expression as:
Find : private final static|final private static|static final private
Replace: private static final
Not very smart, but useful. We can use combination of private/public if this needs to be applied to both private and public. Also, it can be applied at package/path level. Example screenshot below
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With