@@ -8,10 +8,13 @@ object DiffUtil {
8
8
private final val ANSI_DEFAULT = " \u001B [0m"
9
9
private final val ANSI_RED = " \u001B [31m"
10
10
private final val ANSI_GREEN = " \u001B [32m"
11
+ private final val ANSI_EOF = " \u001B [2m"
11
12
12
13
private final val DELETION_COLOR = ANSI_RED
13
14
private final val ADDITION_COLOR = ANSI_GREEN
14
15
16
+ val EOF = new String (" EOF" ) // Unique string up to reference
17
+
15
18
@ tailrec private def splitTokens (str : String , acc : List [String ] = Nil ): List [String ] = {
16
19
if (str == " " ) {
17
20
acc.reverse
@@ -58,22 +61,38 @@ object DiffUtil {
58
61
(fnd, exp, totalChange.toDouble / (expected.length + found.length))
59
62
}
60
63
61
- def mkColoredLineDiff (expected : String , actual : String ): String = {
62
- val tokens = splitTokens(expected, Nil ).toArray
63
- val lastTokens = splitTokens(actual, Nil ).toArray
64
-
65
- val diff = hirschberg(lastTokens, tokens)
64
+ def mkColoredLineDiff (expected : String , actual : String , expectedSize : Int ): String = {
65
+ lazy val diff = {
66
+ val tokens = splitTokens(expected, Nil ).toArray
67
+ val lastTokens = splitTokens(actual, Nil ).toArray
68
+ hirschberg(lastTokens, tokens)
69
+ }
66
70
67
- " |SOF\n " + diff.collect {
68
- case Unmodified (str) =>
69
- " |" + str
70
- case Inserted (str) =>
71
- ADDITION_COLOR + " e |" + str + ANSI_DEFAULT
72
- case Modified (old, str) =>
73
- DELETION_COLOR + " a |" + old + " \n e |" + ADDITION_COLOR + str + ANSI_DEFAULT
74
- case Deleted (str) =>
75
- DELETION_COLOR + " \n a |" + str + ANSI_DEFAULT
76
- }.mkString + " \n |EOF"
71
+ val expectedDiff =
72
+ if (expected eq EOF ) ANSI_EOF + expected + ANSI_DEFAULT
73
+ else diff.collect {
74
+ case Unmodified (str) => str
75
+ case Inserted (str) =>
76
+ ADDITION_COLOR + str + ANSI_DEFAULT
77
+ case Modified (_, str) =>
78
+ ADDITION_COLOR + str + ANSI_DEFAULT
79
+ case Deleted (_) => " "
80
+ }.mkString
81
+
82
+ val actualDiff =
83
+ if (actual eq EOF ) ANSI_EOF + actual + ANSI_DEFAULT
84
+ else diff.collect {
85
+ case Unmodified (str) => str
86
+ case Inserted (_) => " "
87
+ case Modified (str, _) =>
88
+ DELETION_COLOR + str + ANSI_DEFAULT
89
+ case Deleted (str) =>
90
+ DELETION_COLOR + str + ANSI_DEFAULT
91
+ }.mkString
92
+
93
+ val pad = " " * 0 .max(expectedSize - expected.length)
94
+
95
+ expectedDiff + pad + " | " + actualDiff
77
96
}
78
97
79
98
def mkColoredCodeDiff (code : String , lastCode : String , printDiffDel : Boolean ): String = {
0 commit comments