Java: Add support for generics. Fix #1351
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
diff --git a/components/prism-java.js b/components/prism-java.js
index da3e9c6..881b306 100644
--- a/components/prism-java.js
+++ b/components/prism-java.js
@@ -14,3 +14,14 @@ Prism.languages.insertBefore('java','function', {
lookbehind: true
}
});
+
+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: /[<>(),.:]/
+ }
+ }
+});
diff --git a/components/prism-java.min.js b/components/prism-java.min.js
index d6c921a..e583b73 100644
--- a/components/prism-java.min.js
+++ b/components/prism-java.min.js
@@ -1 +1 @@
-Prism.languages.java=Prism.languages.extend("clike",{keyword:/\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)\b/,number:/\b0b[01]+\b|\b0x[\da-f]*\.?[\da-fp-]+\b|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?[df]?/i,operator:{pattern:/(^|[^.])(?:\+[+=]?|-[-=]?|!=?|<<?=?|>>?>?=?|==?|&[&=]?|\|[|=]?|\*=?|\/=?|%=?|\^=?|[?:~])/m,lookbehind:!0}}),Prism.languages.insertBefore("java","function",{annotation:{alias:"punctuation",pattern:/(^|[^.])@\w+/,lookbehind:!0}});
\ No newline at end of file
+Prism.languages.java=Prism.languages.extend("clike",{keyword:/\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)\b/,number:/\b0b[01]+\b|\b0x[\da-f]*\.?[\da-fp-]+\b|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?[df]?/i,operator:{pattern:/(^|[^.])(?:\+[+=]?|-[-=]?|!=?|<<?=?|>>?>?=?|==?|&[&=]?|\|[|=]?|\*=?|\/=?|%=?|\^=?|[?:~])/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
diff --git a/tests/languages/java/generics_feature.test b/tests/languages/java/generics_feature.test
new file mode 100644
index 0000000..66bfef5
--- /dev/null
+++ b/tests/languages/java/generics_feature.test
@@ -0,0 +1,65 @@
+public class Solo<T> {}
+Solo<Integer> val = new Solo<Integer>();
+Duo<Double, Character> dual = new Duo<Double, Character>(12.2585, 'C');
+
+----------------------------------------------------
+
+[
+ ["keyword", "public"],
+ ["keyword", "class"],
+ ["class-name", ["Solo"]],
+ ["generics", [
+ ["punctuation", "<"],
+ "T",
+ ["punctuation", ">"]
+ ]],
+ ["punctuation", "{"],
+ ["punctuation", "}"],
+ "\r\nSolo",
+ ["generics", [
+ ["punctuation", "<"],
+ "Integer",
+ ["punctuation", ">"]
+ ]],
+ " val ",
+ ["operator", "="],
+ ["keyword", "new"],
+ ["class-name", ["Solo"]],
+ ["generics", [
+ ["punctuation", "<"],
+ "Integer",
+ ["punctuation", ">"]
+ ]],
+ ["punctuation", "("],
+ ["punctuation", ")"],
+ ["punctuation", ";"],
+ "\r\nDuo",
+ ["generics", [
+ ["punctuation", "<"],
+ "Double",
+ ["punctuation", ","],
+ " Character",
+ ["punctuation", ">"]
+ ]],
+ " dual ",
+ ["operator", "="],
+ ["keyword", "new"],
+ ["class-name", ["Duo"]],
+ ["generics", [
+ ["punctuation", "<"],
+ "Double",
+ ["punctuation", ","],
+ " Character",
+ ["punctuation", ">"]
+ ]],
+ ["punctuation", "("],
+ ["number", "12.2585"],
+ ["punctuation", ","],
+ ["string", "'C'"],
+ ["punctuation", ")"],
+ ["punctuation", ";"]
+]
+
+----------------------------------------------------
+
+Checks for generics.
\ No newline at end of file
diff --git a/tests/languages/java/issue1351.test b/tests/languages/java/issue1351.test
new file mode 100644
index 0000000..2f95e6b
--- /dev/null
+++ b/tests/languages/java/issue1351.test
@@ -0,0 +1,27 @@
+public class AllChangesIndexer extends SiteIndexer<Change.Id, ChangeData, ChangeIndex> {
+
+----------------------------------------------------
+
+[
+ ["keyword", "public"],
+ ["keyword", "class"],
+ ["class-name", ["AllChangesIndexer"]],
+ ["keyword", "extends"],
+ ["class-name", ["SiteIndexer"]],
+ ["generics", [
+ ["punctuation", "<"],
+ "Change",
+ ["punctuation", "."],
+ "Id",
+ ["punctuation", ","],
+ " ChangeData",
+ ["punctuation", ","],
+ " ChangeIndex",
+ ["punctuation", ">"]
+ ]],
+ ["punctuation", "{"]
+]
+
+----------------------------------------------------
+
+Checks for generics.
\ No newline at end of file