sponsor Vim development Vim logo Vim Book Ad

phpfolding.vim : Automatic folding of PHP functions, classes,.. (also folds related PhpDoc)

 script karma  Rating 164/63, Downloaded by 9577  Comments, bugs, improvements  Vim wiki

created by
Ray Burgemeestre
 
script type
utility
 
description
This script can fold PHP functions and/or classes, properties with their phpdoc
without manually adding marker style folds ({{{ and }}}).
It will generate the following folds when executed:

<?php
/**
* This is Foo...
* @author Foo
*/
class Foo
{
+-- 11 lines: function foo($bar) ** -------------------------------------------------
+--  8 lines: function bar($bar) ** -------------------------------------------------
+-- 24 lines: function baz($bar) ----------------------------------------------------
}
?>

Based on e.g. functions declared like this:

<?php
/**
* This is fooFunction...
*
* @param mixed $bar
* @access public
* @return void
*/
function fooFunction($bar)
{
    ...
}
?>

SCREENSHOT
You can view a screenshot here: http://blog.cppse.nl/phpfolding-vim

FEATURES
- It remembers fold settings. If you add functions and execute the script again,
  your opened folds will not be closed.
- It will not be confused by brackets in comment blocks or string literals.
- The folding of class properties with their PhpDoc comments.
- The folding of all class properties into one fold.
- Folding the original marker style folds too.
- An "**" postfixing the fold indicates PhpDoc is inside (configurable).
- An "**#@+" postfixing the fold indicates PhpDocBlock is inside (configurable).
- Empty lines postfixing the folds can be configured to be included in the fold.
- Nested folds are supported (functions inside functions, etc.)
- Folding private, public, protected class variables + multi-line param.
- Foloding the class is now an option disabled by default.
- Now works properly as ftplugin

FUTURE
- Better 'configurability' as opposed to editting the PHPCustomFolds() function
  and some "Script configuration" global variables.

CONTRIBUTE / GITHUB
This project is hosted on github as a mirror through
    https://github.com/vim-scripts/phpfolding.vim
I do not own vim-scripts, so please fork mine instead:
    https://github.com/rayburgemeestre/phpfolding.vim
It's then easier for me to accept pull requests and upload new version(s) here,
also I cannot put phpfolding.vim in the ftplugin/php directory through the
vim.org interface.


