zokugun / vscode-explicit-folding

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

nested nodes fail with regex. use syntax tree #92

Closed milahu closed 1 year ago

milahu commented 1 year ago

let me use more powerful tools to select nodes

regex is too limited for this task

example:

<div class="foldme">
  <div>inner 1</div>
  <div>inner 2</div>
</div>

result with folding:

<div class="foldme">...
  <div>inner 2</div>
</div>

the last </div> cannot be matched with regex

settings

{
  "explicitFolding.rules": {
    "html": [
        // not working
        {
            "beginRegex": "<(?![a-z]+)(?:\\s+[^>]*)?\\s+class=(?:\"foldme\"|'foldme')",
            "endRegex": "<\\/\\1>",
            "autoFold": true
        },
        // note: meta has no close tag
        {
            "beginRegex": "<meta class=\"foldme\"",
            "endRegex": ">",
            "autoFold": true
        },

        // workaround for broken regex:
        // add some tags manually

        {
            "beginRegex": "<div class=\"foldme\"",
            "endRegex": "<\\/div>",
            "autoFold": true
        },
    ],
  },
  "explicitFolding.autoFold": "none",
}
daiyam commented 1 year ago

@milahu I agree that regex is limiting when try to fold a language but the primary objective of the extension is to support user-specified folding (like {{{ ... }}}).

But to support folding based on a syntax tree is an all new kind of extension.

milahu commented 1 year ago

yeah ... im moving from vscode to lapce editor, in the hope that they will base all plugins on parsers, not regex (and multiple plugins share one parser, to avoid double-parsing)

there are tree-sitter plugins for vscode, but i prefer a fast editor (rust vs javascript)

closing as "out of scope"

daiyam commented 1 year ago

lapce is still new. It still doesn't support folding since it uses the highlights from helix. How do you define foldings in tree-sitter? I'm interested because I find that the foldings definitions the VSCode's language grammar is very limited.

milahu commented 1 year ago

How do you define foldings in tree-sitter?

based on the syntax tree...?

its just faster and more precise than regex, otherwise it should be similar. sorry, i can only guess for now