zokugun / vscode-explicit-folding

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

Troubleshooting folding with brackets #35

Closed inventor96 closed 3 years ago

inventor96 commented 3 years ago

Describe the issue

I'm continuing to develop my PHP folding settings, and I've encountered another situation where the folding disappears for the given block as well as its parents. I'm not sure if it's something with my rules, or if it's too many matches too close together, or something else.

To reproduce

Code Example

<?php
function methodOne($something) {
    if ($something) {
        $something_else = "a string with {$something['key']} in it";
    }

    return $something_else;
}

Settings

"folding": {
    "php": [
        {
            "beginRegex": "(?:case|default)[^:]*:",
            "endRegex": "break;|(.)(?=case|default|\\})",
            "foldLastLine": [true, false]
        },
        {
            "beginRegex": "{",
            "middleRegex": "}[^}]+{",
            "endRegex": "}\\s*(else|if)?",
            "foldLastLine": [true, false]
        },
        {
            "beginRegex": "if\\s*\\(.+\\)\\s*[^\\{]?",
            "middleRegex": "else(?:\\s*if\\s*\\(.+\\)\\s*[^\\{]?)?",
            "endRegex": ";"
        },
        {
            "beginRegex": "<(?!area|base|br|col|embed|hr|img|input|link|menuitem|meta|param|source|track|wbr)([a-zA-Z]+)[^>]*[^>\\/]*>",
            "endRegex": "<\\/\\1>"
        },
        {
            "beginRegex": "<\\?(?:\\=|php)",
            "endRegex": "\\?>"
        },
        {
            "begin": "/*",
            "end": "*/"
        },
        {
            "begin": "\"",
            "end": "\""
        },
        {
            "beginRegex": "(?<!\\w)'",
            "endRegex": "'"
        },
        {
            "begin": "[",
            "end": "]"
        },
        {
            "begin": "(",
            "end": ")"
        }
    ]
}

Expected behavior

I would expect the fold markers to show for the if block and the function block.

Screenshots

I would expect folds for lines 8 and 9 in these first three screenshots image

image

image

It appears that as long as I've removed the square brackets, the folds show up.

image

Additional context

The code in the third screenshot is invalid (hence the red underline), but I included it for demonstration purposes.

daiyam commented 3 years ago

@inventor96 Hi, have you found a solution for your issue?

inventor96 commented 3 years ago

Unfortunately not yet.

daiyam commented 3 years ago

By moving the comments and strings first, it should do the trick. Can you try:

"php": [
  {
    "begin": "/*",
    "end": "*/",
    "nested": false
  },
  {
    "begin": "\"",
    "end": "\"",
    "nested": false
  },
  {
    "beginRegex": "(?<!\\w)'",
    "endRegex": "'",
    "nested": false
  },
  {
    "beginRegex": "(?:case|default)[^:]*:",
    "endRegex": "break;|(.)(?=case|default|\\})",
    "foldLastLine": [true, false]
  },
  {
    "beginRegex": "{",
    "middleRegex": "}[^}]+{",
    "endRegex": "}\\s*(else|if)?",
    "foldLastLine": [true, false]
  },
  {
    "beginRegex": "if\\s*\\(.+\\)\\s*[^\\{]?",
    "middleRegex": "else(?:\\s*if\\s*\\(.+\\)\\s*[^\\{]?)?",
    "endRegex": ";"
  },
  {
    "beginRegex": "<(?!area|base|br|col|embed|hr|img|input|link|menuitem|meta|param|source|track|wbr)([a-zA-Z]+)[^>]*[^>\\/]*>",
    "endRegex": "<\\/\\1>"
  },
  {
    "beginRegex": "<\\?(?:\\=|php)",
    "endRegex": "\\?>"
  },
  {
    "begin": "[",
    "end": "]"
  },
  {
    "begin": "(",
    "end": ")"
  }
]
inventor96 commented 3 years ago

That seems to have done the trick! Thanks @daiyam!