Skip to content

Commit 01768ca

Browse files
committed
Use tokens (instead of trees) to highlight literals
Tokens position is less fragile. Specially when code does not parse
1 parent cfe896d commit 01768ca

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,17 @@ object SyntaxHighlighting {
5757
scanner.nextToken()
5858
val end = scanner.lastOffset
5959

60-
if (alphaKeywords.contains(token))
60+
// Branch order is important. For example,
61+
// `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))
6165
highlightRange(start, end, KeywordColor)
6266
else if (token == IDENTIFIER && name == nme.???)
6367
highlightRange(start, end, Console.RED_B)
6468
}
6569

66-
val treeHighlighter = new untpd.UntypedTreeTraverser {
70+
object TreeHighlighter extends untpd.UntypedTreeTraverser {
6771
import untpd._
6872

6973
def ignored(tree: NameTree) = {
@@ -72,6 +76,9 @@ object SyntaxHighlighting {
7276
name == nme.ERROR || name == nme.CONSTRUCTOR
7377
}
7478

79+
def highlight(trees: List[Tree])(implicit ctx: Context): Unit =
80+
trees.foreach(traverse)
81+
7582
def traverse(tree: Tree)(implicit ctx: Context): Unit = {
7683
tree match {
7784
case tree: NameTree if ignored(tree) =>
@@ -85,8 +92,6 @@ object SyntaxHighlighting {
8592
highlightPosition(tree.pos, TypeColor)
8693
case _: TypTree =>
8794
highlightPosition(tree.pos, TypeColor)
88-
case _: Literal =>
89-
highlightPosition(tree.pos, LiteralColor)
9095
case _ =>
9196
}
9297
traverseChildren(tree)
@@ -95,8 +100,7 @@ object SyntaxHighlighting {
95100

96101
val parser = new Parser(source)
97102
val trees = parser.blockStatSeq()
98-
for (tree <- trees)
99-
treeHighlighter.traverse(tree)
103+
TreeHighlighter.highlight(trees)
100104

101105
val highlighted = new StringBuilder()
102106

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class SyntaxHighlightingTests extends DottyTest {
3030
@Test
3131
@Ignore("Comments are currently not supported")
3232
def comments = {
33-
test("//a", "<C|//a>")
33+
test("// a", "<C|// a>")
3434
test("/** a */", "<C|/** a */>")
3535
test("/* a */", "<C|/* a */>")
3636
}
@@ -50,7 +50,7 @@ class SyntaxHighlightingTests extends DottyTest {
5050
test("1.1", "<L|1.1>")
5151
test("1.1.toString", "<L|1.1>.toString")
5252
test("1L", "<L|1L>")
53-
test("1Lx", "1Lx")
53+
test("1Lx", "<L|1L>x")
5454
test("1f", "<L|1f>")
5555
test("1.1f", "<L|1.1f>")
5656
test("1.1fx", "1.1fx")
@@ -94,9 +94,9 @@ class SyntaxHighlightingTests extends DottyTest {
9494
test("var", "<K|var>")
9595
test("var foo", "<K|var> <V|foo>")
9696
test("var foo:", "<K|var> <V|foo>:")
97-
test("var foo: Int", "<K|var> <V|foo>: <T|int>")
98-
test("var foo: Int =", "<K|var> <V|foo>: <T|int> =")
99-
test("var foo: Int = 123", "<K|var> <V|foo>: <T|int> = <L|123>")
97+
test("var foo: Int", "<K|var> <V|foo>: <T|Int>")
98+
test("var foo: Int =", "<K|var> <V|foo>: <T|Int> =")
99+
test("var foo: Int = 123", "<K|var> <V|foo>: <T|Int> = <L|123>")
100100

101101
test("def", "<K|def>")
102102
test("def foo", "<K|def> <V|foo>")

0 commit comments

Comments
 (0)