Change modifier symbols in IntelliJ

Is there a way to automatically reorder 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;

      

Change it to:

private final static int x = 0;
private final static int y = 0;
private final static int z = 0;

      

+3


source to share


2 answers


Go to the "Settings" section and enable the editor | Inspections | Java | Style Code Problems | Checking disabled modifiers. It has a quick fix for sorting modifiers. This inspection is also part of the analysis | Code Cleanup ... so another solution would be to call this on your code.



+8


source


I was once looking for a similar need for formatting. Explored "reorder", "patterns (command + shift + M)", etc., but that didn't help. The tab Editor->Code Style->Java->Arrangement

has the option "Negotiation". It looks like a rule editor, but that didn't help either. As far as I understand, this is an ordering (sorting) of the members in the class, not a keyword ordering. It would be really cool to have this feature under the Reform / Reorder section.

Anyway, the only way I could think of is using Find | Replace with "regex", So in the example you provided, we can use find / replace (⌘R on OS X) with regex like:

To find: private final static|final private static|static final private



Replace: private static final

Not very smart, but helpful. We can use a combination of private / public if it needs to be applied to both private and public. Additionally, it can be applied at the package / path level. Sample screenshot below

Search Replace panel

+1


source







All Articles