Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 61 additions & 30 deletions README
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
This is a mirror of http://www.vim.org/scripts/script.php?script_id=1623

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:
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 fooFunction($bar) ** -----------------------------------------
+-- 8 lines: function fooFunction2($bar) ** ----------------------------------------
+-- 24 lines: function fooFunction3($bar) -------------------------------------------
* This is Foo...
* @author Foo
*/
class Foo
{
+-- 11 lines: function foo($bar) ** -------------------------------------------------
+-- 8 lines: function bar($bar) ** -------------------------------------------------
+-- 24 lines: function baz($bar) ----------------------------------------------------
}

+--112 lines: class Foo2 ** -------------------------------------------------------------
?>

Based on e.g. functions declared like this:

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

SCREENSHOT
You can view a screenshot here: http://www.fighterz.net/trig/folding.gif
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,
- 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.
Expand All @@ -47,19 +47,50 @@ FEATURES
- 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.
- Better 'configurability' as opposed to editting the PHPCustomFolds() function
and some "Script configuration" global variables.

NOTE
If anyone has emailed me and I have not replied, it's probably lost. I just found out
hotmail recognizes alot as junk. I now turned off the junk filter..
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 preferably fork mine:
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.

NOTE2:
I'm currently more active again with this project, so if you have any contributions to
this project, please let me know.

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
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..)
110 changes: 66 additions & 44 deletions plugin/phpfolding.vim → ftplugin/php/phpfolding.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
" Plugin for automatic folding of PHP functions (also folds related PHPdoc)
"
" Maintainer: Ray Burgemeestre
" Last Change: 2010 Jan 15
" Last Change: 2013 Aug 26
"
" USAGE
" If you enabled the script in your after/ftplugin directory (see install)
Expand All @@ -17,7 +17,10 @@
" F7 - To remove all folds.
"
" INSTALL
" 1. Put phpfolding.vim in your plugin directory (~/.vim/plugin)
" 1. Put phpfolding.vim in your plugin directory (~/.vim/ftplugin/php/)
"
" make sure you have "filetype plugin on" in your .vimrc!
"
" 2. You might want to add the following keyboard mappings to your .vimrc:
"
" map <F5> <Esc>:EnableFastPHPFolds<Cr>
Expand All @@ -28,9 +31,9 @@
"
" let g:DisableAutoPHPFolding = 1
"
" By default EnableFastPHPFolds is called. Do these mess up your folds,
" By default EnableFastPHPFolds is called. Do these mess up your folds,
" you can try to replace EnableFastPHPFolds by EnablePHPFolds. You can
" change this in function s:CheckAutocmdEnablePHPFold.
" change this in function s:CheckAutocmdEnablePHPFold.
"
" NOTE
" It may be that you need to load the plugin from your .vimrc manually, in
Expand All @@ -51,9 +54,18 @@
" at the "Script configuration" part.
"
" This script is tested with Vim version >= 6.3 on windows and linux.
let s:save_cpo = &cpo
set cpo&vim

" Avoid reloading {{{1
if exists('loaded_phpfolding')
" ftplugin section
if !get(g:, 'DisableAutoPHPFolding', 0)
call s:EnableFastPHPFolds()
endif

let &cpo = s:save_cpo
unlet s:save_cpo
finish
endif

Expand All @@ -64,6 +76,7 @@ let loaded_phpfolding = 1
if !exists("g:DisableAutoPHPFolding")
let g:DisableAutoPHPFolding = 0
endif
let g:DisablePHPFoldingClass = get(g:, 'DisablePHPFoldingClass', 1)
" }}}

command! EnableFastPHPFolds call <SID>EnableFastPHPFolds()
Expand Down Expand Up @@ -109,8 +122,8 @@ function! s:EnablePHPFolds(...) " {{{
let s:savedCursor = line(".")

" Initialize variables
set foldmethod=manual
set foldtext=PHPFoldText()
setlocal foldmethod=manual
setlocal foldtext=PHPFoldText()
let s:openFoldListItems = 0
let s:fileLineCount = line('$')

Expand All @@ -119,26 +132,26 @@ function! s:EnablePHPFolds(...) " {{{


" Move to end of file
exec s:fileLineCount
exec s:fileLineCount

" First pass: Look for Folds, remember opened folds
let s:foldingMode = s:MODE_REMEMBER_FOLD_SETTINGS
call s:PHPCustomFolds()

" Second pass: Recreate Folds, restore previously opened
let s:foldingMode = s:MODE_CREATE_FOLDS
" .. Remove all folds first
normal zE
normal! zE
let s:foldsCreated = 0
call s:PHPCustomFolds()
" .. Fold all
normal zM
normal! zM

" Restore previously opened folds
let currentItem = 0
while currentItem < s:openFoldListItems
exec s:foldsOpenedList{currentItem}
normal zo
normal! zo
let currentItem = currentItem + 1
endwhile

Expand All @@ -147,13 +160,13 @@ function! s:EnablePHPFolds(...) " {{{

" Restore cursor
exec s:savedCursor

endfunction
" }}}
function! s:DisablePHPFolds() " {{{
"set foldmethod=manual
set foldtext=
normal zE
"setlocal foldmethod=manual
setlocal foldtext=
normal! zE
echo "php fold(s) deleted"
endfunction
" }}}
Expand All @@ -166,11 +179,13 @@ function! s:PHPCustomFolds() " {{{
call s:PHPFoldPureBlock('function', s:FOLD_WITH_PHPDOC)

" Fold class properties with PhpDoc (var $foo = NULL;)
call s:PHPFoldProperties('^\s*var\s\$', ";", s:FOLD_WITH_PHPDOC, 1, 1)
call s:PHPFoldProperties('^\s*\(\(private\)\|\(public\)\|\(protected\)\|\(var\)\)\s\$', ";", s:FOLD_WITH_PHPDOC, 1, 1)

