NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Code Folding Improvement


Joined: 06 Aug 2009
Posts: 3
Reply with quote
I have a number of feature requests I'd like to eventually post, but I wanted to get this one out because it would be a significant improvement to the IDE that (I imagine) would be trivial to implement.

A number of code folding enhancements have been requested on this forum in past months. The most common (that I can tell) are code folding for the array() construct and code folding for regions of code. My proposed solution would enable easy code folding for both of these use cases as well as a number of others.

A few screenshots are included at the bottom to illustrate what I have in mind, but the idea is very simple: use PhpED's already-existing bracket-matching logic to determine code folding regions. The bracket-matching logic I am referring to is not only used to determine the opposing brace, bracket, or parenthesis to highlight but also the logic that is used to highlight HTML tags in the code navigator when the caret is placed on either a beginning or ending HTML tag.

When a right-click is made on a "matchable bracket" (either a brace, bracket, parenthesis, or HTML tag) the context menu contains a command to fold the code to the matching bracket. As illustrated in the screenshots, to make the feature usable for quick folding in all circumstances, the location of the command within the context menu should be dependent upon whether the context menu is displayed above the cursor or below it.

If anyone is wondering how region code folding can be accommodated, try placing comments /* { */ and /* } */ in between a section of PHP or CSS declarations and the comments <!-- { --> and <!-- } --> between a section of HTML tags. You'll notice that the opposing brace is highlighted in each case with the required caret placement. Labels, too, can be accommodated by using /* Region Label { */, for example. Matching labels below sections can be accommodated with /* } Region Label */.

This also has a significant benefit of being able to quickly fold sections of PHP code that enclose large blocks of HTML. The downside is that braces must be used to denote the body of a compound statement (as opposed to the alternate control structure syntax), but this is something that PhpED might be able fix. For example:

Code:

<ul id="jurisdictions">

   <? foreach ( $profileSection['data'] as $jurisdiction ) { ?>

      <li class="jurisdiction">
         <div class="clearfix">
            <label class="name">Jurisdiction</label>
            <input class="name" type="text" value="<? echo escape( $jurisdiction['name'] ); ?>" />

            <label class="yearAdmitted">Year Admitted</label>
            <input class="yearAdmitted" type="text" value="<? echo escape( $jurisdiction['yearAdmitted'] ); ?>" />

            <div class="moveButtons">
               <a href="#" class="up" alt="up"></a>
               <a href="#" class="down" alt="down"></a>
               <a href="#" class="remove jurisdiction" alt="remove"></a>
            </div>

         </div>
      </li>

   <? } ?>

</ul>


There are two other benefits to this code folding mechanism that may not be apparent at first. First, it allows you to fold brackets, braces, parentheses, and HTML tags from the bottom up. The popular TextMate editor has this feature, but it is implemented through the gutter, highlighting the next point. Second, at any given time, mousing over to the gutter to fold a block of code may not always be very convenient. This is primarily because of indentation. The more indented code becomes (especially with HTML tags) the more likely your cursor is going to be further away from the gutter, necessitating a long mouse movement aimed at a very tiny target area. So even if folding of the array() construct was implemented through the gutter, at only a few levels of indentation it would often be more convenient to fold as I have described through the context menu.







Last edited by kevan schwitzer on Wed Oct 13, 2010 8:04 am; edited 2 times in total
View user's profileFind all posts by kevan schwitzerSend private message


Joined: 21 Jul 2010
Posts: 3
Location: Batumi, Georgia
Reply with quote
The idea was pleasant.
If developers realise it, it will be very useful and it is convenient!
View user's profileFind all posts by AlkatraZSend private messageICQ Number
How about doing this...


Joined: 02 Dec 2015
Posts: 9
Reply with quote
From documentation on php.net:
http://www.php.net/manual/en/control-structures.intro.php

Quote:
Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well. The various statement types are described in this chapter.


In other words, the following code is valid PHP code:

Code:

echo 'Hello World';
{ //polite stuff
    echo ", it's nice to meet you.\n";
    echo "May I be of service?\n";
}


The curly braces define a statement group similar to when used with if, else, foreach, etc. It does not create a new variable scope, but can be used to format/organize code in some arbitrary way. Statement groups can contain almost anything (assuming nest conventions are followed). Some things that can't be used inside statement blocks are: namespace, use, (global) const, and __halt_compiler().

Anything in curly braces will have a fold mark in PhpED. That includes statement groups which then function just as one would hope/expect. One feature that would be nice is if PhpED showed the comment immediately after the starting brace while the group is folded. One can even make a shortcut to create a foldable statement group using code snippets in PhpED. For example, the following template could be created and assigned to the keyboard combination Ctrl+Alt+B
Code:
{// my_custom_code_block
|}

Thereafter one could highlight a block of code then hit Ctrl+Alt+B to wrap the highlighted statements in curly braces, creating a statement group.
View user's profileFind all posts by pantsmannSend private message
Code Folding Improvement
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic