Improve Java (#1474) 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
diff --git a/components/prism-java.js b/components/prism-java.js
index 229333b..fb13840 100644
--- a/components/prism-java.js
+++ b/components/prism-java.js
@@ -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));
diff --git a/components/prism-java.min.js b/components/prism-java.min.js
index ea61255..c718247 100644
--- a/components/prism-java.min.js
+++ b/components/prism-java.min.js
@@ -1 +1 @@
-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:!0}}),Prism.languages.insertBefore("java","function",{annotation:{alias:"punctuation",pattern:/(^|[^.])@\w+/,lookbehind:!0}}),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:/[<>(),.:]/}}});
\ No newline at end of file
+!function(a){var e=/\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/,t=/\b[A-Z](?:\w*[a-z]\w*)?\b/;a.languages.java=a.languages.extend("clike",{"class-name":[t,/\b[A-Z]\w*(?=\s+\w+\s*[;,=())])/],keyword:e,"function":[a.languages.clike.function,{pattern:/(\:\:)[a-z_]\w*/,lookbehind:!0}],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:!0}}),a.languages.insertBefore("java","class-name",{annotation:{alias:"punctuation",pattern:/(^|[^.])@\w+/,lookbehind:!0},namespace:{pattern:/\b(package\s+|import\s+(?:static\s+)?)[a-z]\w*(\.[a-z]\w*)+/,lookbehind:!0,inside:{punctuation:/\./}},generics:{pattern:/<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<[\w\s,.&?]*>)*>)*>)*>/,inside:{"class-name":t,keyword:e,punctuation:/[<>(),.:]/,operator:/[?&|]/}}})}(Prism);
\ No newline at end of file
diff --git a/tests/languages/java/function_featrue.test b/tests/languages/java/function_featrue.test
new file mode 100644
index 0000000..2ca6169
--- /dev/null
+++ b/tests/languages/java/function_featrue.test
@@ -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.
diff --git a/tests/languages/java/generics_feature.test b/tests/languages/java/generics_feature.test
index 66bfef5..d1d5e53 100644
--- a/tests/languages/java/generics_feature.test
+++ b/tests/languages/java/generics_feature.test
@@ -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", "("],
@@ -62,4 +64,4 @@ Duo<Double, Character> dual = new Duo<Double, Character>(12.2585, 'C');
----------------------------------------------------
-Checks for generics.
\ No newline at end of file
+Checks for generics.
diff --git a/tests/languages/java/issue1351.test b/tests/languages/java/issue1351.test
index 0162eec..3034e3e 100644
--- a/tests/languages/java/issue1351.test
+++ b/tests/languages/java/issue1351.test
@@ -5,18 +5,18 @@ 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", "{"]
@@ -24,4 +24,4 @@ public class AllChangesIndexer extends SiteIndexer<Change.Id, ChangeData, Change
----------------------------------------------------
-Checks for generics. See #1351
\ No newline at end of file
+Checks for generics. See #1351
diff --git a/tests/languages/java/keyword_feature.test b/tests/languages/java/keyword_feature.test
index 4a6613c..e700d47 100644
--- a/tests/languages/java/keyword_feature.test
+++ b/tests/languages/java/keyword_feature.test
@@ -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.
\ No newline at end of file
+Checks for all keywords.
diff --git a/tests/languages/java/number_feature.test b/tests/languages/java/number_feature.test
index b88f082..014ed37 100644
--- a/tests/languages/java/number_feature.test
+++ b/tests/languages/java/number_feature.test
@@ -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.
\ No newline at end of file
+Checks for binary, hexadecimal and decimal numbers.
diff --git a/tests/languages/java/package_feature.test b/tests/languages/java/package_feature.test
new file mode 100644
index 0000000..9fe6f1f
--- /dev/null
+++ b/tests/languages/java/package_feature.test
@@ -0,0 +1,80 @@
+package java.lang;
+
+import java.lang.Math;
+import java.lang.*;
+
+import static java.lang.Math.PI;
+import static java.lang.Math.sin;
+import static java.lang.Math.*;
+
+----------------------------------------------------
+
+[
+ ["keyword", "package"],
+ ["namespace", [
+ "java",
+ ["punctuation", "."],
+ "lang"
+ ]],
+ ["punctuation", ";"],
+
+ ["keyword", "import"],
+ ["namespace", [
+ "java",
+ ["punctuation", "."],
+ "lang"
+ ]],
+ ["punctuation", "."],
+ ["class-name", "Math"],
+ ["punctuation", ";"],
+
+ ["keyword", "import"],
+ ["namespace", [
+ "java",
+ ["punctuation", "."],
+ "lang"
+ ]],
+ ["punctuation", "."],
+ "*",
+ ["punctuation", ";"],
+
+ ["keyword", "import"],
+ ["keyword", "static"],
+ ["namespace", [
+ "java",
+ ["punctuation", "."],
+ "lang"
+ ]],
+ ["punctuation", "."],
+ ["class-name", "Math"],
+ ["punctuation", "."],
+ "PI",
+ ["punctuation", ";"],
+
+ ["keyword", "import"],
+ ["keyword", "static"],
+ ["namespace", [
+ "java",
+ ["punctuation", "."],
+ "lang"
+ ]],
+ ["punctuation", "."],
+ ["class-name", "Math"],
+ ["punctuation", "."],
+ "sin",
+ ["punctuation", ";"],
+ ["keyword", "import"],
+ ["keyword", "static"],
+ ["namespace", [
+ "java",
+ ["punctuation", "."], "lang"
+ ]], ["punctuation", "."],
+ ["class-name", "Math"],
+ ["punctuation", "."],
+ "*",
+ ["punctuation", ";"]
+]
+
+----------------------------------------------------
+
+Checks for packages.