Skip to content

Commit

Permalink
- Prepare comment stripped version for inclusion in VIm RT files.
Browse files Browse the repository at this point in the history
- Update version reported by GetPhpIndentVersion()
  • Loading branch information
2072 committed Jul 21, 2019
1 parent 7e08d60 commit 53baca9
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ PHP indenting can be altered in several ways by modifying the values of some
global variables:

*php-comment* *PHP_autoformatcomment*
To not enable auto-formatting of comments by default (if you want to use your
To not enable auto-formating of comments by default (if you want to use your
own 'formatoptions'): >
:let g:PHP_autoformatcomment = 0
Expand Down Expand Up @@ -114,5 +114,40 @@ You will obtain the following result: >
->age()
->info();
-------------

*PHP_IndentFunctionCallParameters*
Extra indentation levels to add to parameters in multi-line function calls. >
let g:PHP_IndentFunctionCallParameters = 1
Function call arguments will indent 1 extra level. For two-space indentation: >
function call_the_thing(
$with_this,
$and_that
) {
$this->do_the_thing(
$with_this,
$and_that
);
}
-------------

*PHP_IndentFunctionDeclarationParameters*
Extra indentation levels to add to arguments in multi-line function definitions. >
let g:PHP_IndentFunctionDeclarationParameters = 1
Function arguments in declarations will indent 1 extra level. For two-space indentation: >
function call_the_thing(
$with_this,
$and_that
) {
$this->do_the_thing(
$with_this,
$and_that
);
}
vim:tw=78:ts=8:ft=help:norl:
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
" Vim indent file
" Language: PHP
" Author: John Wellesz <John.wellesz (AT) teaser (DOT) fr>
" URL: http://www.2072productions.com/vim/indent/php.vim
" Author: John Wellesz <John.wellesz (AT) gmail (DOT) com>
" URL: https://www.2072productions.com/vim/indent/php.vim
" Home: https://github.com/2072/PHP-Indenting-for-VIm
" Last Change: 2018 May 18th
" Version: 1.66
" Last Change: 2019 Jully 21st
" Version: 1.70
"
"
" Type :help php-indent for available options
Expand All @@ -19,7 +19,7 @@

" NOTE: This script must be used with PHP syntax ON and with the php syntax
" script by Lutz Eymers (http://www.isp.de/data/php.vim ) or with the
" script by Peter Hodge (http://www.vim.org/scripts/script.php?script_id=1571 )
" script by Peter Hodge (https://www.vim.org/scripts/script.php?script_id=1571 )
" the later is bunbdled by default with Vim 7.
"
"
Expand All @@ -41,7 +41,6 @@
" silently remove them when VIM load this script (at each bufread).



if exists("b:did_indent")
finish
endif
Expand Down Expand Up @@ -95,7 +94,17 @@ else
let b:PHP_vintage_case_default_indent = 0
endif

if exists("PHP_IndentFunctionCallParameters")
let b:PHP_IndentFunctionCallParameters = PHP_IndentFunctionCallParameters
else
let b:PHP_IndentFunctionCallParameters = 0
endif

if exists("PHP_IndentFunctionDeclarationParameters")
let b:PHP_IndentFunctionDeclarationParameters = PHP_IndentFunctionDeclarationParameters
else
let b:PHP_IndentFunctionDeclarationParameters = 0
endif

let b:PHP_lastindented = 0
let b:PHP_indentbeforelast = 0
Expand Down Expand Up @@ -133,11 +142,15 @@ if exists("*GetPhpIndent")
endif


let s:PHP_validVariable = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
let s:notPhpHereDoc = '\%(break\|return\|continue\|exit\|die\|else\)'
let s:blockstart = '\%(\%(\%(}\s*\)\=else\%(\s\+\)\=\)\=if\>\|\%(}\s*\)\?else\>\|do\>\|while\>\|switch\>\|case\>\|default\>\|for\%(each\)\=\>\|declare\>\|class\>\|trait\>\|use\>\|interface\>\|abstract\>\|final\>\|try\>\|\%(}\s*\)\=catch\>\|\%(}\s*\)\=finally\>\)'
let s:functionDecl = '\<function\>\%(\s\+&\='.s:PHP_validVariable.'\)\=\s*(.*'
let s:endline = '\s*\%(//.*\|#.*\|/\*.*\*/\s*\)\=$'
let s:PHP_validVariable = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
let s:notPhpHereDoc = '\%(break\|return\|continue\|exit\|die\|else\|end\%(if\|while\|for\|foreach\|switch\)\)'
let s:blockstart = '\%(\%(\%(}\s*\)\=else\%(\s\+\)\=\)\=if\>\|\%(}\s*\)\?else\>\|do\>\|while\>\|switch\>\|case\>\|default\>\|for\%(each\)\=\>\|declare\>\|class\>\|trait\>\|\%()\s*\)\=use\>\|interface\>\|abstract\>\|final\>\|try\>\|\%(}\s*\)\=catch\>\|\%(}\s*\)\=finally\>\)'
let s:functionDeclPrefix = '\<function\>\%(\s\+&\='.s:PHP_validVariable.'\)\=\s*('
let s:functionDecl = s:functionDeclPrefix.'.*'
let s:multilineFunctionDecl = s:functionDeclPrefix.s:endline
let s:arrayDecl = '\<array\>\s*(.*'
let s:multilineFunctionCall = s:PHP_validVariable.'\s*('.s:endline
let s:unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@<!\<e'.'lse\>\)'.s:endline


