Skip to content

Commit

Permalink
Scala: Added support for interpolated strings
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Jan 6, 2022
1 parent 4b19b50 commit fc5175a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 4 deletions.
32 changes: 32 additions & 0 deletions components/prism-scala.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
2 changes: 1 addition & 1 deletion components/prism-scala.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 67 additions & 3 deletions tests/languages/scala/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -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 }"

----------------------------------------------------

Expand All @@ -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.

0 comments on commit fc5175a

Please sign in to comment.