Skip to content

Commit

Permalink
Java: Improved package and class name detection (#2599)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Nov 4, 2020
1 parent fc60282 commit 0889bc7
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 81 deletions.
52 changes: 35 additions & 17 deletions components/prism-java.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,40 @@

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

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

// based on the java naming conventions
var className = /\b[A-Z](?:\w*[a-z]\w*)?\b/;
var className = {
pattern: RegExp(classNamePrefix + /[A-Z](?:\w*[a-z]\w*)?\b/.source),
lookbehind: true,
inside: {
'namespace': {
pattern: /^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/,
inside: {
'punctuation': /\./
}
},
'punctuation': /\./
}
};

Prism.languages.java = Prism.languages.extend('clike', {
'class-name': [
className,

// variables and parameters
// this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
/\b[A-Z]\w*(?=\s+\w+\s*[;,=())])/
{
// variables and parameters
// this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
pattern: RegExp(classNamePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=())])/.source),
lookbehind: true,
inside: className.inside
}
],
'keyword': keywords,
'function': [
Prism.languages.clike.function,
{
pattern: /(\:\:)[a-z_]\w*/,
pattern: /(\:\:\s*)[a-z_]\w*/,
lookbehind: true
}
],
Expand All @@ -39,18 +57,9 @@

Prism.languages.insertBefore('java', 'class-name', {
'annotation': {
alias: 'punctuation',
pattern: /(^|[^.])@\w+/,
lookbehind: true
},
'namespace': {
pattern: RegExp(
/(\b(?:exports|import(?:\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\s+)(?!<keyword>)[a-z]\w*(?:\.[a-z]\w*)*\.?/
.source.replace(/<keyword>/g, function () { return keywords.source; })),
pattern: /(^|[^.])@\w+(?:\s*\.\s*\w+)*/,
lookbehind: true,
inside: {
'punctuation': /\./,
}
alias: 'punctuation'
},
'generics': {
pattern: /<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<[\w\s,.&?]*>)*>)*>)*>/,
Expand All @@ -60,6 +69,15 @@
'punctuation': /[<>(),.:]/,
'operator': /[?&|]/
}
},
'namespace': {
pattern: RegExp(
/(\b(?:exports|import(?:\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\s+)(?!<keyword>)[a-z]\w*(?:\.[a-z]\w*)*\.?/
.source.replace(/<keyword>/g, function () { return keywords.source; })),
lookbehind: true,
inside: {
'punctuation': /\./,
}
}
});
}(Prism));
2 changes: 1 addition & 1 deletion components/prism-java.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions tests/languages/java/annotation_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@Override
@Documented
@java.long.annotation.Documented

@Retention(value=SOURCE)
@Target(value={PACKAGE,TYPE})

----------------------------------------------------

[
["annotation", "@Override"],
["annotation", "@Documented"],
["annotation", "@java.long.annotation.Documented"],

["annotation", "@Retention"],
["punctuation", "("],
"value",
["operator", "="],
"SOURCE",
["punctuation", ")"],

["annotation", "@Target"],
["punctuation", "("],
"value",
["operator", "="],
["punctuation", "{"],
"PACKAGE",
["punctuation", ","],
"TYPE",
["punctuation", "}"],
["punctuation", ")"]
]
94 changes: 94 additions & 0 deletions tests/languages/java/class-name_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
class Foo extends foo.bar.Foo {

java.util.List<foo.bar.Foo.Bar> bar(foo.bar.Baz bat) throws java.lang.IOException {
throw new java.lang.UnsupportedOperationException("Not implemented");
}

}

----------------------------------------------------