Expand Down Expand Up @@ -188,6 +201,7 @@ function! GetLastRealCodeLNum(startline) " {{{
if lastline =~ '^\s*/\*'
let lnum = lnum - 1
else

break
endif

Expand All @@ -210,7 +224,7 @@ function! GetLastRealCodeLNum(startline) " {{{
while getline(lnum) !~? tofind && lnum > 1
let lnum = lnum - 1
endwhile
elseif lastline =~ '^[^''"`]*[''"`][;,]'.s:endline
elseif lastline =~ '^\s*[''"`][;,]' || (lastline =~ '^[^''"`]*[''"`][;,]'.s:endline && IslinePHP(lnum, "") == "SpecStringEntrails")

let tofind=substitute( lastline, '^.*\([''"`]\)[;,].*$', '^[^\1]\\+[\1]$\\|^[^\1]\\+[=([]\\s*[\1]', '')
let trylnum = lnum
Expand Down Expand Up @@ -289,17 +303,23 @@ function! FindOpenBracket(lnum, blockStarter) " {{{
endfun " }}}

let s:blockChars = {'{':1, '[': 1, '(': 1, ')':-1, ']':-1, '}':-1}
let s:blockCharsLUT = {'{':'{', '}':'{', '[':'[', ']':'[', '(':'(', ')':'('}
function! BalanceDirection (str)

let balance = 0
let balance = {'{':0, '[': 0, '(': 0, 'none':0}
let director = 'none'

for c in split(a:str, '\zs')
if has_key(s:blockChars, c)
let balance += s:blockChars[c]
let balance[s:blockCharsLUT[c]] += s:blockChars[c]

if balance[s:blockCharsLUT[c]]
let director = s:blockCharsLUT[c]
endif
endif
endfor

return balance
return balance[director]
endfun

function! StripEndlineComments (line)
Expand All @@ -308,39 +328,55 @@ endfun

function! FindArrowIndent (lnum) " {{{

let parrentArrowPos = 0
let parrentArrowPos = -1
let cursorPos = -1
let lnum = a:lnum
while lnum > 1
let last_line = getline(lnum)
if last_line =~ '^\s*->'
let parrentArrowPos = indent(a:lnum)
break
else
call cursor(lnum, 1)
let cleanedLnum = StripEndlineComments(last_line)
if cleanedLnum =~ '->'
if ! b:PHP_noArrowMatching
let parrentArrowPos = searchpos('->', 'W', lnum)[1] - 1
else
let parrentArrowPos = indent(lnum) + shiftwidth()
endif

if b:PHP_noArrowMatching
break
elseif cleanedLnum =~ ')'.s:endline && BalanceDirection(last_line) < 0
call searchpos(')'.s:endline, 'cW', lnum)
let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()')
if openedparent != lnum
let lnum = openedparent
endif

let cleanedLnum = StripEndlineComments(last_line)

if cleanedLnum =~ ')'.s:endline
if BalanceDirection(cleanedLnum) <= 0
call cursor(lnum, 1)
call searchpos(')'.s:endline, 'cW', lnum)
let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()')
let cursorPos = col(".")
if openedparent != lnum
let lnum = openedparent
continue
else
endif
else
let openedparent = -1
endif
let parrentArrowPos = -1
break
end
endif

if cleanedLnum =~ '->'
call cursor(lnum, cursorPos == -1 ? strwidth(cleanedLnum) : cursorPos)
let parrentArrowPos = searchpos('->', 'cWb', lnum)[1] - 1

break
else
let parrentArrowPos = indent(lnum) + shiftwidth()
let parrentArrowPos = -1
break
endif
endif
endwhile

if parrentArrowPos == -1
let parrentArrowPos = indent(lnum) + shiftwidth()
end

