سؤال

هل من الممكن طي P Preprocessor في VIM. علي سبيل المثال:

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

أريد طيها بحيث يصبح:

 +--  4 lines: #if defined(DEBUG)---
هل كانت مفيدة؟

المحلول

هذا غير تافهة نظرا لقيود محرك 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 (أو مسار Vimfiles المكافئ على Windows):

    " 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
    

نصائح أخرى

لقد تمكنت من الحصول عليها من العمل على التروق عن طريق إضافة هذه الأسطر إلى ملف بناء جملة 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")
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top