Yesterday I tried to import come complex c++ code into Leo. Leo's c++ 
importer didn't do well with templates. A lot of hand tweaking was 
necessary.

This work did create a head-slapping moment: the hard part of Leo's 
importers is determining the lines on which classes, methods and functions 
begin and end. Well, vs code must do something very similar! vs code 
automatically determines the *folding units*, the places where code folding 
begins and ends.

So how *does* vs code determine folding units? Might vs code use something 
like LSP <https://langserver.org/>? Well, LSP does not appear to offer 
language parsing/folding features.

I use vs code to study vs code's own sources. So let's take a look. The 
extensions/cpp folder contains lots of data. Here is 
extensions/cpp/language-configuration.json:

{
    "comments": {
        "lineComment": "//",
        "blockComment": ["/*", "*/"]
    },
    "brackets": [
        ["{", "}"],
        ["[", "]"],
        ["(", ")"]
    ],
    "autoClosingPairs": [
        { "open": "[", "close": "]" },
        { "open": "{", "close": "}" },
        { "open": "(", "close": ")" },
        { "open": "'", "close": "'", "notIn": ["string", "comment"] },
        { "open": "\"", "close": "\"", "notIn": ["string"] }
    ],
    "surroundingPairs": [
        ["{", "}"],
        ["[", "]"],
        ["(", ")"],
        ["\"", "\""],
        ["'", "'"],
        ["<", ">"]
    ],
    "folding": {
        "markers": {
            "start": "^\\s*#pragma\\s+region\\b",
            "end": "^\\s*#pragma\\s+endregion\\b"
        }
    }
}

This is promising! We have a .json file that provides basic data about the 
c++ tokens.

And what's this at the end? It looks like vs code natively supports two 
pragmas that users can use to define custom folds. A quick experiment 
confirms that these pragmas do create custom folding regions.

*The big question*

I have not yet discovered how vs code parses c++ files in order to 
determine folding regions. Googling hasn't revealed any answers yet. 
Incremental parsing is always tricky: vs code can *not* assume the text is 
syntactically valid!

I wonder whether the electron editor might be the base on which folding 
happens. Iirc, electron is part of vs code, so the answer is likely present 
somewhere in vs code's sources.

*Summary*

vs code can calculate fold regions and c++ and many other language. Does 
anyone know how (and where) vs code calculates these fold regions? 

The vs code way probably won't help Leo's importers, but I'd like to know 
for sure.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/075d7f9b-1163-4850-baee-37737c8a11e4o%40googlegroups.com.

Reply via email to