在Vim中突出显示嵌套注释折叠的语法

时间:2011-12-09 09:03:38

标签: vim syntax-highlighting folding

在Vim中,我使用标准倍数标记{{{}}}和折叠的命名约定(例如{{{ collection)。一些命名的折叠定义了一个注释(例如{{{ documentation),我希望这些注释能够突出显示。所有折叠都以相同的}}}标记结束。

我在以下方面取得了一些成功:

syn region cComment start="{{{ documentation" end="}}}"
    contains=@cCommentGroup,cCommentStartError,cSpaceError,@Spell fold

但问题是评论的折叠还可以包含通用的collection折叠,如下例所示:

{{{ documentation
    {{{ collection
    // some text
    }}}
    {{{ collection
    // some text
    }}}
}}}}

在这种情况下,评论会在达到第一个}}}时停止,因此第二个collection折叠不会突出显示为评论。

contains选项似乎不相关,因为这会使包含的折叠具有标准突出显示。

我希望评论折叠中的任何折叠都能继承评论语法,而不会影响评论折叠之外的默认语法。

这是否可能在Vim中,因为所有折叠具有相同的终端标记?

2 个答案:

答案 0 :(得分:4)

" Hi. Two syntax regions aren't enough, you have to use a third. There
" are two important things to note. First, 'matchgroup' (see :help
" :syn-matchgroup) prevents contained items from matching in the start
" and end patterns of the containing item. Second 'transparent' (see
" :help :syn-transparent) allows the inheriting of an item's containing
" syntax colouring.
"
" Save this entire text in a file and then :source it to see the effect.
"
"   blah
"   {{{ collection
"       blah
"   }}}
"   blah
"   {{{ documentation
"       {{{ collection
"           blah
"       }}}
"       blah
"       {{{ collection
"           blah
"       }}}
"   }}}
"   blah
"   {{{ collection
"       // some text
"   }}}
"   blah

syn clear

hi documentation guifg=darkcyan ctermfg=darkcyan
hi collection guifg=darkmagenta ctermfg=darkmagenta

syn region genericdoc start="{{{" end="}}}" transparent
syn region collection start="{{{ collection" end="}}}"
syn region documentation matchgroup=documentation
\ start="{{{ documentation" end="}}}" contains=genericdoc

答案 1 :(得分:0)

老实说,我并不完全确定你想要什么,但似乎有办法区分'文档'折叠的结尾和'收集'折叠的结束。只有当找到三个右侧括号作为一行中的第一个字符时,下面的代码才会结束cComment区域:

syn region cComment start="{{{ documentation" end="^}}}"
    contains=@cCommentGroup,cCommentStartError,cSpaceError,@Spell fold

此外,如果您使用标记指定折叠,则语法区域命令中的“折叠”选项不相关。仅当您使用语法指定折叠时,使用折叠和同步区域才有用。