zokugun / vscode-explicit-folding

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

Sharing my C/C++ folding rules #23

Closed HolyBlackCat closed 3 years ago

HolyBlackCat commented 4 years ago

Hello! Not sure if this is the right place to do it, but I just wanted to share the C/C++ folding rules ended up using.

What is folded:

Folding doesn't happen inside of:

Known issues that I don't know how to fix:

Config:

"cpp": [
    // Last version can be found at:  https://github.com/HolyBlackCat/vscode-settings/blob/master/settings.json
    { // `//...` (not actually folded, but forces contents to be ignored)
        "beginRegex": "(?!)",
        "endRegex": "\\/\\/.*",
    },
    { // `/*...*/`
        "begin": "/*",
        "end": "*/",
        "nested": false,
    },
    { // R"(...)" (C++ only)
        "beginRegex": "\\b(?:L|u8|u|U|)R\"([^\\s\\(\\)\\\\]{0,16})\\(",
        "endRegex": "\\)\\1\"",
        "nested": false,
        "foldLastLine": false,
    },
    { // "..." (not actually folded, but forces contents to be ignored)
        "beginRegex": "(?!)",
        "endRegex": "\"(?:[^\"\\\\]|\\\\.)*\"",
    },
    { // '...' (not actually folded, but forces contents to be ignored)
        "beginRegex": "(?!)",
        "endRegex": "'(?:[^'\\\\]|\\\\.)*'",
    },
    { // {...}
        "begin": "{",
        "end": "}",
        "foldLastLine": false,
    },
    { // (...)
        "begin": "(",
        "end": ")",
        "foldLastLine": false,
    },
    { // [...]
        "begin": "[",
        "end": "]",
        "foldLastLine": false,
    },
    { // Preprocessor conditionals
        "beginRegex": "^\\s*#\\s*if(?:(?:n)?def)?\\b", // "#if", "#ifdef", "#ifndef".
        "middleRegex": "^\\s*#\\s*el(?:se|if)\\b", // "#else", "#elif"
        "endRegex": "^\\s*#\\s*endif\\b", // "#endif"
    },
],
daiyam commented 4 years ago

@HolyBlackCat thanks for sharing your rules. Your usage of the extension is way beyond its original purpose (emacs folding). :open_mouth:

For #define, have you tried:

{
  "begin": "#define",
  "continuation": "\\"
}

The "beginRegex": "(?!)" is baffling me... I will have check it...

HolyBlackCat commented 4 years ago

@daiyam (?!) is a regex that never matches anything. I'm basically exploiting #20. :P

The #define is complicated, so I decided to just ignore it.

#define A /* 
// this should be folded
#define B */

#define A {
// this shouldn't be folded
#define B }

The second case (unbalanced braces in #define) almost never appear in actual code, so it's not a big loss.