Commit 441a14221fca2f2d081e1f44e8cfd0e40bfddb7c

Michael Schmidt 2022-01-06T19:39:26

Scala: Added support for interpolated strings (#3293)

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.