Skip to content

Commit 441a142

Browse files
Scala: Added support for interpolated strings (#3293)
1 parent 0b6b1e2 commit 441a142

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed

components/prism-scala.js

+32
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,37 @@ Prism.languages.scala = Prism.languages.extend('java', {
1313
'builtin': /\b(?:Any|AnyRef|AnyVal|Boolean|Byte|Char|Double|Float|Int|Long|Nothing|Short|String|Unit)\b/,
1414
'symbol': /'[^\d\s\\]\w*/
1515
});
16+
17+
Prism.languages.insertBefore('scala', 'triple-quoted-string', {
18+
'string-interpolation': {
19+
pattern: /\b[a-z]\w*(?:"""(?:[^$]|\$(?:[^{]|\{(?:[^{}]|\{[^{}]*\})*\}))*?"""|"(?:[^$"\r\n]|\$(?:[^{]|\{(?:[^{}]|\{[^{}]*\})*\}))*")/i,
20+
greedy: true,
21+
inside: {
22+
'id': {
23+
pattern: /^\w+/,
24+
greedy: true,
25+
alias: 'function'
26+
},
27+
'escape': {
28+
pattern: /\\\$"|\$[$"]/,
29+
greedy: true,
30+
alias: 'symbol'
31+
},
32+
'interpolation': {
33+
pattern: /\$(?:\w+|\{(?:[^{}]|\{[^{}]*\})*\})/,
34+
greedy: true,
35+
inside: {
36+
'punctuation': /^\$\{?|\}$/,
37+
'expression': {
38+
pattern: /[\s\S]+/,
39+
inside: Prism.languages.scala
40+
}
41+
}
42+
},
43+
'string': /[\s\S]+/
44+
}
45+
}
46+
});
47+
1648
delete Prism.languages.scala['class-name'];
1749
delete Prism.languages.scala['function'];

components/prism-scala.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/scala/string_feature.test

+67-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ bar"""
66
"""fo"o
77
// comment
88
bar"""
9+
"""{"name":"James"}"""
910
"foo /* comment */ bar"
10-
'foo // bar'
11+
12+
s"Hello, $name"
13+
s"1 + 1 = ${1 + 1}"
14+
s"New offers starting at $$14.99"
15+
f"$name%s is $height%2.2f meters tall"
16+
json"{ name: $name, id: $id }"
1117

1218
----------------------------------------------------
1319

@@ -17,10 +23,68 @@ bar"""
1723

1824
["triple-quoted-string", "\"\"\"fo\"o\r\nbar\"\"\""],
1925
["triple-quoted-string", "\"\"\"fo\"o\r\n// comment\r\nbar\"\"\""],
26+
["triple-quoted-string", "\"\"\"{\"name\":\"James\"}\"\"\""],
2027
["string", "\"foo /* comment */ bar\""],
21-
["string", "'foo // bar'"]
28+
29+
["string-interpolation", [
30+
["id", "s"],
31+
["string", "\"Hello, "],
32+
["interpolation", [
33+
["punctuation", "$"],
34+
["expression", ["name"]]
35+
]],
36+
["string", "\""]
37+
]],
38+
["string-interpolation", [
39+
["id", "s"],
40+
["string", "\"1 + 1 = "],
41+
["interpolation", [
42+
["punctuation", "${"],
43+
["expression", [
44+
["number", "1"],
45+
["operator", "+"],
46+
["number", "1"]
47+
]],
48+
["punctuation", "}"]
49+
]],
50+
["string", "\""]
51+
]],
52+
["string-interpolation", [
53+
["id", "s"],
54+
["string", "\"New offers starting at "],
55+
["escape", "$$"],
56+
["string", "14.99\""]
57+
]],
58+
["string-interpolation", [
59+
["id", "f"],
60+
["string", "\""],
61+
["interpolation", [
62+
["punctuation", "$"],
63+
["expression", ["name"]]
64+
]],
65+
["string", "%s is "],
66+
["interpolation", [
67+
["punctuation", "$"],
68+
["expression", ["height"]]
69+
]],
70+
["string", "%2.2f meters tall\""]
71+
]],
72+
["string-interpolation", [
73+
["id", "json"],
74+
["string", "\"{ name: "],
75+
["interpolation", [
76+
["punctuation", "$"],
77+
["expression", ["name"]]
78+
]],
79+
["string", ", id: "],
80+
["interpolation", [
81+
["punctuation", "$"],
82+
["expression", ["id"]]
83+
]],
84+
["string", " }\""]
85+
]]
2286
]
2387

2488
----------------------------------------------------
2589

26-
Checks for characters and strings.
90+
Checks for strings.

0 commit comments

Comments
 (0)