#8 Fold { on relevant line

open
Neil Hodgson
None
3
2001-12-13
2001-08-11
Steven Fodstad
No

When using code like:

while (foo)
-{
bar;
}

collapsing at the brace yields:

while (foo)
+{
It would be nice if it yielded
+while (foo)

instead.

Discussion

1 2 > >> (Page 1 of 2)
  • Neil Hodgson
    Neil Hodgson
    2001-08-11

    • priority: 5 --> 3
     
  • Neil Hodgson
    Neil Hodgson
    2001-08-11

    Logged In: YES
    user_id=12579

    Yes, this would be a good preference to have although its
    harder to implement than the current folding based on
    braces.

     
  • Neil Hodgson
    Neil Hodgson
    2001-12-13

    • assigned_to: nobody --> nyamatongwe
     
  • Will Gaines
    Will Gaines
    2002-01-07

    Logged In: YES
    user_id=420333

    Better yet would be to allow the user to set arbitrary fold
    points wherever desired. Personally, I would like to right
    click in the grey column wherever I want a fold to begin,
    and again wherever I want it to end, and delete them
    whenever desired. I would also like to be able to nest
    folds like I can nest outline levels in MS Word Outline
    Mode.

    If that is too hard to implement, another possibility would
    be to allow special characters for starting and ending a
    fold, instead of forcing everyone to fold by the same
    character scheme, such as braces or whatever.

    Here's a conversation I had inre with Trent Mick at Komodo:

    > This forces Komodo to fold like this:
    >
    > <?php //This script automates the xyz process.
    > <?php //Defines a variable, $parts, that holds the
    quantity.
    > <?php //Figures the price, based on $/lb.
    > <?php //Calculates the total for the customer.
    >
    > This works out pretty good so far (I am just a beginner).
    However, I am
    > worried that somewhere in future, I will find that all
    those extra <?php ...
    > ?> are going to get me into trouble. Do you think they
    will?

    Will,

    I don't see how they would get you into trouble.

    > You asked: "Is there a common pattern that you find you
    would like to
    > "hide" in your code?"
    >
    > So, if it is too difficult to allow arbitrary points by a
    click-on-it method
    > like I originally asked for, perhaps it would be easier
    to allow the user to
    > enter a comment, say: #f, which would trigger a fold-
    begin point, and then
    > another comment, say #e, that would trigger the fold-end
    point.

    That might be possible, though this is something that is
    best proposed
    to the scintilla-dev list (see the Scintilla project on
    SOurceForge).

    > Are you familiar with MS Word Outline Mode?
    >
    > If not, I recommend that you play around with it a bit
    until you see why I
    > think it would be such a great feature for a code editor.

    You are right. I will take a look at MS Word. I am sure
    that what theyy
    have is a good example. Thank you for the pointer.

    Trent

    --
    Trent Mick
    TrentM@ActiveState.com

    Trent,

    The idea is to compress the code to the max to get more
    literally under my
    eye at the same time. Somehow, this helps me to keep long
    files organized in
    my head.

    So, what I do now is simulate the arbitrary set points that
    I asked for. I
    do it like this:

    <?php //This script automates the xyz process.
    code 1
    code
    code
    code
    code
    ?>
    <?php //Defines a variable, $parts, that holds the quantity.
    code 2
    code
    code
    code
    ?>
    <?php //Figures the price, based on $/lb.
    code 3
    code
    code
    code
    ?>
    <?php //Calculates the total for the customer.
    code 4
    code
    code
    code
    ?>

    This forces Komodo to fold like this:

    <?php //This script automates the xyz process.
    <?php //Defines a variable, $parts, that holds the quantity.
    <?php //Figures the price, based on $/lb.
    <?php //Calculates the total for the customer.

    This works out pretty good so far (I am just a beginner).
    However, I am
    worried that somewhere in future, I will find that all
    those extra <?php ...
    ?> are going to get me into trouble. Do you think they will?

    It appears to me that the fold mechanism reads the code and
    folds according
    to what is written.

    You asked: "Is there a common pattern that you find you
    would like to
    "hide" in your code?"

    So, if it is too difficult to allow arbitrary points by a
    click-on-it method
    like I originally asked for, perhaps it would be easier to
    allow the user to
    enter a comment, say: #f, which would trigger a fold-begin
    point, and then
    another comment, say #e, that would trigger the fold-end
    point.

    Between these two points, all other automatic folding would
    be disabled.

    Then we could write like this:

    <?php #f //This script automates the xyz process.
    code 1
    code
    code
    code
    code
    #e
    #f //Defines a variable, $parts, that holds the quantity.
    code 2
    code
    code
    code
    #e
    #f //Figures the price, based on $/lb.
    code 3
    code
    code
    code
    #e
    #f //Calculates the total for the customer.
    code 4
    code
    code
    code
    #e
    ?>

    Which, folded would look like this:

    <?php #f //This script automates the xyz process.
    #f //Defines a variable, $parts, that holds the quantity.
    #f //Figures the price, based on $/lb.
    #f //Calculates the total for the customer.
    ?>

    More terse, would be to also allow a combination fold-end
    and fold-begin
    comment, say #c.

    Then we could write like this:

    <?php #f //This script automates the xyz process.
    code 1
    code
    code
    code
    code
    #c //Defines a variable, $parts, that holds the quantity.
    code 2
    code
    code
    code
    #c //Figures the price, based on $/lb.
    code 3
    code
    code
    code
    #c //Calculates the total for the customer.
    code 4
    code
    code
    code
    #e
    ?>

    Which would fold:

    <?php #f //This script automates the xyz process.
    #c //Defines a variable, $parts, that holds the quantity.
    #c //Figures the price, based on $/lb.
    #c //Calculates the total for the customer.
    ?>

    Whatcha think?

    If you like it so far, I would suggest some ideas for
    nesting folds as I
    mentioned in my original wish.

    Are you familiar with MS Word Outline Mode?

    If not, I recommend that you play around with it a bit
    until you see why I
    think it would be such a great feature for a code editor.

    MSW-OM will nest folds to many levels. Indeed, an entire
    book can be
    nest-folded to a single line, yet opened, nest-by-nest, any
    way you have set
    it up. Thus, if say, Windows XP were so coded, it, too,
    could be nest-folded
    to a single line.

    Will

    > Will Gaines wrote:
    >
    > >
    > >
    > > I would like to be able to control the range of what
    is collapsed. The
    > > very best feature for me might be to right click in the
    margin (where
    > > the + and - are) at the point where I want to start a
    collapse range,
    > > to get a context menu containing, say, "Set start of
    collapse
    > > range", then right click again to set where the
    collapse would end, with
    > > say, "Set end of collapse range".
    > >
    > >
    > >
    > > Alternatively, I could enter a mark in the editing
    page. I note that if
    > > I enter <>, that will automatically start a collapse
    range. However, it
    > > sets the collapse range from there all the way to the
    bottom. I can find
    > > no way to set a point where the range ends. Is there a
    way to set the
    > > end of the range?
    > >
    > >
    > >
    > > I am not objecting to the automatic sets that you have
    now. They too are
    > > good. However, I would like to be able to override
    them, cancel any one,
    > > or any number of them, or reset any one to another
    point.
    > >
    > >
    > >
    > > It would also be wonderful to be able to nest ranges,
    the way MS Word
    > > does when in outline mode. This would greatly
    facilitate editing long
    files.
    > >
    > >
    > >
    > > Will Gaines
    > >
    > > willgaines@starband.net <mailto:willgaines@starband.net>
    > >
    >
    > Will,
    > Thanks for the request. Currently it would not be very
    easy for us to
    > allow arbitrary fold point in Komodo. Mind you, Komodo's
    editor
    > component is an open source project (called scintilla) so
    such a feature
    > might come down the road. As well, the main scintilla
    author is working
    > on a similar editting component with grander plans that
    could include
    > this feature.
    >
    > What would be more likely is a different standard fold
    point selection
    > behaviour. Is there a common pattern that you find you
    would like to
    > "hide" in your code?
    >
    > Thanks,
    > Trent
    >
    > --
    > Trent Mick
    > TrentM@ActiveState.com
    >

     
  • Neil Hodgson
    Neil Hodgson
    2002-01-09

    Logged In: YES
    user_id=12579

    All this is possible, its just work so you need to look at
    writing your own folder (unfortunately PHP is part of the
    very complex HTML + Scripting lexer and folder) which could
    check for either hard coded fold comments or strings sent
    into the lexer as properties.

    Scintilla allows, but does not provide support for, client
    code performing its own definitions of folding such as user
    defined fold ranges. Coding may be a little tricky, coping
    with the deletion of user fold end points correctly. Folds
    are strictly nested - what happens when the user defines a
    fold range that partially overlaps one defined by the
    document?

    I am unlikely to work on this in the near future and its
    even less likely I'd completely implement any of the
    suggestions although there may be some generic features
    coming that may make these easier to implement. This may
    include more forms of client owned persistant state stored
    by Scintilla such as per line data which could then be used
    to store the user's fold ranges.

     
  • Logged In: NO

    Actually it would be exceedingly nice to be able to both
    fold on the function name instead of the brace, to fold on
    arbitary comment positions, and to do nested folding.

    One thing I should like to be able to do but currently
    can't is to be able to fold tag pairs within HTML / XML
    i.e. <table><tr><td></td></tr></table>
    should collapse successively to :
    <table><tr>[+]</tr></table>
    <table>[+]</table>
    [+]

    To my mind the folding feature is the core thing that sets
    Scintilla apart from the rest - it would be really great if
    you would fully explore the possibilities of this tech...

     
  • Neil Hodgson
    Neil Hodgson
    2002-02-06

    Logged In: YES
    user_id=12579

    There are some great things that could be done with folding
    but implementation is complex. Not currently a high
    priority for me. Your example looks like intra-line folding
    and Scintilla only currently supports full-line folding.

     
  • Duncan Kimpton
    Duncan Kimpton
    2002-02-06

    Logged In: YES
    user_id=110804

    Yes, the html/xml stuff most definately would require
    inline folding...
    The function wrapping would require some form of prefix
    folding.

    I can see that this would not be easy to implement! However
    I think that when you get sinkworks under way it would be
    of significant benefit to alot of users - setting you way
    ahead of the field (even MS)...

     
  • Yair Siegel
    Yair Siegel
    2003-06-16

    Logged In: YES
    user_id=778430

    an easier implementation (and yet user friendly) might be
    this way:
    +while(foo)
    {
    }

     
  • Mischa
    Mischa
    2003-11-06

    Logged In: YES
    user_id=724617

    what is about this idea:

    while(foo){
    }

     
  • Neil Hodgson
    Neil Hodgson
    2003-11-08

    Logged In: YES
    user_id=12579

    I don't understand what you are proposing. Rewriting the
    code to match the more sensible brace layout which puts the
    brace on the initial line is unacceptable to many people.

     
  • Micah
    Micah
    2005-08-14

    Logged In: YES
    user_id=1328955

    I'm not sure how to submit something like this, so I'm
    posting it here. Please let me the correct process if this
    is not it.

    The following change will create a new property called
    "fold.prev.line". When flagged, the previous line will fold
    when a { is the first character on a line.

    This means that the following code:
    while(foo)
    {
    bar;
    }
    will fold in to:
    while(foo)

    I have not tested this extensively, so there may be any
    number of problems with it. However, at first glance it
    appears to work well.

    298d297
    < bool foldPrevLine =
    styler.GetPropertyInt("fold.prev.line", 1) != 0;
    350,357c349,352
    < //not first character in line
    < if(visibleChars) {
    < // Measure the
    minimum before a '{' to allow
    < // folding on "} else {"
    < if (levelMinCurrent
    > levelNext) {
    <
    levelMinCurrent = levelNext;
    < }
    < levelNext++;
    ---
    > // Measure the minimum
    before a '{' to allow
    > // folding on "} else {"
    > if (levelMinCurrent >
    levelNext) {
    > levelMinCurrent =
    levelNext;
    358a354
    > levelNext++;
    367,382d362
    < }
    < //if the next non-whitespace
    character is a { and we aren't folding this line, then fold here
    < if(foldPrevLine && levelUse ==
    levelNext) {
    < for(unsigned int j = i+1; j
    < endPos; j++) {
    < char currentChar =
    styler.SafeGetCharAt(j);
    < //if it's whitespace
    go on to next character
    <
    if(isspacechar(currentChar)) {
    < continue;
    < }
    < //if it's a { then
    increment the level
    < if(currentChar == '{') {
    < levelNext++;
    < break;
    < }
    < else break;
    < }

     
  • Neil Hodgson
    Neil Hodgson
    2005-08-14

    Logged In: YES
    user_id=12579

    Use the "Attach a file" section at the bottom of this page
    to attach the whole file, rather than a patch. Code pasted
    into the text field gets wrapped and is very difficult to use.

     
  • Micah
    Micah
    2005-08-14

    Logged In: YES
    user_id=1328955

    http://www.videoquesting.com/LexCPP/

    LexCPP.cxx - Modified file.
    LexCPP.old.cxx - Original file.
    LexCPP.diff - Difference file between the two.

     
  • Neil Hodgson
    Neil Hodgson
    2005-08-15

    Logged In: YES
    user_id=12579

    This has problems with adding too many levels when there is
    a blank line before the '{' like

    FilePath FilePath::BaseName() const

    {

    To debug folding problems, it is an idea to turn on line
    numbers and show the fold levels and flags with set
    fold.flags=64
    line.margin.width=10

     
  • Logged In: NO

    Okay, I updated the .diff and .cxx file with a fix to that
    bug. It now adds the - on the line above the {, even if
    it's blank.

    So
    FilePath FilePath::BaseName() const

    {

    collapses to
    FilePath FilePath::BaseName() const
    -

     
  • Neil Hodgson
    Neil Hodgson
    2005-08-17

    Logged In: YES
    user_id=12579

    Try this example where you don't get a fold for the second '{'

    int x()
    {
    }
    {
    int y = x();
    }

    Or if there is a '{' at the start of the file.

     
  • Micah
    Micah
    2005-08-18

    Logged In: YES
    user_id=1328955

    Updated files again. Tested all the bugs with the code you
    have mentioned so far, along with a couple others. However,
    I make no guarentees that it's bug free. :)

     
  • Micah
    Micah
    2005-08-18

    Logged In: YES
    user_id=1328955

    Nevermind, found a bug with that one too, so don't bother
    testing it. :p

     
  • Micah
    Micah
    2005-08-18

    Logged In: YES
    user_id=1328955

    Updated to fix a couple bugs where it wouldn't fold right if
    you changed some code after initially writing it. I also set
    it to fold if the first character in the file is a {.

    As for the folding:
    {
    }
    {
    }
    ...if I am not mistaken, c and c++ do not allow code blocks
    without statements before them. So the above will never
    occur within a c code file because you will always have
    something between the } and {. Please correct me if I'm
    wrong about this and I'll try to figure out a way to get it
    to work without any visible characters between the two }{.

    With the way I currently have it set up it is proving very
    difficult to handle this scenario. The best solution I can
    come up with is to loop backwards through the characters
    until the first visible character is found, and if it's a }
    then handle it differently. However, if that is not
    allowable c or c++ code I do not think it's worth the
    processor time to add that functionality.

     
  • Neil Hodgson
    Neil Hodgson
    2005-08-22

    Logged In: YES
    user_id=12579

    C++ allows blocks anywhere inside a function. This is
    sometimes used to scope variables particularly when
    destructors are used to automatically clean up:
    int x(Data &d)
    {
    {
    FileResource("x.txt").write(d);
    }
    {
    ImageResource("y.png").mergefrom("x.txt");
    }
    }

    Unfortunately there are many possible cases: if this feature
    was easy to add, I would have done it. Currently the
    behaviour is poor but stable for this layout style.
    Replacing the current code with something that works much of
    the time but sometimes failed in a way that is hard to
    understand would be a step backwards.

    The patch does not ignore '{' inside comments. It also
    changes behaviour when the option is turned off, ignoring an
    initial bare '{' on each call of the folder.

     
  • Micah
    Micah
    2005-11-04

    Logged In: YES
    user_id=1328955

    I have updated the files at
    http://www.videoquesting.com/LexCPP/ to reflect a new
    approach to this situation. I tried to test it with all the
    scenarios listed below along with any other possible
    combinations of { and } characters on various lines.

    Known Issues:
    -Depending on the line endings for the file, the first 1-4
    lines will fold like they do now, as if you disabled the
    fold.prev.line.
    Everything still works, it just causes the new code to not
    take effect.

    -The following code will fold like it does now:
    function(...)
    {...}
    As above, everything works, however the new code doesn't run
    because it counts the braces and sees no change.

     
  • Neil Hodgson
    Neil Hodgson
    2005-11-06

    Logged In: YES
    user_id=12579

    The back seeking code is complex and wrong, assuming, for
    example that \r only occurs in \r\n sequences. It is easier
    to find the start of the previous line with
    styler.LineStart(lineCurrent-1) (with a start of file check)
    rather than performing a chracter loop.
    It also assumes that the only reason for folding is an
    operator { where it may be caused by a fold comment or
    preprocessor line and will then get the levels wrong. What
    you are really after here is whether the previous line
    changed fold level (before being affected by the current line).
    There is a new naming convention that properties for the cpp
    folder should start with fold.cpp. so they can be discovered
    by scripts.

     
  • Neil Hodgson
    Neil Hodgson
    2005-11-07

    Logged In: YES
    user_id=12579

    A simpler way of thinking about this may be to say that if
    the only fold level modifier on the current line is an
    operator { and the previous line is a header candidate then
    the current line should have its levelCurrent incremented
    (that is levelCurrent = levelNext) and the previous line
    should have its header flag set but with no other changes to
    its level. This makes the '{' line subordinate to the
    previous line.
    Since the actual levels of the previous line are not
    modified that then allows the previous line to be considered
    a candidate if its levelCurrent == levelNext.
    I'm thinking of making some generic changes in folding to
    make it simpler to implement.

     
  • Micah
    Micah
    2005-11-07

    Logged In: YES
    user_id=1328955

    I didn't know about styler.LineStart() so, I'll make some
    adjustments to the code to make it use that instead.

    "...the current line should have its levelCurrent incremented
    (that is levelCurrent = levelNext) and the previous line
    should have its header flag set but with no other changes to
    its level..."

    What happens if you do this? I assumed that level current
    had to be equal to the previous lines level next. I also
    assumed that every line that had the header flag would also
    have a change in levels. I never considered that the two
    could be different.

     
1 2 > >> (Page 1 of 2)