With foldmethod=syntax, is it possible to create a syntax group that matches my fold markers, so that i can use both language-specific syntax folding and whatever markers might already be in my code?

I tried

syntax region MarkerFold start=/\V{{{/ end=/\V}}}/ fold transparent

But this won't work if there is already a matching syntax highlighting group. The problem still happens if I omit the transparent option.

Am I asking for the impossible? Or is there some way to achieve this in a language-agnostic way (i.e. not writing contains= with every possible vim filetype).

Edit with a Python example:

# Section of Code {{{             <-  Fold at marker
def func1(x):                     <-  Fold at syntax (function def)
    """ This is the docstring     <-  Fold at syntax (docstring)

    Here is documentation.
    More docs.
    print('This is func1')
    return x+1
# }}}
  • 1
    maybe with a custom fold expression that tests for markers and syntax-fold elements? – D. Ben Knoble Nov 18 '20 at 21:07
  • Does transparent work here? The example under :help :syn-fold uses it too... But yeah, syntax rules can be tricky, there's often too much interference when you try to add or remove something, so it's not always easy to add a single rule and keep everything else working as it did before the addition. – filbranden Nov 18 '20 at 23:05
  • 1
    @filbranden I tried transparent and it has the same result. – shadowtalker Nov 19 '20 at 14:09
  • @D.BenKnoble how would I implement such a thing, without reimplementing Vim's entire syntax folding mechanism in Vimscript? Or is it not so complicated to do? – shadowtalker Nov 19 '20 at 16:38
  • 2
    I'm not certain you can avoid it, and I have no idea how complex it is. There are some syntax functions that would let you extract the info (I hope), and the algorithm is "simple" (:help :syn-fold, :help fold-syntax, :help syntax-functions) – D. Ben Knoble Nov 19 '20 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.