Skip to content

Commit

Permalink
Improve Java (#1474)
Browse files Browse the repository at this point in the history
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
RunDevelopment authored and mAAdhaTTah committed Dec 1, 2018
1 parent 3b1e091 commit 81bd8f0
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 70 deletions.
75 changes: 51 additions & 24 deletions components/prism-java.js
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));
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.

32 changes: 32 additions & 0 deletions tests/languages/java/function_featrue.test
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.
28 changes: 15 additions & 13 deletions tests/languages/java/generics_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,51 @@ Duo<Double, Character> dual = new Duo<Double, Character>(12.2585, 'C');
[
["keyword", "public"],
["keyword", "class"],
["class-name", ["Solo"]],
["class-name", "Solo"],
["generics", [
["punctuation", "<"],
"T",
["class-name", "T"],
["punctuation", ">"]
]],
["punctuation", "{"],
["punctuation", "}"],
"\r\nSolo",

["class-name", "Solo"],
["generics", [
["punctuation", "<"],
"Integer",
["class-name", "Integer"],
["punctuation", ">"]
]],
" val ",
["operator", "="],
["keyword", "new"],
["class-name", ["Solo"]],
["class-name", "Solo"],
["generics", [
["punctuation", "<"],
"Integer",
["class-name", "Integer"],
["punctuation", ">"]
]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],
"\r\nDuo",

["class-name", "Duo"],
["generics", [
["punctuation", "<"],
"Double",
["class-name", "Double"],
["punctuation", ","],
" Character",
["class-name", "Character"],
["punctuation", ">"]
]],
" dual ",
["operator", "="],
["keyword", "new"],
["class-name", ["Duo"]],
["class-name", "Duo"],
["generics", [
["punctuation", "<"],
"Double",
["class-name", "Double"],
["punctuation", ","],
" Character",
["class-name", "Character"],
["punctuation", ">"]
]],
["punctuation", "("],
Expand All @@ -62,4 +64,4 @@ Duo<Double, Character> dual = new Duo<Double, Character>(12.2585, 'C');

----------------------------------------------------

Checks for generics.
Checks for generics.
14 changes: 7 additions & 7 deletions tests/languages/java/issue1351.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ public class AllChangesIndexer extends SiteIndexer<Change.Id, ChangeData, Change
[
["keyword", "public"],
["keyword", "class"],
["class-name", ["AllChangesIndexer"]],
["class-name", "AllChangesIndexer"],
["keyword", "extends"],
["class-name", ["SiteIndexer"]],
["class-name", "SiteIndexer"],
["generics", [
["punctuation", "<"],
"Change",
["class-name", "Change"],
["punctuation", "."],
"Id",
["class-name", "Id"],
["punctuation", ","],
" ChangeData",
["class-name", "ChangeData"],
["punctuation", ","],
" ChangeIndex",
["class-name", "ChangeIndex"],
["punctuation", ">"]
]],
["punctuation", "{"]
]

----------------------------------------------------

Checks for generics. See #1351
Checks for generics. See #1351
32 changes: 17 additions & 15 deletions tests/languages/java/keyword_feature.test
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.
53 changes: 43 additions & 10 deletions tests/languages/java/number_feature.test
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.
Loading

0 comments on commit 81bd8f0

Please sign in to comment.