Skip to content

Commit 81e707a

Browse files
committed
feature #173 Update the php syntax file to highlight attributes (javiereguiluz, wouterj)
This PR was merged into the main branch. Discussion ---------- Update the php syntax file to highlight attributes Alternative to #172 (as per my suggestion). `@javiereguiluz` what do you think of this? The output is slightly different from yours, but that's how highlight.js works :) I tried my suggestion of converting the latest highlight.js syntax file, this unfortunately didn't work (there are too many BC breaks in v10). So I've copy pasted their regex and used it to what is available in our syntax file already. If we want to go this way, I can also follow up with a PR adding more support for PHP features (e.g. arrow functions). For now, I've just added a couple keywords that plainly were missing (such as trait and enum). Commits ------- 020a281 Add support for PHP attributes 9764478 Add tests for highlighting PHP attributes
2 parents 35ae0d5 + 020a281 commit 81e707a

File tree

4 files changed

+233
-17
lines changed

4 files changed

+233
-17
lines changed

src/Templates/highlight.php/php.json

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,23 @@
88
"php7"
99
],
1010
"case_insensitive": true,
11-
"keywords": "and include_once list abstract global private echo interface as static endswitch array null if endwhile or const for endforeach self var while isset public protected exit foreach throw elseif include __FILE__ empty require_once do xor return parent clone use __CLASS__ __LINE__ else break print eval new catch __METHOD__ case exception default die require __FUNCTION__ enddeclare final try switch continue endfor endif declare unset true false trait goto instanceof insteadof __DIR__ __NAMESPACE__ yield finally",
11+
"keywords": "and include_once list abstract global private echo interface as static endswitch array null if endwhile or const for endforeach self var while isset public protected exit foreach throw elseif include __FILE__ empty require_once do xor return parent clone use __CLASS__ __LINE__ else break print eval new catch __METHOD__ case exception default die require __FUNCTION__ enddeclare final try match switch continue endfor endif declare unset true false goto instanceof insteadof __DIR__ __NAMESPACE__ yield finally",
1212
"contains": [
13+
{
14+
"className": "php-attribute",
15+
"begin": "#\\[\\s*(\\\\?[A-Z][A-Za-z0-9_\\x7f-\\xff]+|\\\\?[A-Z]+(?=[A-Z][a-z0-9_\\x7f-\\xff])){1,}(?![A-Za-z0-9])(?![$])",
16+
"end": "]",
17+
"contains": [
18+
{
19+
"begin": "\\(",
20+
"end": "\\)",
21+
"keywords": "true false null new array",
22+
"contains": {
23+
"$ref": "#contains.10.contains.1.contains"
24+
}
25+
}
26+
]
27+
},
1328
{
1429
"className": "comment",
1530
"begin": "#",
@@ -35,7 +50,7 @@
3550
"begin": "<\\?(php)?|\\?>"
3651
},
3752
{
38-
"$ref": "#contains.0.contains.0"
53+
"$ref": "#contains.1.contains.0"
3954
},
4055
{
4156
"className": "doctag",
@@ -54,7 +69,7 @@
5469
"begin": "@[A-Za-z]+"
5570
},
5671
{
57-
"$ref": "#contains.0.contains.0"
72+
"$ref": "#contains.1.contains.0"
5873
},
5974
{
6075
"className": "doctag",
@@ -69,7 +84,7 @@
6984
"end": false,
7085
"contains": [
7186
{
72-
"$ref": "#contains.0.contains.0"
87+
"$ref": "#contains.1.contains.0"
7388
},
7489
{
7590
"className": "doctag",
@@ -105,7 +120,7 @@
105120
]
106121
},
107122
{
108-
"$ref": "#contains.1.contains.0"
123+
"$ref": "#contains.2.contains.0"
109124
},
110125
{
111126
"className": "variable",
@@ -137,18 +152,19 @@
137152
"className": "params",
138153
"begin": "\\(",
139154
"end": "\\)",
155+
"keywords": "true false null new array",
140156
"contains": [
141157
"self",
142158
{
143-
"$ref": "#contains.7"
159+
"$ref": "#contains.8"
144160
},
145161
{
146162
"className": "comment",
147163
"begin": "/\\*",
148164
"end": "\\*/",
149165
"contains": [
150166
{
151-
"$ref": "#contains.0.contains.0"
167+
"$ref": "#contains.1.contains.0"
152168
},
153169
{
154170
"className": "doctag",
@@ -161,10 +177,10 @@
161177
"className": "string",
162178
"contains": [
163179
{
164-
"$ref": "#contains.4.contains.0"
180+
"$ref": "#contains.5.contains.0"
165181
},
166182
{
167-
"$ref": "#contains.1.contains.0"
183+
"$ref": "#contains.2.contains.0"
168184
}
169185
],
170186
"variants": [
@@ -183,7 +199,7 @@
183199
"illegal": null,
184200
"contains": [
185201
{
186-
"$ref": "#contains.4.contains.0"
202+
"$ref": "#contains.5.contains.0"
187203
}
188204
]
189205
},
@@ -194,7 +210,7 @@
194210
"illegal": null,
195211
"contains": [
196212
{
197-
"$ref": "#contains.4.contains.0"
213+
"$ref": "#contains.5.contains.0"
198214
},
199215
{
200216
"className": "subst",
@@ -230,7 +246,7 @@
230246
},
231247
{
232248
"className": "class",
233-
"beginKeywords": "class interface",
249+
"beginKeywords": "class interface trait enum",
234250
"end": "{",
235251
"excludeEnd": true,
236252
"illegal": "[:\\(\\$\"]",
@@ -239,7 +255,7 @@
239255
"beginKeywords": "extends implements"
240256
},
241257
{
242-
"$ref": "#contains.9.contains.0"
258+
"$ref": "#contains.10.contains.0"
243259
}
244260
]
245261
},
@@ -249,7 +265,7 @@
249265
"illegal": "[\\.']",
250266
"contains": [
251267
{
252-
"$ref": "#contains.9.contains.0"
268+
"$ref": "#contains.10.contains.0"
253269
}
254270
]
255271
},
@@ -258,18 +274,18 @@
258274
"end": ";",
259275
"contains": [
260276
{
261-
"$ref": "#contains.9.contains.0"
277+
"$ref": "#contains.10.contains.0"
262278
}
263279
]
264280
},
265281
{
266282
"begin": "=>"
267283
},
268284
{
269-
"$ref": "#contains.9.contains.1.contains.3"
285+
"$ref": "#contains.10.contains.1.contains.3"
270286
},
271287
{
272-
"$ref": "#contains.9.contains.1.contains.4"
288+
"$ref": "#contains.10.contains.1.contains.4"
273289
}
274290
]
275291
}

tests/IntegrationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ public function parserUnitBlockProvider()
290290
'blockName' => 'code-blocks/php-annotations',
291291
];
292292

293+
yield 'code-block-php-attributes' => [
294+
'blockName' => 'code-blocks/php-attributes',
295+
];
296+
293297
yield 'code-block-text' => [
294298
'blockName' => 'code-blocks/text',
295299
];
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<div translate="no" data-loc="54" class="notranslate codeblock codeblock-length-md codeblock-php-attributes codeblock-php">
2+
<div class="codeblock-scroll">
3+
<pre class="codeblock-lines">1
4+
2
5+
3
6+
4
7+
5
8+
6
9+
7
10+
8
11+
9
12+
10
13+
11
14+
12
15+
13
16+
14
17+
15
18+
16
19+
17
20+
18
21+
19
22+
20
23+
21
24+
22
25+
23
26+
24
27+
25
28+
26
29+
27
30+
28
31+
29
32+
30
33+
31
34+
32
35+
33
36+
34
37+
35
38+
36
39+
37
40+
38
41+
39
42+
40
43+
41
44+
42
45+
43
46+
44
47+
45
48+
46
49+
47
50+
48
51+
49
52+
50
53+
51
54+
52
55+
53
56+
54</pre>
57+
<pre class="codeblock-code">
58+
<code>
59+
<span class="hljs-comment">// src/SomePath/SomeClass.php</span>
60+
<span class="hljs-keyword">namespace</span> <span class="hljs-title">App</span>\<span class="hljs-title">SomePath</span>;
61+
<span class="hljs-keyword">use</span><span class="hljs-title">Symfony</span>\<span class="hljs-title">Component</span>\<span class="hljs-title">Validator</span>\<span class="hljs-title">Constraints</span> <span class="hljs-title">as</span> <span class="hljs-title">Assert</span>;
62+
<span class="hljs-class">
63+
<span class="hljs-keyword">class</span>
64+
<span class="hljs-title">SomeClass</span>
65+
</span>
66+
{
67+
<span class="hljs-php-attribute">#[AttributeName]</span>
68+
<span class="hljs-keyword">private</span>
69+
<span class="hljs-variable">
70+
<span class="hljs-variable-other-marker">$</span> property1</span>
71+
;
72+
<span class="hljs-php-attribute">#[AttributeName()]</span>
73+
<span class="hljs-keyword">private</span>
74+
<span class="hljs-variable">
75+
<span class="hljs-variable-other-marker">$</span> property2</span>
76+
;
77+
<span class="hljs-php-attribute">#[AttributeName(<span class="hljs-string">'value'</span>)]</span>
78+
<span class="hljs-keyword">private</span>
79+
<span class="hljs-variable">
80+
<span class="hljs-variable-other-marker">$</span> property3</span>
81+
;
82+
<span class="hljs-php-attribute">#[AttributeName(<span class="hljs-string">'value'</span>, option: <span class="hljs-string">'value'</span>)]</span>
83+
<span class="hljs-keyword">private</span>
84+
<span class="hljs-variable">
85+
<span class="hljs-variable-other-marker">$</span> property4</span>
86+
;
87+
<span class="hljs-php-attribute">#[AttributeName([<span class="hljs-string">'value'</span> =&gt; <span class="hljs-string">'value'</span>])]</span>
88+
<span class="hljs-keyword">private</span>
89+
<span class="hljs-variable">
90+
<span class="hljs-variable-other-marker">$</span> property5</span>
91+
;
92+
<span class="hljs-php-attribute">#[AttributeName(
93+
<span class="hljs-string">'value'</span>,
94+
option: <span class="hljs-string">'value'</span>
95+
)]</span>
96+
<span class="hljs-keyword">private</span>
97+
<span class="hljs-variable">
98+
<span class="hljs-variable-other-marker">$</span> property6</span>
99+
;
100+
<span class="hljs-php-attribute">#[Assert\AttributeName(<span class="hljs-string">'value'</span>)]</span>
101+
<span class="hljs-keyword">private</span>
102+
<span class="hljs-variable">
103+
<span class="hljs-variable-other-marker">$</span> property7</span>
104+
;
105+
<span class="hljs-php-attribute">#[Assert\AttributeName(
106+
<span class="hljs-string">'value'</span>,
107+
option: <span class="hljs-string">'value'</span>
108+
)]</span>
109+
<span class="hljs-keyword">private</span>
110+
<span class="hljs-variable">
111+
<span class="hljs-variable-other-marker">$</span> property8</span>
112+
;
113+
<span class="hljs-php-attribute">#[Route(<span class="hljs-string">'/blog/{page&lt;\d+&gt;}'</span>, name: <span class="hljs-string">'blog_list'</span>)]</span>
114+
<span class="hljs-keyword">private</span>
115+
<span class="hljs-variable">
116+
<span class="hljs-variable-other-marker">$</span> property9</span>
117+
;
118+
<span class="hljs-php-attribute">#[Assert\GreaterThanOrEqual(
119+
value: <span class="hljs-number">18</span>,
120+
)]</span>
121+
<span class="hljs-keyword">private</span>
122+
<span class="hljs-variable">
123+
<span class="hljs-variable-other-marker">$</span> property10</span>
124+
;
125+
<span class="hljs-php-attribute">#[ORM\CustomIdGenerator(class: <span class="hljs-string">'doctrine.uuid_generator'</span>)]</span>
126+
<span class="hljs-keyword">private</span>
127+
<span class="hljs-variable">
128+
<span class="hljs-variable-other-marker">$</span> property11</span>
129+
;
130+
<span class="hljs-php-attribute">#[Assert\AtLeastOneOf([
131+
<span class="hljs-keyword">new</span> Assert\Regex(<span class="hljs-string">'/#/'</span>),
132+
<span class="hljs-keyword">new</span> Assert\Length(min: <span class="hljs-number">10</span>),
133+
])]</span>
134+
<span class="hljs-keyword">private</span>
135+
<span class="hljs-variable">
136+
<span class="hljs-variable-other-marker">$</span> property12</span>
137+
;
138+
}</code></pre>
139+
</div>
140+
</div>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
.. code-block:: php-attributes
2+
3+
// src/SomePath/SomeClass.php
4+
namespace App\SomePath;
5+
6+
use Symfony\Component\Validator\Constraints as Assert;
7+
8+
class SomeClass
9+
{
10+
#[AttributeName]
11+
private $property1;
12+
13+
#[AttributeName()]
14+
private $property2;
15+
16+
#[AttributeName('value')]
17+
private $property3;
18+
19+
#[AttributeName('value', option: 'value')]
20+
private $property4;
21+
22+
#[AttributeName(['value' => 'value'])]
23+
private $property5;
24+
25+
#[AttributeName(
26+
'value',
27+
option: 'value'
28+
)]
29+
private $property6;
30+
31+
#[Assert\AttributeName('value')]
32+
private $property7;
33+
34+
#[Assert\AttributeName(
35+
'value',
36+
option: 'value'
37+
)]
38+
private $property8;
39+
40+
#[Route('/blog/{page<\d+>}', name: 'blog_list')]
41+
private $property9;
42+
43+
#[Assert\GreaterThanOrEqual(
44+
value: 18,
45+
)]
46+
private $property10;
47+
48+
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
49+
private $property11;
50+
51+
#[Assert\AtLeastOneOf([
52+
new Assert\Regex('/#/'),
53+
new Assert\Length(min: 10),
54+
])]
55+
private $property12;
56+
}

0 commit comments

Comments
 (0)