Skip to content

Commit

Permalink
Update comment stripped version to 1.75 for inclusion in VIm
Browse files Browse the repository at this point in the history
I used to send an email to Bram after doing this. :'( RIP Bram and thanks again for accepting this indent script almost 20 years ago.
I started programming when I was a teenager and I always used VIm. It feels strange using VIm knowing its author is no more, I'm really sad.
  • Loading branch information
2072 committed Sep 4, 2023
1 parent 7c76e31 commit 61e03ca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
" 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 21st
" Version: 1.70
" Last Change: 2023 August 18th
" Version: 1.75
"
"
" Type :help php-indent for available options
Expand Down Expand Up @@ -128,7 +128,7 @@ setlocal nolisp
setlocal indentexpr=GetPhpIndent()
setlocal indentkeys=0{,0},0),0],:,!^F,o,O,e,*<Return>,=?>,=<?,=*/


let b:undo_indent = "setl ai< cin< inde< indk< lisp< si<"

let s:searchpairflags = 'bWr'

Expand All @@ -142,10 +142,10 @@ if exists("*GetPhpIndent")
endif


let s:endline = '\s*\%(//.*\|#.*\|/\*.*\*/\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:notPhpHereDoc = '\<\%(break\|return\|continue\|exit\|die\|true\|false\|elseif\|else\|end\%(if\|while\|for\|foreach\|match\|switch\)\)\>'
let s:blockstart = '\%(\%(\%(}\s*\)\=else\%(\s\+\)\=\)\=if\>\|\%(}\s*\)\?else\>\|do\>\|while\>\|match\>\|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
Expand All @@ -156,7 +156,8 @@ let s:unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@<!\<e'.'lse\>\)'.s:endl

let s:terminated = '\%(\%(;\%(\s*\%(?>\|}\)\)\=\|<<<\s*[''"]\=\a\w*[''"]\=$\|^\s*}\|^\s*'.s:PHP_validVariable.':\)'.s:endline.'\)'
let s:PHP_startindenttag = '<?\%(.*?>\)\@!\|<script[^>]*>\%(.*<\/script>\)\@!'
let s:structureHead = '^\s*\%(' . s:blockstart . '\)\|'. s:functionDecl . s:endline . '\|\<new\s\+class\>'
let s:matchStart = 'match\s*(\s*\$\?'.s:PHP_validVariable.'\s*)\s*{'. s:endline
let s:structureHead = '^\s*\%(' . s:blockstart . '\)\|'. s:functionDecl . s:endline . '\|\<new\s\+class\>\|' . s:matchStart


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

let tofind=substitute( lastline, '^.*\([''"`]\)[;,].*$', '^[^\1]\\+[\1]$\\|^[^\1]\\+[=([]\\s*[\1]', '')
let trylnum = lnum
Expand Down Expand Up @@ -266,7 +267,7 @@ function! Skippmatch2()

let line = getline(".")

if line =~ "\\([\"']\\).*/\\*.*\\1" || line =~ '\%(//\|#\).*/\*'
if line =~ "\\([\"']\\).*/\\*.*\\1" || line =~ '\%(//\|#\[\@!\).*/\*'
return 1
else
return 0
Expand Down Expand Up @@ -322,18 +323,22 @@ function! BalanceDirection (str)
endfun

function! StripEndlineComments (line)
return substitute(a:line,"\\(//\\|#\\)\\(\\(\\([^\"']*\\([\"']\\)[^\"']*\\5\\)\\+[^\"']*$\\)\\|\\([^\"']*$\\)\\)",'','')

let cleaned = substitute(a:line,'\v(//|#\[\@!)((([^"'']*(["''])[^"'']*\5)+[^"'']*$)|([^"'']*$))','','')
if cleaned != a:line
endif
return cleaned
endfun

function! FindArrowIndent (lnum) " {{{

let parrentArrowPos = -1
let parentArrowPos = -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)
let parentArrowPos = indent(a:lnum)
break
else

Expand All @@ -355,28 +360,28 @@ function! FindArrowIndent (lnum) " {{{
else
endif
else
let parrentArrowPos = -1
let parentArrowPos = -1
break
end
endif

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

break
else
let parrentArrowPos = -1
let parentArrowPos = -1
break
endif
endif
endwhile

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

return parrentArrowPos
return parentArrowPos
endfun "}}}

function! FindTheIfOfAnElse (lnum, StopAfterFirstPrevElse) " {{{
Expand Down Expand Up @@ -490,7 +495,7 @@ function! ResetPhpOptions()
if ! b:optionsset && &filetype =~ "php"
if b:PHP_autoformatcomment

setlocal comments=s1:/*,mb:*,ex:*/,://,:#
setlocal comments=s1:/*,mb:*,ex:*/,://,f:#[,:#

setlocal formatoptions-=t
setlocal formatoptions+=q
Expand All @@ -506,7 +511,7 @@ endfunc
call ResetPhpOptions()

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

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

elseif last_line =~ '^[^''"`]\+[''"`]$' && last_line !~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)' " 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 All @@ -674,7 +679,7 @@ function! GetPhpIndent()

" Indent successive // or # comment the same way the first is {{{
let addSpecial = 0
if cline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)'
if cline =~ '^\s*\%(//\|#\[\@!\|/\*.*\*/\s*$\)'
let addSpecial = b:PHP_outdentSLComments
if b:PHP_LastIndentedWasComment == 1
return indent(real_PHP_lastindented)
Expand Down Expand Up @@ -715,7 +720,7 @@ function! GetPhpIndent()
return 0
endif

