使用Vim,实现仅折叠函数块的PHP代码折叠的最佳方法是什么?

发布于 2024-11-30 16:46:13 字数 370 浏览 4 评论 0原文

我目前正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落在眉间の轻吻 2024-12-07 16:46:13

我通过使用内置的 PHP 插件实现了我所需要的,通过将其放入我的 .vimrc 文件中而不使用任何其他折叠设置来激活。

let php_folding = 1        "Set PHP folding of classes and functions.
let php_htmlInStrings = 1  "Syntax highlight HTML code inside PHP strings.
let php_sql_query = 1      "Syntax highlight SQL code inside PHP strings.
let php_noShortTags = 1    "Disable PHP short tags.

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.

let php_folding = 1        "Set PHP folding of classes and functions.
let php_htmlInStrings = 1  "Syntax highlight HTML code inside PHP strings.
let php_sql_query = 1      "Syntax highlight SQL code inside PHP strings.
let php_noShortTags = 1    "Disable PHP short tags.
傲鸠 2024-12-07 16:46:13

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.

你没皮卡萌 2024-12-07 16:46:13

查看 phpfolding 插件

这比内置的 php_folding 语法要好得多,因为它理解 phpdoc/doxygen docblocks,折叠代码,这样您仍然可以看到函数名称(但隐藏了它前面的文档)。

示例:

screenshot

这有点奇怪,您需要运行 :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:

screenshot

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文