COMPATIBILITY
This script is tested successfully with Vim version >= 6.3 on windows and linux
(With 6.0 it works *sometimes*, I don't recommend using it in that version)
 
install details
INSTALL
1. Put phpfolding.vim in your plugin directory (~/.vim/ftplugin/php/)

      Make sure you have "filetype plugin on" in your .vimrc!

      (folds will then be created after opening a file is recognized as php.)

2. Alternatively, if you want to control loading manually, you might want to
   add the following keyboard mappings to your .vimrc:

      map <F5> <Esc>:EnableFastPHPFolds<Cr>
      map <F6> <Esc>:EnablePHPFolds<Cr>
      map <F7> <Esc>:DisablePHPFolds<Cr>

It might be necessary that you load the plugin from your .vimrc, i.e.:
   let php_folding=0
     (if you can't use the after directory in step 3)
   source ~/path/to/phpfolding.vim
     (if you're not using the default plugin directory)

KNOWN ISSUES
1. C++ style commented brackets can still interfere with the bracket matching.
For example comments like are not recognized as comments:  // old: for (...) {
Whereas C-style comments are, e.g.:  /* old: for (...) { */
(Edit: not sure if this is an issue anymore..)
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
phpfolding.vim 2.3 2021-08-14 6.0 Ray Burgemeestre Thanks to Michael Härtl (mikehaertl):
- Fix detection of static and visibility keywords (can now appear in any order)
- Fix detection of functions and abstract functions properly
(https://github.com/rayburgemeestre/phpfolding.vim/pull/8)
phpfolding.vim 2.2 2021-03-19 6.0 Ray Burgemeestre 1. No longer :redraw after folds are being created by default.

There is a setting that can be modified to enable it back, however, on my machine with vim 8.1.2269, whenever I exited vim I would notice tildes (~) all over the place, likely caused by the script executing :redraw during initialization.

If vim version 6 or 7 has drawing issues let me know and I'll make it conditional based on the vim version being used.
phpfolding.vim 2.1 2018-07-10 6.0 Ray Burgemeestre Thanks to a bunch people:
- Fix camelCase names and abstract functions (PR#7) (sutem)
- Fix: Fixed function folding (PR#5) César D. Rodas (crodas)
- Remove echo for fold(s) created (Glendon Solsberry (gms8994))
- The 'function' pattern will match function calls like 'register_shutdown_function' (Fix by blu3gui7ar)
- Changed line-endings from CRLF -> LF (Sudaraka Wijesinghe (sudaraka))
- Move phpfolding.vim to ftplugin/php/phpfolding.vim (github only change) (Tak Nishigori (nishigori))
- PHP5-aware(ish) class member & multi-line parameter folding (Jess Telford)
phpfolding.vim 2.0 2016-05-19 6.0 Ray Burgemeestre Thanks to 吕海涛 (lvht):
- Support for folding use statements
Thanks to Mikkel Paulson:
- Support for folding consts into their docblocks
phpfolding.vim 1.9 2014-06-01 6.0 Ray Burgemeestre Thanks to Yuki Matsukura (matsubo):
1. Line ending fixed
2. README now in Markdown
Thanks to Jan Mollowitz (phux):
- Expanded regex to match optional static declared class properties.
phpfolding.vim 1.8 2013-08-25 6.0 Ray Burgemeestre Thanks to Dmitri Perunov for informing me of the fixes pending on github!
1. Fix for folding private, public, protected class variables + multi-line param by Jess Telford
2. Fix to make it work well as ftplugin (default now), added option for folding classes (disabled by default) and fix for fatal behavior, all by pekepeke
phpfolding.vim 1.7 2010-01-16 6.0 Ray Burgemeestre The following much appreciated contributions are from LiTWoL (Oleg Terenchuk):
1. Check if filetype is PHP before automatically creating folds
2. Call EnableFastPHPFolds after reading file into the buffer
3. Avoid reload check
4. Added configuration variable to disable autofolding.
phpfolding.vim 1.6 2006-08-26 6.0 Ray Burgemeestre 1. Added nested fold support (recursion).
2. Improved install instructions. Thanks to Peter Hodge for some useful information.
3. Added phpdoc "docblock" support (/**#@+ and #@- templates).
4. Minor adjustments in FindPureBlockStart and FindPatternStart.
phpfolding.vim 1.5 2006-08-16 6.0 Ray Burgemeestre 1. Previously opened folds are now remembered again. I accidentally broke this in another version.
2. The script now can fold abstract classes too. Thanks to Stefan Walk for adjusting the regex.
3. Changed behaviour and fixed a bug in searching for trailing empty lines.
4. Correct folding of nested folds will be supported in the *next* version.
5. Changed some default settings.
phpfolding.vim 1.4.1 2006-08-08 6.0 Ray Burgemeestre 1. Removed windows carriage returns (^M) throughout the file.
phpfolding.vim 1.4 2006-08-06 6.0 Ray Burgemeestre 1. The sequence of creating the custom folds no longer matters.
2. Added some more custom folds (includes, defines, .. with phpdoc can now be folded too).
3. It can search farther for PhpDoc comments.
4. It can be configured to fold more empty 'trailing' lines.
5. Adjusted PHPFoldText a bit.
6. Fixed a minor bug in the class fold regex (PEAR's QuickForm.php works now too).
phpfolding.vim 1.3 2006-07-30 6.0 Ray Burgemeestre 1. Extensive/slow parsing of brackets is now optional.
2. Class variables "var $foo = '';" can be folded with their PhpDoc.
3. Marker style folds ({{{, }}}) can be folded now too.
phpfolding.vim 1.2 2006-07-30 6.0 Ray Burgemeestre 1. Previously opened folds are restored when executing the script multiple times (thanks to Marijn Koesen for that idea)
2. The cursor position is now remembered and restored in Linux
3. Cleaned up foldtext a bit
phpfolding.vim 1.1 2006-07-29 6.0 Ray Burgemeestre 1. PHP API comments no longer appear in the fold names.
2. The following function header is now recognized:

function (...,
    ....)
{
phpfolding.vim 1.0 2006-07-29 6.0 Ray Burgemeestre Initial upload
ip used for rating: 54.224.52.210

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github