if cline =~? '^\s*\a\w*;$\|^\a\w*$\|^\s*[''"`][;,]' && cline !~? s:notPhpHereDoc
if (cline =~? '^\s*\a\w*;$\|^\a\w*$' || (cline =~? '^\s*[''"`][;,]' && IslinePHP(v:lnum, '[;,]') !~? '^\(phpString[SD]\|phpBacktick\)') ) && cline !~? s:notPhpHereDoc
return 0
endif " }}}

Expand All @@ -738,6 +743,9 @@ function! GetPhpIndent()
if cline =~ '^\s*}\%(}}\)\@!'
let ind = indent(FindOpenBracket(v:lnum, 1))
let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
if b:PHP_BracesAtCodeLevel
let ind = ind + shiftwidth()
endif
return ind
endif

Expand All @@ -761,7 +769,7 @@ function! GetPhpIndent()
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
elseif b:PHP_indentinghuge && ind==b:PHP_CurrentIndentLevel && cline !~# '^\s*\%(else\|\%(case\|default\).*:\|[})];\=\)' && last_line !~# '^\s*\%(\%(}\s*\)\=else\)\|^\(\s*\S\+\s*\)\+}'.endline && getline(GetLastRealCodeLNum(lnum - 1))=~';'.endline
return b:PHP_CurrentIndentLevel + addSpecial
endif
endif
Expand Down Expand Up @@ -929,7 +937,7 @@ function! GetPhpIndent()
let ind = ind + shiftwidth()


elseif AntepenultimateLine =~ '{'.endline && AntepenultimateLine !~? '^\s*use\>' || AntepenultimateLine =~ terminated || AntepenultimateLine =~# s:defaultORcase
elseif AntepenultimateLine =~ '{'.endline && AntepenultimateLine !~? '^\s*use\>' && AntepenultimateLine !~? s:matchStart || AntepenultimateLine =~ terminated || AntepenultimateLine =~# s:defaultORcase
let ind = ind + shiftwidth()
endif

Expand Down
25 changes: 12 additions & 13 deletions indent/php.vim
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"
" Changes: 1.70 - Rename PHP_IndentFunctionParameters to PHP_IndentFunctionCallParameters and
" also implement PHP_IndentFunctionDeclarationParameters.
" - Update documentation.
" - Update documentation.
"
" Changes: 1.69 - Fix vim/vim#4562 where Vim would freeze on multiline-string declarations ending with a comma.
" - Fix #69: Indenting was incorrect for closures with single-line `use` statements.
Expand Down Expand Up @@ -656,7 +656,6 @@ function! GetLastRealCodeLNum(startline) " {{{
let lnum = lnum - 1
else
" DEBUG call DebugPrintReturn('613: break' )

break
endif

Expand Down Expand Up @@ -835,14 +834,14 @@ function! FindArrowIndent (lnum) " {{{
" - 2.2 It's a non-terminated line or anything else (first '->' or not a chained call)
" just return the indent of the previous line + &sw (normal indent)

let parrentArrowPos = -1
let parentArrowPos = -1
let cursorPos = -1
let lnum = a:lnum
while lnum > 1
let last_line = getline(lnum)
" the simple case
if last_line =~ '^\s*->'
let parrentArrowPos = indent(a:lnum)
let parentArrowPos = indent(a:lnum)
" DEBUG call DebugPrintReturn(767 . "FindArrowIndent simple case")
break
else
Expand Down Expand Up @@ -874,7 +873,7 @@ function! FindArrowIndent (lnum) " {{{
" on the same line
else
" if the () are imbalanced just resort to default
let parrentArrowPos = -1
let parentArrowPos = -1
" DEBUG call DebugPrintReturn(780 . "FindArrowIndent default +&sw (arrow was inside matching ())")
break
end
Expand All @@ -883,24 +882,24 @@ function! FindArrowIndent (lnum) " {{{
" the previous line does contain an arrow
if cleanedLnum =~ '->'
call cursor(lnum, cursorPos == -1 ? strwidth(cleanedLnum) : cursorPos)
let parrentArrowPos = searchpos('->', 'cWb', lnum)[1] - 1
let parentArrowPos = searchpos('->', 'cWb', lnum)[1] - 1
" DEBUG call DebugPrintReturn(792 . "FindArrowIndent returning arrow searchposition on: " . lnum . " xxx adjust is " . col("."))

break
else
let parrentArrowPos = -1
let parentArrowPos = -1
" DEBUG call DebugPrintReturn(808 . "FindArrowIndent default +&sw")
break
endif
endif
endwhile

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

" DEBUG call DebugPrintReturn(818 . "FindArrowIndent returns: " . parrentArrowPos)
return parrentArrowPos
" DEBUG call DebugPrintReturn(818 . "FindArrowIndent returns: " . parentArrowPos)
return parentArrowPos
endfun "}}}

function! FindTheIfOfAnElse (lnum, StopAfterFirstPrevElse) " {{{
Expand Down Expand Up @@ -1051,7 +1050,7 @@ function! ResetPhpOptions()

" Set the comment setting to something correct for PHP
" Note that in php 8.0, #[ is used as the start of an attribute,
" which is not actually a comment but the 'f:#[' override is
" which is not actually a comment but the 'f:#[' override is
" added to avoid incorrectly completing it.
setlocal comments=s1:/*,mb:*,ex:*/,://,f:#[,:#

Expand All @@ -1075,7 +1074,7 @@ endfunc
call ResetPhpOptions()

function! GetPhpIndentVersion()
return "1.71"
return "1.75"
endfun

function! GetPhpIndent()
Expand Down

0 comments on commit 61e03ca

Please sign in to comment.