Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java: Improved class name detection #3351

Merged
merged 2 commits into from Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 35 additions & 4 deletions components/prism-java.js
Expand Up @@ -3,11 +3,11 @@
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/;

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

// based on the java naming conventions
var className = {
pattern: RegExp(classNamePrefix + /[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source),
pattern: RegExp(/(^|[^\w.])/.source + classNamePrefix + /[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source),
lookbehind: true,
inside: {
'namespace': {
Expand All @@ -29,9 +29,16 @@
'class-name': [
className,
{
// variables and parameters
// variables, parameters, and constructor references
// 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),
pattern: RegExp(/(^|[^\w.])/.source + classNamePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=()]|\s*(?:\[[\s,]*\]\s*)?::\s*new\b)/.source),
lookbehind: true,
inside: className.inside
},
{
// class names based on keyword
// this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
pattern: RegExp(/(\b(?:class|enum|extends|implements|instanceof|interface|new|record|throws)\s+)/.source + classNamePrefix + /[A-Z]\w*\b/.source),
lookbehind: true,
inside: className.inside
}
Expand Down Expand Up @@ -79,6 +86,30 @@
'operator': /[?&|]/
}
},
'import': [
{
pattern: RegExp(/(\bimport\s+)/.source + classNamePrefix + /(?:[A-Z]\w*|\*)(?=\s*;)/.source),
lookbehind: true,
inside: {
'namespace': className.inside.namespace,
'punctuation': /\./,
'operator': /\*/,
'class-name': /\w+/
}
},
{
pattern: RegExp(/(\bimport\s+static\s+)/.source + classNamePrefix + /(?:\w+|\*)(?=\s*;)/.source),
lookbehind: true,
alias: 'static',
inside: {
'namespace': className.inside.namespace,
'static': /\b\w+$/,
'punctuation': /\./,
'operator': /\*/,
'class-name': /\w+/
}
}
],
'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*)*\.?/
Expand Down
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.

285 changes: 163 additions & 122 deletions tests/languages/java/class-name_feature.test
@@ -1,122 +1,163 @@
class Foo extends foo.bar.Foo {

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

}

ID id;

String.valueOf(5);

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

[
["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", "."]
]],
"Exception"
]],
["punctuation", "{"],

["keyword", "var"],
" foo ",
["operator", "="],
["keyword", "new"],
["class-name", [
["namespace", [
"java",
["punctuation", "."],
"lang",
["punctuation", "."]
]],
"UnsupportedOperationException"
]],
["punctuation", "("],
["string", "\"Not implemented\""],
["punctuation", ")"],
["punctuation", ";"],

["class-name", ["Exception"]],
" e ",
["operator", "="],
" foo",
["punctuation", ";"],

["keyword", "throw"],
" e",
["punctuation", ";"],

["punctuation", "}"],

["punctuation", "}"],

["class-name", ["ID"]], " id", ["punctuation", ";"],

["class-name", ["String"]],
["punctuation", "."],
["function", "valueOf"],
["punctuation", "("],
["number", "5"],
["punctuation", ")"],
["punctuation", ";"]
]
class Foo extends foo.bar.Foo {

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

}

import com.lib.ID;
ID id = new ID();
ID.Nested id;
ID::new
ID[]::new

String.valueOf(5);

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

[
["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", "."]
]],
"Exception"
]],
["punctuation", "{"],

["keyword", "var"],
" foo ",
["operator", "="],
["keyword", "new"],
["class-name", [
["namespace", [
"java",
["punctuation", "."],
"lang",
["punctuation", "."]
]],
"UnsupportedOperationException"
]],
["punctuation", "("],
["string", "\"Not implemented\""],
["punctuation", ")"],
["punctuation", ";"],

["class-name", ["Exception"]],
" e ",
["operator", "="],
" foo",
["punctuation", ";"],

["keyword", "throw"],
" e",
["punctuation", ";"],

["punctuation", "}"],

["punctuation", "}"],

["keyword", "import"],
["import", [
["namespace", [
"com",
["punctuation", "."],
"lib",
["punctuation", "."]
]],
["class-name", "ID"]
]],
["punctuation", ";"],

["class-name", ["ID"]],
" id ",
["operator", "="],
["keyword", "new"],
["class-name", ["ID"]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],

["class-name", [
"ID",
["punctuation", "."],
"Nested"
]],
" id",
["punctuation", ";"],

["class-name", ["ID"]],
["operator", "::"],
["keyword", "new"],

["class-name", ["ID"]],
["punctuation", "["],
["punctuation", "]"],
["operator", "::"],
["keyword", "new"],

["class-name", ["String"]],
["punctuation", "."],
["function", "valueOf"],
["punctuation", "("],
["number", "5"],
["punctuation", ")"],
["punctuation", ";"]
]