X

An Oracle blog about NetBeans for PHP

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.


Join the discussion

Comments ( 11 )
  • Azrael Valedhel Friday, July 18, 2008

    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!


  • alex_mayorga Tuesday, July 22, 2008

    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.


  • Petr Wednesday, July 23, 2008

    Thanks for the comments.

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

    Thanks,

    Petr


  • Bryan Wilhite Friday, August 1, 2008

    What are the keyboard shortcuts for toggling code folding?


  • Petr Sunday, August 3, 2008

    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

    Regards,

    Petr


  • Blekos Ilias Friday, January 2, 2009

    Hello,

    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".


  • Siddharth Wednesday, February 18, 2009

    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.


  • tsousa Tuesday, June 30, 2009

    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.

    Example:

    <?php if (bla) { ?>

    <table>...</table>

    <? } ?>

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


  • Jeremy Clarke Friday, July 31, 2009

    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:

    http://www.netbeans.org/issues/show_bug.cgi?id=169660

    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 :)


  • links of london Thursday, October 29, 2009

    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.


  • guest Wednesday, July 13, 2011

    Code fold if else and braces!


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.