我的语法高亮语法注入有什么问题?
What's wrong with my syntax highlighting grammar injection?
我正在关注 this example 在 markdown 中进行简单的语法注入。
{
"fileTypes": [],
"injectionSelector": "L:markup.fenced_code.block.markdown",
"patterns": [
{
"include": "#superjs-code-block"
}
],
"repository": {
"superjs-code-block": {
"begin": "superjs",
"end": "(^|\G)(?=\s*[`~]{3,}\s*$)",
"contentName": "meta.embedded.block.superjs",
"patterns": [
{
"include": "source.js"
}
]
}
},
"scopeName": "markdown.superjs.codeblock"
}
上面的代码有一个小问题——只要有一个字符串superjs
出现在fenced代码块中,剩下的就会一直渲染成superjs,这会破坏fenced代码块的语法突出显示其他嵌入式语言。
{
"foo": "superjs"
}
我想按照 markdown.tmLanguage.json.
来修复它
{
"fileTypes": [],
"injectionSelector": "text.html.markdown",
"patterns": [
{
"include": "#fenced_code_block_superjs"
}
],
"repository": {
"fenced_code_block_superjs": {
"begin": "(^|\G)(\s*)(`{3,}|~{3,})\s*(?i:(superjs)(\s+[^`~]*)?$)",
"beginCaptures": {
"3": {
"name": "punctuation.definition.markdown"
},
"5": {
"name": "fenced_code.block.language"
},
"6": {
"name": "fenced_code.block.language.attributes"
}
},
"end": "(^|\G)(\2|\s{0,3})(\3)\s*$",
"endCaptures": {
"3": {
"name": "punctuation.definition.markdown"
}
},
"name": "markup.fenced_code.block.markdown",
"patterns": [
{
"begin": "(^|\G)(\s*)(.*)",
"contentName": "meta.embedded.block.superjs",
"patterns": [
{
"include": "source.js"
}
],
"while": "(^|\G)(?!\s*([`~]{3,})\s*$)"
}
]
}
},
"scopeName": "markdown.superjs.codeblock"
}
但是还是不行,我也不知道怎么调试
这是一个规则优先级问题。
我必须在 "injectionSelector": "L:text.html.markdown"
中使用 L:
前缀,而不是 documented here:
The L:
in the injection selector means that the injection is added to the left of existing grammar rules. This basically means that our injected grammar's rules will be applied before any existing grammar rules.
我正在关注 this example 在 markdown 中进行简单的语法注入。
{
"fileTypes": [],
"injectionSelector": "L:markup.fenced_code.block.markdown",
"patterns": [
{
"include": "#superjs-code-block"
}
],
"repository": {
"superjs-code-block": {
"begin": "superjs",
"end": "(^|\G)(?=\s*[`~]{3,}\s*$)",
"contentName": "meta.embedded.block.superjs",
"patterns": [
{
"include": "source.js"
}
]
}
},
"scopeName": "markdown.superjs.codeblock"
}
上面的代码有一个小问题——只要有一个字符串superjs
出现在fenced代码块中,剩下的就会一直渲染成superjs,这会破坏fenced代码块的语法突出显示其他嵌入式语言。
{
"foo": "superjs"
}
我想按照 markdown.tmLanguage.json.
来修复它{
"fileTypes": [],
"injectionSelector": "text.html.markdown",
"patterns": [
{
"include": "#fenced_code_block_superjs"
}
],
"repository": {
"fenced_code_block_superjs": {
"begin": "(^|\G)(\s*)(`{3,}|~{3,})\s*(?i:(superjs)(\s+[^`~]*)?$)",
"beginCaptures": {
"3": {
"name": "punctuation.definition.markdown"
},
"5": {
"name": "fenced_code.block.language"
},
"6": {
"name": "fenced_code.block.language.attributes"
}
},
"end": "(^|\G)(\2|\s{0,3})(\3)\s*$",
"endCaptures": {
"3": {
"name": "punctuation.definition.markdown"
}
},
"name": "markup.fenced_code.block.markdown",
"patterns": [
{
"begin": "(^|\G)(\s*)(.*)",
"contentName": "meta.embedded.block.superjs",
"patterns": [
{
"include": "source.js"
}
],
"while": "(^|\G)(?!\s*([`~]{3,})\s*$)"
}
]
}
},
"scopeName": "markdown.superjs.codeblock"
}
但是还是不行,我也不知道怎么调试
这是一个规则优先级问题。
我必须在 "injectionSelector": "L:text.html.markdown"
中使用 L:
前缀,而不是 documented here:
The
L:
in the injection selector means that the injection is added to the left of existing grammar rules. This basically means that our injected grammar's rules will be applied before any existing grammar rules.