zokugun / vscode-explicit-folding

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

Add option to end region when indentation level does not match #107

Open DeltaRazero opened 12 months ago

DeltaRazero commented 12 months ago

Describe the issue

I use a certain way to split my code into regions which doesn't use explicit region endings. I use the 'seperatorRegex' function for this, and this works well in general. However, there is one drawback with this, and that is that indentation levels are ignored with this (see the code example and current behaviour).

I would suggest adding an option to end the region when a line — that includes non-whitespace/tab characters — has less indentation than the line on which the region was started.

This could potentially also be implemented for when begin/end parameters are being instead, but that is not my current use-case though.

To reproduce

Code Example

Python Example ```python class Foo: # :: REGION 01 :: # def function_01(self): pass def function_02(self): pass # :: REGION 02 :: # def function_03(self): pass def function_04(self): pass class Bar: # :: REGION 03 :: # def function_05(self): pass def function_06(self): pass # :: REGION 04 :: # def function_07(self): pass def function_08(self): pass ```
C++ Example ```cpp class Foo { // :: REGION 01 :: // void function_01() {} void function_02() {} // :: REGION 02 :: // void function_03() {} void function_04() {} } class Bar { // :: REGION 03 :: // void function_05() {} void function_06() {} // :: REGION 04 :: // void function_07() {} void function_08() {} } ```

Settings


"editor.foldingStrategy": "auto",
"editor.defaultFoldingRangeProvider": "zokugun.explicit-folding",
"explicitFolding.rules": {
    "*":
        {
            "separatorRegex": "([a-zA-Z@&]+.*? :: )",
            "indentation": true // I propose to add this option
        },
},

Expected behavior

Python Example ```python class Foo: # :: REGION 01 :: # def function_01(self): pass def function_02(self): pass # :: REGION 02 :: # # Folded class Bar: # :: REGION 03 :: # def function_05(self): pass def function_06(self): pass # :: REGION 04 :: # def function_07(self): pass def function_08(self): pass ```
C++ Example ```cpp class Foo { // :: REGION 01 :: // void function_01() {} void function_02() {} // :: REGION 02 :: // // Folded } class Bar { // :: REGION 03 :: // void function_05() {} void function_06() {} // :: REGION 04 :: // void function_07() {} void function_08() {} } ```

Current behavior

Python Example ```python class Foo: # :: REGION 01 :: # def function_01(self): pass def function_02(self): pass # :: REGION 02 :: # # Folded # :: REGION 03 :: # def function_05(self): pass def function_06(self): pass # :: REGION 04 :: # def function_07(self): pass def function_08(self): pass ```
C++ Example ```cpp class Foo { // :: REGION 01 :: // void function_01() {} void function_02() {} // :: REGION 02 :: // // Folded // :: REGION 03 :: // void function_05() {} void function_06() {} // :: REGION 04 :: // void function_07() {} void function_08() {} } ```
daiyam commented 12 months ago

I could modify begin/while to capture groups so you could do:

{
    "beginRegex": "^(\\s*)(?:#|\\/\\/)\\s*[a-zA-Z@&]+.*? :: ",
    "whileRegex": "^\\1"
},
DeltaRazero commented 12 months ago

That would probably work perfectly fine for me