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.