Code Folding in PHP editor

What is code folding? I have looked for a definition and I have found this at wikipedia:

Code folding is a feature of some text editors, source code editors and IDEs that allows the user to selectively hide and display sections of a currently-edited file as a part of routine edit operations. This allows the user to manage large regions of potentially complicated text within one window, while still viewing only those subsections of the text that are specifically relevant during a particular editing session.

The PHP editor, like other editors in NetBeans, provides this functionality. By default no fold is collapsed. The setting you can specify in general editor options (Tools -> Options -> Editor).  On the picture below you can see which folds are supported in PHP and how they are mapped in editor options.

When you collapse a fold, you can see the hidden content, if you place the mouse cursor over the fold description.

The PHP editor also supports custome folds, which are defined by user. At the line, where you want to start the fold, place comment //<editor-fold> and at line where the fold should end place //</editor-fold>. Content between these tags, will be folded. These folds can be also nested.

The functionality, which provides the custome folds, is available from today. It is available in development builds or you have to wait until NetBeans 6.5 Milestone 2 will be published.


I personally like code folding very much, it becomes usefull to visually shorten the code and see what functions we have, or what methods are in the class - with no code or comments that are not that important at the moment.
BUT... I think leaving the first commented line (the one with text) is more efficient. You already know - I'm talking about the "Eclipse-way" code folding - that way in a block comment or in phpdoc/javadoc I can write the most important key info in the first line and when the comment is folded - I can see the key info. So I know better where in the code I am and what is the function below the phpdoc doing without hovering, unfolding or anything.
I would be happy to see that way of folding (at least as an option) cause it's way... well - better than just seeing [...] ;)
Keep up the good work!

Posted by Azrael Valedhel on July 19, 2008 at 12:10 AM CEST #

I don't know how resource intensive or if this is even feasible, but it would be nice if when you hovered on "folded code" it would "unfold" down and "push" the rest of the code so you get to see the code "unfold" inline with the rest of the code. Let's use the last screen capture in this post as an example. If you implemented my suggestion right after the "unfolded code" (line 10 - 16) showing when you hover, the rest of the code would "flow" so you get to see line 17 instead of line 24 as it is now. Hope I made sense.
IMHO that would be a bit more useful.
Keep up the excellent work.

Posted by alex_mayorga on July 22, 2008 at 11:30 AM CEST #

Thanks for the comments.

Both suggestions are valid. I will talk with people from editor about this.


Posted by Petr on July 23, 2008 at 04:35 AM CEST #

What are the keyboard shortcuts for toggling code folding?

Posted by Bryan Wilhite on August 02, 2008 at 01:11 AM CEST #

Hi Bryan,
the keyboard shortcuts you can find in the Options -> Keymap -> View.

The default shortcuts are:
-collapse current fold: CTRL + MINUS
-expand current fold : CTRL + PLUS
-collapse all folds: CTRL + SHIFT + MINUS
-expand all folds: CTRL + SHIFT + PLUS


Posted by Petr on August 03, 2008 at 08:42 PM CEST #

I'm a fairly new programmer and I was using Activestates Edit till I found you. What I'm really missing is automatic code folding for the "{". When you have multiple nested ifs else etc with lots of {
then it is useful to be able to "hide" a couple of them to see what is going on with your code. Maybe you could add it to the "collapse by default options".

Posted by Blekos Ilias on January 02, 2009 at 05:28 AM CET #

I agree with Blekos. Actually, even if we cannot fold all { (braces), can we at least get the ability to fold if statements and loops?

PS - I am an Eclipse convert to NetBeans since November 2008. It simply rocks.

Posted by Siddharth on February 18, 2009 at 05:15 PM CET #

Code folding "if's", "for's", foreach's, etc, is the only thing that Netbeans lacks of (and most text-editors/IDE, for that matter). Yes, you can use custom-folds, but that's not pratical at all.
If done right, you could code fold a "if" with html code inside of if.


<?php if (bla) { ?>
<? } ?>

( or with "if (bla):" and "endif;" )

Posted by tsousa on June 30, 2009 at 10:33 AM CEST #

Hi guys, I'm wondering if anything ever came of the first commenter's recommendation for showing the first line of PHPDOC when a function and its comment are folded. I think its DEFINITELY worth a setting if you don't want to fully switch but that really, it's almost 100% better than the current system. As it is you're still using up a whole line but there is no information at all in it. Showing at least some of the first line of documentation is so much more worthwhile and would make the code folding a lot more valuable.

Another consideration is that there's no way to say "fold all functions but leave documentation open". I love the 'fold all' and 'unfold all' keyboard shortcuts, but i really want to see at least some of the docs for it to be useful.

I made an enhancement ticket with a careful explanation of why its important here:

Let me know if I'm missing something important, and thanks for the amazing work, I'm so impressed that NetBeans understands PHP and its users so well :)

Posted by Jeremy Clarke on July 31, 2009 at 12:33 PM CEST #

It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.

Posted by links of london on October 29, 2009 at 08:08 PM CET #

Code fold if else and braces!

Posted by guest on July 13, 2011 at 11:29 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed

This blogs is written by NetBeans developers who contribute to the PHP support mainly.


« April 2014