Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 97327

Set language type in pattern - vs code language extension

$
0
0

I want for vscode to understand that the language in between <go> tags in a html file should be validated as golang code.

So given:

<go>
    // I want to get intellisense and syntax highlighting for golang here
</go>

I currently have the following insides grammars in package.json:

{
    "scopeName": "go.html.injection",
    "path": "./syntaxes/go.tmLanguage.json",
    "injectTo": [
        "text.html"
    ],
    "embeddedLanguages": {
        "source.go": "go"
    }
}

and in syntaxes/go.tmLanguage.json:

{
    "scopeName": "go.html.injection",
    "injectionSelector": "L:text.html",
    "patterns": [
        {
            "include": "#go-tag"
        }
    ],
    "repository": {
        "go-tag": {
            "begin": "<go>",
            "end": "<\/go>",
            "name": "go"
        }
    }
}

Inspecting it using the debug gives it the name go as a textmate scope but the language is still set to html. How can I set the language of the match to golang:

result of the debugger

Inspecting the content of script tags show the language set to javascript so this should be possible? I also realise that then the match includes the <go> tag so I understand I now need to add pattern matching and evaluation for that.

Update 20/02/20:

After referring to the vscode svelte extension I figured out how to get syntax highlighting for the tag and innerHTML using this inside syntaxes/go.tmLanguage.json (same package.json):

{
    "scopeName": "go.html.injection",
    "injectionSelector": "L:text.html",
    "patterns": [
        {
            "include": "#go-tag"
        }
    ],
    "repository": {
        "go-tag": {
            "begin": "(<)(go)",
            "beginCaptures": {
                "1": {
                    "name": "punctuation.definition.tag.begin.html"
                },
                "2": {
                    "name": "entity.name.tag.html"
                },
                "3": {
                    "name": "punctuation.definition.tag.end.html"
                }
            },
            "end": "(<\/)(go)(>)",
            "endCaptures": {
                "1": {
                    "name": "punctuation.definition.tag.begin.html"
                },
                "2": {
                    "name": "entity.name.tag.html"
                },
                "3": {
                    "name": "punctuation.definition.tag.end.html"
                }
            },
            "patterns": [
                {
                    "contentName": "source.go",
                    "begin": "(>)",
                    "beginCaptures": {
                        "1": {
                            "name": "punctuation.definition.tag.end.html"
                        }
                    },
                    "end": "(?=</go>)",
                    "patterns": [
                        {
                            "include": "source.go"
                        }
                    ]
                }
            ]
        }
    }
}

I can now see that vscode is correctly highlighting syntax for the tag and using the imported golang syntax tokens. However it is still displays the language as "html".

new token debug screen


Viewing all articles
Browse latest Browse all 97327

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>