diff --git a/CHANGES.md b/CHANGES.md
index b1413c29a5..fa877292a3 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -26,6 +26,7 @@ Core Grammars:
- enh(json) add json5 support [Kerry Shetline][]
- fix(css) `unicode-range` parsing, issue #4253 [Kerry Shetline][]
- fix(csharp) Support digit separators [te-ing][]
+- enh(java) improve detection of types, including generic and array types [Hannes Wallnoefer][]
Documentation:
@@ -55,6 +56,7 @@ CONTRIBUTORS
[te-ing]: https://github.com/te-ing
[Anthony Martin]: https://github.com/anthony-c-martin
[NriotHrreion]: https://github.com/NriotHrreion
+[Hannes Wallnoefer]: https://github.com/hns
## Version 11.11.1
diff --git a/src/languages/java.js b/src/languages/java.js
index b826eb94cf..b986ffe7b6 100644
--- a/src/languages/java.js
+++ b/src/languages/java.js
@@ -30,8 +30,9 @@ function recurRegex(re, substitution, depth) {
export default function(hljs) {
const regex = hljs.regex;
const JAVA_IDENT_RE = '[\u00C0-\u02B8a-zA-Z_$][\u00C0-\u02B8a-zA-Z_$0-9]*';
- const GENERIC_IDENT_RE = JAVA_IDENT_RE
- + recurRegex('(?:<' + JAVA_IDENT_RE + '~~~(?:\\s*,\\s*' + JAVA_IDENT_RE + '~~~)*>)?', /~~~/g, 2);
+ const TYPE_ARG_RE = '(?:(?:' + JAVA_IDENT_RE + '~~~)|(?:\\?\\s+(?:extends|super)\\s+' + JAVA_IDENT_RE + '~~~)|(?:\\?))';
+ const GENERIC_RE = recurRegex('(?:\\s*<\\s*' + TYPE_ARG_RE + '(?:\\s*,\\s*' + TYPE_ARG_RE + ')*\\s*>)?', /~~~/g, 2);
+ const ARRAY_RE = '(?:(?:\\s*\\[\\s*])+)?';
const MAIN_KEYWORDS = [
'synchronized',
'abstract',
@@ -188,7 +189,7 @@ export default function(hljs) {
{
begin: [
regex.concat(/(?!else)/, JAVA_IDENT_RE),
- /\s+/,
+ regex.concat(GENERIC_RE, ARRAY_RE, /\s+/),
JAVA_IDENT_RE,
/\s+/,
/=(?!=)/
@@ -223,11 +224,15 @@ export default function(hljs) {
},
{
begin: [
- '(?:' + GENERIC_IDENT_RE + '\\s+)',
- hljs.UNDERSCORE_IDENT_RE,
+ JAVA_IDENT_RE,
+ regex.concat(GENERIC_RE, ARRAY_RE, /\s+/),
+ JAVA_IDENT_RE,
/\s*(?=\()/
],
- className: { 2: "title.function" },
+ className: {
+ 1: "type",
+ 3: "title.function"
+ },
keywords: KEYWORDS,
contains: [
{
diff --git a/test/api/highlight.js b/test/api/highlight.js
index 0f85e7247c..f50ab9e459 100644
--- a/test/api/highlight.js
+++ b/test/api/highlight.js
@@ -36,7 +36,7 @@ describe('.highlight()', () => {
result.value.should.equal(
'public ' +
- 'void moveTo' +
+ 'void moveTo' +
'(int x, ' +
'int y, ' +
'int z);'
@@ -48,7 +48,7 @@ describe('.highlight()', () => {
result.value.should.equal(
'public ' +
- 'void moveTo' +
+ 'void moveTo' +
'(int x, ' +
'int y, ' +
'int z);'
diff --git a/test/markup/java/annotations.expect.txt b/test/markup/java/annotations.expect.txt
index 3f801e1606..9b22b4530c 100644
--- a/test/markup/java/annotations.expect.txt
+++ b/test/markup/java/annotations.expect.txt
@@ -8,5 +8,5 @@
}
class Example {
- void foo(@SuppressWarnings("unused") int bar) { }
+ void foo(@SuppressWarnings("unused") int bar) { }
}
diff --git a/test/markup/java/arrays.expect.txt b/test/markup/java/arrays.expect.txt
new file mode 100644
index 0000000000..f43d5c8e3d
--- /dev/null
+++ b/test/markup/java/arrays.expect.txt
@@ -0,0 +1,4 @@
+String[] helloWorld = new String() {"hello", "world"};
+int[][] matrix = {{1, 2}, {3, 4}};
+private byte[] getBytes();
+public Object [ ] [ ] [ ] cubic() { ... }
diff --git a/test/markup/java/arrays.txt b/test/markup/java/arrays.txt
new file mode 100644
index 0000000000..4e48bc77c2
--- /dev/null
+++ b/test/markup/java/arrays.txt
@@ -0,0 +1,4 @@
+String[] helloWorld = new String() {"hello", "world"};
+int[][] matrix = {{1, 2}, {3, 4}};
+private byte[] getBytes();
+public Object [ ] [ ] [ ] cubic() { ... }
diff --git a/test/markup/java/functions.expect.txt b/test/markup/java/functions.expect.txt
index 8f638287f3..bc77cf546a 100644
--- a/test/markup/java/functions.expect.txt
+++ b/test/markup/java/functions.expect.txt
@@ -1,5 +1,5 @@
-public static <A,B,C> Tuple<A,B,C> fun(Future<Tuple<A,B,C>> future) throws Exceptions {
+public static <A,B,C> Tuple<A,B,C> fun(Future<Tuple<A,B,C>> future) throws Exceptions {
}
-static Optional<List<Token>> parseAll(String s) {
+static Optional<List<Token>> parseAll(String s) {
}
diff --git a/test/markup/java/generics.expect.txt b/test/markup/java/generics.expect.txt
new file mode 100644
index 0000000000..9081997180
--- /dev/null
+++ b/test/markup/java/generics.expect.txt
@@ -0,0 +1,10 @@
+List<String> strings = new ArrayList<>();
+Generic<Object, ?, String> g = null;
+public static <T, R> Generic<T, ?, R> map(Function<? super T, ? extends R> mapper);
+Map<? extends Key, List<? super Value>> map = getMap();
+static Set < Integer > getInts() { ... }
+
+public interface Map<K, V> {
+ void put(K key, V value);
+ V get(K key);
+}
diff --git a/test/markup/java/generics.txt b/test/markup/java/generics.txt
new file mode 100644
index 0000000000..db24764324
--- /dev/null
+++ b/test/markup/java/generics.txt
@@ -0,0 +1,10 @@
+List strings = new ArrayList<>();
+Generic