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.

About Andrey Cheptsov

Andrey Cheptsov is IntelliJ IDEA product marketing manager at JetBrains. He's passionate about productivity, programming languages and tools.
This entry was posted in EAP Releases and tagged , , . Bookmark the permalink.

19 Responses to Custom code folding regions in IntelliJ IDEA 11.1

  1. Stephen Friedrich says:

    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.

  2. Ingo says:

    +1 for default collapsing

  3. alximik says:

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

  4. jtonic says:

    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. jtonic says:

    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. jtonic says:

    I’ve also filed another issue regarding this feature.

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

  7. Jason says:

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

  8. 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. Sebastien says:

    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. yole says:

    Sebastien,
    Please file a YouTrack issue for the problem.

  11. Sebastien says:

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

  12. Folder says:

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

  13. user says:

    doesn’t work in .sql files either

  14. couldBeBetter says:

    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.

  15. Diego Cirujano says:

    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)

  16. Konstantin says:

    Hi. Doesn’t work on AppCode (((

  17. 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!

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 class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">