Idea logo

The IntelliJ IDEA Blog

The Capable & Ergonomic Java IDE by JetBrains

Early Access Program

Custom code folding regions in IntelliJ IDEA 11.1

This is a cross-post from WebStorm & PhpStorm blog, originally posted by Rustam Vishnyakov.

This long-awaited feature gives you a possibility to define your own code-folding regions with special line comments.

Let me give a simple example. I start with the following code and select a code region I would like to fold:

I press Ctrl+Alt+T now to get “surround with” action options:

As you can see there are two custom folding items: “<editor-fold..>” for NetBeans-like style and “region..endregion” for VisualStudio style. If you don’t have any custom folding regions yet, you can choose either style. For example, NetBeans-like “<editor-fold..>”:

Now I can collapse the region just like any other code folding region:

The whole code gets hidden behind “My description”, the text I have specified as a value of “desc” attribute.”defaultstate” attribute is supported too but not added automatically.

For VisualStudio-like range comments IntelliJ IDEA adds “//” comment instead of “#” although “#range” is recognized too. In general the IDE uses typical language line comments for any kind of style.

Note that you can not mix up two styles in one file. The IDE will recognize only the first it can find and assume this is a style you have chosen for your code. I guess it’s needless to say that mixing custom region styles in one project is a bad practice. I would like also to stress that using custom folding regions is not a panacea from a badly structured code. Keep your code clean and readable even without any extra folding regions.

Try this feature in IntelliJ IDEA 11.1 EAP. Also I hope you get back to us with your findings and improvement suggestions which can be submitted at http://youtrack.jetbrains.com.

Comments below can no longer be edited.

27 Responses to Custom code folding regions in IntelliJ IDEA 11.1

  1. Avatar

    Stephen Friedrich says:

    March 7, 2012

    Now if only there’d be a way to specific the region as collapsed by default. Would be nice for example to hide all those stupid getters and setters in my dtos.

    Also the “Replace with block comment” intention should not be there for this kind of comment.

    • Avatar

      Reed Owens says:

      April 27, 2018

      You can specify a block to be collapsed by default.

      //

      I’ve tried it out in Webstorm and it works.

    • Avatar

      Michail Almyros says:

      May 17, 2018

      Have you ever heard of Lombok https://projectlombok.org/ it allows you to generate the getters and setters and they are not visible.

  2. Avatar

    Ingo says:

    March 7, 2012

    +1 for default collapsing

  3. Avatar

    alximik says:

    March 7, 2012

    really? in 2012? 🙁
    googled 2 years ago, found in netbeans but didn’t in greatest Jetbrains Idea 🙁

  4. Avatar

    jtonic says:

    March 7, 2012

    The IDEA hanging on defining a collapseable area with private annotated fields.

    =============
    idea.log (excerp)
    =============
    2012-03-07 20:54:38,297 [ 272889] ERROR – aemon.impl.PassExecutorService – null
    java.util.ConcurrentModificationException
    at com.intellij.openapi.editor.impl.IntervalTreeImpl$2.hasNext(IntervalTreeImpl.java:540)
    at com.intellij.openapi.editor.impl.IterationState$PushBackIterator.hasNext(IterationState.java:520)

  5. Avatar

    jtonic says:

    March 7, 2012

    I filed in youtrack.

    http://youtrack.jetbrains.com/issue/IDEA-82401.

    Also I noticed that pressing ENTER after typing the description introduce a new line and alter the collapseable region.

  6. Avatar

    jtonic says:

    March 7, 2012

    I’ve also filed another issue regarding this feature.

    http://youtrack.jetbrains.com/issue/IDEA-82402

  7. Avatar

    Jason says:

    March 8, 2012

    Since having to stop and reach for the mouse can get annoying, is this capable of autocomplete if I start typing?

  8. Avatar

    Eugene Toporov says:

    March 9, 2012

    Jason, which autocompletion do you have in mind?

    Thanks for the issues in the tracker, jtonic! That was just the right thing to do.

  9. Avatar

    Sebastien says:

    June 11, 2012

    The collapse/expand behavior not working when an enum is inside the block.

    IDEA-87312 Custom code folding: editor-fold; Folding/unfolding behavior not available when an enum inside the block

    public static enum STATUS {
    ACTIVE,
    STOPPED
    }

  10. Avatar

    yole says:

    June 11, 2012

    Sebastien,
    Please file a YouTrack issue for the problem.

  11. Avatar

    Sebastien says:

    June 19, 2012

    @Yole, I did and in my post.
    IDEA-87312

  12. Avatar

    Sebastien says:

    June 22, 2012

    here’s the link

    http://youtrack.jetbrains.com/issue/IDEA-87312

  13. Avatar

    Folder says:

    July 20, 2012

    “” doesn’t work in “*.properties” files

  14. Avatar

    user says:

    July 4, 2013

    doesn’t work in .sql files either

  15. Avatar

    couldBeBetter says:

    July 22, 2013

    yes, could be better.

    if one could define a keyword/keyword-pair (a lá region/endregion, , etc that could be used/enabled/disabled across files, independant of language, it would be great.

  16. Avatar

    Diego Cirujano says:

    March 14, 2014

    How could I set “collapsed by default” from “Settings”->”Editor”->”Code Folding” ? I just checked if all are selected it´s automatically collapsed but I don´t know which one is the one that makes it.
    Thanks
    (// from Netbeans is a great feature +1)

  17. Avatar

    Konstantin says:

    March 28, 2014

    Hi. Doesn’t work on AppCode (((

  18. Avatar

    Marcel Bradea says:

    April 21, 2014

    This feature is GREAT, except that the regions don’t show up within the code preview region – which pretty much all but defeats the purpose of this feature, which is to allow you to very quickly navigate to the portion of the class that you need to work with. Yes there is the keyboard shortcut that brings up the little dialog, but then that dialog shows ONLY the regions without any of the methods in it, again not giving you the context you need to know where you want to navigate.

    Could you guys please add this??

    Cheers!

    • Avatar

      Marcel Bradea says:

      April 21, 2014

      Sorry I meant the ‘Structure’ pane, not ‘Preview’.

  19. Avatar

    LB says:

    December 6, 2015

    Yet another hotkey combination to remember?
    Why didn’t they use the approach of Ecilpse, where there is a single combination that does it all, based on the context ?

  20. Avatar

    dluc says:

    January 23, 2017

    I tried the two annotations and double checked the preferences, unfortunately this doesn’t seem to work for Scala files

  21. Avatar

    mehdi says:

    April 27, 2018

    Have any custom folding regions for html.

Discover more