Page 1 of 1

Tcl and Code Folding

Posted: 20 Aug 2011 04:49
by jgodfrey
Hello,

I just started working on a Tcl syntax file, but I'm struggling a bit with the code folding. First, here's some sample code:

Code: Select all

proc resolveMacros {string} {
    set serial [file tail $::globals(serialDir)]
    if {$serial eq ""} {set serial "Undefined"}
    set part [file tail $::globals(partDir)]
    if {$part eq ""} {set part "Undefined"}

    set time [clock seconds]
    set mapString [list]
    # --- build up a string map of all possible substitutions
    set macroList [dbMem eval {select macro from macro}]
    set sortedList [lsortby {string length} -int -decreasing $macroList]
    foreach macro $sortedList {
        if {$macro eq "%part"} {
            lappend mapString $macro $part
        } elseif {$macro eq "%serial"} {
            lappend mapString $macro $serial
        } else {
            set val [clock format $time -format $macro]
            lappend mapString $macro $val
        }
    }

    # --- return the substituted result
    return [string map $mapString $string]
}
Below is an image of how I'd like it to fold in TextEd (the screenshot is from another editor).

Any input would be appreciated.

Jeff

Re: Tcl and Code Folding

Posted: 20 Aug 2011 17:01
by Rickard Johansson
It's difficult because {} is used to enclose conditions, parameters... But maybe something like this:

Code: Select all

[Fold]
1_Id=proc
1_Begin=
1_End=
1_EndBefore=\in
1_Section=
1_NoParentOfId=0
1_NotAlwaysEnded=0
1_ListItem=ceMethod
2_Id=foreach
2_Begin=
2_End=
2_EndBefore=\in
2_Section=
2_NoParentOfId=0
2_NotAlwaysEnded=0
2_ListItem=
3_Id=if
3_Begin=
3_End=
3_EndBefore=\in
3_Section=
3_NoParentOfId=0
3_NotAlwaysEnded=1
3_ListItem=
...

Re: Tcl and Code Folding

Posted: 20 Aug 2011 17:10
by jgodfrey
Rickard Johansson wrote:It's difficult because {} is used to enclose conditions, parameters...
Yeah, that's the problem I ran into, but I figured there might be a way around it that I was unaware of...
Rickard Johansson wrote:But maybe something like this:
Thanks, I'll give your suggestions a try.

Jeff

Re: Tcl and Code Folding

Posted: 20 Aug 2011 17:39
by jgodfrey
Richard,

I've played with your suggested folding rules. They're definitely on the right track, though they suffer from something I noticed while playing with the "\in" item last night.

That is, since the folding stops just *above* the line with a matching or less indent, the closing brace of a proc never becomes part of the fold. So, a fully folded proc is always 2 lines: the proc line itself and the closing brace as a second line.

I'd really like a proc to fold to just a single line. Any thoughts on how I might make that happen?

Thanks for your input.

Jeff

Re: Tcl and Code Folding

Posted: 20 Aug 2011 19:55
by Rickard Johansson
Not without adding some kind of fold option.

To make it work is it safe to:

1. Always ignore { ... } on the same line?

2. Or assume the last { on the same line (or first on the next) is the beginning of a fold?

Re: Tcl and Code Folding

Posted: 20 Aug 2011 20:20
by jgodfrey
Rickard Johansson wrote:Not without adding some kind of fold option.

To make it work is it safe to:

1. Always ignore { ... } on the same line?

2. Or assume the last { on the same line (or first on the next) is the beginning of a fold?
I *think* either of those sound OK. In my original screenshot above, take a look at line 9285. I'd prefer that line to not be (independently) folded as it's complete in just 1 line anyway. Would both of the above suggestions work OK with that line?

Jeff

Re: Tcl and Code Folding

Posted: 20 Aug 2011 20:51
by Rickard Johansson
Yes that will work. It's already does in other programming languages e.g. with functions like in C++

Code: Select all

int myAdd(int x) { return 10*x; }

Re: Tcl and Code Folding

Posted: 22 Aug 2011 14:05
by jgodfrey
Here's one more snippet of valid code that might impact which of the above two methods makes the most sense...

Code: Select all

proc buildUI {parent {name "Test"} {root .}} {
   ...
   ...
}
So, within a procedure's argument list, *optional* arguments with default values can be defined by enclosing them in braces. Option #2 still sounds perfectly valid. Option #1 might be also, depending on implementation (since the above braces are nested).

Jeff