2011-12-09 83 views
3

在Vim中,我使用標準摺疊標記{{{,}}}以及摺疊命名約定(例如{{{ collection)。一些命名的摺疊定義了一個評論(例如{{{ documentation),我希望這些突出顯示。所有摺疊以相同的}}}標記結束。Vim中嵌套註釋摺疊的語法高亮

我有一些成功與以下:

syn region cComment start="{{{ documentation" end="}}}" 
    [email protected],cCommentStartError,cSpaceError,@Spell fold 

但問題是,評論褶皺也可以包含通用collection褶皺,如下面的例子:

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

在這種情況下, ,當到達第一個}}}時,註釋停止,因此第二個collection摺疊未作爲註釋突出顯示。

contains選項似乎並不相關,因爲這會使摺疊包含標準突出顯示。

我希望評論中的任何摺疊可以繼承評論語法,而不會影響評論摺疊之外的默認語法。

鑑於所有褶皺都有相同的endmarkers,這可能在Vim中嗎?

回答

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 
+0

棒極了! 「透明」設置正是我所需要的!非常感謝。 –

0

我真的不完全確定你想要什麼,但似乎有一種方法可以區分'文檔'摺疊的結束和'收集'摺疊的結束。下面的代碼將結束cComment區域只有當三個權brackes被發現的第一個字符的行:

syn region cComment start="{{{ documentation" end="^}}}" 
    [email protected],cCommentStartError,cSpaceError,@Spell fold 

此外,「折」在你的語法區域命令選項,如果你正在使用的標記是不相關的指定摺疊。只有在使用語法來指定摺疊時,使用摺疊和同義區域纔是有用的,而不是。

+0

感謝您的建議。這肯定會適用於我給出的例子,但我不想依賴嵌套摺疊的縮進級別。 'contains'選項幾乎是我想要的 - 它允許Vim忽略文檔摺疊內匹配摺疊的最終摺疊標記,但是它們不再被評論。 –