zokugun / vscode-explicit-folding

Customize your Folding for Visual Studio Code
MIT License
95 stars 14 forks source link

ability to set fold level based on single line regex capture #96

Closed edgimar closed 11 months ago

edgimar commented 1 year ago

Emacs has a nice package called outline-minor-mode which allows one to create explicit folds by simply adding special comment lines that mark only the beginning of a folding region (and indicate the folding level). For example:

/// * 1
... text / code W ...
/// ** (a)
... text / code X ...
/// ** (b)
... text / code Y ...
/// * 2
... text / code Z ...

When fully folded, this would appear as:

/// * 1
/// * 2

and if we unfold '1' and '(b)' (which is a fold nested within fold '1'), we will see:

/// * 1
... text / code W ...
/// ** (a)
/// ** (b)
... text / code Y ...
/// * 2

It isn't clear to me if there's a way to do this using the "Auto Fold" functionality. If not, it would be nice if one could simply specify a regular expression that includes a capture group, and when the expression matches, the length of the captured text indicates the depth of the fold -- in the example above, the regex would attempt to capture the series of asterisks on lines like /// ** foo.

daiyam commented 1 year ago

Maybe with a config like:

"explicitFolding.rules": [
    {
        "separatorRegex": "\\/\\/\\/ \\*(?=\s|$)",
        "strict": "never",
        "nested": [
            {
                "separatorRegex": "\\/\\/\\/ \\*\\*",
            }
        ]
    }
],

autoFold is only there to fold the folding ranges when a file is opened.

edgimar commented 1 year ago

Will those rules handle more than one nested level? My example above only shows two levels, but the idea is to have an arbitrary nesting depth (or, at least up to 5 or 6).

I guess something like the following should work:

    "explicitFolding.rules": {
        "": {
                "separatorRegex": "\\/\\/\\/ \\*(?=\\s|$)",
                "strict": "never",
                "nested": [
                    {
                        "separatorRegex": "\\/\\/\\/ \\*\\*(?=\\s|$)",
                        "nested": [
                            {
                                "separatorRegex": "\\/\\/\\/ \\*\\*\\*(?=\\s|$)",
                                "nested": [
                                    {
                                        "separatorRegex": "\\/\\/\\/ \\*\\*\\*\\*(?=\\s|$)",
                                        "nested": [
                                            {
                                                "separatorRegex": "\\/\\/\\/ \\*\\*\\*\\*\\*(?=\\s|$)",
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
    },