Skip to content

Commit cb5862e

Browse files
committed
Add basic diffing for shown values
1 parent 1e1725e commit cb5862e

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/dotty/tools/dotc/util/DiffUtil.scala

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ object DiffUtil {
1212
private final val DELETION_COLOR = ANSI_RED
1313
private final val ADDITION_COLOR = ANSI_GREEN
1414

15-
def mkColoredCodeDiff(code: String, lastCode: String, printDiffDel: Boolean): String = {
16-
17-
@tailrec def splitTokens(str: String, acc: List[String] = Nil): List[String] = {
15+
@tailrec private def splitTokens(str: String, acc: List[String] = Nil): List[String] = {
1816
if (str == "") {
1917
acc.reverse
2018
} else {
@@ -33,6 +31,30 @@ object DiffUtil {
3331
}
3432
}
3533

34+
35+
/** @return a tuple of the (found, expected) diffs as strings */
36+
def mkColoredTypeDiff(found: String, expected: String): (String, String) = {
37+
val foundTokens = splitTokens(found, Nil).toArray
38+
val expectedTokens = splitTokens(expected, Nil).toArray
39+
40+
val diffExp = hirschberg(foundTokens, expectedTokens)
41+
val diffAct = hirschberg(expectedTokens, foundTokens)
42+
43+
val exp = diffExp.collect {
44+
case Unmodified(str) => str
45+
case Inserted(str) => ADDITION_COLOR + str + ANSI_DEFAULT
46+
}.mkString
47+
48+
val fnd = diffAct.collect {
49+
case Unmodified(str) => str
50+
case Inserted(str) => DELETION_COLOR + str + ANSI_DEFAULT
51+
}.mkString
52+
53+
(fnd, exp)
54+
}
55+
56+
def mkColoredCodeDiff(code: String, lastCode: String, printDiffDel: Boolean): String = {
57+
3658
val tokens = splitTokens(code, Nil).toArray
3759
val lastTokens = splitTokens(lastCode, Nil).toArray
3860

0 commit comments

Comments
 (0)