Scala: Added support for interpolated strings (#3293)
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
diff --git a/components/prism-scala.js b/components/prism-scala.js
index f886067..40b96fa 100644
--- a/components/prism-scala.js
+++ b/components/prism-scala.js
@@ -13,5 +13,37 @@ Prism.languages.scala = Prism.languages.extend('java', {
'builtin': /\b(?:Any|AnyRef|AnyVal|Boolean|Byte|Char|Double|Float|Int|Long|Nothing|Short|String|Unit)\b/,
'symbol': /'[^\d\s\\]\w*/
});
+
+Prism.languages.insertBefore('scala', 'triple-quoted-string', {
+ 'string-interpolation': {
+ pattern: /\b[a-z]\w*(?:"""(?:[^$]|\$(?:[^{]|\{(?:[^{}]|\{[^{}]*\})*\}))*?"""|"(?:[^$"\r\n]|\$(?:[^{]|\{(?:[^{}]|\{[^{}]*\})*\}))*")/i,
+ greedy: true,
+ inside: {
+ 'id': {
+ pattern: /^\w+/,
+ greedy: true,
+ alias: 'function'
+ },
+ 'escape': {
+ pattern: /\\\$"|\$[$"]/,
+ greedy: true,
+ alias: 'symbol'
+ },
+ 'interpolation': {
+ pattern: /\$(?:\w+|\{(?:[^{}]|\{[^{}]*\})*\})/,
+ greedy: true,
+ inside: {
+ 'punctuation': /^\$\{?|\}$/,
+ 'expression': {
+ pattern: /[\s\S]+/,
+ inside: Prism.languages.scala
+ }
+ }
+ },
+ 'string': /[\s\S]+/
+ }
+ }
+});
+
delete Prism.languages.scala['class-name'];
delete Prism.languages.scala['function'];
diff --git a/components/prism-scala.min.js b/components/prism-scala.min.js
index 5917fa9..1f3db0b 100644
--- a/components/prism-scala.min.js
+++ b/components/prism-scala.min.js
@@ -1 +1 @@
-Prism.languages.scala=Prism.languages.extend("java",{"triple-quoted-string":{pattern:/"""[\s\S]*?"""/,greedy:!0,alias:"string"},string:{pattern:/("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,greedy:!0},keyword:/<-|=>|\b(?:abstract|case|catch|class|def|do|else|extends|final|finally|for|forSome|if|implicit|import|lazy|match|new|null|object|override|package|private|protected|return|sealed|self|super|this|throw|trait|try|type|val|var|while|with|yield)\b/,number:/\b0x(?:[\da-f]*\.)?[\da-f]+|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e\d+)?[dfl]?/i,builtin:/\b(?:Any|AnyRef|AnyVal|Boolean|Byte|Char|Double|Float|Int|Long|Nothing|Short|String|Unit)\b/,symbol:/'[^\d\s\\]\w*/}),delete Prism.languages.scala["class-name"],delete Prism.languages.scala.function;
\ No newline at end of file
+Prism.languages.scala=Prism.languages.extend("java",{"triple-quoted-string":{pattern:/"""[\s\S]*?"""/,greedy:!0,alias:"string"},string:{pattern:/("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,greedy:!0},keyword:/<-|=>|\b(?:abstract|case|catch|class|def|do|else|extends|final|finally|for|forSome|if|implicit|import|lazy|match|new|null|object|override|package|private|protected|return|sealed|self|super|this|throw|trait|try|type|val|var|while|with|yield)\b/,number:/\b0x(?:[\da-f]*\.)?[\da-f]+|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e\d+)?[dfl]?/i,builtin:/\b(?:Any|AnyRef|AnyVal|Boolean|Byte|Char|Double|Float|Int|Long|Nothing|Short|String|Unit)\b/,symbol:/'[^\d\s\\]\w*/}),Prism.languages.insertBefore("scala","triple-quoted-string",{"string-interpolation":{pattern:/\b[a-z]\w*(?:"""(?:[^$]|\$(?:[^{]|\{(?:[^{}]|\{[^{}]*\})*\}))*?"""|"(?:[^$"\r\n]|\$(?:[^{]|\{(?:[^{}]|\{[^{}]*\})*\}))*")/i,greedy:!0,inside:{id:{pattern:/^\w+/,greedy:!0,alias:"function"},escape:{pattern:/\\\$"|\$[$"]/,greedy:!0,alias:"symbol"},interpolation:{pattern:/\$(?:\w+|\{(?:[^{}]|\{[^{}]*\})*\})/,greedy:!0,inside:{punctuation:/^\$\{?|\}$/,expression:{pattern:/[\s\S]+/,inside:Prism.languages.scala}}},string:/[\s\S]+/}}}),delete Prism.languages.scala["class-name"],delete Prism.languages.scala.function;
\ No newline at end of file
diff --git a/tests/languages/scala/string_feature.test b/tests/languages/scala/string_feature.test
index 75878ba..7dad0c2 100644
--- a/tests/languages/scala/string_feature.test
+++ b/tests/languages/scala/string_feature.test
@@ -6,8 +6,14 @@ bar"""
"""fo"o
// comment
bar"""
+"""{"name":"James"}"""
"foo /* comment */ bar"
-'foo // bar'
+
+s"Hello, $name"
+s"1 + 1 = ${1 + 1}"
+s"New offers starting at $$14.99"
+f"$name%s is $height%2.2f meters tall"
+json"{ name: $name, id: $id }"
----------------------------------------------------
@@ -17,10 +23,68 @@ bar"""
["triple-quoted-string", "\"\"\"fo\"o\r\nbar\"\"\""],
["triple-quoted-string", "\"\"\"fo\"o\r\n// comment\r\nbar\"\"\""],
+ ["triple-quoted-string", "\"\"\"{\"name\":\"James\"}\"\"\""],
["string", "\"foo /* comment */ bar\""],
- ["string", "'foo // bar'"]
+
+ ["string-interpolation", [
+ ["id", "s"],
+ ["string", "\"Hello, "],
+ ["interpolation", [
+ ["punctuation", "$"],
+ ["expression", ["name"]]
+ ]],
+ ["string", "\""]
+ ]],
+ ["string-interpolation", [
+ ["id", "s"],
+ ["string", "\"1 + 1 = "],
+ ["interpolation", [
+ ["punctuation", "${"],
+ ["expression", [
+ ["number", "1"],
+ ["operator", "+"],
+ ["number", "1"]
+ ]],
+ ["punctuation", "}"]
+ ]],
+ ["string", "\""]
+ ]],
+ ["string-interpolation", [
+ ["id", "s"],
+ ["string", "\"New offers starting at "],
+ ["escape", "$$"],
+ ["string", "14.99\""]
+ ]],
+ ["string-interpolation", [
+ ["id", "f"],
+ ["string", "\""],
+ ["interpolation", [
+ ["punctuation", "$"],
+ ["expression", ["name"]]
+ ]],
+ ["string", "%s is "],
+ ["interpolation", [
+ ["punctuation", "$"],
+ ["expression", ["height"]]
+ ]],
+ ["string", "%2.2f meters tall\""]
+ ]],
+ ["string-interpolation", [
+ ["id", "json"],
+ ["string", "\"{ name: "],
+ ["interpolation", [
+ ["punctuation", "$"],
+ ["expression", ["name"]]
+ ]],
+ ["string", ", id: "],
+ ["interpolation", [
+ ["punctuation", "$"],
+ ["expression", ["id"]]
+ ]],
+ ["string", " }\""]
+ ]]
]
----------------------------------------------------
-Checks for characters and strings.
+Checks for strings.