Skip to content

Commit 16b3f7c

Browse files
authored
Merge pull request #482 from fabacino/bugfix/highlight-nullable-char
Highlight the ? character for nullable type hints and return types
2 parents c70d5ae + f7b6edc commit 16b3f7c

File tree

2 files changed

+64
-55
lines changed

2 files changed

+64
-55
lines changed

php-mode.el

+14-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
(defconst php-mode-version-number "1.19.1"
1616
"PHP Mode version number.")
1717

18-
(defconst php-mode-modified "2018-06-08"
18+
(defconst php-mode-modified "2018-08-28"
1919
"PHP Mode build date.")
2020

2121
;; This file is free software; you can redistribute it and/or
@@ -1698,12 +1698,21 @@ a completion list."
16981698
" \\(\\sw+\\)")
16991699
1 font-lock-type-face)
17001700

1701-
;; Highlight return types in functions and methods.
1702-
("function.+:\\s-*\\??\\(\\(?:\\sw\\|\\s_\\)+\\)" 1 font-lock-type-face)
1703-
(")\\s-*:\\s-*\\??\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*\\(?:\{\\|;\\)" 1 font-lock-type-face)
1701+
;; Highlight the ? character for nullable return types.
1702+
("function.+:\\s-*\\(\\?\\)\\(?:\\sw\\|\\s_\\|\\\\\\)+" 1 font-lock-type-face)
1703+
(")\\s-*:\\s-*\\(\\?\\)\\(?:\\sw\\|\\s_\\|\\\\\\)+\\s-*\\(?:\{\\|;\\)" 1 font-lock-type-face)
1704+
1705+
;; Highlight the ? character for nullable type hints.
1706+
("\\(\\?\\)\\(:?\\sw\\|\\s_\\|\\\\\\)+\\s-+\\$" 1 font-lock-type-face)
17041707

1705-
;; Highlight class names used as nullable type hints
1708+
;; Class names without a namespace are not highlighted at all when they
1709+
;; are used as nullable type hints or return types (both nullable and
1710+
;; non-nullable). We have to use separate regular expressions, because
1711+
;; we want to capture the class name as well, not just the ? character
1712+
;; like the regexps above.
17061713
("\\?\\(\\(:?\\sw\\|\\s_\\)+\\)\\s-+\\$" 1 font-lock-type-face)
1714+
("function.+:\\s-*\\??\\(\\(?:\\sw\\|\\s_\\)+\\)" 1 font-lock-type-face)
1715+
(")\\s-*:\\s-*\\??\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*\\(?:\{\\|;\\)" 1 font-lock-type-face)
17071716
))
17081717
"Detailed highlighting for PHP Mode.")
17091718

tests/type-hints.php.faces

