Vim: 如何在匹配正则表达式后的行上开始一次语法折叠?(Python函数)

2024-07-07 09:21:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在python代码中使用基于Vim语法的折叠来折叠函数定义。所以我在my.vim/语法中添加了以下内容/Python.vim公司名称:

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

这是可行的;它还吸收函数之间的空行(不像foldmethod=indent),这是我想要的。但是,我不希望“def function():”行被折叠。据我所知,我可以做以下工作:

^{pr2}$

但这导致维姆根本没有创造任何褶皱。(当然,我已经尝试过使用纯搜索的start regexp,而且很有效)。这两种方法都不起作用:

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

hs=e,rs=e也不一样

我所尝试的一切要么包括def,要么根本不起作用。(我猜vim不喜欢我的语法regexps中的新行)。有没有一些简单的方法可以在regexp匹配之后的行开始语法折叠?在

编辑:我也尝试了以下方法:

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

当我搜索模式(使用/)时,它完全匹配我要折叠的区域,“:help syntax”声明支持多行匹配。但我还是没法折叠。在


Tags: 方法函数def语法foldvimregionclass
1条回答
网友
1楼 · 发布于 2024-07-07 09:21:07

恐怕这不可能。从syntax.txt,在: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.

相关问题 更多 >