Vim 自定义语法,不包括区域中的先前匹配突出显示

Vim custom syntax, excluding previous match highlighting in a region

我正在为一个文件编写一个 vim 语法高亮脚本,该脚本使用 * 表示注释的开始,但被 {} 包围时除外。即

* This is a comment, bellow is math
{ x_variable * y_variable + 10.0 } 

我想只高亮括号,忽略里面的注释高亮,同时仍然保持数字高亮。

到目前为止我有:

syn match mathSym "[{}]"
syn region mathRegion start=+{+ send=+}+ contains=numberHi
syn match commentHi "\*.*$" display contains=@Spell

hi link commentHi Comment
hi link mathSym Statement
hi link mathRegion Normal

我不确定这样做是否正确。它似乎忽略了 * 作为注释,并提供数字突出显示,但没有突出显示括号。

我试过了

region mathRegion start=+{+ send=+}+ contains=numberHi, mathSym

但这最终会将文件中的所有突出显示设置为 Normal

您的问题缺少 numberHi 内容,但这应该可以解决问题:

syn region mathRegion matchgroup=mathSym start=+{+ end=+}+ contains=numberHi,mathSym
syn match commentHi "\*.*$" display contains=@Spell

hi link commentHi Comment
hi link mathSym Statement
  • 您可以使用 matchgroup=mathSym 突出显示开始和结束,而不是单独匹配 mathSym
  • 此外,您不需要 link mathRegionNormal;仅用于结构。
  • x * y 不会在 mathRegion 中匹配,因为它不包含在此处。我没有发现任何问题。

PS:你知道SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor插件吗?对写语法很有帮助