2010-03-09 93 views
6

在VIM中摺疊C預處理器是否可能?例如:在VIM中摺疊C預處理器

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

我希望把它摺疊,使之成爲:

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

回答

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 
    
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")