When switching from C# to JAVA, I found that I missed the #region/#endregion tags that let you group related code together. Fortunately, the guys at Netbeans have solved this problem (indirectly). For any IDE built code, they have created a tag called editor-fold.
If you put an editor fold around a code block (related methods/properties), you can collapse it whenever you want. Since the tag has a comment in front of it, there is no harm if you open the code in other IDEs or editors. It is just ignored. Of course, why would you use anything but Netbeans? :-)
Syntax
// <editor-fold defaultstate="collapsed" desc="describe what is in here">Usage
Your code goes here...
// </editor-fold>
Usage is very simple. The most basic has no description or default state
You may define the default description of a collapsed fold by adding a "desc" tag:
// <editor-fold desc="describe what is in here">You may set a fold to be collapsed by default by adding a "defaultstate" tag:
Your code goes here...
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="describe what is in here">Netbeans Code Templates
Your code goes here...
// </editor-fold>
Another trick is to go into Netbeans Tools->Options->Editor->Code Templates and add a few Abbreviations. For my typical development I created three:
Abbreviation : EF
Description : Insert the opening code fold
Expanded Text: //
Abbreviation : EEF
Description : Insert the closing code fold
Expanded Text: //
Abbreviation : EFB
Description : Insert opening and closing code folds
Expanded Text: //
//
If you have never used Code Templates before, they are simple to use. In the Netbeans editor, just type the abbreviation and then a space. The editor will expand the abbreviation to become the Expanded Text you specify.
You may need to play with white space and such to get it to appear where you want. I usually indent the closing editor-fold for EFB. Everyone types different though.
Last Thoughts
I personally think that SUN should consider adding this (or something similar) to the JAVA language. I use them a good amount to group related methods within a class together.
Update - 2008-04-30
Netbeans has posted a similar approach that can be looked at as an alternative to the solution above. Visit this link on their wiki for more information. It is pretty much the same, however, they have a few tags that did not exist when the blog was written. The nice thing about this updated version (which may require Netbeans 6.x) is that you just highlight the code you want to include in the fold vs having to type EF and EEF before and after the code.
The only bad part is that every time you select a code block, it will give you a lightbulb asking if you want to put this block in a code fold. It is all give and take. I will try this out for a while in addition to what I already use.
- - -





9 comments:
Thanks for this.
Was looking for something like #region as its a great way of helping write clean and understandable code and the only place i found a hint of the solution was in the middle of a C# v Java Flame war.
Nice and succinct help thanks
Thanks for the post^^!!!
I was looking for this to tidy up my code. It was taking time to find the most suited word key for this article in google :(
But finally i have reached to your blog. THanK's BrO :)
Do you know if there is a way to do this for Eclipse?
To my knowledge, there isn't a way to do this in Eclipse. I wish that a standard Java approach is created.
Gracias!!!
Awesome! works just like C#
Very nice. I tryed to put a #region "code template" but the # unfortunatelly appears. So i just created region...endregion :-D
Regards
Ezequias
I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!
Sorry for offtopic
Post a Comment