[
["keyword", "class"],
["class-name", [
"Foo"
]],
["keyword", "extends"],
["class-name", [
["namespace", [
"foo",
["punctuation", "."],
"bar",
["punctuation", "."]
]],
"Foo"
]],
["punctuation", "{"],

["class-name", [
["namespace", [
"java",
["punctuation", "."],
"util",
["punctuation", "."]
]],
"List"
]],
["generics", [
["punctuation", "<"],
["class-name", [
["namespace", [
"foo",
["punctuation", "."],
"bar",
["punctuation", "."]
]],
"Foo",
["punctuation", "."],
"Bar"
]],
["punctuation", ">"]
]],
["function", "bar"],
["punctuation", "("],
["class-name", [
["namespace", [
"foo",
["punctuation", "."],
"bar",
["punctuation", "."]
]],
"Baz"
]],
" bat",
["punctuation", ")"],
["keyword", "throws"],
["class-name", [
["namespace", [
"java",
["punctuation", "."],
"lang",
["punctuation", "."]
]],
"IOException"
]],
["punctuation", "{"],
["keyword", "throw"],
["keyword", "new"],
["class-name", [
["namespace", [
"java",
["punctuation", "."],
"lang",
["punctuation", "."]
]],
"UnsupportedOperationException"
]],
["punctuation", "("],
["string", "\"Not implemented\""],
["punctuation", ")"],
["punctuation", ";"],
["punctuation", "}"],

["punctuation", "}"]
]
4 changes: 3 additions & 1 deletion tests/languages/java/function_featrue.test
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ Bar::foo;
["punctuation", ")"],
["punctuation", ";"],

["class-name", "Bar"],
["class-name", [
"Bar"
]],
["operator", "::"],
["function", "foo"],
["punctuation", ";"]
Expand Down
61 changes: 47 additions & 14 deletions tests/languages/java/generics_feature.test
Original file line number Diff line number Diff line change
@@ -1,57 +1,90 @@
public class Solo<T> {}
Solo<Integer> val = new Solo<Integer>();
public class Solo<T extends com.foo.Foo.Bar> {}
Solo<Integer> val = new Solo<>();
Duo<Double, Character> dual = new Duo<Double, Character>(12.2585, 'C');

----------------------------------------------------

[
["keyword", "public"],
["keyword", "class"],
["class-name", "Solo"],
["class-name", [
"Solo"
]],
["generics", [
["punctuation", "<"],
["class-name", "T"],
["class-name", [
"T"
]],
["keyword", "extends"],
["class-name", [
["namespace", [
"com",
["punctuation", "."],
"foo",
["punctuation", "."]
]],
"Foo",
["punctuation", "."],
"Bar"
]],
["punctuation", ">"]
]],
["punctuation", "{"],
["punctuation", "}"],

["class-name", "Solo"],
["class-name", [
"Solo"
]],
["generics", [
["punctuation", "<"],
["class-name", "Integer"],
["class-name", [
"Integer"
]],
["punctuation", ">"]
]],
" val ",
["operator", "="],
["keyword", "new"],
["class-name", "Solo"],
["class-name", [
"Solo"
]],
["generics", [
["punctuation", "<"],
["class-name", "Integer"],
["punctuation", ">"]
]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],

["class-name", "Duo"],
["class-name", [
"Duo"
]],
["generics", [
["punctuation", "<"],
["class-name", "Double"],
["class-name", [
"Double"
]],
["punctuation", ","],
["class-name", "Character"],
["class-name", [
"Character"
]],
["punctuation", ">"]
]],
" dual ",
["operator", "="],
["keyword", "new"],
["class-name", "Duo"],
["class-name", [
"Duo"
]],
["generics", [
["punctuation", "<"],
["class-name", "Double"],
["class-name", [
"Double"
]],
["punctuation", ","],
["class-name", "Character"],
["class-name", [
"Character"
]],
["punctuation", ">"]
]],
["punctuation", "("],
Expand Down
26 changes: 18 additions & 8 deletions tests/languages/java/issue1351.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,33 @@ public class AllChangesIndexer extends SiteIndexer<Change.Id, ChangeData, Change
[
["keyword", "public"],
["keyword", "class"],
["class-name", "AllChangesIndexer"],
["class-name", [
"AllChangesIndexer"
]],
["keyword", "extends"],
["class-name", "SiteIndexer"],
["class-name", [
"SiteIndexer"
]],
["generics", [
["punctuation", "<"],
["class-name", "Change"],
["punctuation", "."],
["class-name", "Id"],
["class-name", [
"Change",
["punctuation", "."],
"Id"
]],
["punctuation", ","],
["class-name", "ChangeData"],
["class-name", [
"ChangeData"
]],
["punctuation", ","],
["class-name", "ChangeIndex"],
["class-name", [
"ChangeIndex"
]],
["punctuation", ">"]
]],
["punctuation", "{"]
]

----------------------------------------------------

Checks for generics. See #1351
Checks for generics. See #1351
Loading

0 comments on commit 0889bc7

Please sign in to comment.