Question

I want to fold function definitions in python code using Vim syntax based folding. So I've added the following to my .vim/syntax/python.vim :

setlocal foldmethod=syntax
syn sync fromstart
syn region  pythonFunctionFostart="^\z(\s*\)\%(def\|class\) " skip="^\s*$" end="^\ze\%(\z1\s\)\@!." fold transparent

This works; it also swallows up empty lines between functions (unlike foldmethod=indent), which I want. However, I don't want the "def function():" line to be folded. As far as I understood the docs I could do the following:

syn region  pythonFunctionFold  start="^\z(\s*\)\%(def\|class\) .*\n\zs." skip="^\s*$" end="^\ze\%(\z1\s\)\@!." fold transparent

But this causes vim to not create any fold at all. (I've tried the start regexp using plain search of course, and it works). Neither works the following:

syn region  pythonFunctionFold  start="^\z(\s*\)\%(def\|class\) .*\n."ms=e skip="^\s*$" end="^\ze\%(\z1\s\)\@!." fold transparent

nor does hs=e, rs=e

Everything I try either includes the def, or doesn't work at all. ( I guess vim doesn't like newlines inside my syntax regexps). Is there some simple way to make a syntax fold start the line after a regexp match?

Edit: I also tried the following:

syn match pythonFunctionFold "^\(\s*\)\%(def\|class\) .*\n\zs\(\s*\n\|\1\s\+.*\n\)\+" fold transparent

When I search for the pattern (using /) it matches exactly the regions I want to fold, and ":help syntax" claims support for multiline matches. But I still get no folding.

Was it helpful?

Solution

I fear this may not be possible. From syntax.txt, under :syn-multi-line:

When using a start pattern with an offset, the start of the match is not allowed to start in a following line. The highlighting can start in a following line though. Using the "\zs" item also requires that the start of the match doesn't move to another line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top