隐藏颜色标签并根据颜色格式化标签内容

Hide color tags and format tag contents in according color

我有一个带有颜色标签的文本文件,例如: foo <color=#c16e0d>bar</color> baz

我希望标签内容在 #c16e0d 中着色并隐藏标签,这样结果是 "foo bar baz",除了 bar 不是粗体,而是在 #c16e0d 中着色。 我试过了

:syn region String start=/<color=#[0-9a-f]*>/ end=/<\/color>/ concealends

这会使标签及其内容变成粉红色,但不会隐藏标签本身,即使我 运行 在 JSON 文件中的命令隐藏工作正常(:set conceallevel=3).

对于着色,我查看了 https://github.com/ap/vim-css-color 的来源,这对于我的简单任务来说似乎过于复杂。 我只能为几种不同的颜色指定单独的语法规则。

我一开始以为我是看bug,但是重读:help :syn-concealends,最后一句意义重大:

The ends of a region can only be concealed separately in this way when they have their own highlighting via "matchgroup"

换句话说,concealends只有在指定matchgroup=...时才会生效;我猜这是因为实施原因;隐藏与某些高亮组相关联。


因此,要修复您的示例,请使用:

:syn region myColorTag matchgroup=String start=/<color=#[0-9a-f]*>/ end=/<\/color>/ concealends

您已经知道以一般方式解析颜色信息的插件。我同意你的看法,在有限的颜色集下,最好只编写单独的语法规则并突出显示组。