From 88e41465dd1dcecaa3cd8f0971e8d71e61d48490 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Tue, 20 Sep 2016 17:32:23 +0200 Subject: Add basic diffing for shown values --- src/dotty/tools/dotc/util/DiffUtil.scala | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/dotty/tools/dotc/util/DiffUtil.scala b/src/dotty/tools/dotc/util/DiffUtil.scala index b7c77ad62..8bb39c88a 100644 --- a/src/dotty/tools/dotc/util/DiffUtil.scala +++ b/src/dotty/tools/dotc/util/DiffUtil.scala @@ -12,9 +12,7 @@ object DiffUtil { private final val DELETION_COLOR = ANSI_RED private final val ADDITION_COLOR = ANSI_GREEN - def mkColoredCodeDiff(code: String, lastCode: String, printDiffDel: Boolean): String = { - - @tailrec def splitTokens(str: String, acc: List[String] = Nil): List[String] = { + @tailrec private def splitTokens(str: String, acc: List[String] = Nil): List[String] = { if (str == "") { acc.reverse } else { @@ -33,6 +31,30 @@ object DiffUtil { } } + + /** @return a tuple of the (found, expected) diffs as strings */ + def mkColoredTypeDiff(found: String, expected: String): (String, String) = { + val foundTokens = splitTokens(found, Nil).toArray + val expectedTokens = splitTokens(expected, Nil).toArray + + val diffExp = hirschberg(foundTokens, expectedTokens) + val diffAct = hirschberg(expectedTokens, foundTokens) + + val exp = diffExp.collect { + case Unmodified(str) => str + case Inserted(str) => ADDITION_COLOR + str + ANSI_DEFAULT + }.mkString + + val fnd = diffAct.collect { + case Unmodified(str) => str + case Inserted(str) => DELETION_COLOR + str + ANSI_DEFAULT + }.mkString + + (fnd, exp) + } + + def mkColoredCodeDiff(code: String, lastCode: String, printDiffDel: Boolean): String = { + val tokens = splitTokens(code, Nil).toArray val lastTokens = splitTokens(lastCode, Nil).toArray -- cgit v1.2.3