return parrentArrowPos
endfun "}}}

Expand Down Expand Up @@ -432,7 +468,7 @@ function! IslinePHP (lnum, tofind) " {{{
let synname = synIDattr(synID(a:lnum, coltotest, 0), "name")

if synname ==? 'phpStringSingle' || synname ==? 'phpStringDouble' || synname ==? 'phpBacktick'
if cline !~ '^\s*[''"`]'
if cline !~ '^\s*[''"`]' " ??? XXX
return "SpecStringEntrails"
else
return synname
Expand Down Expand Up @@ -471,7 +507,7 @@ endfunc
call ResetPhpOptions()

function! GetPhpIndentVersion()
return "1.66-bundle"
return "1.70"
endfun

function! GetPhpIndent()
Expand Down Expand Up @@ -615,7 +651,7 @@ function! GetPhpIndent()
let b:InPHPcode_and_script = 1
endif

elseif last_line =~ '^[^''"`]\+[''"`]$' " a string identifier with nothing after it and no other string identifier before
elseif last_line =~ '^[^''"`]\+[''"`]$' && last_line !~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)' " a string identifier with nothing after it and no other string identifier before
let b:InPHPcode = -1
let b:InPHPcode_tofind = substitute( last_line, '^.*\([''"`]\).*$', '^[^\1]*\1[;,]$', '')
elseif last_line =~? '<<<\s*[''"]\=\a\w*[''"]\=$'
Expand Down Expand Up @@ -723,7 +759,7 @@ function! GetPhpIndent()
endif


if last_line =~ '[;}]'.endline && last_line !~ '^[)\]]' && last_line !~# s:defaultORcase
if last_line =~ '[;}]'.endline && last_line !~ '^[)\]]' && last_line !~# s:defaultORcase && last_line !~ '^\s*[''"`][;,]'
if ind==b:PHP_default_indenting
return b:PHP_default_indenting + addSpecial
elseif b:PHP_indentinghuge && ind==b:PHP_CurrentIndentLevel && cline !~# '^\s*\%(else\|\%(case\|default\).*:\|[})];\=\)' && last_line !~# '^\s*\%(\%(}\s*\)\=else\)' && getline(GetLastRealCodeLNum(lnum - 1))=~';'.endline
Expand Down Expand Up @@ -869,6 +905,14 @@ function! GetPhpIndent()
let ind = ind + shiftwidth()
endif

if b:PHP_IndentFunctionCallParameters && last_line =~ s:multilineFunctionCall && last_line !~ s:structureHead && last_line !~ s:arrayDecl
let ind = ind + b:PHP_IndentFunctionCallParameters * shiftwidth()
endif

if b:PHP_IndentFunctionDeclarationParameters && last_line =~ s:multilineFunctionDecl
let ind = ind + b:PHP_IndentFunctionDeclarationParameters * shiftwidth()
endif

if b:PHP_BracesAtCodeLevel || b:PHP_vintage_case_default_indent == 1
let b:PHP_CurrentIndentLevel = ind

Expand Down Expand Up @@ -897,10 +941,15 @@ function! GetPhpIndent()
endif

if cline =~ '^\s*[)\]];\='
let ind = ind - shiftwidth()
endif
call cursor(v:lnum, 1)
call searchpos('[)\]]', 'cW')
let matchedBlockChar = cline[col('.')-1]
let openedparent = searchpair('\M'.s:blockCharsLUT[matchedBlockChar], '', '\M'.matchedBlockChar, 'bW', 'Skippmatch()')
if openedparent != v:lnum
let ind = indent(openedparent)
endif

if last_line =~ '^\s*->' && last_line !~? s:structureHead && BalanceDirection(last_line) <= 0
elseif last_line =~ '^\s*->' && last_line !~? s:structureHead && BalanceDirection(last_line) <= 0
let ind = ind - shiftwidth()
endif

Expand Down
4 changes: 2 additions & 2 deletions indent/php.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
" Author: John Wellesz <John.wellesz (AT) gmail (DOT) com>
" URL: https://www.2072productions.com/vim/indent/php.vim
" Home: https://github.com/2072/PHP-Indenting-for-VIm
" Last Change: 2019 Jully 7th
" Last Change: 2019 Jully 21st
" Version: 1.70
"
"
Expand Down Expand Up @@ -1039,7 +1039,7 @@ endfunc
call ResetPhpOptions()

function! GetPhpIndentVersion()
return "1.69"
return "1.70"
endfun

function! GetPhpIndent()
Expand Down

0 comments on commit 53baca9

Please sign in to comment.