Skip to content

Commit

Permalink
Java: Improved class name detection (#3351)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Mar 5, 2022
1 parent f95dd19 commit 4cb3d03
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 183 deletions.
39 changes: 35 additions & 4 deletions components/prism-java.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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", ";"]
]
Loading

0 comments on commit 4cb3d03

Please sign in to comment.