我正在研究自定义语法高亮显示,并从 HTML 语法中复制了一些片段,因为这篇文章非常相似。我一直在尝试弄清楚如何突出显示标签内的文本。这是我的定义:
- match: "(</?)([a-zA-Z0-9:]+)"
captures:
1: punctuation.definition.tag.begin.html
2: entity.name.tag.other.html
push:
- meta_scope: meta.tag.other.html
- match: '>'
scope: punctuation.definition.tag.end.html
pop: true
- match: '.'
scope: string.quoted.single.html
示例文本:
<file bash>
Some bash code block
Some bash code block
</file>
我的代码突出显示了括号 <>
, file
和 bash
关键字,但我不知道如何为里面的 block 添加颜色。最终我想把它作为 block 评论或类似的东西,这样它就会脱颖而出。有什么建议吗?
我需要一个解决方案来避免为没有结束标记的标记添加注释突出显示。例如,我正在使用的标记中有某些标签不使用关闭,例如 <tag without close>
没有 </tag>
.有什么方法可以在正则表达式中添加排除项,使其仅在存在打开和关闭标记时才起作用,但在只有打开标记时不起作用?
<tag without close>
This should not be a comment.
<file bash>
This should be a comment.
</file>
This also should not be a comment.
只会使用一小部分标签,例如 <tag>
以上,主要用于元数据。
最佳答案
一种方式,松散地基于 interiors of both <style>
and <script>
are managed 的方式, 是使用 with_prototype
它有一个 pop
.
- match: '(?:^\s+)?(<)([a-zA-Z0-9:]+)\b(?![^>]*/>)'
captures:
1: punctuation.definition.tag.begin.html
2: entity.name.tag.other.html
push:
- match: '(</)(\2)(>)'
captures:
1: punctuation.definition.tag.begin.html
2: entity.name.tag.other.html
3: punctuation.definition.tag.end.html
pop: true
- match: '>'
scope: punctuation.definition.tag.end.html
push:
- match: '.'
scope: comment.block.html
with_prototype:
- match: (?=</\2)
pop: true
- include: main
- match: '.'
scope: string.quoted.single.html
请注意 ([a-zA-Z0-9:]+)
正如您在问题中所提到的,这里匹配任何有效的标签名称,并且 \2
用于稍后匹配该组,两者都在立即解耦match
条件和 with_prototype
健康)状况。 with_protoype
定义应用于当前上下文中所有内容的模式,因此我们在这里使用它来确保我们 pop
一旦我们到达 </file>
,评论式的高亮显示,而不是被视为评论的一部分。
在with_prototype
内, - include: main
声明确保您的评论内容中的任何标签都像外部 <file>
一样标签。例如,<hello>
下面的行为与 <file>
相同.
<file bash>
Some bash code block
<hello stuff>
Some bash code block
</hello>
Some bash code block
</file>
如果您的标签没有匹配的结束标签,您可以通过为堆栈更高层的标签定义特定行为来覆盖此行为,如下所示:
- match: '(?:^\s+)?(<)(hello)\b(?![^>]*/>)'
captures:
1: punctuation.definition.tag.begin.html
2: entity.name.tag.other.html
push:
- match: '>'
scope: punctuation.definition.tag.end.html
pop: true
- match: '.'
scope: string.quoted.single.html
前提是早于 match: '(?:^\s+)?(<)([a-zA-Z0-9:]+)\b(?![^>]*/>)'
线,任何<hello>
标签不会调用评论式突出显示。
此文本不是 comment.block.html。 一些 bash 代码块 一些 bash 代码块 一些 bash 代码块
关于sublimetext3 - Sublime Text 自定义语法高亮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39879737/