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.
Stephen Friedrich says:
March 7, 2012Now 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.
Reed Owens says:
April 27, 2018You can specify a block to be collapsed by default.
//
I’ve tried it out in Webstorm and it works.
Michail Almyros says:
May 17, 2018Have you ever heard of Lombok https://projectlombok.org/ it allows you to generate the getters and setters and they are not visible.
Ingo says:
March 7, 2012+1 for default collapsing
alximik says:
March 7, 2012really? in 2012? 🙁
googled 2 years ago, found in netbeans but didn’t in greatest Jetbrains Idea 🙁
jtonic says:
March 7, 2012The 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)
jtonic says:
March 7, 2012I 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.
jtonic says:
March 7, 2012I’ve also filed another issue regarding this feature.
http://youtrack.jetbrains.com/issue/IDEA-82402
Jason says:
March 8, 2012Since having to stop and reach for the mouse can get annoying, is this capable of autocomplete if I start typing?
Eugene Toporov says:
March 9, 2012Jason, which autocompletion do you have in mind?
Thanks for the issues in the tracker, jtonic! That was just the right thing to do.
Sebastien says:
June 11, 2012The 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
}
yole says:
June 11, 2012Sebastien,
Please file a YouTrack issue for the problem.
Sebastien says:
June 19, 2012@Yole, I did and in my post.
IDEA-87312
Sebastien says:
June 22, 2012here’s the link
http://youtrack.jetbrains.com/issue/IDEA-87312
Folder says:
July 20, 2012“” doesn’t work in “*.properties” files
user says:
July 4, 2013doesn’t work in .sql files either
couldBeBetter says:
July 22, 2013yes, 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.
Diego Cirujano says:
March 14, 2014How 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)
Konstantin says:
March 28, 2014Hi. Doesn’t work on AppCode (((
Marcel Bradea says:
April 21, 2014This 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!
Marcel Bradea says:
April 21, 2014Sorry I meant the ‘Structure’ pane, not ‘Preview’.
LB says:
December 6, 2015Yet 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 ?
dluc says:
January 23, 2017I tried the two annotations and double checked the preferences, unfortunately this doesn’t seem to work for Scala files
Rob Mayoff says:
March 23, 2017In 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
Rob Mayoff says:
March 23, 2017Part of my comment got munched. In Scala, you have to put a blank line after the opening editor-fold line.
Morgen Peschke says:
July 12, 2017Thanks Rob!
Do you happen to know if there’s a workaround to get `defaultstate=”collapsed”` working?
mehdi says:
April 27, 2018Have any custom folding regions for html.