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.

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.
You can specify a block to be collapsed by default.
//
I’ve tried it out in Webstorm and it works.
Have you ever heard of Lombok https://projectlombok.org/ it allows you to generate the getters and setters and they are not visible.
+1 for default collapsing
really? in 2012?

googled 2 years ago, found in netbeans but didn’t in greatest Jetbrains Idea
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)
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.
I’ve also filed another issue regarding this feature.
http://youtrack.jetbrains.com/issue/IDEA-82402
Since having to stop and reach for the mouse can get annoying, is this capable of autocomplete if I start typing?
Jason, which autocompletion do you have in mind?
Thanks for the issues in the tracker, jtonic! That was just the right thing to do.
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
}
Sebastien,
Please file a YouTrack issue for the problem.
@Yole, I did and in my post.
IDEA-87312
here’s the link
http://youtrack.jetbrains.com/issue/IDEA-87312
“” doesn’t work in “*.properties” files
doesn’t work in .sql files either
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.
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)
Hi. Doesn’t work on AppCode (((
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!
Sorry I meant the ‘Structure’ pane, not ‘Preview’.
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 ?
I tried the two annotations and double checked the preferences, unfortunately this doesn’t seem to work for Scala files
In Scala, you have to put a blank line after the line.
http://stackoverflow.com/questions/37292028/how-to-custom-code-folding-for-scala-in-intellij-idea
Part of my comment got munched. In Scala, you have to put a blank line after the opening editor-fold line.
Thanks Rob!
Do you happen to know if there’s a workaround to get
defaultstate="collapsed"
working?Have any custom folding regions for html.