Skip to content

Commit

Permalink
enh(java) improve Java annotation support (highlightjs#2377)
Browse files Browse the repository at this point in the history
* (java) annotations can include numbers. Closes highlightjs#1478.
* (java) annotations can take params. Closes highlightjs#1324.
* enh(java) allow annotations inside function call params
  • Loading branch information
joshgoebel authored and taufik-nurrohman committed Feb 18, 2020
1 parent c4ecc39 commit 325a57d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Core Changes:

Language Improvements:

- enh(java) annotations can include numbers (#2377) [Josh Goebel][]
- enh(java) annotations can take params (#2377) [Josh Goebel][]
- enh(java) allow annotations inside function call params (#2377) [Josh Goebel][]
- enh(parser) pre/post-highlightBlock callbacks via plugin (#2285) [Josh Goebel][]
- (fortran) Add Fortran 2018 keywords and coarray intrinsics (#2361) [Sam Miller][]
- (delphi) highlight hexadecimal, octal, and binary numbers (#2370) [Robert Riebisch]()
Expand Down
16 changes: 13 additions & 3 deletions src/languages/java.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ function(hljs) {
'package default double public try this switch continue throws protected public private ' +
'module requires exports do';

var ANNOTATION = {
className: 'meta',
begin: '@' + JAVA_IDENT_RE,
contains:[
{
begin: /\(/,
end: /\)/,
contains: ["self"] // allow nested () inside our annotation
},
]
}
// https://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html
var JAVA_NUMBER_RE = '\\b' +
'(' +
Expand Down Expand Up @@ -95,6 +106,7 @@ function(hljs) {
keywords: KEYWORDS,
relevance: 0,
contains: [
ANNOTATION,
hljs.APOS_STRING_MODE,
hljs.QUOTE_STRING_MODE,
hljs.C_NUMBER_MODE,
Expand All @@ -106,9 +118,7 @@ function(hljs) {
]
},
JAVA_NUMBER_MODE,
{
className: 'meta', begin: '@[A-Za-z]+'
}
ANNOTATION
]
};
}
12 changes: 12 additions & 0 deletions test/markup/java/annotations.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<span class="hljs-meta">@SpringBootTest</span>
<span class="hljs-meta">@AutoConfigureMockMvc</span>
<span class="hljs-meta">@Transactional</span>
<span class="hljs-meta">@Slf4j</span>
<span class="hljs-meta">@RunWith(SpringRunner.class)</span>
<span class="hljs-meta">@Something(1+(3+4))</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BoardControllerTest</span> </span>{
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Example</span> </span>{
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">foo</span><span class="hljs-params">(<span class="hljs-meta">@SuppressWarnings("unused")</span> <span class="hljs-keyword">int</span> bar)</span> </span>{ }
}
12 changes: 12 additions & 0 deletions test/markup/java/annotations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@Slf4j
@RunWith(SpringRunner.class)
@Something(1+(3+4))
public class BoardControllerTest {
}

class Example {
void foo(@SuppressWarnings("unused") int bar) { }
}

0 comments on commit 325a57d

Please sign in to comment.