Skip to content

Commit 0889bc7

Browse files
Java: Improved package and class name detection (#2599)
1 parent fc60282 commit 0889bc7

11 files changed

+322
-81
lines changed

components/prism-java.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,40 @@
22

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|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

5+
// full package (optional) + parent classes (optional)
6+
var classNamePrefix = /(^|[^\w.])(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source;
7+
58
// based on the java naming conventions
6-
var className = /\b[A-Z](?:\w*[a-z]\w*)?\b/;
9+
var className = {
10+
pattern: RegExp(classNamePrefix + /[A-Z](?:\w*[a-z]\w*)?\b/.source),
11+
lookbehind: true,
12+
inside: {
13+
'namespace': {
14+
pattern: /^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/,
15+
inside: {
16+
'punctuation': /\./
17+
}
18+
},
19+
'punctuation': /\./
20+
}
21+
};
722

823
Prism.languages.java = Prism.languages.extend('clike', {
924
'class-name': [
1025
className,
11-
12-
// variables and parameters
13-
// this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
14-
/\b[A-Z]\w*(?=\s+\w+\s*[;,=())])/
26+
{
27+
// variables and parameters
28+
// this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
29+
pattern: RegExp(classNamePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=())])/.source),
30+
lookbehind: true,
31+
inside: className.inside
32+
}
1533
],
1634
'keyword': keywords,
1735
'function': [
1836
Prism.languages.clike.function,
1937
{
20-
pattern: /(\:\:)[a-z_]\w*/,
38+
pattern: /(\:\:\s*)[a-z_]\w*/,
2139
lookbehind: true
2240
}
2341
],
@@ -39,18 +57,9 @@
3957

4058
Prism.languages.insertBefore('java', 'class-name', {
4159
'annotation': {
42-
alias: 'punctuation',
43-
pattern: /(^|[^.])@\w+/,
44-
lookbehind: true
45-
},
46-
'namespace': {
47-
pattern: RegExp(
48-
/(\b(?:exports|import(?:\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\s+)(?!<keyword>)[a-z]\w*(?:\.[a-z]\w*)*\.?/
49-
.source.replace(/<keyword>/g, function () { return keywords.source; })),
60+
pattern: /(^|[^.])@\w+(?:\s*\.\s*\w+)*/,
5061
lookbehind: true,
51-
inside: {
52-
'punctuation': /\./,
53-
}
62+
alias: 'punctuation'
5463
},
5564
'generics': {
5665
pattern: /<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<[\w\s,.&?]*>)*>)*>)*>/,
@@ -60,6 +69,15 @@
6069
'punctuation': /[<>(),.:]/,
6170
'operator': /[?&|]/
6271
}
72+
},
73+
'namespace': {
74+
pattern: RegExp(
75+
/(\b(?:exports|import(?:\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\s+)(?!<keyword>)[a-z]\w*(?:\.[a-z]\w*)*\.?/
76+
.source.replace(/<keyword>/g, function () { return keywords.source; })),
77+
lookbehind: true,
78+
inside: {
79+
'punctuation': /\./,
80+
}
6381
}
6482
});
6583
}(Prism));

components/prism-java.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@Override
2+
@Documented
3+
@java.long.annotation.Documented
4+
5+
@Retention(value=SOURCE)
6+
@Target(value={PACKAGE,TYPE})
7+
8+
----------------------------------------------------
9+
10+
[
11+
["annotation", "@Override"],
12+
["annotation", "@Documented"],
13+
["annotation", "@java.long.annotation.Documented"],
14+
15+
["annotation", "@Retention"],
16+
["punctuation", "("],
17+
"value",
18+
["operator", "="],
19+
"SOURCE",
20+
["punctuation", ")"],
21+
22+
["annotation", "@Target"],
23+
["punctuation", "("],
24+
"value",
25+
["operator", "="],
26+
["punctuation", "{"],
27+
"PACKAGE",
28+
["punctuation", ","],
29+
"TYPE",
30+
["punctuation", "}"],
31+
["punctuation", ")"]
32+
]
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
class Foo extends foo.bar.Foo {
2+
3+
java.util.List<foo.bar.Foo.Bar> bar(foo.bar.Baz bat) throws java.lang.IOException {
4+
throw new java.lang.UnsupportedOperationException("Not implemented");
5+
}
6+
7+
}
8+
9+
----------------------------------------------------
10+
11+
[
12+
["keyword", "class"],
13+
["class-name", [
14+
"Foo"
15+
]],
16+
["keyword", "extends"],
17+
["class-name", [
18+
["namespace", [
19+
"foo",
20+
["punctuation", "."],
21+
"bar",
22+
["punctuation", "."]
23+
]],
24+
"Foo"
25+
]],
26+
["punctuation", "{"],
27+
28+
["class-name", [
29+
["namespace", [
30+
"java",
31+
["punctuation", "."],
32+
"util",
33+
["punctuation", "."]
34+
]],
35+
"List"
36+
]],
37+
["generics", [
38+
["punctuation", "<"],
39+
["class-name", [
40+
["namespace", [
41+
"foo",
42+
["punctuation", "."],
43+
"bar",
44+
["punctuation", "."]
45+
]],
46+
"Foo",
47+
["punctuation", "."],
48+
"Bar"
49+
]],
50+
["punctuation", ">"]
51+
]],
52+
["function", "bar"],
53+
["punctuation", "("],
54+
["class-name", [
55+
["namespace", [
56+
"foo",
57+
["punctuation", "."],
58+
"bar",
59+
["punctuation", "."]
60+
]],
61+
"Baz"
62+
]],
63+
" bat",
64+
["punctuation", ")"],
65+
["keyword", "throws"],
66+
["class-name", [
67+
["namespace", [
68+
"java",
69+
["punctuation", "."],
70+
"lang",
71+
["punctuation", "."]
72+
]],
73+
"IOException"
74+
]],
75+
["punctuation", "{"],
76+
["keyword", "throw"],
77+
["keyword", "new"],
78+
["class-name", [
79+
["namespace", [
80+
"java",
81+
["punctuation", "."],
82+
"lang",
83+
["punctuation", "."]
84+
]],
85+
"UnsupportedOperationException"
86+
]],
87+
["punctuation", "("],
88+
["string", "\"Not implemented\""],
89+
["punctuation", ")"],
90+
["punctuation", ";"],
91+
["punctuation", "}"],
92+
93+
["punctuation", "}"]
94+
]

