0

This syntax region fold consecutive comments:

syn region vimFoldComment
            \ start="^\z(\s*\"\)"
            \ end="\(\z1\)\@!"
            \ transparent fold
            \ contains=ALLBUT, vimFoldComment

This, for example:

" a
" b 
" c

Folds to

+-- 3 lines: a-----

but doesn't work inside functions, if, etc. How will I achieve this?

1
  • 1
    I don't have that region defined in syntax/vim.vim, but if I did I would suspect it needs to be containedin=ALLBUT,.*String (and possibly others)
    – D. Ben Knoble
    Aug 9, 2021 at 17:27

2 Answers 2

0

UPDATE: As pointed out by @D.BenKnoble in comments, the problem seems to be that you're using contains= instead of containedin=.

If you use containedin= instead (more specifically, containedin=ALLBUT,vimFoldComment), to indicate that your vimFoldComment rule can be contained in about any other rule, it starts to work:

syn region vimFoldComment
            \ start="^\z(\s*\"\)"
            \ end="\(\z1\)\@!"
            \ transparent fold
            \ containedin=ALLBUT,vimFoldComment

But see the NOTE below, on how adding new syntax rules for folding can be an issue.


(Original answer:) The syntax definition for Vimscript includes specific rules for the function body and restricts which syntax groups are allowed there through a vimFuncBodyList cluster.

So if you want to use a syntax rule (such as your vimFoldComment) to define folding, you might need to add that rule to the list of rules that can be contained in specific regions, in this case, the vimFuncBodyList cluster, which defines what's allowed inside a function block. You can do that with:

syntax cluster vimFuncBodyList add=vimFoldComment

NOTE: Generally, adding new syntax rules to existing syntax can be pretty tricky, as you may end up running into other situations where the list of contained rules is restricted, you might not always have clusters that are easy to modify. You might also end up making the original rules stop matching where they're supposed to.

You might want to look into using a different mechanism to define folds, such as defining a function for 'foldexpr'.

0
0

For more folding than the standard currently (vim 8.1) provides, see (Fandom) Syntax folding of Vim scripts

It has an explanation and a script to add folding for:

  • for/endfor
  • while/endwhile
  • if/elseif/else/endif
  • try/catch/finally/endtry
  • comments

I recently added this fragment, to provide the comment folding.

" Fold multiple comment lines, except those in a continuation line.
syn region vimFoldComment
        \ start=+\v^\z(\s*")(\\ )@!+
        "\ \z(...) marks ... for later reference (with \z1)
        "\ (\\ )@! excludes continuation comments
        \ skip=+^\z1+
        \ end=+^\v(\z1)@!+
        \ fold
        \ contains=@vimCommentGroup
        \ containedin=ALLBUT,@vimNoFold
syn cluster vimNoFold add=vimFoldComment
hi def link vimFoldComment vimComment

You will have to create a personal addition to the standard vim syntax file. As @filbranden already mentioned, this can create problems.

2
  • Welcome to Vi and Vim! Note that the current version is 8.2+
    – D. Ben Knoble
    Sep 7, 2021 at 12:44
  • That is true; but that is not the version that I tested this on (yet). Sep 7, 2021 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.