Skip to content

Commit 4cb3d03

Browse files
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+
]

0 commit comments

Comments
 (0)