Using Vim, what is the best way to implement PHP code folding that only folds functions blocks?

£可爱£侵袭症+ 提交于 2019-12-11 03:13:04

问题


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?


回答1:


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.




回答2:


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.



回答3:


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)



来源:https://stackoverflow.com/questions/7149063/using-vim-what-is-the-best-way-to-implement-php-code-folding-that-only-folds-fu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!