Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions src/languages/java.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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+/,
/=(?!=)/
Expand Down Expand Up @@ -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: [
{
Expand Down
4 changes: 2 additions & 2 deletions test/api/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('.highlight()', () => {

result.value.should.equal(
'<span class="hljs-keyword">public</span> ' +
'<span class="hljs-keyword">void</span> <span class="hljs-title function_">moveTo</span>' +
'<span class="hljs-type">void</span> <span class="hljs-title function_">moveTo</span>' +
'<span class="hljs-params">(<span class="hljs-type">int</span> x, ' +
'<span class="hljs-type">int</span> y, ' +
'<span class="hljs-type">int</span> z)</span>;'
Expand All @@ -48,7 +48,7 @@ describe('.highlight()', () => {

result.value.should.equal(
'<span class="hljs-keyword">public</span> ' +
'<span class="hljs-keyword">void</span> <span class="hljs-title function_">moveTo</span>' +
'<span class="hljs-type">void</span> <span class="hljs-title function_">moveTo</span>' +
'<span class="hljs-params">(<span class="hljs-type">int</span> x, ' +
'<span class="hljs-type">int</span> y, ' +
'<span class="hljs-type">int</span> z)</span>;'
Expand Down
2 changes: 1 addition & 1 deletion test/markup/java/annotations.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
}

<span class="hljs-keyword">class</span> <span class="hljs-title class_">Example</span> {
<span class="hljs-keyword">void</span> <span class="hljs-title function_">foo</span><span class="hljs-params">(<span class="hljs-meta">@SuppressWarnings(&quot;unused&quot;)</span> <span class="hljs-type">int</span> bar)</span> { }
<span class="hljs-type">void</span> <span class="hljs-title function_">foo</span><span class="hljs-params">(<span class="hljs-meta">@SuppressWarnings(&quot;unused&quot;)</span> <span class="hljs-type">int</span> bar)</span> { }
}
4 changes: 4 additions & 0 deletions test/markup/java/arrays.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<span class="hljs-type">String</span>[] <span class="hljs-variable">helloWorld</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>() {<span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-string">&quot;world&quot;</span>};
<span class="hljs-type">int</span>[][] <span class="hljs-variable">matrix</span> <span class="hljs-operator">=</span> {{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">3</span>, <span class="hljs-number">4</span>}};
<span class="hljs-keyword">private</span> <span class="hljs-type">byte</span>[] <span class="hljs-title function_">getBytes</span><span class="hljs-params">()</span>;
<span class="hljs-keyword">public</span> <span class="hljs-type">Object</span> [ ] [ ] [ ] <span class="hljs-title function_">cubic</span><span class="hljs-params">()</span> { ... }
4 changes: 4 additions & 0 deletions test/markup/java/arrays.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
String[] helloWorld = new String() {"hello", "world"};
int[][] matrix = {{1, 2}, {3, 4}};
private byte[] getBytes();
public Object [ ] [ ] [ ] cubic() { ... }
4 changes: 2 additions & 2 deletions test/markup/java/functions.expect.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;A,B,C&gt; Tuple&lt;A,B,C&gt; <span class="hljs-title function_">fun</span><span class="hljs-params">(Future&lt;Tuple&lt;A,B,C&gt;&gt; future)</span> <span class="hljs-keyword">throws</span> Exceptions {
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;A,B,C&gt; <span class="hljs-type">Tuple</span>&lt;A,B,C&gt; <span class="hljs-title function_">fun</span><span class="hljs-params">(Future&lt;Tuple&lt;A,B,C&gt;&gt; future)</span> <span class="hljs-keyword">throws</span> Exceptions {
}

<span class="hljs-keyword">static</span> Optional&lt;List&lt;Token&gt;&gt; <span class="hljs-title function_">parseAll</span><span class="hljs-params">(String s)</span> {
<span class="hljs-keyword">static</span> <span class="hljs-type">Optional</span>&lt;List&lt;Token&gt;&gt; <span class="hljs-title function_">parseAll</span><span class="hljs-params">(String s)</span> {
}
10 changes: 10 additions & 0 deletions test/markup/java/generics.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<span class="hljs-type">List</span>&lt;String&gt; <span class="hljs-variable">strings</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();
<span class="hljs-type">Generic</span>&lt;Object, ?, String&gt; <span class="hljs-variable">g</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T, R&gt; <span class="hljs-type">Generic</span>&lt;T, ?, R&gt; <span class="hljs-title function_">map</span><span class="hljs-params">(Function&lt;? <span class="hljs-built_in">super</span> T, ? extends R&gt; mapper)</span>;
<span class="hljs-type">Map</span>&lt;? extends Key, List&lt;? <span class="hljs-built_in">super</span> Value&gt;&gt; <span class="hljs-variable">map</span> <span class="hljs-operator">=</span> getMap();
<span class="hljs-keyword">static</span> <span class="hljs-type">Set</span> &lt; Integer &gt; <span class="hljs-title function_">getInts</span><span class="hljs-params">()</span> { ... }

<span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">Map</span>&lt;K, V&gt; {
<span class="hljs-type">void</span> <span class="hljs-title function_">put</span><span class="hljs-params">(K key, V value)</span>;
<span class="hljs-type">V</span> <span class="hljs-title function_">get</span><span class="hljs-params">(K key)</span>;
}
10 changes: 10 additions & 0 deletions test/markup/java/generics.txt
Original file line number Diff line number Diff line change
@@ -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);
}
Loading