Search code examples
visual-studio-codevscode-extensionstmlanguage

VS Code Extension: how to match "strings" without overwrite the color of another pattern (tmLanguage.json)


I am writing an extension for a specific language for Visual Studio Code and I made it so far that I can set the colors for several commands.

I have my mylang.tmLanguage.json which contains (simplified) the following:

{
    "\\$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
    "name": "F-Script",
    "patterns": [
        { 
            "include": "#goto"      
        },       
        {
            "include": "#strings"
        }
    ],
    "repository": {
        "GOTO": 
        {
            "patterns": 
            [{
                "name": "support.function",
                "match": "/(?!«)[a-zA-Z0-9_.\\-«»/(/)]{1,}(?=»)\\b"
            },
            {
                "name": "support.function",
                "match":"GOTO[(]{1}(# [a-zA-Z0-9_.-]{1,}|/)[)]{1}"
            }]
        },
        "strings": {
            "name": "string.quoted.double.f-script",
            "begin": "\"|\\'",
            "end": "\"|\\'",
            "patterns": [
                {
                    "name": "constant.character.escape.f-script",
                    "match": "$"
                }
            ]
        }                                       
    },
    "scopeName": "source.f-script"
}

When I try to test the color for "strings" overrules the command GOTO.

«GOTO(# THIS.COLOR.GOTO)»
"This Should be formated like a String"
"This Should be formated like a String «GOTO(# THIS.COLOR.GOTO)»"

What i try to do is that the GOTO on the 3rd line is colored alike the first goto. My problem is that the whole line is colored like a string (including the GOTO command).

Does anyone have an idea how I can set this that the string is formated as a string and the contained commands are colored different?


Solution

  • You could simply include goto in the patterns for strings:

    "strings": {
        "name": "string.quoted.double.f-script",
        "begin": "\"|\\'",
        "end": "\"|\\'",
        "patterns": [
            {
                "name": "constant.character.escape.f-script",
                "match": "$"
            },
            {
                "include": "#goto"
            }
        ]
    }