gpt4 book ai didi

visual-studio-code - 如何在自定义 VSCode 语言中引用注入(inject)的语法

转载 作者:行者123 更新时间:2023-12-04 15:34:05 24 4
gpt4 key购买 nike

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

我可以在原生 VSCode 语言中引用注入(inject)的语法

指定这个(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)

enter image description here

到现在为止还挺好。

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

更新,现在,这个(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 未突出显示)

enter image description here

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

如果我切换 "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 突出显示)

enter image description here

最佳答案

您需要注入(inject)mylang-js.tmLanguage.json进入 source.mylang同样( injectTo 仅适用于顶级范围名称,请参阅说明 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"

关于visual-studio-code - 如何在自定义 VSCode 语言中引用注入(inject)的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60384141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com