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