Skip to content

Commit 05c0901

Browse files
committed
Removed useless code from tests, renamed lastToken
1 parent 578af35 commit 05c0901

File tree

2 files changed

+32
-101
lines changed

2 files changed

+32
-101
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ object SyntaxHighlighting {
4949
var prev: Char = 0
5050
var remaining = chars.toStream
5151
val newBuf = new StringBuilder
52-
var lastToken = ""
52+
var lastValDefToken = ""
5353

5454
@inline def keywordStart =
5555
prev == 0 || prev == ' ' || prev == '{' || prev == '(' ||
@@ -292,19 +292,19 @@ object SyntaxHighlighting {
292292

293293
val valDefStarterTokens = "var" :: "val" :: "def" :: "case" :: Nil
294294

295-
/** lastToken is used to check whether we want to show something
296-
* in valDef color or not. There are only a few cases when lastToken
295+
/** lastValDefToken is used to check whether we want to show something
296+
* in valDef color or not. There are only a few cases when lastValDefToken
297297
* should be updated, that way we can avoid stopping coloring too early.
298298
* eg.: case A(x, y, z) => ???
299299
* Without this function only x would be colored.
300300
*/
301301
def updateLastToken(currentToken: String): String =
302-
(lastToken, currentToken) match {
302+
(lastValDefToken, currentToken) match {
303303
case _ if valDefStarterTokens.contains(currentToken) => currentToken
304304
case (("val" | "var"), "=") => currentToken
305305
case ("case", ("=>" | "class" | "object")) => currentToken
306306
case ("def", _) => currentToken
307-
case _ => lastToken
307+
case _ => lastValDefToken
308308
}
309309

310310
while (remaining.nonEmpty && !delim(curr)) {
@@ -316,12 +316,12 @@ object SyntaxHighlighting {
316316
val toAdd =
317317
if (shouldHL(str))
318318
highlight(str)
319-
else if (valDefStarterTokens.contains(lastToken) && !List("=", "=>").contains(str))
319+
else if (valDefStarterTokens.contains(lastValDefToken) && !List("=", "=>").contains(str))
320320
valDef(str)
321321
else str
322322
val suffix = if (delim(curr)) s"$curr" else ""
323323
newBuf append (toAdd + suffix)
324-
lastToken = updateLastToken(str)
324+
lastValDefToken = updateLastToken(str)
325325
prev = curr
326326
}
327327

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

Lines changed: 25 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -61,107 +61,38 @@ class SyntaxHighlightingTests {
6161
}
6262

6363
@Test
64-
def valVarDef = {
65-
val source =
66-
"""
67-
|package test
68-
|
69-
|object A {
70-
| val a = 123
71-
| var b = 123 /*Int*/
72-
| var c = "123" // String
73-
| var d: Int = 123
74-
| var e:Int = 123;e
75-
| e
76-
| print(a)
77-
| 123;123
78-
| def f = 123
79-
| def f1(x: Int) = 123
80-
| def f2[T](x: T) = { 123 }
81-
|}
82-
""".stripMargin
83-
val expected =
84-
"""
85-
|<K|package> test
86-
|
87-
|<K|object> <T|A> {
88-
| <K|val> <V|a> = <L|123>
89-
| <K|var> <V|b> = <L|123> <C|/*Int*/>
90-
| <K|var> <V|c> = <L|"123"> <C|// String
91-
|> <K|var> <V|d>: <T|Int> = <L|123>
92-
| <K|var> <V|e>:<T|Int> = <L|123>;e
93-
| e
94-
| print(a)
95-
| <L|123>;<L|123>
96-
| <K|def> <V|f> = <L|123>
97-
| <K|def> <V|f1>(x: <T|Int>) = <L|123>
98-
| <K|def> <V|f2>[<T|T>](x: <T|T>) = { <L|123> }
99-
|}
100-
""".stripMargin
101-
test(source, expected)
64+
def valDef = {
65+
test("val a = 123", "<K|val> <V|a> = <L|123>")
66+
test("var b = 123 /*Int*/", "<K|var> <V|b> = <L|123> <C|/*Int*/>")
67+
test("""var c = "123" // String""", """<K|var> <V|c> = <L|"123"> <C|// String>""")
68+
test("var e:Int = 123;e", "<K|var> <V|e>:<T|Int> = <L|123>;e")
69+
test("def f = 123", "<K|def> <V|f> = <L|123>")
70+
test("def f1(x: Int) = 123", "<K|def> <V|f1>(x: <T|Int>) = <L|123>")
71+
test("def f2[T](x: T) = { 123 }", "<K|def> <V|f2>[<T|T>](x: <T|T>) = { <L|123> }")
10272
}
10373

10474
@Test
10575
def patternMatching = {
106-
val source =
107-
"""
108-
|val aFruit: Fruit = Apple("red", 123)
109-
|
110-
|val Apple(color, weight) = aFruit
111-
|
112-
|sealed trait Fruit
113-
|case class Apple(color: String, weight: Double) extends Fruit
114-
|case class Orange(weight: Double) extends Fruit
115-
|case class Melon(weight: Double) extends Fruit
116-
|
117-
|aFruit match {
118-
| case Apple(_, weight) => println(s"apple: $weight kgs")
119-
| case o: Orange => println(s"orange ${o.weight} kgs")
120-
| case m @ Melon(weight) => println(s"melon: ${m.weight} kgs")
121-
|}
122-
""".stripMargin
123-
val expected =
124-
"""
125-
|<K|val> <V|aFruit>: <T|Fruit> = <T|Apple>(<L|"red">, <L|123>)
126-
|
127-
|<K|val> <T|Apple>(<V|color>, <V|weight>) = aFruit
128-
|
129-
|<K|sealed> <K|trait> <T|Fruit>
130-
|<K|case> <K|class> <T|Apple>(color: <T|String>, weight: <T|Double>) <K|extends> <T|Fruit>
131-
|<K|case> <K|class> <T|Orange>(weight: <T|Double>) <K|extends> <T|Fruit>
132-
|<K|case> <K|class> <T|Melon>(weight: <T|Double>) <K|extends> <T|Fruit>
133-
|
134-
|aFruit <K|match> {
135-
| <K|case> <T|Apple>(<V|_>, <V|weight>) <T|=>> println(s<L|"apple: <V|$weight <L|kgs">)
136-
| <K|case> <V|o>: <T|Orange> <T|=>> println(s<L|"orange <V|${o.weight}<L| kgs">)
137-
| <K|case> <V|m> @ <T|Melon>(<V|weight>) <T|=>> println(s<L|"melon: <V|${m.weight}<L| kgs">)
138-
|}
139-
""".stripMargin
140-
test(source, expected)
76+
test("""val aFruit: Fruit = Apple("red", 123)""",
77+
"""<K|val> <V|aFruit>: <T|Fruit> = <T|Apple>(<L|"red">, <L|123>)""")
78+
test("""val Apple(color, weight) = aFruit""",
79+
"""<K|val> <T|Apple>(<V|color>, <V|weight>) = aFruit""")
80+
test("""case Apple(_, weight) => println(s"apple: $weight kgs")""",
81+
"""<K|case> <T|Apple>(<V|_>, <V|weight>) <T|=>> println(s<L|"apple: <V|$weight <L|kgs">)""")
82+
test("""case o: Orange => println(s"orange ${o.weight} kgs")""",
83+
"""<K|case> <V|o>: <T|Orange> <T|=>> println(s<L|"orange <V|${o.weight}<L| kgs">)""")
84+
test("""case m @ Melon(weight) => println(s"melon: ${m.weight} kgs")""",
85+
"""<K|case> <V|m> @ <T|Melon>(<V|weight>) <T|=>> println(s<L|"melon: <V|${m.weight}<L| kgs">)""")
14186
}
14287

14388
@Test
14489
def unionTypes = {
145-
val source =
146-
"""
147-
|type A = String|Int| Long
148-
|type B = String |Int| Long
149-
|type C = String | Int | Long
150-
|type D = String&Int& Long
151-
|type E = String &Int& Long
152-
|type F = String & Int & Long
153-
|fn[String|Char](input)
154-
""".stripMargin
155-
val expected =
156-
"""
157-
|<K|type> <T|A> = <T|String>|<T|Int>| <T|Long>
158-
|<K|type> <T|B> = <T|String> |<T|Int>| <T|Long>
159-
|<K|type> <T|C> = <T|String> | <T|Int> | <T|Long>
160-
|<K|type> <T|D> = <T|String>&<T|Int>& <T|Long>
161-
|<K|type> <T|E> = <T|String> &<T|Int>& <T|Long>
162-
|<K|type> <T|F> = <T|String> & <T|Int> & <T|Long>
163-
|fn[<T|String>|<T|Char>](input)
164-
""".stripMargin
165-
test(source, expected)
90+
test("type A = String|Int| Long", "<K|type> <T|A> = <T|String>|<T|Int>| <T|Long>")
91+
test("type B = String |Int| Long", "<K|type> <T|B> = <T|String> |<T|Int>| <T|Long>")
92+
test("type C = String | Int | Long", "<K|type> <T|C> = <T|String> | <T|Int> | <T|Long>")
93+
test("type D = String&Int& Long", "<K|type> <T|D> = <T|String>&<T|Int>& <T|Long>")
94+
test("type E = String &Int& Long", "<K|type> <T|E> = <T|String> &<T|Int>& <T|Long>")
95+
test("type F = String & Int & Long", "<K|type> <T|F> = <T|String> & <T|Int> & <T|Long>")
96+
test("fn[String|Char](input)", "fn[<T|String>|<T|Char>](input)")
16697
}
16798
}

0 commit comments

Comments
 (0)