Custom code folding regions in IntelliJ IDEA 11.1
March 7th, 2012 by Andrey CheptsovThis 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.





March 7th, 2012 at 8:46 am
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.
March 7th, 2012 at 10:43 am
+1 for default collapsing
March 7th, 2012 at 12:04 pm
really? in 2012?

googled 2 years ago, found in netbeans but didn’t in greatest Jetbrains Idea
March 7th, 2012 at 2:06 pm
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)
March 7th, 2012 at 2:17 pm
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.
March 7th, 2012 at 2:25 pm
I’ve also filed another issue regarding this feature.
http://youtrack.jetbrains.com/issue/IDEA-82402
March 8th, 2012 at 5:53 am
Since having to stop and reach for the mouse can get annoying, is this capable of autocomplete if I start typing?
March 9th, 2012 at 7:16 am
Jason, which autocompletion do you have in mind?
Thanks for the issues in the tracker, jtonic! That was just the right thing to do.
June 11th, 2012 at 10:23 am
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
}
June 11th, 2012 at 11:11 am
Sebastien,
Please file a YouTrack issue for the problem.
June 19th, 2012 at 11:33 am
@Yole, I did and in my post.
IDEA-87312
June 22nd, 2012 at 8:25 am
here’s the link
http://youtrack.jetbrains.com/issue/IDEA-87312
July 20th, 2012 at 11:00 am
“” doesn’t work in “*.properties” files