Skip to content

Commit 4cb3d03

Browse files
authoredMar 5, 2022
Java: Improved class name detection (#3351)
1 parent f95dd19 commit 4cb3d03

File tree

5 files changed

+372
-183
lines changed

5 files changed

+372
-183
lines changed
 

‎components/prism-java.js

+35-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
var keywords = /\b(?:abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|exports|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|module|native|new|non-sealed|null|open|opens|package|permits|private|protected|provides|public|record(?!\s*[(){}[\]<>=%~.:,;?+\-*/&|^])|requires|return|sealed|short|static|strictfp|super|switch|synchronized|this|throw|throws|to|transient|transitive|try|uses|var|void|volatile|while|with|yield)\b/;
44

55
// full package (optional) + parent classes (optional)
6-
var classNamePrefix = /(^|[^\w.])(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source;
6+
var classNamePrefix = /(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source;
77

88
// based on the java naming conventions
99
var className = {
10-
pattern: RegExp(classNamePrefix + /[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source),
10+
pattern: RegExp(/(^|[^\w.])/.source + classNamePrefix + /[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source),
1111
lookbehind: true,
1212
inside: {
1313
'namespace': {
@@ -29,9 +29,16 @@
2929
'class-name': [
3030
className,
3131
{
32-
// variables and parameters
32+
// variables, parameters, and constructor references
3333
// this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
34-
pattern: RegExp(classNamePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=()])/.source),
34+
pattern: RegExp(/(^|[^\w.])/.source + classNamePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=()]|\s*(?:\[[\s,]*\]\s*)?::\s*new\b)/.source),
35+
lookbehind: true,
36+
inside: className.inside
37+
},
38+
{
39+
// class names based on keyword
40+
// this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
41+
pattern: RegExp(/(\b(?:class|enum|extends|implements|instanceof|interface|new|record|throws)\s+)/.source + classNamePrefix + /[A-Z]\w*\b/.source),
3542
lookbehind: true,
3643
inside: className.inside
3744
}
@@ -79,6 +86,30 @@
7986
'operator': /[?&|]/
8087
}
8188
},
89+
'import': [
90+
{
91+
pattern: RegExp(/(\bimport\s+)/.source + classNamePrefix + /(?:[A-Z]\w*|\*)(?=\s*;)/.source),
92+
lookbehind: true,
93+
inside: {
94+
'namespace': className.inside.namespace,
95+
'punctuation': /\./,
96+
'operator': /\*/,
97+
'class-name': /\w+/
98+
}
99+
},
100+
{
101+
pattern: RegExp(/(\bimport\s+static\s+)/.source + classNamePrefix + /(?:\w+|\*)(?=\s*;)/.source),
102+
lookbehind: true,
103+
alias: 'static',
104+
inside: {
105+
'namespace': className.inside.namespace,
106+
'static': /\b\w+$/,
107+
'punctuation': /\./,
108+
'operator': /\*/,
109+
'class-name': /\w+/
110+
}
111+
}
112+
],
82113
'namespace': {
83114
pattern: RegExp(
84115
/(\b(?:exports|import(?:\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\s+)(?!<keyword>)[a-z]\w*(?:\.[a-z]\w*)*\.?/

‎components/prism-java.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+163-122
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,163 @@
1-
class Foo extends foo.bar.Foo {
2-
3-
java.util.List<foo.bar.Foo.Bar> bar(foo.bar.Baz bat) throws java.lang.Exception {
4-
var foo = new java.lang.UnsupportedOperationException("Not implemented");
5-
Exception e = foo;
6-
throw e;
7-
}
8-
9-
}
10-
11-
ID id;
12-
13-
String.valueOf(5);
14-
15-
----------------------------------------------------
16-
17-
[
18-
["keyword", "class"],
19-
["class-name", ["Foo"]],
20-
["keyword", "extends"],
21-
["class-name", [
22-
["namespace", [
23-
"foo",
24-
["punctuation", "."],
25-
"bar",
26-
["punctuation", "."]
27-
]],
28-
"Foo"
29-
]],
30-
["punctuation", "{"],
31-
32-
["class-name", [
33-
["namespace", [
34-
"java",
35-
["punctuation", "."],
36-
"util",
37-
["punctuation", "."]
38-
]],
39-
"List"
40-
]],
41-
["generics", [
42-
["punctuation", "<"],
43-
["class-name", [
44-
["namespace", [
45-
"foo",
46-
["punctuation", "."],
47-
"bar",
48-
["punctuation", "."]
49-
]],
50-
"Foo",
51-
["punctuation", "."],
52-
"Bar"
53-
]],
54-
["punctuation", ">"]
55-
]],
56-
["function", "bar"],
57-
["punctuation", "("],
58-
["class-name", [
59-
["namespace", [
60-
"foo",
61-
["punctuation", "."],
62-
"bar",
63-
["punctuation", "."]
64-
]],
65-
"Baz"
66-
]],
67-
" bat",
68-
["punctuation", ")"],
69-
["keyword", "throws"],
70-
["class-name", [
71-
["namespace", [
72-
"java",
73-
["punctuation", "."],
74-
"lang",
75-
["punctuation", "."]
76-
]],
77-
"Exception"
78-
]],
79-
["punctuation", "{"],
80-
81-
["keyword", "var"],
82-
" foo ",
83-
["operator", "="],
84-
["keyword", "new"],
85-
["class-name", [
86-
["namespace", [
87-
"java",
88-
["punctuation", "."],
89-
"lang",
90-
["punctuation", "."]
91-
]],
92-
"UnsupportedOperationException"
93-
]],
94-
["punctuation", "("],
95-
["string", "\"Not implemented\""],
96-
["punctuation", ")"],
97-
["punctuation", ";"],
98-
99-
["class-name", ["Exception"]],
100-
" e ",
101-
["operator", "="],
102-
" foo",
103-
["punctuation", ";"],
104-
105-
["keyword", "throw"],
106-
" e",
107-
["punctuation", ";"],
108-
109-
["punctuation", "}"],
110-
111-
["punctuation", "}"],
112-
113-
["class-name", ["ID"]], " id", ["punctuation", ";"],
114-
115-
["class-name", ["String"]],
116-
["punctuation", "."],
117-
["function", "valueOf"],
118-
["punctuation", "("],
119-
["number", "5"],
120-
["punctuation", ")"],
121-
["punctuation", ";"]
122-
]
1+
class Foo extends foo.bar.Foo {
2+
3+
java.util.List<foo.bar.Foo.Bar> bar(foo.bar.Baz bat) throws java.lang.Exception {
4+
var foo = new java.lang.UnsupportedOperationException("Not implemented");
5+
Exception e = foo;
6+
throw e;
7+
}
8+
9+
}
10+
11+
import com.lib.ID;
12+
ID id = new ID();
13+
ID.Nested id;
14+
ID::new
15+
ID[]::new
16+
17+
String.valueOf(5);
18+
19+
----------------------------------------------------
20+
21+
[
22+
["keyword", "class"],
23+
["class-name", ["Foo"]],
24+
["keyword", "extends"],
25+
["class-name", [
26+
["namespace", [
27+
"foo",
28+
["punctuation", "."],
29+
"bar",
30+
["punctuation", "."]
31+
]],
32+
"Foo"
33+
]],
34+
["punctuation", "{"],
35+
36+
["class-name", [
37+
["namespace", [
38+
"java",
39+
["punctuation", "."],
40+
"util",
41+
["punctuation", "."]
42+
]],
43+
"List"
44+
]],
45+
["generics", [
46+
["punctuation", "<"],
47+
["class-name", [
48+
["namespace", [
49+
"foo",
50+
["punctuation", "."],
51+
"bar",
52+
["punctuation", "."]
53+
]],
54+
"Foo",
55+
["punctuation", "."],
56+
"Bar"
57+
]],
58+
["punctuation", ">"]
59+
]],
60+
["function", "bar"],
61+
["punctuation", "("],
62+
["class-name", [
63+
["namespace", [
64+
"foo",
65+
["punctuation", "."],
66+
"bar",
67+
["punctuation", "."]
68+
]],
69+
"Baz"
70+
]],
71+
" bat",
72+
["punctuation", ")"],
73+
["keyword", "throws"],
74+
["class-name", [
75+
["namespace", [
76+
"java",
77+
["punctuation", "."],
78+
"lang",
79+
["punctuation", "."]
80+
]],
81+
"Exception"
82+
]],
83+
["punctuation", "{"],
84+
85+
["keyword", "var"],
86+
" foo ",
87+
["operator", "="],
88+
["keyword", "new"],
89+
["class-name", [
90+
["namespace", [
91+
"java",
92+
["punctuation", "."],
93+
"lang",
94+
["punctuation", "."]
95+
]],
96+
"UnsupportedOperationException"
97+
]],
98+
["punctuation", "("],
99+
["string", "\"Not implemented\""],
100+
["punctuation", ")"],
101+
["punctuation", ";"],
102+
103+
["class-name", ["Exception"]],
104+
" e ",
105+
["operator", "="],
106+
" foo",
107+
["punctuation", ";"],
108+
109+
["keyword", "throw"],
110+
" e",
111+
["punctuation", ";"],
112+
113+
["punctuation", "}"],
114+
115+
["punctuation", "}"],
116+
117+
["keyword", "import"],
118+
["import", [
119+
["namespace", [
120+
"com",
121+
["punctuation", "."],
122+
"lib",
123+
["punctuation", "."]
124+
]],
125+
["class-name", "ID"]
126+
]],
127+
["punctuation", ";"],
128+
129+
["class-name", ["ID"]],
130+
" id ",
131+
["operator", "="],
132+
["keyword", "new"],
133+
["class-name", ["ID"]],
134+
["punctuation", "("],
135+
["punctuation", ")"],
136+
["punctuation", ";"],
137+
138+
["class-name", [
139+
"ID",
140+
["punctuation", "."],
141+
"Nested"
142+
]],
143+
" id",
144+
["punctuation", ";"],
145+
146+
["class-name", ["ID"]],
147+
["operator", "::"],
148+
["keyword", "new"],
149+
150+
["class-name", ["ID"]],
151+
["punctuation", "["],
152+
["punctuation", "]"],
153+
["operator", "::"],
154+
["keyword", "new"],
155+
156+
["class-name", ["String"]],
157+
["punctuation", "."],
158+
["function", "valueOf"],
159+
["punctuation", "("],
160+
["number", "5"],
161+
["punctuation", ")"],
162+
["punctuation", ";"]
163+
]

‎tests/languages/java/issue3350.test

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.nio.charset.StandardCharsets;
2+
import java.security.MessageDigest;
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.UUID;
6+
import java.util.regex.Pattern;
7+
8+
----------------------------------------------------
9+
10+
[
11+
["keyword", "import"],
12+
["import", [
13+
["namespace", [
14+
"java",
15+
["punctuation", "."],
16+
"nio",
17+
["punctuation", "."],
18+
"charset",
19+
["punctuation", "."]
20+
]],
21+
["class-name", "StandardCharsets"]
22+
]],
23+
["punctuation", ";"],
24+
25+
["keyword", "import"],
26+
["import", [
27+
["namespace", [
28+
"java",
29+
["punctuation", "."],
30+
"security",
31+
["punctuation", "."]
32+
]],
33+
["class-name", "MessageDigest"]
34+
]],
35+
["punctuation", ";"],
36+
37+
["keyword", "import"],
38+
["import", [
39+
["namespace", [
40+
"java",
41+
["punctuation", "."],
42+
"util",
43+
["punctuation", "."]
44+
]],
45+
["class-name", "Arrays"]
46+
]],
47+
["punctuation", ";"],
48+
49+
["keyword", "import"],
50+
["import", [
51+
["namespace", [
52+
"java",
53+
["punctuation", "."],
54+
"util",
55+
["punctuation", "."]
56+
]],
57+
["class-name", "HashSet"]
58+
]],
59+
["punctuation", ";"],
60+
61+
["keyword", "import"],
62+
["import", [
63+
["namespace", [
64+
"java",
65+
["punctuation", "."],
66+
"util",
67+
["punctuation", "."]
68+
]],
69+
["class-name", "UUID"]
70+
]],
71+
["punctuation", ";"],
72+
73+
["keyword", "import"],
74+
["import", [
75+
["namespace", [
76+
"java",
77+
["punctuation", "."],
78+
"util",
79+
["punctuation", "."],
80+
"regex",
81+
["punctuation", "."]
82+
]],
83+
["class-name", "Pattern"]
84+
]],
85+
["punctuation", ";"]
86+
]

‎tests/languages/java/package_feature.test

+87-56
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package java;
22
package java.lang;
33

44
import foo.Bar;
5+
import foo.ID;
56
import java.lang.Math;
67
import java.lang.*;
78

89
import static foo.Bar.BAZ;
10+
import static foo.ID.DEFAULT;
911
import static java.lang.Math.PI;
1012
import static java.lang.Math.sin;
1113
import static java.lang.Math.*;
@@ -14,10 +16,9 @@ import static java.lang.Math.*;
1416

1517
[
1618
["keyword", "package"],
17-
["namespace", [
18-
"java"
19-
]],
19+
["namespace", ["java"]],
2020
["punctuation", ";"],
21+
2122
["keyword", "package"],
2223
["namespace", [
2324
"java",
@@ -27,91 +28,121 @@ import static java.lang.Math.*;
2728
["punctuation", ";"],
2829

2930
["keyword", "import"],
30-
["namespace", [
31-
"foo",
32-
["punctuation", "."]
31+
["import", [
32+
["namespace", [
33+
"foo",
34+
["punctuation", "."]
35+
]],
36+
["class-name", "Bar"]
3337
]],
34-
["class-name", [
35-
"Bar"
38+
["punctuation", ";"],
39+
40+
["keyword", "import"],
41+
["import", [
42+
["namespace", [
43+
"foo",
44+
["punctuation", "."]
45+
]],
46+
["class-name", "ID"]
3647
]],
3748
["punctuation", ";"],
49+
3850
["keyword", "import"],
39-
["namespace", [
40-
"java",
41-
["punctuation", "."],
42-
"lang",
43-
["punctuation", "."]
51+
["import", [
52+
["namespace", [
53+
"java",
54+
["punctuation", "."],
55+
"lang",
56+
["punctuation", "."]
57+
]],
58+
["class-name", "Math"]
4459
]],
45-
["class-name", [
46-
"Math"
60+
["punctuation", ";"],
61+
62+
["keyword", "import"],
63+
["import", [
64+
["namespace", [
65+
"java",
66+
["punctuation", "."],
67+
"lang",
68+
["punctuation", "."]
69+
]],
70+
["operator", "*"]
4771
]],
4872
["punctuation", ";"],
73+
4974
["keyword", "import"],
50-
["namespace", [
51-
"java",
75+
["keyword", "static"],
76+
["import", [
77+
["namespace", [
78+
"foo",
79+
["punctuation", "."]
80+
]],
81+
["class-name", "Bar"],
5282
["punctuation", "."],
53-
"lang",
54-
["punctuation", "."]
83+
["static", "BAZ"]
5584
]],
56-
["operator", "*"],
5785
["punctuation", ";"],
5886

5987
["keyword", "import"],
6088
["keyword", "static"],
61-
["namespace", [
62-
"foo",
63-
["punctuation", "."]
64-
]],
65-
["class-name", [
66-
"Bar"
89+
["import", [
90+
["namespace", [
91+
"foo",
92+
["punctuation", "."]
93+
]],
94+
["class-name", "ID"],
95+
["punctuation", "."],
96+
["static", "DEFAULT"]
6797
]],
68-
["punctuation", "."],
69-
"BAZ",
7098
["punctuation", ";"],
99+
71100
["keyword", "import"],
72101
["keyword", "static"],
73-
["namespace", [
74-
"java",
102+
["import", [
103+
["namespace", [
104+
"java",
105+
["punctuation", "."],
106+
"lang",
107+
["punctuation", "."]
108+
]],
109+
["class-name", "Math"],
75110
["punctuation", "."],
76-
"lang",
77-
["punctuation", "."]
78-
]],
79-
["class-name", [
80-
"Math"
111+
["static", "PI"]
81112
]],
82-
["punctuation", "."],
83-
"PI",
84113
["punctuation", ";"],
114+
85115
["keyword", "import"],
86116
["keyword", "static"],
87-
["namespace", [
88-
"java",
117+
["import", [
118+
["namespace", [
119+
"java",
120+
["punctuation", "."],
121+
"lang",
122+
["punctuation", "."]
123+
]],
124+
["class-name", "Math"],
89125
["punctuation", "."],
90-
"lang",
91-
["punctuation", "."]
92-
]],
93-
["class-name", [
94-
"Math"
126+
["static", "sin"]
95127
]],
96-
["punctuation", "."],
97-
"sin",
98128
["punctuation", ";"],
129+
99130
["keyword", "import"],
100131
["keyword", "static"],
101-
["namespace", [
102-
"java",
132+
["import", [
133+
["namespace", [
134+
"java",
135+
["punctuation", "."],
136+
"lang",
137+
["punctuation", "."]
138+
]],
139+
["class-name", "Math"],
103140
["punctuation", "."],
104-
"lang",
105-
["punctuation", "."]
106-
]],
107-
["class-name", [
108-
"Math"
141+
["operator", "*"]
109142
]],
110-
["punctuation", "."],
111-
"*",
112143
["punctuation", ";"]
113144
]
114145

115146
----------------------------------------------------
116147

117-
Checks for packages.
148+
Checks for packages.

0 commit comments

Comments
 (0)
Please sign in to comment.