6

是否可以在 VIM 中折叠 C 预处理器。例如:

#if defined(DEBUG)
  //some block of code
  myfunction();
#endif

我想把它折叠成:

 +--  4 lines: #if defined(DEBUG)---
4

2 回答 2

1

由于 Vim 高亮引擎的限制,这并非易事:它不能很好地高亮重叠区域。在我看来,您有两个选择:

  1. 使用语法高亮和contains=选项,直到它适合你(可能取决于一些插件):

    syn region cMyFold start="#if" end="#end" transparent fold contains=ALL
    " OR
    syn region cMyFold start="#if" end="#end" transparent fold contains=ALLBUT,cCppSkip
    " OR something else along those lines
    " Use syntax folding
    set foldmethod=syntax
    

    这可能会花费很多时间,而且您可能永远无法令人满意地工作。把它放在vimfiles/after/syntax/c.vimor中~/.vim/after/syntax/c.vim

  2. 使用折叠标记。这会起作用,但是您将无法折叠大括号或您可能喜欢的任何其他东西。将其放入~/.vim/after/ftplugin/c.vim(或 Windows 上等效的 vimfiles 路径):

    " This function customises what is displayed on the folded line:
    set foldtext=MyFoldText()
    function! MyFoldText()
        let line = getline(v:foldstart)
        let linecount = v:foldend + 1 - v:foldstart
        let plural = ""
        if linecount != 1
            let plural = "s"
        endif
        let foldtext = printf(" +%s %d line%s: %s", v:folddashes, linecount, plural, line)
        return foldtext
    endfunction
    " This is the line that works the magic
    set foldmarker=#if,#endif
    set foldmethod=marker
    
于 2010-03-09T12:30:41.243 回答
0

通过将这些行添加到我的 c.vim 语法文件中,我已经能够让它按照我的喜好工作:

syn match   cPreConditMatch display "^\s*\zs\(%:\|#\)\s*\(else\|endif\)\>"

+syn region cCppIfAnyWrapper  start="^\(%:\|#\)\s*\(if\|ifdef\|ifndef\|elif\)\s\+.*\s*\($\|//\|/\*\|&\)" end="^\s*\(%:\|#\)\s*endif\>" contains=TOP,cCppInIfAny,cCppInElseAny fold
+syn region cCppInIfAny  start="^\(%:\|#\)\s*\(if\|ifdef\|ifndef\|elif\)\s\+.*\s*\($\|//\|/\*\|&\)" end="^\s*\(%:\|#\)\s*\(else\s*\|elif\s\+\|endif\)\>"me=s-1 containedin=cCppIfAnyWrapper contains=TOP
+syn region cCppInElseAny  start="^\s*\(%:\|#\)\s*\(else\|elif\)" end="^\s*\(%:\|#\)\s*endif\>"me=s-1 containedin=cCppIfAnyWrapper contains=TOP

if !exists("c_no_if0")
于 2017-12-15T03:09:16.263 回答