mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathphp-ts-mode.el
1866 lines (1667 loc) · 74.2 KB
/
php-ts-mode.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
;;; php-ts-mode.el --- Major mode for editing PHP files using tree-sitter -*- lexical-binding: t; -*-
;; Copyright (C) 2024-2025 Free Software Foundation, Inc.
;; Author: Vincenzo Pupillo <v.pupillo@gmail.com>
;; Maintainer: Vincenzo Pupillo <v.pupillo@gmail.com>
;; Created: Jun 2024
;; Keywords: PHP language tree-sitter
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Tree-sitter language versions
;;
;; php-ts-mode is known to work with the following languages and version:
;; - tree-sitter-phpdoc: fe3202e468bc17332bec8969f2b50ff1f1da3a46
;; - tree-sitter-css: v0.23.1-1-g6a442a3
;; - tree-sitter-jsdoc: v0.23.2
;; - tree-sitter-javascript: v0.23.1-2-g108b2d4
;; - tree-sitter-html: v0.23.2-1-gd9219ad
;; - tree-sitter-php: v0.23.11
;;
;; We try our best to make builtin modes work with latest grammar
;; versions, so a more recent grammar version has a good chance to work.
;; Send us a bug report if it doesn't.
;;; Commentary:
;;
;; This package provides `php-ts-mode' which is a major mode
;; for editing PHP files with embedded HTML, JavaScript, CSS and phpdoc.
;; Tree Sitter is used to parse each of these languages.
;;
;; Please note that this package requires `html-ts-mode', which
;; registers itself as the major mode for editing HTML.
;;
;; This package is compatible and has been tested with the following
;; tree-sitter grammars:
;; * https://github.com/tree-sitter/tree-sitter-php
;; * https://github.com/tree-sitter/tree-sitter-html
;; * https://github.com/tree-sitter/tree-sitter-javascript
;; * https://github.com/tree-sitter/tree-sitter-jsdoc
;; * https://github.com/tree-sitter/tree-sitter-css
;; * https://github.com/claytonrcarter/tree-sitter-phpdoc
;;
;; Features
;;
;; * Indent
;; * IMenu
;; * Navigation
;; * Which-function
;; * Flymake
;; * Tree-sitter parser installation helper
;; * PHP built-in server support
;; * Shell interaction: execute PHP code in a inferior PHP process
;;; Code:
(require 'treesit)
(require 'c-ts-common) ;; For comment indent and filling.
(require 'css-mode) ;; for embed css into html
(require 'js) ;; for embed javascript into html
(require 'comint)
(treesit-declare-unavailable-functions)
(eval-when-compile
(require 'cl-lib)
(require 'rx)
(require 'subr-x))
;;; Install treesitter language parsers
(defvar php-ts-mode--language-source-alist
'((php "https://github.com/tree-sitter/tree-sitter-php" "v0.23.11" "php/src")
(phpdoc "https://github.com/claytonrcarter/tree-sitter-phpdoc")
(html "https://github.com/tree-sitter/tree-sitter-html" "v0.23.2")
(javascript "https://github.com/tree-sitter/tree-sitter-javascript" "v0.23.1")
(jsdoc "https://github.com/tree-sitter/tree-sitter-jsdoc" "v0.23.2")
(css "https://github.com/tree-sitter/tree-sitter-css" "v0.23.1"))
"Treesitter language parsers required by `php-ts-mode'.
You can customize `treesit-language-source-alist' if you want
to stick to a specific commit and/or use different parsers.")
(setq treesit-language-source-alist
(append treesit-language-source-alist
php-ts-mode--language-source-alist))
(defun php-ts-mode-install-parsers ()
"Install all the required treesitter parsers.
`php-ts-mode--language-source-alist' defines which parsers to install."
(interactive)
(dolist (item php-ts-mode--language-source-alist)
(treesit-install-language-grammar (car item))))
;;; Custom variables
(defgroup php-ts-mode nil
"Major mode for editing PHP files."
:prefix "php-ts-mode-"
:group 'languages)
(defcustom php-ts-mode-indent-offset 4
"Number of spaces for each indentation step in `php-ts-mode'."
:tag "PHP indent offset"
:version "30.1"
:type 'integer
:safe 'integerp)
(defcustom php-ts-mode-js-css-indent-offset 2
"JavaScript and CSS indent spaces related to the <script> and <style> HTML tags.
By default should have same value as `html-ts-mode-indent-offset'."
:tag "PHP javascript or css indent offset"
:version "30.1"
:type 'integer
:safe 'integerp)
(defcustom php-ts-mode-css-fontify-colors t
"Whether CSS colors should be fontified using the color as the background.
If non-nil, text representing a CSS color will be fontified
such that its background is the color itself.
Works like `css--fontify-region'."
:tag "PHP colors the CSS properties values."
:version "30.1"
:type 'boolean
:safe 'booleanp)
(defcustom php-ts-mode-php-default-executable (or (executable-find "php") "/usr/bin/php")
"The default PHP executable."
:tag "PHP Executable"
:version "30.1"
:type 'file)
(defvar-local php-ts-mode-alternative-php-program-name nil
"An alternative to the usual `php' program name.
In non-nil, `php-ts-mode--executable' try to find this executable.")
(defcustom php-ts-mode-php-config nil
"The location of php.ini file.
If nil the default one is used to run the embedded webserver or
inferior PHP process."
:tag "PHP Init file"
:version "30.1"
:type '(choice (const :tag "Default init file" nil) file))
(defcustom php-ts-mode-ws-hostname "localhost"
"The hostname that will be served by the PHP built-in webserver.
If nil then `php-ts-mode-run-php-webserver' will ask you for the hostname.
See `https://www.php.net/manual/en/features.commandline.webserver.php'."
:tag "PHP built-in web server hostname"
:version "30.1"
:type 'string
:safe 'stringp)
(defcustom php-ts-mode-ws-port nil
"The port on which the PHP built-in webserver will listen.
If nil `php-ts-mode-run-php-webserver' will ask you for the port number."
:tag "PHP built-in web server port"
:version "30.1"
:type '(choice (const :tag "Ask which port to use" nil) integer)
:safe 'integer-or-null-p)
(defcustom php-ts-mode-ws-document-root nil
"The root of the documents that the PHP built-in webserver will serve.
If nil `php-ts-mode-run-php-webserver' will ask you for the document root."
:tag "PHP built-in web server document root"
:version "30.1"
:type '(choice (const :tag "Ask for document root" nil) directory))
(defcustom php-ts-mode-ws-workers nil
"The number of workers the PHP built-in webserver will fork.
Useful for testing code against multiple simultaneous requests."
:tag "PHP built-in number of workers"
:version "30.1"
:type '(choice (const :tag "No workers" nil) integer)
:safe 'integer-or-null-p)
(defcustom php-ts-mode-inferior-php-buffer "*PHP*"
"Name of the inferior PHP buffer."
:tag "PHP inferior process buffer name"
:version "30.1"
:type 'string
:safe 'stringp)
(defcustom php-ts-mode-inferior-history nil
"File used to save command history of the inferior PHP process."
:tag "PHP inferior process history file."
:version "30.1"
:type '(choice (const :tag "None" nil) file)
:safe 'string-or-null-p)
(defvar php-ts-mode--inferior-prompt "php >"
"Prompt used by PHP inferior process.")
(defun php-ts-mode--indent-style-setter (sym val)
"Custom setter for `php-ts-mode-set-style'.
Apart from setting the default value of SYM to VAL, also change
the value of SYM in `php-ts-mode' buffers to VAL.
SYM should be `php-ts-mode-indent-style', and VAL should be a style
symbol."
(set-default sym val)
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when (derived-mode-p 'php-ts-mode)
(php-ts-mode-set-style val)))))
;; taken from c-ts-mode
(defun php-ts-indent-style-safep (style)
"Non-nil if STYLE's value is safe for file-local variables."
(and (symbolp style) (not (functionp style))))
(defcustom php-ts-mode-indent-style 'psr2
"Style used for indentation.
The selected style could be one of:
`PSR-2/PSR-12' - use PSR standards (PSR-2, PSR-12), this is the default.
`PEAR' - use coding styles preferred for PEAR code and modules.
`Drupal' - use coding styles preferred for working with Drupal projects.
`WordPress' - use coding styles preferred for working with WordPress projects.
`Symfony' - use coding styles preferred for working with Symfony projects.
`Zend' - use coding styles preferred for working with Zend projects.
If one of the supplied styles doesn't suffice, a function could be
set instead. This function is expected return a list that
follows the form of `treesit-simple-indent-rules'."
:tag "PHP indent style"
:version "30.1"
:type '(choice (const :tag "PSR-2/PSR-12" psr2)
(const :tag "PEAR" pear)
(const :tag "Drupal" drupal)
(const :tag "WordPress" wordpress)
(const :tag "Symfony" symfony)
(const :tag "Zend" zend)
(function :tag "A function for user customized style" ignore))
:set #'php-ts-mode--indent-style-setter
:safe #'php-ts-indent-style-safep)
;;; Flymake integration
;; based on lua-ts-mode
(defvar-local php-ts-mode--flymake-process nil
"Store the Flymake process.")
;; TODO: add phpmd and phpcs
(defun php-ts-mode-flymake-php (report-fn &rest _args)
"PHP backend for Flymake.
Calls REPORT-FN directly."
(when (process-live-p php-ts-mode--flymake-process)
(kill-process php-ts-mode--flymake-process))
(let ((source (current-buffer))
(diagnostics-pattern (eval-when-compile
(rx bol (? "PHP ") ;; every diagnostic line start with PHP
(group (or "Fatal" "Parse")) ;; 1: type
" error:" (+ (syntax whitespace))
(group (+? nonl)) ;; 2: msg
" in " (group (+? nonl)) ;; 3: file
" on line " (group (+ num)) ;; 4: line
eol))))
(save-restriction
(widen)
(setq php-ts-mode--flymake-process
(make-process
:name "php-ts-mode-flymake"
:noquery t
:connection-type 'pipe
:buffer (generate-new-buffer " *php-ts-mode-flymake*")
:command `(,(php-ts-mode--executable)
"-l" "-d" "display_errors=0")
:sentinel
(lambda (proc _event)
(when (eq 'exit (process-status proc))
(unwind-protect
(if (with-current-buffer source
(eq proc php-ts-mode--flymake-process))
(with-current-buffer (process-buffer proc)
(goto-char (point-min))
(let (diags)
(while (search-forward-regexp
diagnostics-pattern
nil t)
(let* ((beg
(car (flymake-diag-region
source
(string-to-number (match-string 4)))))
(end
(cdr (flymake-diag-region
source
(string-to-number (match-string 4)))))
(msg (match-string 2))
(type :error))
(push (flymake-make-diagnostic
source beg end type msg)
diags)))
(funcall report-fn diags)))
(flymake-log :warning "Canceling obsolete check %s" proc))
(kill-buffer (process-buffer proc)))))))
(process-send-region php-ts-mode--flymake-process (point-min) (point-max))
(process-send-eof php-ts-mode--flymake-process))))
;;; Utils
(defun php-ts-mode--executable ()
"Return the absolute filename of the php executable.
If the `default-directory' is remote, search on a remote host, otherwise
it searches locally. If `php-ts-mode-alternative-php-program-name' is
non-zero, it searches for this program instead of the usual `php'.
If the search fails, it returns `php-ts-mode-php-default-executable'."
(or (executable-find
(or php-ts-mode-alternative-php-program-name "php") t)
php-ts-mode-php-default-executable))
(defun php-ts-mode--get-indent-style ()
"Helper function to set indentation style.
MODE can be `psr2', `pear', `drupal', `wordpress', `symfony', `zend'."
(let ((style
(if (functionp php-ts-mode-indent-style)
(funcall php-ts-mode-indent-style)
(cl-case php-ts-mode-indent-style
(psr2 (alist-get 'psr2 (php-ts-mode--indent-styles)))
(pear (alist-get 'pear (php-ts-mode--indent-styles)))
(drupal (alist-get 'drupal (php-ts-mode--indent-styles)))
(wordpress (alist-get 'wordpress (php-ts-mode--indent-styles)))
(symfony (alist-get 'symfony (php-ts-mode--indent-styles)))
(zend (alist-get 'zend (php-ts-mode--indent-styles)))
(t (alist-get 'psr2 (php-ts-mode--indent-styles)))))))
`((php ,@style))))
(defun php-ts-mode--prompt-for-style ()
"Prompt for an indent style and return the symbol for it."
(intern
(completing-read
"Style: "
(mapcar #'car (php-ts-mode--indent-styles))
nil t nil nil "default")))
(defun php-ts-mode-set-global-style (style)
"Set the indent style of PHP modes globally to STYLE.
This changes the current indent style of every PHP buffer and
the default PHP indent style for `php-ts-mode'
in this Emacs session."
(interactive (list (php-ts-mode--prompt-for-style)))
(php-ts-mode--indent-style-setter 'php-ts-mode-indent-style style))
(defun php-ts-mode--set-indent-property (style)
"Set the offset, tab, etc. according to STYLE."
(cl-case style
(psr2 (setq php-ts-mode-indent-offset 4
tab-width 4
indent-tabs-mode nil))
(pear (setq php-ts-mode-indent-offset 4
tab-width 4
indent-tabs-mode nil))
(drupal (setq php-ts-mode-indent-offset 2
tab-width 2
indent-tabs-mode nil))
(wordpress (setq php-ts-mode-indent-offset 4
tab-width 4
indent-tabs-mode t))
(symfony (setq php-ts-mode-indent-offset 4
tab-width 4
indent-tabs-mode nil))
(zend (setq php-ts-mode-indent-offset 4
tab-width 4
indent-tabs-mode nil))))
(defun php-ts-mode-set-style (style)
"Set the PHP indent style of the current buffer to STYLE.
To set the default indent style globally, use
`php-ts-mode-set-global-style'."
(interactive (list (php-ts-mode--prompt-for-style)))
(cond
((not (derived-mode-p 'php-ts-mode))
(user-error "The current buffer is not in `php-ts-mode'"))
((equal php-ts-mode-indent-style style)
(message "The style is already %s" style));; nothing to do
(t (progn
(setq-local php-ts-mode-indent-style style)
(php-ts-mode--set-indent-property style)
(let ((rules (assq-delete-all 'php treesit-simple-indent-rules))
(new-style (car (treesit--indent-rules-optimize
(php-ts-mode--get-indent-style)))))
(setq treesit-simple-indent-rules (cons new-style rules))
(message "Switch to %s style" style))))))
(defun php-ts-mode--get-parser-ranges ()
"Return the ranges covered by the parsers.
`php-ts-mode' use five parsers, this function returns, for the
current buffer, the ranges covered by each parser.
Useful for debugging."
(let ((ranges)
(parsers (treesit-parser-list nil nil t)))
(if (not parsers)
(message "At least one parser must be initialized"))
(cl-loop
for parser in parsers
do (push (list parser (treesit-parser-included-ranges parser)) ranges)
finally return ranges)))
;;; Syntax table
(defvar php-ts-mode--syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?\\ "\\" table)
(modify-syntax-entry ?+ "." table)
(modify-syntax-entry ?- "." table)
(modify-syntax-entry ?= "." table)
(modify-syntax-entry ?% "." table)
(modify-syntax-entry ?< "." table)
(modify-syntax-entry ?> "." table)
(modify-syntax-entry ?& "." table)
(modify-syntax-entry ?| "." table)
(modify-syntax-entry ?\' "\"" table)
(modify-syntax-entry ?\240 "." table)
(modify-syntax-entry ?/ ". 124b" table)
(modify-syntax-entry ?* ". 23" table)
(modify-syntax-entry ?\n "> b" table)
(modify-syntax-entry ?\^m "> b" table)
;; php specific syntax
(modify-syntax-entry ?_ "w" table)
(modify-syntax-entry ?` "\"" table)
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?\r "> b" table)
(modify-syntax-entry ?# "< b" table)
(modify-syntax-entry ?$ "_" table)
table)
"Syntax table for `php-ts-mode'.")
;;; Indent
(defconst php-ts-mode--possibly-braceless-keyword-re
(regexp-opt '("if" "for" "foreach" "while" "do") 'symbols)
"Regexp matching keywords optionally followed by an opening brace.")
(defun php-ts-mode--open-statement-group-heuristic (node _parent bol &rest _)
"Heuristic matcher for statement-group without closing bracket.
Return `php-ts-mode-indent-offset' plus 1 when BOL is after
`php-ts-mode--possibly-braceless-keyword-re', otherwise return 0. It's
useful for matching incomplete compound_statement or colon_block.
PARENT is NODE's parent, BOL is the beginning of non-whitespace
characters of the current line."
(and (null node)
(save-excursion
(forward-line -1)
(if (re-search-forward
php-ts-mode--possibly-braceless-keyword-re
bol t)
(+ 1 php-ts-mode-indent-offset)
0))))
;; taken from c-ts-mode
(defun php-ts-mode--else-heuristic (node parent bol &rest _)
"Heuristic matcher for when \"else\" is followed by a closing bracket.
PARENT is NODE's parent, BOL is the beginning of non-whitespace
characters of the current line."
(and (null node)
(save-excursion
(forward-line -1)
(looking-at (rx (* whitespace) "else" (* whitespace) eol)))
(let ((next-node (treesit-node-first-child-for-pos parent bol)))
(equal (treesit-node-type next-node) "}"))))
;; taken from c-ts-mode
(defun php-ts-mode--first-sibling (node parent &rest _)
"Matches when NODE is the \"first sibling\".
\"First sibling\" is defined as: the first child node of PARENT
such that it's on its own line. NODE is the node to match and
PARENT is its parent."
(let ((prev-sibling (treesit-node-prev-sibling node t)))
(or (null prev-sibling)
(save-excursion
(goto-char (treesit-node-start prev-sibling))
(<= (line-beginning-position)
(treesit-node-start parent)
(line-end-position))))))
(defun php-ts-mode--js-css-tag-bol (_node parent &rest _)
"Find the first non-space characters of html tags <script> or <style>.
Return `line-beginning-position' when `treesit-node-at' is HTML or PHP.
Otherwise go to the PARENT and search backward for <script> or <style> tags.
Should be used only for Javascript or CSS indenting rules.
NODE, ignored, is the node to match and PARENT is its parent."
(let ((lang (treesit-language-at (point))))
(if (or (eq lang 'javascript)
(eq lang 'css))
(save-excursion
(goto-char (treesit-node-start parent))
(re-search-backward "<script.*>\\|<style.*>" nil t))
(line-beginning-position))))
(defun php-ts-mode--parent-eol (_node parent &rest _)
"Find the last non-space characters of the PARENT of the current NODE.
NODE is the node to match and PARENT is its parent."
(save-excursion
(goto-char (treesit-node-start parent))
(line-end-position)))
(defun php-ts-mode--parent-html-bol (node parent bol &rest _)
"Find the first non-space characters of the HTML tags before NODE.
When NODE is nil call `php-ts-mode--open-statement-group-heuristic'.
PARENT is NODE's parent, BOL is the beginning of non-whitespace
characters of the current line."
(if (null node)
;; If NODE is nil it could be an open statement-group.
(php-ts-mode--open-statement-group-heuristic node parent bol)
(save-excursion
(let ((html-node (treesit-search-forward node "text" t)))
(if html-node
(let ((end-html (treesit-node-end html-node)))
(goto-char end-html)
(backward-word)
(back-to-indentation)
(point))
(treesit-node-start parent))))))
(defun php-ts-mode--parent-html-heuristic (node parent bol &rest _)
"Return position based on html indentation.
Returns 0 if the NODE is after the </html>, otherwise returns the
indentation point of the last word before the NODE, plus the indentation
offset. If there is no HTML tag, it returns the beginning of the
parent. When NODE is nil call `php-ts-mode--open-statement-group-heuristic'.
It can be used when you want to indent PHP code relative to the HTML.
PARENT is NODE's parent, BOL is the beginning of non-whitespace
characters of the current line."
(if (null node)
;; If NODE is nil it could be an open statement-group.
(php-ts-mode--open-statement-group-heuristic node parent bol)
(let ((html-node (treesit-search-forward node "text" t)))
(if html-node
(let ((end-html (treesit-node-end html-node)))
(save-excursion
(goto-char end-html)
(backward-word)
(back-to-indentation)
(if (search-forward "</html>" end-html t 1)
0
(+ (point) php-ts-mode-indent-offset))))
;; Maybe it's better to use bol?
(treesit-node-start parent)))))
(defun php-ts-mode--array-element-heuristic (_node parent _bol &rest _)
"Return of the position of the first element of the array.
PARENT is NODE's parent, BOL is the beginning of non-whitespace
characters of the current line."
(let ((parent-start
(treesit-node-start parent))
(parent-first-child-start
(treesit-node-start (treesit-node-child parent 2))))
(if (equal
(line-number-at-pos parent-start)
(line-number-at-pos parent-first-child-start))
;; if array_creation_expression and the first
;; array_element_initializer are on the same same line
parent-first-child-start
;; else return parent-bol plus the offset
(save-excursion
(goto-char (treesit-node-start parent))
(back-to-indentation)
(+ (point) php-ts-mode-indent-offset)))))
(defun php-ts-mode--anchor-first-sibling (_node parent _bol &rest _)
"Return the start of the first child of a sibling of PARENT.
If the fist sibling of PARENT and the first child of the sibling are
on the same line return the start position of the first child of the
sibling. Otherwise return the start of the first sibling.
PARENT is NODE's parent, BOL is the beginning of non-whitespace
characters of the current line."
(let ((first-sibling-start
(treesit-node-start (treesit-node-child parent 0)))
(first-sibling-child-start
(treesit-node-start (treesit-node-child parent 1))))
(if (equal
(line-number-at-pos first-sibling-start)
(line-number-at-pos first-sibling-child-start))
;; if are on the same line return the child start
first-sibling-child-start
first-sibling-start)))
;; adapted from c-ts-mode--anchor-prev-sibling
(defun php-ts-mode--anchor-prev-sibling (node parent bol &rest _)
"Return the start of the previous named sibling of NODE.
Return nil if a) there is no prev-sibling, or b) prev-sibling
doesn't have a child.
PARENT is NODE's parent, BOL is the beginning of non-whitespace
characters of the current line."
(when-let* ((prev-sibling
(or (treesit-node-prev-sibling node t)
(treesit-node-prev-sibling
(treesit-node-first-child-for-pos parent bol) t)
(treesit-node-child parent -1 t)))
(continue t))
(save-excursion
(while (and prev-sibling continue)
(goto-char (treesit-node-start prev-sibling))
(if (looking-back (rx bol (* whitespace))
(line-beginning-position))
(setq continue nil)
(setq prev-sibling
(treesit-node-prev-sibling prev-sibling)))))
(treesit-node-start prev-sibling)))
(defun php-ts-mode--indent-styles ()
"Indent rules supported by `php-ts-mode'."
(let ((common
`((php-ts-mode--else-heuristic prev-line php-ts-mode-indent-offset)
((query "(ERROR (ERROR)) @indent") column-0 0)
((node-is ")") parent-bol 0)
((node-is "]") parent-bol 0)
((node-is "else_clause") parent-bol 0)
((node-is "case_statement") parent-bol php-ts-mode-indent-offset)
((node-is "default_statement") parent-bol php-ts-mode-indent-offset)
((parent-is "default_statement") parent-bol php-ts-mode-indent-offset)
((and
(parent-is "expression_statement")
(node-is ";"))
parent-bol 0)
((parent-is "expression_statement") parent-bol php-ts-mode-indent-offset)
;; `c-ts-common-looking-at-star' has to come before
;; `c-ts-common-comment-2nd-line-matcher'.
((and (parent-is "comment") c-ts-common-looking-at-star)
c-ts-common-comment-start-after-first-star -1)
(c-ts-common-comment-2nd-line-matcher
c-ts-common-comment-2nd-line-anchor
1)
((parent-is "comment") prev-adaptive-prefix 0)
((parent-is "method_declaration") parent-bol 0)
((node-is "class_interface_clause") parent-bol php-ts-mode-indent-offset)
((query "(class_interface_clause (name) @indent)") php-ts-mode--parent-eol 1)
((query "(class_interface_clause (qualified_name) @indent)")
parent-bol php-ts-mode-indent-offset)
((parent-is "class_declaration") parent-bol 0)
((parent-is "namespace_use_declaration") parent-bol php-ts-mode-indent-offset)
((parent-is "namespace_use_group") parent-bol php-ts-mode-indent-offset)
((parent-is "function_definition") parent-bol 0)
((parent-is "member_call_expression") first-sibling php-ts-mode-indent-offset)
((parent-is "conditional_expression") parent-bol php-ts-mode-indent-offset)
((parent-is "assignment_expression") parent-bol php-ts-mode-indent-offset)
((parent-is "array_creation_expression") parent-bol php-ts-mode-indent-offset)
((parent-is "parenthesized_expression") first-sibling 1)
((parent-is "binary_expression") parent 0)
((or (parent-is "arguments")
(parent-is "formal_parameters"))
parent-bol php-ts-mode-indent-offset)
((query "(for_statement (assignment_expression left: (_)) @indent)")
parent-bol php-ts-mode-indent-offset)
((query "(for_statement (binary_expression left: (_)) @indent)")
parent-bol php-ts-mode-indent-offset)
((query "(for_statement (update_expression (_)) @indent)")
parent-bol php-ts-mode-indent-offset)
((query "(function_call_expression arguments: (_) @indent)")
parent php-ts-mode-indent-offset)
((query "(member_call_expression arguments: (_) @indent)")
parent php-ts-mode-indent-offset)
((query "(scoped_call_expression name: (_) @indent)")
parent php-ts-mode-indent-offset)
((parent-is "scoped_property_access_expression")
parent php-ts-mode-indent-offset)
;; Closing bracket. Must stay here, the rule order matter.
((node-is "}") standalone-parent 0)
;; handle multiple single line comment that start at the and of a line
((match "comment" "declaration_list") php-ts-mode--anchor-prev-sibling 0)
((parent-is "declaration_list") column-0 php-ts-mode-indent-offset)
((parent-is "initializer_list") parent-bol php-ts-mode-indent-offset)
;; Statement in {} blocks.
((or (and (or (parent-is "compound_statement")
(parent-is "colon_block"))
;; If the previous sibling(s) are not on their
;; own line, indent as if this node is the first
;; sibling
php-ts-mode--first-sibling)
(or (match null "compound_statement")
(match null "colon_block")))
standalone-parent php-ts-mode-indent-offset)
((or (parent-is "compound_statement")
(parent-is "colon_block"))
parent-bol php-ts-mode-indent-offset)
;; Opening bracket.
((or (node-is "compound_statement")
(node-is "colon_block"))
standalone-parent php-ts-mode-indent-offset)
((parent-is "match_block") parent-bol php-ts-mode-indent-offset)
((parent-is "switch_block") parent-bol 0)
;; These rules are for cases where the body is bracketless.
((match "while" "do_statement") parent-bol 0)
;; rule for PHP alternative syntax
((or (node-is "else_if_clause")
(node-is "endif")
(node-is "endfor")
(node-is "endforeach")
(node-is "endwhile"))
parent-bol 0)
((or (parent-is "if_statement")
(parent-is "else_clause")
(parent-is "for_statement")
(parent-is "foreach_statement")
(parent-is "while_statement")
(parent-is "do_statement")
(parent-is "switch_statement")
(parent-is "case_statement")
(parent-is "empty_statement"))
parent-bol php-ts-mode-indent-offset)
;; Workaround: handle "for" open statement group. Currently
;; the grammar handles it differently than other control structures.
(no-node php-ts-mode--open-statement-group-heuristic 0))))
`((psr2
((parent-is "program") php-ts-mode--open-statement-group-heuristic 0)
((parent-is "text_interpolation") column-0 0)
((parent-is "function_call_expression") parent-bol php-ts-mode-indent-offset)
,@common)
(pear
((parent-is "program") php-ts-mode--parent-html-heuristic 0)
((parent-is "text_interpolation") php-ts-mode--parent-html-heuristic 0)
((or (node-is "case_statement")
(node-is "default_statement"))
parent-bol 0)
((parent-is "binary_expression") parent-bol php-ts-mode-indent-offset)
,@common)
(drupal
((parent-is "program") php-ts-mode--parent-html-heuristic 0)
((parent-is "text_interpolation") php-ts-mode--parent-html-bol 0)
((parent-is "if_statement") parent-bol 0)
((parent-is "binary_expression") parent-bol php-ts-mode-indent-offset)
((parent-is "function_call_expression") parent-bol php-ts-mode-indent-offset)
,@common)
(symfony
((parent-is "function_call_expression") parent-bol php-ts-mode-indent-offset)
,@common)
(wordpress
((parent-is "program") php-ts-mode--parent-html-bol 0)
((parent-is "text_interpolation") php-ts-mode--parent-html-bol 0)
,@common)
(zend
((parent-is "class_interface_clause") php-ts-mode--anchor-first-sibling 0)
((parent-is "function_call_expression") first-sibling 0)
((parent-is "array_creation_expression") php-ts-mode--array-element-heuristic 0)
,@common))))
(defvar php-ts-mode--phpdoc-indent-rules
'((phpdoc
((and (parent-is "document") c-ts-common-looking-at-star)
c-ts-common-comment-start-after-first-star -1)
(c-ts-common-comment-2nd-line-matcher
c-ts-common-comment-2nd-line-anchor
1)))
"Tree-sitter indentation rules for `phpdoc'.")
;;; Font-lock
(defconst php-ts-mode--keywords
'("abstract" "and" "array" "as" "break" "callable" "case" "catch"
"class" "clone" "const" "continue" "declare" "default" "do" "echo"
"else" "elseif" "enddeclare" "endfor" "endforeach" "endif"
"endswitch" "endwhile" "enum" "exit" "extends" "final" "finally" "fn"
"for" "foreach" "from" "function" "global" "goto" "if" "implements"
"include" "include_once" "instanceof" "insteadof" "interface"
"list" "match" "namespace" "new" "null" "or" "print" "private"
"protected" "public" "readonly" "require" "require_once" "return"
"static" "switch" "throw" "trait" "try" "unset" "use" "while" "xor"
"yield")
"PHP keywords for tree-sitter font-locking.")
(defconst php-ts-mode--operators
'("--" "**=" "*=" "/=" "%=" "+=" "-=" ".=" "<<=" ">>=" "&=" "^="
"|=" "??" "??=" "||" "&&" "|" "^" "&" "==" "!=" "<>" "===" "!=="
"<" ">" "<=" ">=" "<=>" "<<" ">>" "+" "-" "." "*" "**" "/" "%"
"->" "?->" "...")
"PHP operators for tree-sitter font-locking.")
(defconst php-ts-mode--predefined-constant
'(;; predefined constant
"PHP_VERSION" "PHP_MAJOR_VERSION" "PHP_MINOR_VERSION"
"PHP_RELEASE_VERSION" "PHP_VERSION_ID" "PHP_EXTRA_VERSION"
"ZEND_THREAD_SAFE" "ZEND_DEBUG_BUILD" "PHP_ZTS" "PHP_DEBUG"
"PHP_MAXPATHLEN" "PHP_OS" "PHP_OS_FAMILY" "PHP_SAPI" "PHP_EOL"
"PHP_INT_MAX" "PHP_INT_MIN" "PHP_INT_SIZE" "PHP_FLOAT_DIG"
"PHP_FLOAT_EPSILON" "PHP_FLOAT_MIN" "PHP_FLOAT_MAX"
"PHP_WINDOWS_EVENT_CTRL_C" "PHP_WINDOWS_EVENT_CTRL_BREAK"
"DEFAULT_INCLUDE_PATH" "PEAR_INSTALL_DIR" "PEAR_EXTENSION_DIR"
"PHP_EXTENSION_DIR" "PHP_PREFIX" "PHP_BINDIR" "PHP_BINARY"
"PHP_MANDIR" "PHP_LIBDIR" "PHP_DATADIR" "PHP_SYSCONFDIR"
"PHP_LOCALSTATEDIR" "PHP_CONFIG_FILE_PATH" "PHP_CONFIG_FILE_SCAN_DIR"
"PHP_SHLIB_SUFFIX" "PHP_FD_SETSIZE" "E_ERROR" "E_WARNING" "E_PARSE"
"E_NOTICE" "E_CORE_ERROR" "E_CORE_WARNING" "E_COMPILE_ERROR"
"E_COMPILE_WARNING" "E_USER_ERROR" "E_USER_WARNING"
"E_USER_NOTICE" "E_DEPRECATED" "E_USER_DEPRECATED"
"E_ALL" "E_STRICT"
;; math constant
"M_PI" "M_E" "M_LOG2E" "M_LOG10E" "M_LN2" "M_LN10" "M_PI_2"
"M_PI_4" "M_1_PI" "M_2_PI" "M_SQRTPI" "M_2_SQRTPI" "M_SQRT2"
"M_SQRT3" "M_SQRT1_2" "M_LNPI" "M_EULER" "PHP_ROUND_HALF_UP"
"PHP_ROUND_HALF_DOWN" "PHP_ROUND_HALF_EVEN" "PHP_ROUND_HALF_ODD"
"NAN" "INF"
;; magic constant
"__COMPILER_HALT_OFFSET__" "__CLASS__" "__DIR__" "__FILE__"
"__FUNCTION__" "__LINE__" "__METHOD__" "__NAMESPACE__" "__TRAIT__")
"PHP predefined constant.")
(defconst php-ts-mode--class-magic-methods
'("__construct" "__destruct" "__call" "__callStatic" "__get" "__set"
"__isset" "__unset" "__sleep" "__wakeup" "__serialize" "__unserialize"
"__toString" "__invoke" "__set_state" "__clone" "__debugInfo")
"PHP predefined magic methods.")
(defun php-ts-mode--test-namespace-name-as-prefix-p ()
"Return t if namespace_name_as_prefix is a named node, nil otherwise."
(treesit-query-valid-p 'php "(namespace_name_as_prefix)"))
(defun php-ts-mode--test-namespace-aliasing-clause-p ()
"Return t if namespace_aliasing_clause is a named node, nil otherwise."
(treesit-query-valid-p 'php "(namespace_aliasing_clause)"))
(defun php-ts-mode--test-namespace-use-group-clause-p ()
"Return t if namespace_use_group_clause is a named node, nil otherwise."
(treesit-query-valid-p 'php "(namespace_use_group_clause)"))
(defun php-ts-mode--test-visibility-modifier-operation-clause-p ()
"Return t if (visibility_modifier (operation)) is defined, nil otherwise."
(treesit-query-valid-p 'php "(visibility_modifier (operation))"))
(defun php-ts-mode--test-property-hook-clause-p ()
"Return t if property_hook is a named node, nil otherwise."
(treesit-query-valid-p 'php "(property_hook)"))
(defun php-ts-mode--font-lock-settings ()
"Tree-sitter font-lock settings."
(treesit-font-lock-rules
:language 'php
:feature 'keyword
:override t
`([,@php-ts-mode--keywords] @font-lock-keyword-face
,@(when (php-ts-mode--test-visibility-modifier-operation-clause-p)
'((visibility_modifier (operation) @font-lock-builtin-face)))
(var_modifier) @font-lock-builtin-face)
:language 'php
:feature 'comment
:override t
'((comment) @font-lock-comment-face)
:language 'php
:feature 'constant
`((boolean) @font-lock-constant-face
(null) @font-lock-constant-face
;; predefined constant or built in constant (part of PHP core)
((name) @font-lock-builtin-face
(:match ,(rx-to-string
`(: bos (or ,@php-ts-mode--predefined-constant) eos))
@font-lock-builtin-face))
;; user defined constant
((name) @font-lock-constant-face
(:match "\\`_*[A-Z][0-9A-Z_]+\\'" @font-lock-constant-face))
(const_declaration
(const_element (name) @font-lock-constant-face))
;; declare directive
(declare_directive ["strict_types" "encoding" "ticks"] @font-lock-constant-face))
:language 'php
:feature 'name
'((goto_statement (name) @font-lock-constant-face)
(named_label_statement (name) @font-lock-constant-face))
:language 'php
:feature 'delimiter
`((["," ":" ";" "\\"]) @font-lock-delimiter-face)
:language 'php
:feature 'operator
`((error_suppression_expression "@" @font-lock-keyword-face)
[,@php-ts-mode--operators] @font-lock-operator-face)
:language 'php
:feature 'variable-name
:override t
'(((name) @font-lock-keyword-face (:equal "this" @font-lock-keyword-face))
(variable_name (name) @font-lock-variable-name-face)
(relative_scope ["parent" "self" "static"] @font-lock-builtin-face)
(relative_scope) @font-lock-constant-face
(dynamic_variable_name (name) @font-lock-variable-name-face)
(member_access_expression
name: (_) @font-lock-variable-name-face)
(scoped_property_access_expression
scope: (name) @font-lock-constant-face))
:language 'php
:feature 'string
`(("\"") @font-lock-string-face
(encapsed_string) @font-lock-string-face
(string_content) @font-lock-string-face
(string) @font-lock-string-face)
:language 'php
:feature 'literal
'((integer) @font-lock-number-face
(float) @font-lock-number-face
(heredoc identifier: (heredoc_start) @font-lock-constant-face)
(heredoc_body (string_content) @font-lock-string-face)
(heredoc end_tag: (heredoc_end) @font-lock-constant-face)
(nowdoc identifier: (heredoc_start) @font-lock-constant-face)
(nowdoc_body (nowdoc_string) @font-lock-string-face)
(nowdoc end_tag: (heredoc_end) @font-lock-constant-face)
(shell_command_expression) @font-lock-string-face)
:language 'php
:feature 'type
:override t
'((union_type "|" @font-lock-operator-face)
(union_type) @font-lock-type-face
(bottom_type) @font-lock-type-face
(primitive_type) @font-lock-type-face
(cast_type) @font-lock-type-face
(named_type) @font-lock-type-face
(optional_type) @font-lock-type-face)
:language 'php
:feature 'definition
:override t
`((php_tag) @font-lock-preprocessor-face
("?>") @font-lock-preprocessor-face
;; Highlights identifiers in declarations.
(class_declaration
name: (_) @font-lock-type-face)
(class_interface_clause (name) @font-lock-type-face)
(interface_declaration
name: (_) @font-lock-type-face)
(trait_declaration
name: (_) @font-lock-type-face)
(enum_declaration
name: (_) @font-lock-type-face)
(function_definition
name: (_) @font-lock-function-name-face)
,@(when (php-ts-mode--test-property-hook-clause-p)
'((property_hook (name) @font-lock-function-name-face)))
(method_declaration
name: (_) @font-lock-function-name-face)
(method_declaration
name: (name) @font-lock-builtin-face
(:match ,(rx-to-string
`(: bos (or ,@php-ts-mode--class-magic-methods) eos))
@font-lock-builtin-face))
("=>") @font-lock-keyword-face
(object_creation_expression
(name) @font-lock-type-face)
,@(when (php-ts-mode--test-namespace-name-as-prefix-p)
'((namespace_name_as_prefix "\\" @font-lock-delimiter-face)
(namespace_name_as_prefix
(namespace_name (name)) @font-lock-type-face)))
,@(if (php-ts-mode--test-namespace-aliasing-clause-p)
'((namespace_aliasing_clause (name) @font-lock-type-face))
'((namespace_use_clause alias: (name) @font-lock-type-face)))
,@(when (not (php-ts-mode--test-namespace-use-group-clause-p))
'((namespace_use_group
(namespace_use_clause (name) @font-lock-type-face))))
(namespace_use_clause (name) @font-lock-type-face)
(namespace_name "\\" @font-lock-delimiter-face)
(namespace_name (name) @font-lock-type-face)
(use_declaration (name) @font-lock-property-use-face)
(use_instead_of_clause (name) @font-lock-type-face)
(binary_expression
operator: "instanceof"
right: (name) @font-lock-type-face))
:language 'php