-
Notifications
You must be signed in to change notification settings - Fork 2
/
phps-mode-indent.el
1786 lines (1642 loc) · 60.1 KB
/
phps-mode-indent.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; phps-mode-indent.el -- Indentation for phps-mode -*- lexical-binding: t -*-
;; Copyright (C) 2018-2024 Free Software Foundation, Inc.
;;; Commentary:
;;; Code:
(require 'phps-mode-macros)
;; General helper functions
(defun phps-mode-indent--string-indentation (string)
"Count indentation of STRING."
(if (string-match "\\(^[\t ]+\\)" string)
(length (substring string (match-beginning 0) (match-end 0)))
0))
(defun phps-mode-indent--backwards-looking-at (regexp)
"Backward string if point is backwards looking at REGEXP, otherwise nil."
(let ((point (point))
(limit 100))
(when (< point limit)
(setq limit (1- point)))
(let* ((start (- point limit))
(backward-string
(buffer-substring-no-properties
start
(1+ point))))
(if (string-match
regexp
backward-string)
backward-string
nil))))
(defun phps-mode-indent--string-starts-with-regexp (string regexp &optional match-index)
"If STRING start with REGEXP, return it,
otherwise nil. With optional MATCH-INDEX."
(phps-mode-indent--string-match-regexp
string
(concat "^" regexp)
match-index))
(defun phps-mode-indent--string-ends-with-regexp (string regexp &optional match-index)
"If STRING end with REGEXP, return it,
otherwise nil. With optional MATCH-INDEX."
(phps-mode-indent--string-match-regexp
string
(concat regexp "$")
match-index))
(defun phps-mode-indent--string-match-regexp (string regexp &optional match-index)
"If STRING match REGEXP, return it, otherwise nil.
With optional MATCH-INDEX."
(unless match-index
(setq match-index 0))
(if
(string-match regexp string)
(match-string match-index string)
nil))
;; Specific helper functions
(defun phps-mode-indent--string-starts-with-closing-bracket (string)
"If STRING start with closing bracket, return it, otherwise return nil."
(phps-mode-indent--string-starts-with-regexp
string
"[\t ]*\\(<\\?php[\t\n ]*\\)?\\([\]})]\\)"
2))
(defun phps-mode-indent--string-starts-with-opening-bracket (string)
"If STRING start with opening bracket return it otherwise nil."
(phps-mode-indent--string-starts-with-regexp
string
"[\t ]*\\(<\\?php[\t\n ]*\\)?\\([\[{(]\\)"
2))
(defun phps-mode-indent--string-starts-with-opening-doc-comment (string)
"Does STRING start with opening doc comment?"
(phps-mode-indent--string-starts-with-regexp
string
"[\t ]*\\(<\\?php[\t\n ]*\\)?\\(/\\*\\*\\)"
2))
(defun phps-mode-indent--string-ends-with-assignment (string)
"If STRING end with terminus, return it, otherwise return nil."
(phps-mode-indent--string-ends-with-regexp
string
"\\(=>?\\)[\t ]*\\(\\?>[\t\n ]*\\)?"
1))
(defun phps-mode-indent--string-ends-with-closing-bracket (string)
"If STRING end with closing bracket, return it, otherwise nil."
(phps-mode-indent--string-ends-with-regexp
string
"\\([\]})]\\)[\t ]*\\(\\?>[\t\n ]*\\)?"
1))
(defun phps-mode-indent--string-ends-with-closing-doc-comment (string)
"If STRING end with closing doc comment, return it, otherwise nil."
(phps-mode-indent--string-ends-with-regexp
string
"\\(\\*/\\)[\t ]*\\(\\?>[\t\n ]*\\)?"
1))
(defun phps-mode-indent--string-ends-with-opening-bracket (string)
"If STRING end with opening bracket, return it, otherwise nil."
(phps-mode-indent--string-ends-with-regexp
string
"\\([\[{(]\\)[\t ]*\\(\\?>[\t\n ]*\\)?"
1))
(defun phps-mode-indent--string-ends-with-terminus (string)
"If STRING end with terminus, return it, otherwise return nil."
(phps-mode-indent--string-ends-with-regexp
string
"\\(;\\|,\\)[\t ]*\\(\\?>[\t\n ]*\\)?"
1))
(defun phps-mode-indent--get-string-brackets-count
(string)
"Get bracket count for STRING."
(let ((bracket-level 0)
(start 0)
(line-is-empty
(string-match-p "^[ \t\f\r\n]*$" string))
(test-string "\\([\]{}()[]\\|^[\t ]/\\*\\*\\|^[\t\\* ]*\\*/\\)"))
(unless line-is-empty
;; (message "string: %S" string)
(while
(string-match
test-string
string
start)
(setq
start
(match-end 0))
(let ((bracket (substring string (match-beginning 0) (match-end 0))))
;; (message "bracket: %S from %S" bracket string)
(cond
((or
(string= bracket "{")
(string= bracket "[")
(string= bracket "(")
(string= bracket "<"))
(setq bracket-level (+ bracket-level tab-width)))
((string-match "^[\t\\* ]*\\*/" bracket)
(setq bracket-level (- bracket-level 1)))
((string-match "^[\t ]/\\*\\*" bracket)
(setq bracket-level (+ bracket-level 1)))
(t
(setq bracket-level (- bracket-level tab-width)))))))
bracket-level))
(defun phps-mode-indent--get-html-string-bracket-level (string)
"Get HTML bracket-level for STRING."
(let* ((html-bracket-level 0)
(start 0)
(next-item
(string-match
"\\(<[^>]+>\\)"
string
start)))
(while next-item
(let ((match (match-string 0 string)))
(setq
start
(match-end 0))
(cond
;; Self-closing tag does not change indentation
((string-match
"<\\([a-zA-Z]\\)+[^>]+/>"
match))
;; Opening tag changes indentation for most tags
((string-match
"<\\([a-zA-Z]+\\)"
match)
(let ((tag (match-string 1 match)))
(unless
(string-match-p
"^\\(html\\|meta\\|br\\|em\\|strong\\|i\\|b\\)$"
tag)
(setq
html-bracket-level
(1+ html-bracket-level)))))
;; Closing tag changes indentation for most tags
((string-match
"</\\([a-zA-Z]+\\)"
match)
(let ((tag (match-string 1 match)))
(unless
(string-match-p
"^\\(html\\|meta\\|br\\|em\\|strong\\|i\\|b\\)$"
tag)
(setq
html-bracket-level
(1- html-bracket-level)))))
)
(setq
next-item
(string-match
"\\(<[^>]+>\\)"
string
start))))
(if (= html-bracket-level 0)
nil
html-bracket-level)))
(defun phps-mode-indent--get-previous-reference-index-line ()
"Get previous index line as reference, if any exist.
A index line is a previous element line inside current bracket scope."
(let ((reference-line))
(save-excursion
(end-of-line)
(search-backward-regexp "," nil t) ;; Skip trailing comma
(let ((not-found-bracket-start t)
(found-colon)
(parenthesis-level 0))
(while
(and
not-found-bracket-start
(search-backward-regexp
"\\([][{}(),:]\\|=>\\)"
nil
t))
(let ((match (match-string-no-properties 0)))
;; (message "match: %S" match)
(cond
((or
(string= "(" match)
(string= "[" match)
(string= "{" match))
(setq
parenthesis-level
(1+ parenthesis-level))
(cond
((= parenthesis-level 1)
(setq
not-found-bracket-start
nil))
((= parenthesis-level 0)
(setq
reference-line
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
))
((or
(string= ")" match)
(string= "]" match)
(string= "}" match))
(setq
parenthesis-level
(1- parenthesis-level)))
;; The second occurrence of a colon
;; is a significant marker of
;; a starting bracket row
((string= "," match)
(when (= parenthesis-level 0)
(if found-colon
(setq
not-found-bracket-start
nil)
(setq
found-colon
t)
(setq
reference-line
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))))
;; The first occurrence of a => or :
;; is a significant marker of
;; a starting bracket row
((or
(string= "=>" match)
(string= ":" match))
(when (= parenthesis-level 0)
(setq
reference-line
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(setq
not-found-bracket-start
nil)))
)))))
reference-line))
(defun phps-mode-indent--get-previous-start-of-bracket-line
(&optional from-end-of-line)
"Get previous start of bracket line as reference, if any exist.
Optionally start FROM-END-OF-LINE."
(let ((reference-line))
(save-excursion
(if from-end-of-line
(end-of-line)
(beginning-of-line)
;; Step over optional PHP starting tag
(when (looking-at-p "^[\t ]*<\\?php[\t ]+")
(search-forward-regexp "^[\t ]*<\\?php[\t ]+" nil t))
(if (search-forward-regexp "[^])}\t ]" nil t)
(forward-char -1)
(end-of-line)))
(let ((not-found-bracket-start t)
(parenthesis-level 0)
(same-line-p t))
(while
(and
not-found-bracket-start
(search-backward-regexp
"[][{}()\n]"
nil
t))
(let ((match (match-string-no-properties 0)))
(cond
((string= "\n" match)
(setq
same-line-p
nil))
((or
(string= "(" match)
(string= "[" match)
(string= "{" match))
(setq
parenthesis-level
(1+ parenthesis-level))
(when (= parenthesis-level 0)
(setq
reference-line
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(setq
not-found-bracket-start
nil)))
((or
(string= ")" match)
(string= "]" match)
(string= "}" match))
(setq
parenthesis-level
(1- parenthesis-level)))
)))
(if same-line-p
nil
reference-line)))))
(defun phps-mode-indent--get-previous-start-of-chaining ()
"Get previous start of bracket line as reference, if any exist."
(let ((reference-line))
(save-excursion
(end-of-line)
(let ((not-found-bracket-start t)
(parenthesis-level 0)
(found-chain-on-this-line)
(reference-line-previous)
(reference-line-delta)
(reference-line-previous-delta)
(line-delta 0)
(same-line-p t)
(rewind-reference-line))
(while
(and
not-found-bracket-start
(search-backward-regexp
"\\(=>\\|[][{}()=\n;]\\|->\\|^[\ t]*\\.\\|\\.[\t ]*$\\)"
nil
t))
(let ((match (match-string-no-properties 0)))
;; (message "match: %S" match)
(cond
((string-match-p
"\\(^[\ t]*\\.\\|\\.[\t ]*$\\)"
match)
(setq
not-found-bracket-start
nil))
((string= "=>" match))
((string= "\n" match)
(when (and
same-line-p
(> parenthesis-level 0))
(setq parenthesis-level 0))
(setq
same-line-p
nil)
(if found-chain-on-this-line
(progn
(setq
reference-line-previous
reference-line)
(setq
reference-line-previous-delta
reference-line-delta)
(setq
reference-line
found-chain-on-this-line)
(setq
found-chain-on-this-line
nil)
(setq
reference-line-delta
line-delta)))
(setq
line-delta
(1- line-delta)))
((string= "->" match)
(setq
found-chain-on-this-line
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
((or
(string= "=" match)
(and
(not same-line-p)
(string= ";" match)))
(setq
not-found-bracket-start
nil))
((or
(string= "(" match)
(string= "[" match)
(string= "{" match))
(setq
parenthesis-level
(1+ parenthesis-level))
(when (and
(not same-line-p)
(= parenthesis-level 1))
(setq
rewind-reference-line
t)
(setq
line-delta
(1+ line-delta))
(setq
not-found-bracket-start
nil)))
((or
(string= ")" match)
(string= "]" match)
(string= "}" match))
(setq
parenthesis-level
(1- parenthesis-level)))
)))
(when (or
rewind-reference-line
not-found-bracket-start)
(setq
reference-line
reference-line-previous)
(setq
reference-line-delta
reference-line-previous-delta))
(if (and
reference-line
reference-line-delta
(not (= reference-line-delta 0)))
reference-line
nil)))))
(defun phps-mode-indent--get-previous-reference-command-line ()
"Get previous line that is a command (if any)."
(let ((not-found t)
(reference-line)
(found-semi-colon))
(save-excursion
(while
(and
not-found
(search-backward-regexp
"^[\t ]*[^\t ]+.*$"
nil
t))
(let ((match (match-string-no-properties 0)))
;; (message "match: %S" match)
(cond
;; Commented out line
((string-match-p
"^[\t ]*//"
match))
;; A separate command
((or
(string-match-p
"{[\t ]*$"
match)
(string-match-p
"^[\t ]*<\\?"
match))
(setq
not-found
nil))
;; Alternative control structures are always
;; indication of start of command
((or
(string-match-p
")[\ t]*:[\t ]*$"
match)
(string-match-p
"\\(case\\|default\\).*:[\t ]*$"
match)) ;; Like case '50':
(setq
not-found
nil))
;; A closing curly bracket is indicate of a distinct command
((string-match-p
"}[\t ]*$"
match)
(when found-semi-colon
(setq
not-found
nil)))
;; A second semi-colon is always a indicator of
;; a end of a previous command
;; Some keywords always indicate a start of command
((string-match-p
";[\t ]*$"
match)
(if found-semi-colon
(setq
not-found
nil)
(let ((is-statement
(string-match-p
"^[\t ]*\\(endswitch\\|endforeach\\|endwhile\\|exit\\|die\\|echo[\t ]+.*\\)[\t ]*;$"
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))))
(if is-statement
(progn
(setq
not-found
nil)
(setq
reference-line
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(setq
reference-line
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(setq
found-semi-colon
t)))))
(t
(setq
reference-line
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
))))
reference-line))
;; Main functions
(defun phps-mode-indent-line (&optional initial-point)
"Apply alternative indentation at INITIAL-POINT here."
(let ((point))
(if initial-point
(setq point initial-point)
(setq point (point)))
(let ((new-indentation 0)
(point-at-end-of-line (equal point (line-end-position))))
(save-excursion
(let ((move-length 0)
(move-length1 0)
(current-line-string "")
(previous-line-string "")
(previous-line-is-empty-p)
(previous2-line-string ""))
(when initial-point
(goto-char point))
(move-beginning-of-line nil)
(setq point (point))
;; Current line is line at initial point
(setq
current-line-string
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
;; (message "\nCurrent line: %S" current-line-string)
;; Try to find previous 2 non-empty lines
(let ((line-is-empty-p t)
(line-is-comment-p t)
(searching-previous-lines 2))
(while (and
(= (forward-line -1) 0)
(> searching-previous-lines 0))
(beginning-of-line)
(let ((line-string
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(setq
line-is-empty-p
(string-match-p
"^[ \t\f\r\n]*$"
line-string))
(setq
line-is-comment-p
(string-match-p
"^[\t ]*\\(//\\|#\\)"
line-string))
(unless (or
line-is-empty-p
line-is-comment-p)
;; Does line contain comment?
(when (string-match-p
"\\(//[^'\"]+$\\|#[^'\"]+$\\|/\\*.+\\*/\\)"
line-string)
;; Delete comment region
(setq
line-string
(replace-regexp-in-string
"\\(//[^'\"]+$\\|#[^'\"]+$\\|/\\*.+\\*/\\)"
""
line-string)))
(cond
((= searching-previous-lines 2)
(setq
move-length1
(1+ move-length))
(setq
previous-line-string
line-string))
((= searching-previous-lines 1)
(setq
previous2-line-string
line-string)))
(setq
searching-previous-lines
(1- searching-previous-lines))
(when (= searching-previous-lines 2)
(setq
previous-line-is-empty-p
line-is-empty-p)))
(setq
move-length
(1+ move-length)))))
(goto-char point)
(if previous-line-is-empty-p
(indent-line-to
(phps-mode-indent--string-indentation
previous-line-string))
(let* ((previous-indentation
(phps-mode-indent--string-indentation
previous-line-string))
(current-line-starts-with-closing-bracket
(phps-mode-indent--string-starts-with-closing-bracket
current-line-string))
(current-line-starts-with-opening-bracket
(phps-mode-indent--string-starts-with-opening-bracket
current-line-string))
(current-line-ends-with-terminus
(phps-mode-indent--string-ends-with-terminus
current-line-string))
(previous-line-starts-with-closing-bracket
(phps-mode-indent--string-starts-with-closing-bracket
previous-line-string))
(previous-line-ends-with-closing-bracket
(phps-mode-indent--string-ends-with-closing-bracket
previous-line-string))
(previous-line-starts-with-opening-doc-comment
(phps-mode-indent--string-starts-with-opening-doc-comment
previous-line-string))
(previous-line-ends-with-closing-doc-comment
(phps-mode-indent--string-ends-with-closing-doc-comment
previous-line-string))
(previous-line-ends-with-assignment
(phps-mode-indent--string-ends-with-assignment
previous-line-string))
(previous-line-ends-with-opening-bracket
(phps-mode-indent--string-ends-with-opening-bracket
previous-line-string))
(previous-line-ends-with-terminus
(phps-mode-indent--string-ends-with-terminus
previous-line-string))
(previous-bracket-level
(phps-mode-indent--get-string-brackets-count
previous-line-string))
(match-type 'none))
;; (message "Previous non-empty line: %S with indentation: %S" previous-line-string old-indentation)
;; (message "previous-line-ends-with-terminus: %S" previous-line-ends-with-terminus)
(setq
new-indentation
previous-indentation)
;; debug stuff
(phps-mode-debug-message
(message "\ncurrent-line-string: %S" current-line-string)
(message "previous-line-string: %S" previous-line-string)
(message "previous-indentation: %S" previous-indentation))
;; (message "current-line-starts-with-closing-bracket: %S" current-line-starts-with-closing-bracket)
;; (message "current-line-starts-with-opening-bracket: %S" current-line-starts-with-opening-bracket)
;; (message "previous-line-ends-with-closing-bracket: %S" previous-line-ends-with-closing-bracket)
;; (message "previous-line-ends-with-opening-bracket: %S" previous-line-ends-with-opening-bracket)
;; (message "previous-line-ends-with-terminus: %S" previous-line-ends-with-terminus)
;; (message "previous-bracket-level: %S" previous-bracket-level)
;; (message "previous-indentation: %S" previous-indentation)
;; Case by case logic below - most specific to most general
(cond
((and
(or
(string-match-p
"^[\t ]*\\(<[^>]+>\\)+[\t ]*$"
previous-line-string)
(string-match-p
"^[\t ]*\\(<[^>]+>\\)+[\t ]*$"
current-line-string))
(not
(string-match-p
"<\\?"
previous-line-string))
(not
(string-match-p
"<\\?"
current-line-string)))
(setq
match-type
'line-after-html-line)
(when-let ((html-bracket-level
(phps-mode-indent--get-html-string-bracket-level
previous-line-string)))
(when (> html-bracket-level 0)
(setq
new-indentation
(+ new-indentation tab-width))))
(when-let ((html-bracket-level
(phps-mode-indent--get-html-string-bracket-level
current-line-string)))
(when (< html-bracket-level 0)
(setq
new-indentation
(- new-indentation tab-width)))))
;; LINE AFTER EXTENDS / IMPLEMENTS
;; class MyClass implements
;; myInterface
;; or
;; class MyClass extends
;; myParent
((string-match-p
"[\t ]+\\(extends\\|implements\\)$"
previous-line-string)
(setq
match-type
'line-after-extends-or-implements)
(setq
new-indentation
(+ new-indentation tab-width)))
;; LINE AFTER EXTENDS / IMPLEMENTS that starts on new line
;; class MyClass
;; implements myInterface
;; or
;; class MyClass
;; extends myParent
;; or
;; class MyClass
;; extends myParent
;; implements MyInterface
((string-match-p
"^[\t ]*\\(extends\\|implements\\)"
current-line-string)
(setq
match-type
'line-after-extends-or-implements2)
(when-let ((backwards-string
(phps-mode-indent--backwards-looking-at
"\n+\\([\t ]*\\)class[\n\t ]+[a-zA-Z0-9_]+[\n\t ]+\\(extends[\n\t ]+[a-zA-Z0-9_]+\\)?[\n\t ]*\\(implements[\n\t ]+[a-zA-Z0-9_]+\\)?\\'")))
(let ((old-indentation
(length
(match-string 1 backwards-string))))
(setq
new-indentation
(+ old-indentation tab-width)))))
;; CLASS BODY AFTER implements and extends
;; class MyClass implements
;; myInterface,
;; myInterface2
;; {
;; ignore case
;; class MyClass implements myInterface, myInterface2
;; {
((and
current-line-starts-with-opening-bracket
(string= current-line-starts-with-opening-bracket "{")
(phps-mode-indent--backwards-looking-at
"[\n\t ]+implements\\([\n\t ]+[\\a-zA-Z_0-9_]+,?\\)+[\n\t ]*{\\'")
(not
(string-match-p
"[\t ]*\\(class\\|interface\\)[\t ]+"
previous-line-string)))
(setq
match-type
'class-body-after-extends-or-implements)
(setq
new-indentation
(- new-indentation tab-width)))
;; LINE AFTER OPENING INLINE OR ALTERNATIVE CONTROL STRUCTURE
;; if (true)
;; echo 'Something';
;; or
;; while (true)
;; echo 'Something';
;; or
;; if (true):
;; echo 'Something';
;; or
;; while (true):
;; echo 'Something';
;; or
;; for ($i = 0; $i < 10; $i++):
;; echo 'Something';
;; or
;; foreach ($array as $value):
;; echo 'Something';
((and
current-line-ends-with-terminus
(string= current-line-ends-with-terminus ";")
(string-match-p
"^[\t ]*\\(if\\|while\\|for\\|foreach\\)[\t ]*(.+):?$"
previous-line-string))
(setq
match-type
'line-after-inline-or-alternative-control-structure)
(setq
new-indentation
(+ new-indentation tab-width)))
;; LINE AFTER FUNCTION CALL WITH NAMED ARGUMENT
;; arg1:
;; $something
((and
(string-match-p
"^[\t ]*[a-zA-Z0-9_]+[\t ]*:[\t ]*$"
previous-line-string))
(setq
match-type
'line-after-function-call-with-named-argument)
(setq
new-indentation
(+ new-indentation tab-width)))
;; LINE AFTER INLINE OR ALTERNATIVE ELSE / ELSEIF CONTROL STRUCTURE
;; else
;; echo 'Something';
;; or
;; else if (true)
;; echo 'Something';
;; or
;; elseif (true)
;; echo 'Something';
;; or
;; else:
;; echo 'Something';
;; or
;; else if (true):
;; echo 'Something';
;; or
;; elseif (true):
;; echo 'Something';
((and
current-line-ends-with-terminus
(string=
current-line-ends-with-terminus
";")
(string-match-p
"^[\t ]*else\\([\t ]*$\\|.*\\()\\|:\\)$\\)"
previous-line-string))
(setq
match-type
'line-after-inline-or-alternative-else)
(setq
new-indentation
(+ new-indentation tab-width)))
;; LINE AFTER LINE INSIDE INLINE OR ALTERNATIVE CONTROL STRUCTURE
;; if (true)
;; echo 'Something';
;; else
;; or
;; if (true):
;; echo 'Something';
;; else:
;; or
;; if (true)
;; echo 'Something';
;; elseif (false)
;; or
;; if (true):
;; echo 'Something';
;; elseif (false):
;; or
;; if (true):
;; echo 'Something';
;; endif;
;; or
;; while (true):
;; echo 'Something';
;; endwhile;
;; or
;; for ($i = 0; $i < 10; $i++):
;; echo 'Something';
;; endfor;
;; or
;; foreach ($array as $value):
;; echo 'Something';
;; endforeach;
((and
previous-line-ends-with-terminus
(string=
previous-line-ends-with-terminus
";")
(string-match-p
"^[\t ]*\\(else:?[\t ]*$\\|else.*):?$\\|endif;[\t ]*$\\|endfor;[\t ]*$\\|endforeach;[\t ]*$\\|endwhile;[\t ]*$\\)"
current-line-string))
(setq
match-type
'line-after-line-inside-inline-or-alternative-control-structure)
(setq
new-indentation
(-
new-indentation
tab-width)))
;; LINE AFTER LINE INSIDE INLINE CONTROL STRUCTURE
;; if (true)
;; echo 'Something';