3
3
" Author: John Wellesz <John.wellesz (AT) teaser (DOT) fr>
4
4
" URL: http://www.2072productions.com/vim/indent/php.vim
5
5
" Home: https://github.com/2072/PHP-Indenting-for-VIm
6
- " Last Change: 2017 Jun 13
7
- " Version: 1.62
6
+ " Last Change: 2018 May 18th
7
+ " Version: 1.66
8
8
"
9
9
"
10
10
" Type :help php-indent for available options
82
82
let b: PHP_outdentphpescape = 1
83
83
endif
84
84
85
+ if exists (" PHP_noArrowMatching" )
86
+ let b: PHP_noArrowMatching = PHP_noArrowMatching
87
+ else
88
+ let b: PHP_noArrowMatching = 0
89
+ endif
90
+
85
91
86
92
if exists (" PHP_vintage_case_default_indent" ) && PHP_vintage_case_default_indent
87
93
let b: PHP_vintage_case_default_indent = 1
@@ -130,7 +136,7 @@ endif
130
136
let s: PHP_validVariable = ' [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
131
137
let s: notPhpHereDoc = ' \%(break\|return\|continue\|exit\|die\|else\)'
132
138
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\>\)'
133
- let s: functionDecl = ' \<function\>\%(\s\+' .s: PHP_validVariable .' \)\=\s*(.*'
139
+ let s: functionDecl = ' \<function\>\%(\s\+&\= ' .s: PHP_validVariable .' \)\=\s*(.*'
134
140
let s: endline = ' \s*\%(//.*\|#.*\|/\*.*\*/\s*\)\=$'
135
141
let s: unstated = ' \%(^\s*' .s: blockstart .' .*)\|\%(//.*\)\@<!\<e' .' lse\>\)' .s: endline
136
142
@@ -140,7 +146,6 @@ let s:PHP_startindenttag = '<?\%(.*?>\)\@!\|<script[^>]*>\%(.*<\/script>\)\@!'
140
146
let s: structureHead = ' ^\s*\%(' . s: blockstart . ' \)\|' . s: functionDecl . s: endline . ' \|\<new\s\+class\>'
141
147
142
148
143
-
144
149
let s: escapeDebugStops = 0
145
150
function ! DebugPrintReturn (scriptLine)
146
151
@@ -257,7 +262,7 @@ endfun
257
262
258
263
function ! Skippmatch () " {{{
259
264
let synname = synIDattr (synID (line (" ." ), col (" ." ), 0 ), " name" )
260
- if synname == " Delimiter" || synname == " phpRegionDelimiter" || synname = ~# " ^phpParent" || synname == " phpArrayParens" || synname = ~# ' ^php\%(Block\|Brace\)' || synname == " javaScriptBraces" || synname = ~# ' ^php\%(Doc\)\?Comment' && b: UserIsTypingComment
265
+ if synname == ? " Delimiter" || synname == ? " phpRegionDelimiter" || synname = ~? " ^phpParent" || synname == ? " phpArrayParens" || synname = ~? ' ^php\%(Block\|Brace\)' || synname == ? " javaScriptBraces" || synname = ~? ' ^php\%(Doc\)\?Comment' && b: UserIsTypingComment
261
266
return 0
262
267
else
263
268
return 1
@@ -297,6 +302,48 @@ function! BalanceDirection (str)
297
302
return balance
298
303
endfun
299
304
305
+ function ! StripEndlineComments (line )
306
+ return substitute (a: line ," \\ (//\\ |#\\ )\\ (\\ (\\ ([^\" ']*\\ ([\" ']\\ )[^\" ']*\\ 5\\ )\\ +[^\" ']*$\\ )\\ |\\ ([^\" ']*$\\ )\\ )" ,' ' ,' ' )
307
+ endfun
308
+
309
+ function ! FindArrowIndent (lnum) " {{{
310
+
311
+ let parrentArrowPos = 0
312
+ let lnum = a: lnum
313
+ while lnum > 1
314
+ let last_line = getline (lnum)
315
+ if last_line = ~ ' ^\s*->'
316
+ let parrentArrowPos = indent (a: lnum )
317
+ break
318
+ else
319
+ call cursor (lnum, 1 )
320
+ let cleanedLnum = StripEndlineComments (last_line)
321
+ if cleanedLnum = ~ ' ->'
322
+ if ! b: PHP_noArrowMatching
323
+ let parrentArrowPos = searchpos (' ->' , ' W' , lnum)[1 ] - 1
324
+ else
325
+ let parrentArrowPos = indent (lnum) + shiftwidth ()
326
+ endif
327
+ break
328
+ elseif cleanedLnum = ~ ' )' .s: endline && BalanceDirection (last_line) < 0
329
+ call searchpos (' )' .s: endline , ' cW' , lnum)
330
+ let openedparent = searchpair (' (' , ' ' , ' )' , ' bW' , ' Skippmatch()' )
331
+ if openedparent != lnum
332
+ let lnum = openedparent
333
+ else
334
+ let openedparent = -1
335
+ endif
336
+
337
+ else
338
+ let parrentArrowPos = indent (lnum) + shiftwidth ()
339
+ break
340
+ endif
341
+ endif
342
+ endwhile
343
+
344
+ return parrentArrowPos
345
+ endfun " }}}
346
+
300
347
function ! FindTheIfOfAnElse (lnum, StopAfterFirstPrevElse) " {{{
301
348
302
349
if getline (a: lnum ) = ~# ' ^\s*}\s*else\%(if\)\=\>'
@@ -368,7 +415,7 @@ function! FindTheSwitchIndent (lnum) " {{{
368
415
369
416
endfunction " }}}
370
417
371
- let s: SynPHPMatchGroups = {' phpParent ' :1 , ' Delimiter ' :1 , ' Define ' :1 , ' Storageclass ' :1 , ' StorageClass ' :1 , ' Structure ' : 1 , ' Exception ' :1 }
418
+ let s: SynPHPMatchGroups = {' phpparent ' :1 , ' delimiter ' :1 , ' define ' :1 , ' storageclass ' :1 , ' structure ' :1 , ' exception ' :1 }
372
419
function ! IslinePHP (lnum, tofind) " {{{
373
420
let cline = getline (a: lnum )
374
421
@@ -384,15 +431,15 @@ function! IslinePHP (lnum, tofind) " {{{
384
431
385
432
let synname = synIDattr (synID (a: lnum , coltotest, 0 ), " name" )
386
433
387
- if synname == ' phpStringSingle' || synname == ' phpStringDouble' || synname == ' phpBacktick'
434
+ if synname == ? ' phpStringSingle' || synname == ? ' phpStringDouble' || synname == ? ' phpBacktick'
388
435
if cline !~ ' ^\s*['' "`]'
389
436
return " SpecStringEntrails"
390
437
else
391
438
return synname
392
439
end
393
440
end
394
441
395
- if get (s: SynPHPMatchGroups , synname) || synname = ~ ' ^php' || synname = ~? ' ^javaScript'
442
+ if get (s: SynPHPMatchGroups , tolower ( synname) ) || synname = ~ ' ^php' || synname = ~? ' ^javaScript'
396
443
return synname
397
444
else
398
445
return " "
@@ -423,6 +470,10 @@ endfunc
423
470
424
471
call ResetPhpOptions ()
425
472
473
+ function ! GetPhpIndentVersion ()
474
+ return " 1.66-bundle"
475
+ endfun
476
+
426
477
function ! GetPhpIndent ()
427
478
428
479
let b: GetLastRealCodeLNum_ADD = 0
@@ -480,14 +531,14 @@ function! GetPhpIndent()
480
531
endif
481
532
482
533
if synname!= " "
483
- if synname == " SpecStringEntrails"
534
+ if synname == ? " SpecStringEntrails"
484
535
let b: InPHPcode = -1 " thumb down
485
536
let b: InPHPcode_tofind = " "
486
- elseif synname != " phpHereDoc" && synname != " phpHereDocDelimiter"
537
+ elseif synname !=? " phpHereDoc" && synname !=? " phpHereDocDelimiter"
487
538
let b: InPHPcode = 1
488
539
let b: InPHPcode_tofind = " "
489
540
490
- if synname = ~# ' ^php\%(Doc\)\?Comment'
541
+ if synname = ~? ' ^php\%(Doc\)\?Comment'
491
542
let b: UserIsTypingComment = 1
492
543
let b: InPHPcode_checked = 0
493
544
endif
@@ -556,7 +607,7 @@ function! GetPhpIndent()
556
607
557
608
if 1 == b: InPHPcode
558
609
559
- if ! b: InPHPcode_and_script && last_line = ~ ' \%(<?.*\)\@<!?>\%(.*<?\)\@!' && IslinePHP (lnum, ' ?>' )= ~" Delimiter"
610
+ if ! b: InPHPcode_and_script && last_line = ~ ' \%(<?.*\)\@<!?>\%(.*<?\)\@!' && IslinePHP (lnum, ' ?>' )= ~? " Delimiter"
560
611
if cline !~? s: PHP_startindenttag
561
612
let b: InPHPcode = 0
562
613
let b: InPHPcode_tofind = s: PHP_startindenttag
@@ -712,7 +763,8 @@ function! GetPhpIndent()
712
763
let last_line_num = GetLastRealCodeLNum (last_line_num - 1 )
713
764
let previous_line = getline (last_line_num)
714
765
endwhile
715
-
766
+ elseif cline = ~ ' ^\s*->'
767
+ return FindArrowIndent (lnum)
716
768
elseif last_line = ~# unstated && cline !~ ' ^\s*);\=' .endline
717
769
let ind = ind + shiftwidth () " we indent one level further when the preceding line is not stated
718
770
return ind + addSpecial
@@ -724,7 +776,7 @@ function! GetPhpIndent()
724
776
725
777
let isSingleLineBlock = 0
726
778
while 1
727
- if ! isSingleLineBlock && previous_line = ~ ' ^\s*}\|;\s*}' .endline " XXX
779
+ if ! isSingleLineBlock && previous_line = ~ ' ^\s*}\|;\s*}' .endline
728
780
729
781
call cursor (last_line_num, 1 )
730
782
if previous_line !~ ' ^}'
@@ -793,8 +845,7 @@ function! GetPhpIndent()
793
845
794
846
let AntepenultimateLine = getline (plinnum)
795
847
796
- let last_line = substitute (last_line," \\ (//\\ |#\\ )\\ (\\ (\\ ([^\" ']*\\ ([\" ']\\ )[^\" ']*\\ 5\\ )\\ +[^\" ']*$\\ )\\ |\\ ([^\" ']*$\\ )\\ )" ,' ' ,' ' )
797
-
848
+ let last_line = StripEndlineComments (last_line)
798
849
799
850
if ind == b: PHP_default_indenting
800
851
if last_line = ~ terminated && last_line !~# s: defaultORcase
@@ -804,11 +855,13 @@ function! GetPhpIndent()
804
855
805
856
if ! LastLineClosed
806
857
858
+ let openedparent = -1
859
+
807
860
808
861
if last_line = ~# ' [{(\[]' .endline || last_line = ~? ' \h\w*\s*(.*,$' && AntepenultimateLine !~ ' [,(\[]' .endline && BalanceDirection (last_line) > 0
809
862
810
863
let dontIndent = 0
811
- if last_line = ~ ' \S\+\s*{' .endline && last_line !~ ' ^\s*[)\]]\+\s*{' .endline && last_line !~ s: structureHead
864
+ if last_line = ~ ' \S\+\s*{' .endline && last_line !~ ' ^\s*[)\]]\+\(\s*:\s* ' . s: PHP_validVariable . ' \)\=\ s*{' .endline && last_line !~ s: structureHead
812
865
let dontIndent = 1
813
866
endif
814
867
@@ -819,28 +872,35 @@ function! GetPhpIndent()
819
872
if b: PHP_BracesAtCodeLevel || b: PHP_vintage_case_default_indent == 1
820
873
let b: PHP_CurrentIndentLevel = ind
821
874
822
- return ind + addSpecial
823
875
endif
824
876
825
- elseif last_line = ~ ' \S\+\s* ),' .endline && BalanceDirection (last_line) < 0
877
+ elseif last_line = ~ ' ),' .endline && BalanceDirection (last_line) < 0
826
878
call cursor (lnum, 1 )
827
- call search (' ),' .endline, ' W ' ) " line never begins with ) so no need for 'c' flag
879
+ call searchpos (' ),' .endline, ' cW ' )
828
880
let openedparent = searchpair (' (' , ' ' , ' )' , ' bW' , ' Skippmatch()' )
829
881
if openedparent != lnum
830
882
let ind = indent (openedparent)
831
883
endif
832
884
833
- elseif last_line = ~ ' ^\s* ' . s: blockstart
885
+ elseif last_line = ~ s: structureHead
834
886
let ind = ind + shiftwidth ()
835
887
836
888
837
889
elseif AntepenultimateLine = ~ ' {' .endline && AntepenultimateLine !~? ' ^\s*use\>' || AntepenultimateLine = ~ terminated || AntepenultimateLine = ~# s: defaultORcase
838
890
let ind = ind + shiftwidth ()
839
891
endif
840
892
893
+
894
+ if openedparent >= 0
895
+ let last_line = StripEndlineComments (getline (openedparent))
896
+ endif
897
+ endif
898
+
899
+ if cline = ~ ' ^\s*[)\]];\='
900
+ let ind = ind - shiftwidth ()
841
901
endif
842
902
843
- if cline = ~ ' ^\s*[)\]];\= '
903
+ if last_line = ~ ' ^\s*-> ' && last_line !~? s: structureHead && BalanceDirection (last_line) <= 0
844
904
let ind = ind - shiftwidth ()
845
905
endif
846
906
0 commit comments