Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rearrange modifier keywords in IntelliJ

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;
like image 275
traveh Avatar asked Jul 02 '15 08:07

traveh


2 Answers

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.

like image 115
Bas Leijdekkers Avatar answered Oct 10 '22 00:10

Bas Leijdekkers


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

Search Replace panel

like image 2
ring bearer Avatar answered Oct 10 '22 02:10

ring bearer