zokugun / vscode-explicit-folding

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

PHP switch case fallthrough folding goes away for self and parents #34

Closed inventor96 closed 3 years ago

inventor96 commented 3 years ago

Describe the issue

When I have a switch case in PHP that include fallthrough cases (two cases that go to the same set of actions), the folding of all parent folds go away.

To reproduce

Code Example

<?php

function methodOne($var) {
    switch ($var) {
        case 'hi':
        case 'hi2':
            echo 'hi';
            break;
        case 'hello2':
            echo 'something else';
        case 'hello':
            echo 'hello';
            break;
        default:
            echo 'nothing';
            break;
    }
}

function methodTwo($var) {
    switch ($var) {
        case 'bye':
            echo 'bye';
            break;
        case 'see ya':
            echo 'see ya';
            break;
        default:
            echo 'nothing again';
            break;
    }
}

Settings

"folding": {
    "php": [
        {
            //"begin": "case",
            //"end": "break;",

            "beginRegex": "(?:case|default)[^:]*:",
            "middleRegex": "(?:case|default)[^:]*:",

            //"beginRegex": "(?:[\\r\\n\\s]*?(?:case|default)[^:]*:){1,}",
            //"middleRegex": "(?:[\\r\\n\\s]*?(?:case|default)[^:]*:){1,}",

            "endRegex": "break;",

            //"kind": "comment",
            //"nested": false
        },
        /* {
            "begin": "default",
            "end": "break;"
        }, */
        {
            "beginRegex": "{",
            "middleRegex": "}[^}]+{",
            "endRegex": "}"
        },
        {
            "begin": "/*",
            "end": "*/"
        },
        {
            "begin": "\"",
            "end": "\""
        },
        {
            "begin": "'",
            "end": "'"
        },
        {
            "begin": "[",
            "end": "]"
        },
        {
            "begin": "(",
            "end": ")"
        }
    ]
}

Expected behavior

The code should fold at the last case statement in the group for direct fallthrough setups (e.g. case 'hi2': in my example). The code should fold at both case statements when there's a fallthrough with other actions (e.g. case 'hello2': and case 'hello': in my example). In both cases, the parent folds should still be available.

Screenshots

In this screenshot, I would expect to see folds for lines 3, 4, and 9. image

Additional context

I've tried many variations for the begin(Regex), middle(Regex) keys, as well as played around with nested and kind, but I have yet to achieve the desired results. You can see some of my attempts in the commented portions of my folding settings. The settings included here are what were set at the time I took the screenshot.

daiyam commented 3 years ago

Me too, I can't figure out how to do what you want... 😞

daiyam commented 3 years ago

Maybe something like this (it's a work in progress):

{
    "beginRegex": "(?:case|default)[^:]*:",
    "endRegex": "break;|(.)(?=case|default|\\})",
    "foldLastLine": [true, false]
}

which give me Screenshot

foldLastLine is modified so that "foldLastLine": [true, false]:

What do you think?

inventor96 commented 3 years ago

That sounds good to me. Is it easy for me to try out your code changes so I can see how it works with real code instead of only test stuff?

daiyam commented 3 years ago

I've made the branch fold-switch. Here the steps to test it:

Or uninstall the extension and install explicit-folding-0.9.2.vsix (remove .zip) by dropping the file in the list of extensions.

inventor96 commented 3 years ago

I used the later method for simplicity. Everything looks good in the more complex files I was working in today that had switch statements.

daiyam commented 3 years ago

@inventor96 I've published a new version of the extension with the changes you have tested.

inventor96 commented 3 years ago

Sounds good. I'll close this for now since the original issue has been fixed. Thanks for your work!