Arrange Your Code Automatically with IntelliJ IDEA 12

You probably might have noticed that code always tends to be organized in a particular way. For instance, fields always go before methods, static fields go before instance fields and so forth. This is the easiest way to read your code.

Previously IntelliJ IDEA did not provide any native support for arranging code automatically. There was a third-party Rearranger plugin for Java language. Now we plan to bundle it into the IDE with native support for Java and other programming languages.

How to:

You can use either of the following ways to rearrange your code:

1. Call Rearrange code action explicitly.

2. Enable Rearrange entries in Code Reformat options.

Settings:

Arrangement settings are available now in Code Style settings on a separate tab.

Features:

  • Allows you to keep getters and setters together
  • Allows you to group overridden methods by class and interface. For instance, your class implements MouseListener and KeyListener, so you want to keep your methods grouped by the interface where they are declared.
  • Keeps dependent methods together. For instance, service() method calls util() method, and you want to keep util() after service().
  • Defines elements order as a list of rules where every rules has a set of matchers like modifier or type. For example, a rule can be like type field and modifiers public, static and final.

Coming soon:

  • Better user interface in Arrangement settings.
  • Order elements alphabetically.
  • Name conditions like type field with name like ‘*weight.
  • Arrangement for ActionScript and XML.

The new feature is already available in the latest IntelliJ IDEA 12 EAP. Please feel free to share your feedback and submit bug reports in our issue tracker.

Keep you code arranged with pleasure!

This entry was posted in New Features and tagged . Bookmark the permalink.

26 Responses to Arrange Your Code Automatically with IntelliJ IDEA 12

  1. Tropper says:

    Nice, I’m using IDEA since version 8 now. But today is the first time I see the Enter action or operation dialog. Amazing :-)

  2. Tomek N. says:

    What do you think about arranging methods by their dependencies, i.e. calling method is always after the caller? This way the code can always be read top-to-bottom.

  3. Andrey Cheptsov says:

    @Tomek. As far as I understand, it sounds like the third feature described in the post.

  4. harryhoppel says:

    Niiiiiice! If I may humbly request a feature: I want to automatically move all Spring setters with @Required annotation to the bottom of the file. Will that be possible?

  5. Gilles Philippart says:

    Brillant. Now If you could include pre-configured well-known sensible defaults : “Uncle Bob” style, “Java Standard Conventions” style …

  6. John Hurst says:

    Standard defaults sounds good.

    Perhaps there is a JetBrains standard? :-) I would be interested in seeing it!

    John Hurst

  7. Denis.Zhdanov says:

    @harry:I think it’s worth to provide java- specific annotation-based rules. Can I ask you to file a corresponding feature request to the IJ tracker?

  8. Denis.Zhdanov says:

    @Gilles: good idea! Please submit corresponding request to the tracker. It would be cool if you provide links to the corresponding standards there.

  9. Pete says:

    It might already be there but the ability to sort methods by the class they override or interface they implement.

  10. jkv says:

    Shouldn’t it be in commit dialog as well?

  11. Erik Pragt says:

    Calling rearrange from ‘Find action’ doesn’t work for me, IntelliJ throws a NPE. I’ve submitted the bug.

  12. Wes says:

    Would something like a new line enforcement between fields be possible? Like allowing private ones to have a blank line in between public and for protected?

  13. Ivan says:

    Please add ability to move methods variables into appropriate “sections”. E.g. like in “Rearranger” plugin in plugin repository.

  14. Denis.Zhdanov says:

    Hi guys,

    FYI: the tickets mentioned below target the requested functionality. They are expected to be fixed prior to the v.12 release:
    http://youtrack.jetbrains.com/issue/IDEA-93476
    http://youtrack.jetbrains.com/issue/IDEA-94316
    http://youtrack.jetbrains.com/issue/IDEA-94317

  15. Alex says:

    nice feature, but not for me. It will distort legacy codebase. Maybe it will be applicable for the new code. But automatically rearrange is something absolutely stupid :)

  16. Daniela Waranie says:

    I will upgrade from IDEA 11 to IDEA 12 when all features of this article are available to PHP developers.

  17. Maxim Mossienko says:

    @Daniela Please vote rearranging in PHP for http://youtrack.jetbrains.com/issue/WI-13012

  18. Wojtek Erbetowski says:

    Great feature, we really missed this one when moving from Eclipse.
    Thanks!

  19. kjordan says:

    Can a similar thing to the “Keep dependent methods together” be added for field ordering? i.e. if one field uses another field as an argument to its constructor that that one is kept above it. I believe the old rearranger plugin did this and it’d be nice to avoid null pointer exceptions but keep them sort of sorted.

  20. Denis.Zhdanov says:

    Good spot! Created corresponding ticket – http://youtrack.jetbrains.com/issue/IDEA-99056

    Denis

  21. Roedy Green says:

    You can’t just reorder variables willy nilly if there are initialisation expressions. For safety the expressions would have to be moved to [static] init blocks where the order can be preserved.

    Teams kill each other over such conventions so it is useful to let people safely interconvert between a personal style and a check-in corporate style without introducing any initialisation order problems.

    I spent a fair bit of time fiddling my old rearranger so that it naturally sorted initialisations to work in the right order.

  22. mark says:

    Pity it doesn’t seem to work for an entire module or an entire project.

  23. Shervin says:

    I am trying to rearrange enum to be sorted alphabetically.
    In the Arrange settings, I have chosen it to order by name.
    However, it still doesnt arrange anything when I Rearrange the code.

    Bug? Or am I doing something wronge?

  24. Michel Werren says:

    A further option for getter / setters placement would be nice
    like this:

    1 method public
    2 method package private
    3 method protected
    4 method private
    5 method getter / setter

  25. Luka Obradovic says:

    Rearrange features is great, but I can’t rearrange these:
    final static private int NODE_COUNT_CHUNK_SIZE = 4096;
    final static private int PAIR_COUNT_CHUNK_SIZE = 65536;
    final static private int CHAR_COUNT_CHUNK_SIZE = 1024 * 512;
    final static private int FLOAT_COUNT_CHUNK_SIZE = 1024;
    final static private int DOUBLE_COUNT_CHUNK_SIZE = 1024;

    Although I have “Matching rules” set for fields to be:
    [field] [private] [static] [final]

    it does not get rearranged :(

    Can you please help.

    Thanks,
    Luka

  26. Denis.Zhdanov says:

    Hi Luka,

    The rearranger works at class level, i.e. replaces fields, methods, nested classes and interfaces etc. However, it doesn’t change, say, modifiers order for the same field.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>