Skip to content

Commit 04ef309

Browse files
authored
PHP: Class names at the start of a string are now highlighted correctly (#2731)
Some class names at the start of the input string were not detected correctly due to the way greedy matching works. This fixes the issue by splitting the `class-name` rule into two rules, one before and one after the `keyword` rule that interfered and caused the issue.
1 parent 6183fd9 commit 04ef309

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

components/prism-php.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
'punctuation': /\\/
3434
}
3535
},
36+
'class-name-definition': {
37+
pattern: /(\b(?:class|interface|trait)\s+)\b[a-z_]\w*(?!\\)\b/i,
38+
lookbehind: true,
39+
alias: 'class-name'
40+
},
3641
'keyword': [
3742
{
3843
pattern: /(\(\s*)\b(?:bool|boolean|int|integer|float|string|object|array)\b(?=\s*\))/i,
@@ -85,7 +90,7 @@
8590
'argument-name': /\b[a-z_]\w*(?=\s*:(?!:))/i,
8691
'class-name': [
8792
{
88-
pattern: /(\b(?:class|interface|extends|implements|trait|instanceof|new(?!\s+self|\s+static))\s+|\bcatch\s*\()\b[a-z_]\w*(?!\\)\b/i,
93+
pattern: /(\b(?:extends|implements|instanceof|new(?!\s+self|\s+static))\s+|\bcatch\s*\()\b[a-z_]\w*(?!\\)\b/i,
8994
greedy: true,
9095
lookbehind: true
9196
},

components/prism-php.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/php/attribute_feature.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ function main() {}
224224
["delimiter", "]"]
225225
]],
226226
["keyword", "class"],
227-
["class-name", "Foo"],
227+
["class-name-definition", "Foo"],
228228
["punctuation", "{"],
229229
["keyword", "public"],
230230
["keyword", "function"],

tests/languages/php/class-name_feature.test

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class Foo extends \Package\Bar implements App\Baz {}
170170
["punctuation", "}"],
171171

172172
["keyword", "class"],
173-
["class-name", "Foo"],
173+
["class-name-definition", "Foo"],
174174
["keyword", "extends"],
175175
["class-name", "Bar"],
176176
["keyword", "implements"],
@@ -179,7 +179,7 @@ class Foo extends \Package\Bar implements App\Baz {}
179179
["punctuation", "}"],
180180

181181
["keyword", "class"],
182-
["class-name", "Foo"],
182+
["class-name-definition", "Foo"],
183183
["keyword", "extends"],
184184
["class-name", [
185185
["punctuation", "\\"],

tests/languages/php/issue2614.test

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class First {}
2+
class Second {}
3+
4+
----------------------------------------------------
5+
6+
[
7+
["keyword", "class"],
8+
["class-name-definition", "First"],
9+
["punctuation", "{"],
10+
["punctuation", "}"],
11+
12+
["keyword", "class"],
13+
["class-name-definition", "Second"],
14+
["punctuation", "{"],
15+
["punctuation", "}"]
16+
]
17+
18+
19+
----------------------------------------------------
20+
21+
Checks for issue #2614.

0 commit comments

Comments
 (0)