Search code examples
regexvisual-studio-coderegex-group

Regex for VisualCode explicitFolding extension


my custom file has below format, using explicit folding rules i am able to make custom folding for every [] , but when i collapse it only collapse 1 line not all lines. \n is not accepting in explicit folding extension.

[Table]
s1=raw
s2=meta

[Table2]
S2=raw
s1=meta

How to make entire [] block to collapse and expand until next []

"explicitFolding.rules": {
    "*": {
        "beginRegex": "^\\[.*\\]",
        "endRegex": " "
        }
},

Solution

  • The following setting works for your file format

    "explicitFolding.rules": {
      "*": {
        "separatorRegex": "^\\[.*\\]",
        "foldEOF": true
      }
    }
    

    Edit

    I had to debug the extension to find out why the suggestion by Mark did not work.

    If you have "endRegex": "^\\s*$" the regex used to find folds is a^. This will never be found.

    The extension tests if a specified regex (begin. middle, end) matches the empty string. (possible infinite loops, I fixed that in my extension by explicit testing if the next find is actually advancing the lastIndex after an exec() call, after an issue filed by Mark)

    If you want to use beginRegex and endRegex you can use:

    "explicitFolding.rules": {
      "*": {
        "beginRegex": "^\\[.*\\]",
        "endRegex": "^\\s*$",
        "bypassProtection": true,
        "foldEOF": true
      }
    }
    

    I would suggest to add this folding rule to a particular languageID in the setting.