Skip to content

Fix #3814 Correct highlighting issues in REPL #3987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@ object SyntaxHighlighting {
'q' :: 'r' :: 's' :: 't' :: 'u' :: 'v' :: 'w' :: 'x' :: 'y' :: 'z' :: Nil

private val typeEnders =
'{' :: '}' :: ')' :: '(' :: '[' :: ']' :: '=' :: ' ' :: ',' :: '.' ::
'\n' :: Nil
'{' :: '}' :: ')' :: '(' :: '[' :: ']' :: '=' :: ' ' :: ',' :: '.' :: '|' ::
'&' :: '\n' :: Nil

def apply(chars: Iterable[Char]): Iterable[Char] = {
var prev: Char = 0
var remaining = chars.toStream
val newBuf = new StringBuilder
var lastToken = ""
var lastValDefToken = ""

@inline def keywordStart =
prev == 0 || prev == ' ' || prev == '{' || prev == '(' ||
prev == '\n' || prev == '[' || prev == ','
prev == '\n' || prev == '[' || prev == ',' || prev == ':' ||
prev == '|' || prev == '&'

@inline def numberStart(c: Char) =
c.isDigit && (!prev.isLetter || prev == '.' || prev == ' ' || prev == '(' || prev == '\u0000')
Expand Down Expand Up @@ -289,6 +290,23 @@ object SyntaxHighlighting {
case _ => false
}

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

/** lastValDefToken is used to check whether we want to show something
* in valDef color or not. There are only a few cases when lastValDefToken
* should be updated, that way we can avoid stopping coloring too early.
* eg.: case A(x, y, z) => ???
* Without this function only x would be colored.
*/
def updateLastToken(currentToken: String): String =
(lastValDefToken, currentToken) match {
case _ if valDefStarterTokens.contains(currentToken) => currentToken
case (("val" | "var"), "=") => currentToken
case ("case", ("=>" | "class" | "object")) => currentToken
case ("def", _) => currentToken
case _ => lastValDefToken
}

while (remaining.nonEmpty && !delim(curr)) {
curr = takeChar()
if (!delim(curr)) sb += curr
Expand All @@ -298,12 +316,12 @@ object SyntaxHighlighting {
val toAdd =
if (shouldHL(str))
highlight(str)
else if (("var" :: "val" :: "def" :: "case" :: Nil).contains(lastToken))
else if (valDefStarterTokens.contains(lastValDefToken) && !List("=", "=>").contains(str))
valDef(str)
else str
val suffix = if (delim(curr)) s"$curr" else ""
newBuf append (toAdd + suffix)
lastToken = str
lastValDefToken = updateLastToken(str)
prev = curr
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package dotty.tools.dotc.printing

import org.junit.Assert._
import org.junit.Test

/** Adapted from Ammonite HighlightTests
*/
class SyntaxHighlightingTests {
import SyntaxHighlighting._

private def test(source: String, expected: String): Unit = {
val highlighted = SyntaxHighlighting.apply(source)
.mkString
.replace(NoColor, ">")
.replace(CommentColor, "<C|")
.replace(KeywordColor, "<K|")
.replace(ValDefColor, "<V|")
.replace(LiteralColor, "<L|")
.replace(StringColor, "<S|")
.replace(TypeColor, "<T|")
// .replace(AnnotationColor, "<A|") // is the same color as type color

if (expected != highlighted) {
// assertEquals produces weird expected/found message
fail(s"expected: $expected but was: $highlighted")
}
}

@Test
def comments = {
test("//a", "<C|//a>")
test("/** a */", "<C|/** a */>")
test("/* a */", "<C|/* a */>")
}

@Test
def types = {
test("type Foo = Int", "<K|type> <T|Foo> = <T|Int>")
}

@Test
def literals = {
test("1", "<L|1>")
// test("1L", "<L|1L>")
}

@Test
def strings = {
// For some reason we currently use literal color for string
test("\"Hello\"", "<L|\"Hello\">")
}

@Test
def annotations = {
test("@tailrec", "<T|@tailrec>")
}

@Test
def expressions = {
test("val x = 1 + 2 + 3", "<K|val> <V|x> = <L|1> + <L|2> + <L|3>")
}

@Test
def valDef = {
test("val a = 123", "<K|val> <V|a> = <L|123>")
test("var b = 123 /*Int*/", "<K|var> <V|b> = <L|123> <C|/*Int*/>")
test("""var c = "123" // String""", """<K|var> <V|c> = <L|"123"> <C|// String>""")
test("var e:Int = 123;e", "<K|var> <V|e>:<T|Int> = <L|123>;e")
test("def f = 123", "<K|def> <V|f> = <L|123>")
test("def f1(x: Int) = 123", "<K|def> <V|f1>(x: <T|Int>) = <L|123>")
test("def f2[T](x: T) = { 123 }", "<K|def> <V|f2>[<T|T>](x: <T|T>) = { <L|123> }")
}

@Test
def patternMatching = {
test("""val aFruit: Fruit = Apple("red", 123)""",
"""<K|val> <V|aFruit>: <T|Fruit> = <T|Apple>(<L|"red">, <L|123>)""")
test("""val Apple(color, weight) = aFruit""",
"""<K|val> <T|Apple>(<V|color>, <V|weight>) = aFruit""")
test("""case Apple(_, weight) => println(s"apple: $weight kgs")""",
"""<K|case> <T|Apple>(<V|_>, <V|weight>) <T|=>> println(s<L|"apple: <V|$weight <L|kgs">)""")
test("""case o: Orange => println(s"orange ${o.weight} kgs")""",
"""<K|case> <V|o>: <T|Orange> <T|=>> println(s<L|"orange <V|${o.weight}<L| kgs">)""")
test("""case m @ Melon(weight) => println(s"melon: ${m.weight} kgs")""",
"""<K|case> <V|m> @ <T|Melon>(<V|weight>) <T|=>> println(s<L|"melon: <V|${m.weight}<L| kgs">)""")
}

@Test
def unionTypes = {
test("type A = String|Int| Long", "<K|type> <T|A> = <T|String>|<T|Int>| <T|Long>")
test("type B = String |Int| Long", "<K|type> <T|B> = <T|String> |<T|Int>| <T|Long>")
test("type C = String | Int | Long", "<K|type> <T|C> = <T|String> | <T|Int> | <T|Long>")
test("type D = String&Int& Long", "<K|type> <T|D> = <T|String>&<T|Int>& <T|Long>")
test("type E = String &Int& Long", "<K|type> <T|E> = <T|String> &<T|Int>& <T|Long>")
test("type F = String & Int & Long", "<K|type> <T|F> = <T|String> & <T|Int> & <T|Long>")
test("fn[String|Char](input)", "fn[<T|String>|<T|Char>](input)")
}
}

This file was deleted.