tests/languages/java/function_featrue.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ Bar::foo;
2020
["punctuation", ")"],
2121
["punctuation", ";"],
2222

23-
["class-name", "Bar"],
23+
["class-name", [
24+
"Bar"
25+
]],
2426
["operator", "::"],
2527
["function", "foo"],
2628
["punctuation", ";"]

tests/languages/java/generics_feature.test

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,90 @@
1-
public class Solo<T> {}
2-
Solo<Integer> val = new Solo<Integer>();
1+
public class Solo<T extends com.foo.Foo.Bar> {}
2+
Solo<Integer> val = new Solo<>();
33
Duo<Double, Character> dual = new Duo<Double, Character>(12.2585, 'C');
44

55
----------------------------------------------------
66

77
[
88
["keyword", "public"],
99
["keyword", "class"],
10-
["class-name", "Solo"],
10+
["class-name", [
11+
"Solo"
12+
]],
1113
["generics", [
1214
["punctuation", "<"],
13-
["class-name", "T"],
15+
["class-name", [
16+
"T"
17+
]],
18+
["keyword", "extends"],
19+
["class-name", [
20+
["namespace", [
21+
"com",
22+
["punctuation", "."],
23+
"foo",
24+
["punctuation", "."]
25+
]],
26+
"Foo",
27+
["punctuation", "."],
28+
"Bar"
29+
]],
1430
["punctuation", ">"]
1531
]],
1632
["punctuation", "{"],
1733
["punctuation", "}"],
1834

19-
["class-name", "Solo"],
35+
["class-name", [
36+
"Solo"
37+
]],
2038
["generics", [
2139
["punctuation", "<"],
22-
["class-name", "Integer"],
40+
["class-name", [
41+
"Integer"
42+
]],
2343
["punctuation", ">"]
2444
]],
2545
" val ",
2646
["operator", "="],
2747
["keyword", "new"],
28-
["class-name", "Solo"],
48+
["class-name", [
49+
"Solo"
50+
]],
2951
["generics", [
3052
["punctuation", "<"],
31-
["class-name", "Integer"],
3253
["punctuation", ">"]
3354
]],
3455
["punctuation", "("],
3556
["punctuation", ")"],
3657
["punctuation", ";"],
3758

38-
["class-name", "Duo"],
59+
["class-name", [
60+
"Duo"
61+
]],
3962
["generics", [
4063
["punctuation", "<"],
41-
["class-name", "Double"],
64+
["class-name", [
65+
"Double"
66+
]],
4267
["punctuation", ","],
43-
["class-name", "Character"],
68+
["class-name", [
69+
"Character"
70+
]],
4471
["punctuation", ">"]
4572
]],
4673
" dual ",
4774
["operator", "="],
4875
["keyword", "new"],
49-
["class-name", "Duo"],
76+
["class-name", [
77+
"Duo"
78+
]],
5079
["generics", [
5180
["punctuation", "<"],
52-
["class-name", "Double"],
81+
["class-name", [
82+
"Double"
83+
]],
5384
["punctuation", ","],
54-
["class-name", "Character"],
85+
["class-name", [
86+
"Character"
87+
]],
5588
["punctuation", ">"]
5689
]],
5790
["punctuation", "("],

tests/languages/java/issue1351.test

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,33 @@ public class AllChangesIndexer extends SiteIndexer<Change.Id, ChangeData, Change
55
[
66
["keyword", "public"],
77
["keyword", "class"],
8-
["class-name", "AllChangesIndexer"],
8+
["class-name", [
9+
"AllChangesIndexer"
10+
]],
911
["keyword", "extends"],
10-
["class-name", "SiteIndexer"],
12+
["class-name", [
13+
"SiteIndexer"
14+
]],
1115
["generics", [
1216
["punctuation", "<"],
13-
["class-name", "Change"],
14-
["punctuation", "."],
15-
["class-name", "Id"],
17+
["class-name", [
18+
"Change",
19+
["punctuation", "."],
20+
"Id"
21+
]],
1622
["punctuation", ","],
17-
["class-name", "ChangeData"],
23+
["class-name", [
24+
"ChangeData"
25+
]],
1826
["punctuation", ","],
19-
["class-name", "ChangeIndex"],
27+
["class-name", [
28+
"ChangeIndex"
29+
]],
2030
["punctuation", ">"]
2131
]],
2232
["punctuation", "{"]
2333
]
2434

2535
----------------------------------------------------
2636

27-
Checks for generics. See #1351
37+
Checks for generics. See #1351

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy