使用Vim,实现仅折叠函数块的PHP代码折叠的最佳方法是什么?
我目前正在使用 Vim 编辑 PHP 文件,并且只想实现函数的代码折叠。我尝试在 .vimrc 文件中设置 foldmethod=expr
并使用带有 foldexpr
的正则表达式。问题是我不完全理解 foldexpr
如何使用正则表达式将折叠应用于源代码,而且我似乎无法正确理解。
基本上我希望所有 PHP 函数(也在类内部)被折叠,仅此而已。我得到的最接近的是:
set foldexpr=getline(v:lnum-1)=~'function'?'>1':'='
但这不对,我想看看我是否可以用花括号变得更聪明一点。
有什么想法吗?
I'm currently using Vim to edit PHP files and would like to implement code folding for functions only. I've tried setting the foldmethod=expr
and using a regex with foldexpr
in my .vimrc file. The problem is i don't fully understand how foldexpr
is using the regex to apply folding to the source code and i can't seem to get it right.
Basically i want all PHP functions (inside classes too) to be folded and that's it. The nearest i've got is:
set foldexpr=getline(v:lnum-1)=~'function'?'>1':'='
but it's not right and i want to see if i can be a little more intelligent with the curly braces.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过使用内置的 PHP 插件实现了我所需要的,通过将其放入我的 .vimrc 文件中而不使用任何其他折叠设置来激活。
I achieved what i needed by using the built-in PHP plugin, activated by putting this in my .vimrc file and not using any other folding settings.
IIRC 折叠不适用于正则表达式,因为它会减慢 vim 的速度。您可能会通过使用foldmethod=indent 并设置foldnestmax 来限制创建的嵌套折叠数量来获得所需的结果。
IIRC folding do not work with regexes because it will slow vim down. You might get what you want by using foldmethod=indent and set foldnestmax to limit the number of nested folds created.
查看 phpfolding 插件。
这比内置的 php_folding 语法要好得多,因为它理解 phpdoc/doxygen docblocks,折叠代码,这样您仍然可以看到函数名称(但隐藏了它前面的文档)。
示例:
这有点奇怪,您需要运行
:EnablePHPFolds
来更新内容,当我启动 Vim 时,我收到一条奇怪的消息(但这可能是因为我用 Vundle 安装了它),但它是一件很美妙的事情!(也可参考https://stackoverflow.com/a/11859632/623519)
Check out the phpfolding plugin.
This is much better than the built-in
php_folding
syntax stuff because it understands phpdoc/doxygen docblocks, folds the code so you can still see the function name (but hides the documentation which precedes it).Example:
It is a bit weird, you need to run
:EnablePHPFolds
to update stuff, and I get an odd message when I start up Vim (but that might be because I've installed it with Vundle), but it's a thing of beauty!(Also referenced at https://stackoverflow.com/a/11859632/623519)