【问题标题】:Fold C Preprocessor in VIM在 VIM 中折叠 C 预处理器
【发布时间】:2011-01-25 08:40:00
【问题描述】:

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

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

我想把它折叠成:

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

【问题讨论】:

    标签: vim folding


    【解决方案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.vim~/.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
      

    【讨论】:

      【解决方案2】:

      通过将这些行添加到我的 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")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-22
        • 1970-01-01
        • 1970-01-01
        • 2011-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-16
        相关资源
        最近更新 更多