-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A bunch of small improvements for the Java language. 1. Class highlighting based on [naming conventions](http://www.oracle.com/technetwork/java/javase/overview/codeconventions-135099.html). Because of these conventions, we know that every name which starts with an uppercase letter and contains some lower case letters afterward is a class, interface or enum. 2. Nested generics are now supported. The contents of a generic statement are no longer highlighted as a `function`, but as a `class-name` which is more fitting. 3. Packages will now be highlighted as `namespace`. 4. E.g. `foo::bar`, `bar` will be highlighted as a function. 5. ~Support for `var`.~ #1549 6. `null` is now a keyword. As it should be.
- Loading branch information
1 parent
3b1e091
commit 81bd8f0
Showing
8 changed files
with
246 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,54 @@ | ||
Prism.languages.java = Prism.languages.extend('clike', { | ||
'keyword': /\b(?:var|abstract|continue|for|new|switch|assert|default|goto|package|synchronized|boolean|do|if|private|this|break|double|implements|protected|throw|byte|else|import|public|throws|case|enum|instanceof|return|transient|catch|extends|int|short|try|char|final|interface|static|void|class|finally|long|strictfp|volatile|const|float|native|super|while)\b/, | ||
'number': /\b0b[01]+\b|\b0x[\da-f]*\.?[\da-fp-]+\b|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?[df]?/i, | ||
'operator': { | ||
pattern: /(^|[^.])(?:<<=?|>>>?=?|->|([-+&|])\2|[?:~]|[-+*/%&|^!=<>]=?)/m, | ||
lookbehind: true | ||
} | ||
}); | ||
(function (Prism) { | ||
|
||
Prism.languages.insertBefore('java','function', { | ||
'annotation': { | ||
alias: 'punctuation', | ||
pattern: /(^|[^.])@\w+/, | ||
lookbehind: true | ||
} | ||
}); | ||
var keywords = /\b(?:abstract|continue|for|new|switch|assert|default|goto|package|synchronized|boolean|do|if|private|this|break|double|implements|protected|throw|byte|else|import|public|throws|case|enum|instanceof|return|transient|catch|extends|int|short|try|char|final|interface|static|void|class|finally|long|strictfp|volatile|const|float|native|super|while|var|null)\b/; | ||
|
||
Prism.languages.insertBefore('java', 'class-name', { | ||
'generics': { | ||
pattern: /<\s*\w+(?:\.\w+)?(?:\s*,\s*\w+(?:\.\w+)?)*>/i, | ||
alias: 'function', | ||
inside: { | ||
keyword: Prism.languages.java.keyword, | ||
punctuation: /[<>(),.:]/ | ||
// based on the java naming conventions | ||
var className = /\b[A-Z](?:\w*[a-z]\w*)?\b/; | ||
|
||
Prism.languages.java = Prism.languages.extend('clike', { | ||
'class-name': [ | ||
className, | ||
|
||
// variables and parameters | ||
// this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods) | ||
/\b[A-Z]\w*(?=\s+\w+\s*[;,=())])/ | ||
], | ||
'keyword': keywords, | ||
'function': [ | ||
Prism.languages.clike.function, | ||
{ | ||
pattern: /(\:\:)[a-z_]\w*/, | ||
lookbehind: true | ||
} | ||
], | ||
'number': /\b0b[01][01_]*L?\b|\b0x[\da-f_]*\.?[\da-f_p+-]+\b|(?:\b\d[\d_]*\.?[\d_]*|\B\.\d[\d_]*)(?:e[+-]?\d[\d_]*)?[dfl]?/i, | ||
'operator': { | ||
pattern: /(^|[^.])(?:<<=?|>>>?=?|->|([-+&|])\2|[?:~]|[-+*/%&|^!=<>]=?)/m, | ||
lookbehind: true | ||
} | ||
}); | ||
|
||
Prism.languages.insertBefore('java', 'class-name', { | ||
'annotation': { | ||
alias: 'punctuation', | ||
pattern: /(^|[^.])@\w+/, | ||
lookbehind: true | ||
}, | ||
'namespace': { | ||
pattern: /\b(package\s+|import\s+(?:static\s+)?)[a-z]\w*(\.[a-z]\w*)+/, | ||
lookbehind: true, | ||
inside: { | ||
'punctuation': /\./, | ||
} | ||
}, | ||
'generics': { | ||
pattern: /<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<[\w\s,.&?]*>)*>)*>)*>/, | ||
inside: { | ||
'class-name': className, | ||
'keyword': keywords, | ||
'punctuation': /[<>(),.:]/, | ||
'operator': /[?&|]/ | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
}(Prism)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
void foo(int a) {} | ||
foo(0); | ||
Bar::foo; | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["keyword", "void"], | ||
["function", "foo"], | ||
["punctuation", "("], | ||
["keyword", "int"], | ||
" a", | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
["punctuation", "}"], | ||
|
||
["function", "foo"], | ||
["punctuation", "("], | ||
["number", "0"], | ||
["punctuation", ")"], | ||
["punctuation", ";"], | ||
|
||
["class-name", "Bar"], | ||
["operator", ":"], | ||
["operator", ":"], | ||
["function", "foo"], | ||
["punctuation", ";"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,53 @@ | ||
abstract continue for | ||
new ; | ||
new | ||
switch assert default | ||
goto package synchronized | ||
boolean do if private | ||
this break double | ||
implements ; | ||
implements | ||
protected throw byte else | ||
import public throws case | ||
enum | ||
instanceof ; | ||
instanceof | ||
return transient catch | ||
extends ; | ||
extends | ||
int short try char | ||
final | ||
interface ; | ||
interface | ||
static void | ||
class ; | ||
class | ||
finally long | ||
strictfp volatile const | ||
float native super while | ||
var null | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["keyword", "abstract"], ["keyword", "continue"], ["keyword", "for"], | ||
["keyword", "new"], ["punctuation", ";"], | ||
["keyword", "new"], | ||
["keyword", "switch"], ["keyword", "assert"], ["keyword", "default"], | ||
["keyword", "goto"], ["keyword", "package"], ["keyword", "synchronized"], | ||
["keyword", "boolean"], ["keyword", "do"], ["keyword", "if"], ["keyword", "private"], | ||
["keyword", "this"], ["keyword", "break"], ["keyword", "double"], | ||
["keyword", "implements"], ["punctuation", ";"], | ||
["keyword", "implements"], | ||
["keyword", "protected"], ["keyword", "throw"], ["keyword", "byte"], ["keyword", "else"], | ||
["keyword", "import"], ["keyword", "public"], ["keyword", "throws"], ["keyword", "case"], | ||
["keyword", "enum"], | ||
["keyword", "instanceof"], ["punctuation", ";"], | ||
["keyword", "instanceof"], | ||
["keyword", "return"], ["keyword", "transient"], ["keyword", "catch"], | ||
["keyword", "extends"], ["punctuation", ";"], | ||
["keyword", "extends"], | ||
["keyword", "int"], ["keyword", "short"], ["keyword", "try"], ["keyword", "char"], | ||
["keyword", "final"], | ||
["keyword", "interface"], ["punctuation", ";"], | ||
["keyword", "static"], ["keyword", "void"], | ||
["keyword", "class"], ["punctuation", ";"], | ||
["keyword", "interface"], | ||
["keyword", "static"], ["keyword", "void"], | ||
["keyword", "class"], | ||
["keyword", "finally"], ["keyword", "long"], | ||
["keyword", "strictfp"], ["keyword", "volatile"], ["keyword", "const"], | ||
["keyword", "float"], ["keyword", "native"], ["keyword", "super"], ["keyword", "while"] | ||
["keyword", "float"], ["keyword", "native"], ["keyword", "super"], ["keyword", "while"], | ||
["keyword", "var"], ["keyword", "null"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for all keywords. | ||
Checks for all keywords. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,60 @@ | ||
0b11110000 | ||
0xBadFace | ||
0x1.8p1 | ||
0xa.fp-2 | ||
42 | ||
42d | ||
42L | ||
|
||
1.2e3f | ||
0.1E-4f | ||
0.2e+1f | ||
|
||
0xBadFace | ||
|
||
0x1.8p1 | ||
0xa.fp-2 | ||
0xa.fp+2 | ||
0xa.p+3f | ||
0x.fp+3f | ||
|
||
0b11110000 | ||
|
||
1_2_3 | ||
1_2.3_4e-5_6 | ||
|
||
0x1_2 | ||
0x0_1__2_3 | ||
|
||
0b1_1_1_1__0_0_0_0 | ||
|
||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["number", "0b11110000"], | ||
["number", "0xBadFace"], | ||
["number", "0x1.8p1"], | ||
["number", "0xa.fp-2"], | ||
["number", "42"], | ||
["number", "42d"], | ||
["number", "42L"], | ||
|
||
["number", "1.2e3f"], | ||
["number", "0.1E-4f"], | ||
["number", "0.2e+1f"] | ||
["number", "0.2e+1f"], | ||
|
||
["number", "0xBadFace"], | ||
|
||
["number", "0x1.8p1"], | ||
["number", "0xa.fp-2"], | ||
["number", "0xa.fp+2"], | ||
["number", "0xa.p+3f"], | ||
["number", "0x.fp+3f"], | ||
|
||
["number", "0b11110000"], | ||
|
||
["number", "1_2_3"], | ||
["number", "1_2.3_4e-5_6"], | ||
|
||
["number", "0x1_2"], | ||
["number", "0x0_1__2_3"], | ||
|
||
["number", "0b1_1_1_1__0_0_0_0"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for binary, hexadecimal and decimal numbers. | ||
Checks for binary, hexadecimal and decimal numbers. |
Oops, something went wrong.