Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Code Folding

by sir_lichtkind (Friar)
on Dec 29, 2008 at 14:26 UTC ( #733039=perlmeditation: print w/replies, xml ) Need Help??

Dear Monks, as some here might know i'm writing a editor with Perl. Recently (see here and there, yes it runs under linux, even if there still a hook to it - I'm working on that) i added code folding, what practically scintilla does for me. but unlike Kommodo i added now also key binding, so that you can do everything with keyboard too.

This is only possible because I have only 2 functions (unlike the 6 in Kommodo) which do in my respected view more than these 6. Please let me explain. These 6 functions collapse in first place to 3 because there are fold/unfold twins, but in my OSO (obviously superior opinion) you can only do one of the 2 at once. And since the fold marker tells you in what state the current line is (and you can expect what to happen), just toggle should be enough. Then you have the "fold recursive" call, which also makes no sense to me, because when i fold the head, I don't care if an invisible line is folded in the internal representation of scintilla. with "fold all" I have similar issue. folding/unfolding the current node on click or with key combo is something legitimatly expected, but often when you find yourself clicking through the nodes you want something different instead.

Kephra foldes now (since 0.4.1) on middle click all siblings of this node (e.g. all subs) so you can get a nice overview with only one click over the area of interest. If you prefer keyboard you just have to take additional shift key to get that alternative behaviour.

Please let me know what is your take on this or if you have even some better ideas.

UPDATE: I know implemented 4 functions for TIMTOWTDI. the 2 new are: toggle all and toggle recursive. surprise!

thanks for reading.

Kephra, a beautiful Perl Editor lives at http://kephra.sourceforge.net

Replies are listed 'Best First'.
Re: Code Folding
by afresh1 (Hermit) on Dec 29, 2008 at 22:48 UTC

    For folding it doesn't matter if the below is folded recursively. It is more important for unfolding. It allows you to unfold an entire sub, or just the single level.

    Imagine:

    sub abc { my ($x) = @_; my $z; foreach my $y (@{ $x }) { # do xxx # on # lots # of # lines } return $z; }

    when it is folded it would be (kinda like this in vim)

    +-- 14 lines: sub abc {

    when you unfold, would you get back the initial example, or

    sub abc { my ($x) = @_; my $z; +--- 7 lines: foreach my $y (@{ $x }) { return $z; }

    That is the purpose of recursive folding IMHO.

    In Vim, it remembers the folded status of things, so if it is in the first example and you fold to the second example, when you unfold, it goes back to the first example. However, if you fold recursively, the default unfold will unfold to the third example.

    l8rZ,
    --
    andrew
      Your' right but i was aware of that just didn't want point out all cornercases. My point was however that there is no immediat purpose to fold recursively. you have to make two operations, when i need just one to achieve the same result. hope could make this clearer now.

      Kephra, a beautiful Perl Editor lives at http://kephra.sourceforge.net

        Although vim has many commands for folding things, I really only use 2, za and zA. They toggle folding at the cursor, capital is recursive.

        My point is that folding can appear simple, but depending on features you want to allow it can be more complex.

        l8rZ,
        --
        andrew

        I don't think you do acheive the same result, though i havn't managed to get the wX library to install so i'm not sure.

        sub test { # 5 important lines while ($x) { # 10 lines of good stuff } for (1..5) { # 100 lines of crap } }

        Now if you fold that recursively you get

         sub test { ... }

        and then a recursive unfold yeilds to original back. However a non recursive unfold returns

        sub test { # 5 important lines while ($x) { ... } } for (1..5) { ... } }

        So there are cases where a user might want a recursive fold/unfold, and other where they do not. Now if you through in remembering the fold state, maybe i've already folded the "100 lines of crap" but i left "10 lines of good stuff" unfolded. If i did a non recursive fold on test, and then a nonrecursive unfold i would still be able to see my "5 important lines" and the "10 lines of good stuff" but the "100 lines of crap would not be unfolded". If however you alwas do a recursive fold/unfold then as soon as i unfold test it unfolds my "100 lines of crap"


        ___________
        Eric Hodges

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://733039]
Approved by grep
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2023-11-28 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?