Vim: How to start the syntax-fold on a line after matching a regular expression? (python functions)

I want to collapse functional definitions in python code using a framework based on Vim syntax. So I 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 

It works; it also absorbs blank lines between functions (unlike foldmethod = indent) that I want. However, I do not want the string def def (): "to be collapsed. 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 forces vim to not create any fouls at all. (I tried to run the regex using simple search and it works). None of them work:

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

and hs = e, rs = e

Everything that I try either includes def or does not work at all. (I think vim doesn't like newlines inside my regexps syntax). Is there an easy way to make the syntax reset the beginning of a line after matching a regular expression?

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 a template (using /), it exactly matches the regions I want to reset, and " :help syntax " claims support for multi-line matches. But I'm still not folding.

+4
source share
1 answer

I am afraid that it will be impossible. 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 on the next line. Selection may begin with the next line. Using the \ zs element also requires that the beginning of the match does not move to another line.

+1
source

Source: https://habr.com/ru/post/1388809/


All Articles