Skip to content

Commit 6d7c230

Browse files
committed
Make DiffUtil's rendering readable in logs
1 parent f9db909 commit 6d7c230

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait MessageRendering {
2121
* @return string stripped of ANSI escape codes
2222
*/
2323
def stripColor(str: String): String =
24-
str.replaceAll("\u001B\\[[;\\d]*m", "")
24+
str.replaceAll("\u001b\\[.*?m", "")
2525

2626
/** When inlining a method call, if there's an error we'd like to get the
2727
* outer context and the `pos` at which the call was inlined.

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,25 @@ object DiffUtil {
5858
(fnd, exp, totalChange.toDouble / (expected.length + found.length))
5959
}
6060

61-
def mkColoredCodeDiff(code: String, lastCode: String, printDiffDel: Boolean): String = {
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)
6266

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 + "\ne |" + ADDITION_COLOR + str + ANSI_DEFAULT
74+
case Deleted(str) =>
75+
DELETION_COLOR + "\na |" + str + ANSI_DEFAULT
76+
}.mkString + "\n |EOF"
77+
}
78+
79+
def mkColoredCodeDiff(code: String, lastCode: String, printDiffDel: Boolean): String = {
6380
val tokens = splitTokens(code, Nil).toArray
6481
val lastTokens = splitTokens(lastCode, Nil).toArray
6582

compiler/test/dotty/tools/dotc/ParallelTesting.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ trait ParallelTesting { self =>
456456
if (outputLines.length != checkLines.length || !linesMatch) {
457457
// Print diff to files and summary:
458458
val diff = outputLines.zip(checkLines).map { case (act, exp) =>
459-
DiffUtil.mkColoredCodeDiff(exp, act, true)
459+
DiffUtil.mkColoredLineDiff(exp, act)
460460
}.mkString("\n")
461461
val msg = s"\nOutput from run test '$checkFile' did not match expected, output:\n$diff\n"
462462
echo(msg)

0 commit comments

Comments
 (0)