+50-50
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
("function" . php-keyword)
3535
(" ")
3636
("nullableTitle" . php-function-name)
37-
("(?")
38-
("string" . font-lock-type-face)
37+
("(")
38+
("?string" . font-lock-type-face)
3939
(" ")
4040
("$" . php-variable-sigil)
4141
("title" . php-variable-name)
42-
("): ?")
43-
("string" . font-lock-type-face)
42+
("): ")
43+
("?string" . font-lock-type-face)
4444
("\n {\n }\n\n ")
4545
("public" . php-keyword)
4646
(" ")
@@ -60,13 +60,13 @@
6060
("function" . php-keyword)
6161
(" ")
6262
("nullableNumber" . php-function-name)
63-
("(?")
64-
("int" . font-lock-type-face)
63+
("(")
64+
("?int" . font-lock-type-face)
6565
(" ")
6666
("$" . php-variable-sigil)
6767
("number" . php-variable-name)
68-
("): ?")
69-
("int" . font-lock-type-face)
68+
("): ")
69+
("?int" . font-lock-type-face)
7070
("\n {\n }\n\n ")
7171
("public" . php-keyword)
7272
(" ")
@@ -86,13 +86,13 @@
8686
("function" . php-keyword)
8787
(" ")
8888
("nullableOtherNumber" . php-function-name)
89-
("(?")
90-
("float" . font-lock-type-face)
89+
("(")
90+
("?float" . font-lock-type-face)
9191
(" ")
9292
("$" . php-variable-sigil)
9393
("number" . php-variable-name)
94-
("): ?")
95-
("float" . font-lock-type-face)
94+
("): ")
95+
("?float" . font-lock-type-face)
9696
("\n {\n }\n\n ")
9797
("public" . php-keyword)
9898
(" ")
@@ -112,13 +112,13 @@
112112
("function" . php-keyword)
113113
(" ")
114114
("nullableFlag" . php-function-name)
115-
("(?")
116-
("bool" . font-lock-type-face)
115+
("(")
116+
("?bool" . font-lock-type-face)
117117
(" ")
118118
("$" . php-variable-sigil)
119119
("flag" . php-variable-name)
120-
("): ?")
121-
("bool" . font-lock-type-face)
120+
("): ")
121+
("?bool" . font-lock-type-face)
122122
("\n {\n }\n\n ")
123123
("public" . php-keyword)
124124
(" ")
@@ -138,13 +138,13 @@
138138
("function" . php-keyword)
139139
(" ")
140140
("nullableOptions" . php-function-name)
141-
("(?")
142-
("array" . font-lock-type-face)
141+
("(")
142+
("?array" . font-lock-type-face)
143143
(" ")
144144
("$" . php-variable-sigil)
145145
("options" . php-variable-name)
146-
("): ?")
147-
("array" . font-lock-type-face)
146+
("): ")
147+
("?array" . font-lock-type-face)
148148
("\n {\n }\n\n ")
149149
("public" . php-keyword)
150150
(" ")
@@ -164,13 +164,13 @@
164164
("function" . php-keyword)
165165
(" ")
166166
("nullableObject" . php-function-name)
167-
("(?")
168-
("stdClass" . font-lock-type-face)
167+
("(")
168+
("?stdClass" . font-lock-type-face)
169169
(" ")
170170
("$" . php-variable-sigil)
171171
("object" . php-variable-name)
172-
("): ?")
173-
("stdClass" . font-lock-type-face)
172+
("): ")
173+
("?stdClass" . font-lock-type-face)
174174
("\n {\n }\n\n ")
175175
("public" . php-keyword)
176176
(" ")
@@ -190,13 +190,13 @@
190190
("function" . php-keyword)
191191
(" ")
192192
("nullableNsObject" . php-function-name)
193-
("(?")
194-
("\\path\\to\\my\\Object" . font-lock-type-face)
193+
("(")
194+
("?\\path\\to\\my\\Object" . font-lock-type-face)
195195
(" ")
196196
("$" . php-variable-sigil)
197197
("object" . php-variable-name)
198-
("): ?")
199-
("\\path\\to\\my\\Object" . font-lock-type-face)
198+
("): ")
199+
("?\\path\\to\\my\\Object" . font-lock-type-face)
200200
("\n {\n }\n\n ")
201201
("public" . php-keyword)
202202
(" ")
@@ -216,13 +216,13 @@
216216
("function" . php-keyword)
217217
(" ")
218218
("nullableCallable" . php-function-name)
219-
("(?")
220-
("callable" . font-lock-type-face)
219+
("(")
220+
("?callable" . font-lock-type-face)
221221
(" ")
222222
("$" . php-variable-sigil)
223223
("callable" . php-variable-name)
224-
("): ?")
225-
("callable" . font-lock-type-face)
224+
("): ")
225+
("?callable" . font-lock-type-face)
226226
("\n {\n }\n\n ")
227227
("public" . php-keyword)
228228
(" ")
@@ -286,8 +286,8 @@
286286
("function" . php-keyword)
287287
(" ")
288288
("getNullableTitle" . php-function-name)
289-
("(\n ): ?")
290-
("string" . font-lock-type-face)
289+
("(\n ): ")
290+
("?string" . font-lock-type-face)
291291
(" {\n }\n\n ")
292292
("public" . php-keyword)
293293
(" ")
@@ -302,8 +302,8 @@
302302
("function" . php-keyword)
303303
(" ")
304304
("getNullableNumber" . php-function-name)
305-
("(\n ): ?")
306-
("int" . font-lock-type-face)
305+
("(\n ): ")
306+
("?int" . font-lock-type-face)
307307
(" {\n }\n\n ")
308308
("public" . php-keyword)
309309
(" ")
@@ -318,8 +318,8 @@
318318
("function" . php-keyword)
319319
(" ")
320320
("getNullableOtherNumber" . php-function-name)
321-
("(\n ): ?")
322-
("float" . font-lock-type-face)
321+
("(\n ): ")
322+
("?float" . font-lock-type-face)
323323
(" {\n }\n\n ")
324324
("public" . php-keyword)
325325
(" ")
@@ -334,8 +334,8 @@
334334
("function" . php-keyword)
335335
(" ")
336336
("getNullableFlag" . php-function-name)
337-
("(\n ): ?")
338-
("bool" . font-lock-type-face)
337+
("(\n ): ")
338+
("?bool" . font-lock-type-face)
339339
(" {\n }\n\n ")
340340
("public" . php-keyword)
341341
(" ")
@@ -350,8 +350,8 @@
350350
("function" . php-keyword)
351351
(" ")
352352
("getNullableOptions" . php-function-name)
353-
("(\n ): ?")
354-
("array" . font-lock-type-face)
353+
("(\n ): ")
354+
("?array" . font-lock-type-face)
355355
(" {\n }\n\n ")
356356
("public" . php-keyword)
357357
(" ")
@@ -366,8 +366,8 @@
366366
("function" . php-keyword)
367367
(" ")
368368
("getNullableObject" . php-function-name)
369-
("(\n ): ?")
370-
("stdClass" . font-lock-type-face)
369+
("(\n ): ")
370+
("?stdClass" . font-lock-type-face)
371371
(" {\n }\n\n ")
372372
("abstract" . php-keyword)
373373
(" ")
@@ -386,8 +386,8 @@
386386
("function" . php-keyword)
387387
(" ")
388388
("getOtherNullableObject" . php-function-name)
389-
("(\n ): ?")
390-
("stdClass" . font-lock-type-face)
389+
("(\n ): ")
390+
("?stdClass" . font-lock-type-face)
391391
(";\n\n ")
392392
("public" . php-keyword)
393393
(" ")
@@ -402,8 +402,8 @@
402402
("function" . php-keyword)
403403
(" ")
404404
("getNullableNsObject" . php-function-name)
405-
("(\n ): ?")
406-
("\\path\\to\\my\\Object" . font-lock-type-face)
405+
("(\n ): ")
406+
("?\\path\\to\\my\\Object" . font-lock-type-face)
407407

408408
(" {\n }\n\n ")
409409
("public" . php-keyword)
@@ -419,6 +419,6 @@
419419
("function" . php-keyword)
420420
(" ")
421421
("getNullableCallable" . php-function-name)
422-
("(\n ): ?")
423-
("callable" . font-lock-type-face)
422+
("(\n ): ")
423+
("?callable" . font-lock-type-face)
424424
(" {\n }\n}\n"))

0 commit comments

Comments
 (0)