Skip to content

Commit aac50fe

Browse files
committed
Add syntax highlighting support for string interpolator
1 parent 1e88ab2 commit aac50fe

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,19 @@ object SyntaxHighlighting {
5959

6060
// Branch order is important. For example,
6161
// `true` is at the same time a keyword and a literal
62-
if (literalTokens.contains(token))
63-
highlightRange(start, end, LiteralColor)
64-
else if (alphaKeywords.contains(token))
65-
highlightRange(start, end, KeywordColor)
66-
else if (token == IDENTIFIER && name == nme.???)
67-
highlightRange(start, end, Console.RED_B)
62+
token match {
63+
case _ if literalTokens.contains(token) =>
64+
highlightRange(start, end, LiteralColor)
65+
case STRINGPART =>
66+
// String interpolation parts include `$` but
67+
// we don't highlight it, hence the `-1`
68+
highlightRange(start, end - 1, LiteralColor)
69+
case _ if alphaKeywords.contains(token) =>
70+
highlightRange(start, end, KeywordColor)
71+
case IDENTIFIER if name == nme.??? =>
72+
highlightRange(start, end, Console.RED_B)
73+
case _ =>
74+
}
6875
}
6976

7077
object TreeHighlighter extends untpd.UntypedTreeTraverser {

compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ class SyntaxHighlightingTests extends DottyTest {
6262
test("\"Hello\"", "<L|\"Hello\">")
6363
test("\"\"\"Hello\"\"\"", "<L|\"\"\"Hello\"\"\">")
6464

65-
// FIXME: '$' should not be colored (literal position is off by one)
66-
// test("s\"Hello\"", "s<L|\"Hello\">")
67-
// test("s\"Hello $name\"", "s<L|\"Hello <V|$name<L|\">")
68-
// test("raw\"Hello\"", "raw<L|\"Hello\">")
69-
// test("raw\"\"\"Hello\"\"\"", "raw<L|\"\"\"Hello\"\"\">")
65+
test("s\"Hello\"", "<L|s\"Hello\">")
66+
test("s\"Hello $name\"", "<L|s\"Hello >$name<L|\">")
67+
test("s\"Hello ${name}\"", "<L|s\"Hello >${name}<L|\">")
68+
test("raw\"Hello\"", "<L|raw\"Hello\">")
7069
}
7170

7271
@Test

0 commit comments

Comments
 (0)