如何在自定义 VSCode 语言中引用注入语法

How to reference an injected grammar within a custom VSCode language

在下面做序。可以在以下位置找到说明此问题的演示项目: https://github.com/kirksl/so60384141

我可以在本地 VSCode 语言中引用注入的语法

指定这个 (package.json)

        "grammars": 
        [
            {
                "scopeName": "source.js.mylang",
                "path": "./syntaxes/mylang-js.tmLanguage.json",
                "injectTo": ["source.js"]
            }
        ]

和这个 (./syntaxes/mylang-js.tmLanguage.json)

{
  "scopeName": "source.js.mylang",
  "injectionSelector": "L:source",
  "patterns": [
    {
      "include": "#todo-keyword"
    }
  ],
  "repository": {
    "todo-keyword": {
      "match": "TODO",
      "name": "keyword.todo"
    }
  }
}

我看到了这个(.js 文件,原生 JS)

到目前为止一切顺利。

但是我似乎无法在下面详述的自定义语言中引用相同的注入语法。带有三重引号的围栏代码块应该使用它。

正在更新,现在,这个 (package.json)

        "languages": [
            {
                "id": "mylang",
                "aliases": ["Mylang", "mylang"],
                "extensions": [".mylang"],
                "configuration": "./language-configuration.json"
            }
        ],
        "grammars": 
        [
            {
                "language": "mylang",
                "scopeName": "source.mylang",
                "path": "./syntaxes/mylang.tmLanguage.json",
                "embeddedLanguages": {
                    "meta.embedded.block.javascript": "javascript"
                }
            },
            {
                "scopeName": "source.js.mylang",
                "path": "./syntaxes/mylang-js.tmLanguage.json",
                "injectTo": ["source.js"]
            }
        ]

并另外指定此 (./syntaxes/mylang-js.tmLanguage.json)

    "fenced-expression": {
      "begin": "(^|\G)\s*[\"]{3}\s*$",
      "end": "(^|\G)\s*[\"]{3}\s*$",
      "contentName": "meta.embedded.block",
      "beginCaptures": {
        "0": {
          "name": "string.quoted.triple.mylang"
        }
      },
      "endCaptures": {
        "0": {
          "name": "string.quoted.triple.mylang"
        }
      },
      "patterns": [
        {
          "begin": "^(\s*)(?=[\S&&[^{<\[]])",
          "while": "^(?!\s*[\"]{3}\s*)",
          "contentName": "meta.embedded.block.javascript",
          "patterns": [
            {
              "include": "source.js"
            }
          ]
        }
      ]
    }

我看到了这个(.mylang 文件,突出显示 JS,未突出显示 TODO)

最后只是显示了一些管道可以启用我正在寻找的突出显示

如果我将 "include": "source.js" 切换为 "include": "source.js.mylang"

    "fenced-expression": {
      "begin": "(^|\G)\s*[\"]{3}\s*$",
      "end": "(^|\G)\s*[\"]{3}\s*$",
      "contentName": "meta.embedded.block",
      "beginCaptures": {
        "0": {
          "name": "string.quoted.triple.mylang"
        }
      },
      "endCaptures": {
        "0": {
          "name": "string.quoted.triple.mylang"
        }
      },
      "patterns": [
        {
          "begin": "^(\s*)(?=[\S&&[^{<\[]])",
          "while": "^(?!\s*[\"]{3}\s*)",
          "contentName": "meta.embedded.block.javascript",
          "patterns": [
            {
              "include": "source.js.mylang"
            }
          ]
        }
      ]
    }

我看到了这个(.mylang 文件,JS 未突出显示,TODO 突出显示)

您还需要将mylang-js.tmLanguage.json注入source.mylanginjectTo仅适用于顶级范围名称,请参阅说明here):

{
    "scopeName": "source.js.mylang",
    "path": "./syntaxes/mylang-js.tmLanguage.json",
    "injectTo": ["source.js", "source.mylang"]
}

然后它似乎按预期工作:

此外,您必须调整 injectionSelector 以确保 TODO 突出显示仅应用于嵌入式 JS 块中的注释:

"injectionSelector": "L:comment.line.double-slash.js"