if !g:DisablePHPFoldingClass
" Fold class without PhpDoc (class foo {})
call s:PHPFoldPureBlock('^\s*\(abstract\s*\)\?class', s:FOLD_WITH_PHPDOC)
endif

" Fold class without PhpDoc (class foo {})
call s:PHPFoldPureBlock('^\s*\(abstract\s*\)\?class', s:FOLD_WITH_PHPDOC)

" Fold define()'s with their PhpDoc
call s:PHPFoldProperties('^\s*define\s*(', ";", s:FOLD_WITH_PHPDOC)

Expand Down Expand Up @@ -237,7 +252,7 @@ function! s:PHPFoldPureBlock(startPattern, ...) " {{{

if s:foldingMode != s:MODE_REMEMBER_FOLD_SETTINGS
" Remove created folds
normal zR
normal! zR
endif
endfunction
" }}}
Expand Down Expand Up @@ -275,7 +290,7 @@ function! s:PHPFoldMarkers(startPattern, endPattern, ...) " {{{

if s:foldingMode != s:MODE_REMEMBER_FOLD_SETTINGS
" Remove created folds
normal zR
normal! zR
endif
endfunction
" }}}
Expand Down Expand Up @@ -320,12 +335,12 @@ function! s:PHPFoldProperties(startPattern, endPattern, ...) " {{{

" Goto fold start (remember we're searching upwards)
exec s:lineStart

endwhile

if s:foldingMode != s:MODE_REMEMBER_FOLD_SETTINGS
" Remove created folds
normal zR
normal! zR
endif
endfunction
" }}}
Expand All @@ -351,7 +366,7 @@ function! s:HandleFold() " {{{
let s:foldsOpenedList{s:openFoldListItems} = s:lineStart
let s:openFoldListItems = s:openFoldListItems + 1
endif

elseif s:foldingMode == s:MODE_CREATE_FOLDS
" Correct lineStop if needed (the script might have mistaken lines
" beyond the file's scope for trailing empty lines)
Expand Down Expand Up @@ -380,9 +395,9 @@ function! s:FindPureBlockStart(startPattern) " {{{
" This function can match the line its on *again* if the cursor was
" restored.. hence we search twice if needed..
let currentLine = line('.')
let line = search(a:startPattern . '.*\%[\n].*\%[\n].*{', 'bW')
let line = search(a:startPattern . '.*\(\%[\n].*\)\{,10\}{', 'bW')
if currentLine == line
let line = search(a:startPattern . '.*\%[\n].*\%[\n].*{', 'bW')
let line = search(a:startPattern . '.*\(\%[\n].*\)\{,10\}{', 'bW')
endif
return line
endfunction
Expand Down Expand Up @@ -478,7 +493,7 @@ function! s:FindPureBlockEnd(startPair, endPair, searchStartPairFirst, ...) " {{
" Then be greedy with extra 'trailing' empty line(s)
let s:counter = 0
while s:counter < s:searchEmptyLinesPostfixing
let linestr = getline(line + 1)
let linestr = getline(line + 1)
if (matchstr(linestr, '^\s*$') == linestr)
let line = line + 1
endif
Expand All @@ -497,7 +512,7 @@ function! s:FindPatternEnd(endPattern) " {{{
" Then be greedy with extra 'trailing' empty line(s)
let s:counter = 0
while s:counter < s:searchEmptyLinesPostfixing
let linestr = getline(line + 1)
let linestr = getline(line + 1)
if (matchstr(linestr, '^\s*$') == linestr)
let line = line + 1
endif
Expand Down Expand Up @@ -553,7 +568,7 @@ function! PHPFoldText() " {{{
endwhile
let lineString = getline(currentLine)
endif

" Some common replaces...
" if currentLine != v:foldend
let lineString = substitute(lineString, '/\*\|\*/\d\=', '', 'g')
Expand All @@ -572,7 +587,7 @@ function! PHPFoldText() " {{{
" Append an (a) if there is PhpDoc in the fold (a for API)
if currentLine != v:foldstart
let lineString = lineString . " " . g:phpDocIncludedPostfix . " "
endif
endif

" Return the foldtext
return "+--".lines." lines: " . lineString
Expand All @@ -593,19 +608,26 @@ function! SkipMatch() " {{{
endfun
" }}}

" Check filetype == php before automatically creating (fast) folds {{{1
function! s:CheckAutocmdEnablePHPFold()
if &filetype == "php" && ! g:DisableAutoPHPFolding
call s:EnableFastPHPFolds()
endif
endfunction
" }}}

" Call CheckAutocmdEnablePHPFold on BufReadPost {{{1
augroup SetPhpFolds
au!
au BufReadPost * call s:CheckAutocmdEnablePHPFold()
augroup END
" }}}
" " Check filetype == php before automatically creating (fast) folds {{{1
" function! s:CheckAutocmdEnablePHPFold()
" if &filetype == "php" && ! g:DisableAutoPHPFolding
" call s:EnableFastPHPFolds()
" endif
" endfunction
" " }}}

" " Call CheckAutocmdEnablePHPFold on BufReadPost {{{1
" augroup SetPhpFolds
" au!
" au BufReadPost * call s:CheckAutocmdEnablePHPFold()
" augroup END
" " }}}

" ftplugin section
if !get(g:, 'DisableAutoPHPFolding', 0)
call s:EnableFastPHPFolds()
endif

let &cpo = s:save_cpo
unlet s:save_cpo
" vim:ft=vim:foldmethod=marker:nowrap:tabstop=4:shiftwidth=4