microsoft / vscode-textmate

A library that helps tokenize text using Text Mate grammars.
MIT License
584 stars 116 forks source link

Scope inspector bug when extending C++ syntax highlighting through Textmate grammar #163

Open felipejinli opened 2 years ago

felipejinli commented 2 years ago

Steps to Reproduce:

  1. created a vscode extension
  2. contributed to C++ grammar by writing the following Textmate grammar file (see snippet 1) and setting the extension's package.json to contribute correctly (see snippet 2)
  3. built the extension and ran it on Extension Development Mode
  4. used Inspect Editor Scopes tool to check a token that matches to a custom defined Regex within my extended Textmate grammar file
  5. PROBLEM: inspector doesn't show my custom textmate scope

Snippet 1)

{
    "scopeName": "source.cpp",
    "fileTypes": [
        "cpp",
        "c++",
        "c"
    ],
    "patterns": [
        {
            "include": "#expression"
        }
    ],
    "repository": {
        "expression": {
            "patterns": [
                {
                    "include": "#clog-db"
                },
                {
                    "include": "#indent-DB"
                },
                {
                    "include": "#array-db"
                },
                {
                    "include": "#sample"
                }
            ]
        },
        "clog-db": {
            "begin": "^\\s*clog",
            "end": "<<[^;]+;",
            "name": "cpdebug.clog"
        },
        "indent-DB": {
            "match": "^\\s*DB\\(\\);",
            "name": "cpdebug.indentdb"
        },
        "array-db": {
            "match": "^\\s*dba(2)?\\([^;]+\\);",
            "name": "cpdebug.arraydb"
        },
        "sample": {
            "match": "clog",
            "name": "cpdebug.sample"
        }
    }
}

Snippet 2:

    "contributes": {
            "languages": [
                {
                    "id": "cpp",
                    "extensions": [
                        ".cpp"
                    ]
                }
            ],
            "grammars": [
                {
                    "language": "cpp",
                    "scopeName": "source.cpp.debug",
                    "path": "./syntaxes/cpp.tmGrammar.json",
                    "injectTo": [
                        "source.cpp"
                    ]
                }
            ]
        },

image

jeff-hykin commented 2 years ago

TextMate is pretty finicky. It's possible it's an inspect scope issue, but I'd bet it's a pattern matching issue.

Might want to check if it works when you move the clog outside of main. (Which I know sounds dumb cause you'd never use it top level but it may reveal the issue)

I maintain the C++ syntax so I feel obligated to say injections are not going to be fun. You might be able to get away with something simple like clog though. The ^\\s* you've makes me assume you know what you're doing though 👍.

felipejinli commented 2 years ago

I've tried your suggestion (which is smart since the hypothesis could've been another C++ language rule parsing the main and thus not using my custom expression language rule)

However, the scope inspector is still unable to pick up my custom scope.

Was thinking of using a derived scopename like source.cpp.cpdebug since that would give my language rules higher precedence than that of C++'s original but it doesn't work.

I've uploaded my vscode extension code to the following repo (https://github.com/felipejinli/vscode-hide-logs). Would you mind taking a look? Really appreciate your help :)

jeff-hykin commented 2 years ago

Currently I probably can't take a good look at it. Most of the time textmate scope issues require trial an error to figure it out even for the best of us.

In 4 months I might be doing some work on the C++ syntax. I've wanted injections in the past so I might build-in some handy scopes like a source.cpp#inject-everywhere to make this process easier.

RedCMD commented 2 months ago

same as in https://stackoverflow.com/a/78961689/12817801 you are trying to override the C++ grammar when you should instead be injecting into it

Remove "languages": [] and "language": "cpp" Add an "injectionSelector" to your grammar file

"injectionSelector": "L:source.cpp -string -comment"

image

https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide#injection-grammars