Home > Back-end >  Embedding Language Syntax in Visual Studio Code Extension
Embedding Language Syntax in Visual Studio Code Extension

Time:01-16

I'm trying to get VSCode to use syntax highlighting for an embedded language in an extension I'm writing.

Much like Razor I'd like c# code to be highlighted between @{ and }.

In my package.json I have the following in the contributes section:

        "languages": [
        {
            "id": "csasm",
            "aliases": ["CS Asm", "csasm"],
            "extensions": [".csasm"],
            "configuration": "./language-configuration.csasm.json"
        }],
        "grammars": [
        {
            "language": "csasm",
            "scopeName": "source.csasm",
            "path": "./syntaxes/csasm.tmLanguage.json",
            "embeddedLanguages": {
                "meta.embedded.block.csharp" : "csharp"
            }
        }]

And the csasm.tmLanguage.json file:

{
    "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
    "name": "CS Asm",
    "patterns": [
        {
            "include":"#csasm"
        }
    ],
    "repository": {
        "csasm" :{
            "patterns": [{
                "name" : "meta.embedded.block.csharp",
                "begin": "@{",
                "end": "}",
            }]
        }
    },
    "scopeName": "source.csasm"
}

My understand is that the matching of @{ and } would tag the lines with meta.embedded.block.csharp, which links to the embeddedLanguages in the project.json, but that doesn't appear to be the case.

I can't find any examples of the embeddedLanguages tag. (I've tried searching for how Razor implements this, but they appear to have a proper Language Server?)

I can't even find a way to debug the problem.

How can I get the embeddedLanguages to work? Or failing that, how can I debug this?

Edit: Changed the language file so csasm is as below and removed the embeddedLanguages section.

            "patterns": [{
                "name" : "meta.embedded.block.csharp",
                "begin": "@{",
                "end": "}",
                "patterns" : [{
                    "include" : "source.csharp"
                }]
            },

Still no joy with c#. I'll open another question.

CodePudding user response:

A working example of reStructuredText language can be found here,

https://github.com/trond-snekvik/vscode-rst/blob/v1.5.1/syntaxes/rst.tmLanguage.json#L284

The syntax rule (domain-cpp) captures a block of text and then mark a portion of that as C/C source code (source.cpp).

Then VSCode is smart enough to highlight that embedded section using C/C syntax and you don't need embeddedLanguages in project.json.

  •  Tags:  
  • Related