Skip to content

Commit e754a2d

Browse files
committed
Change typeDiff to highlight changes less than 50%
1 parent d2b6205 commit e754a2d

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/dotty/tools/dotc/printing/Formatting.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,17 @@ object Formatting {
242242
* correct `Context` for printing should also be passed when calling the
243243
* method.
244244
*
245-
* @return the (found, expected) with coloring to highlight the difference
245+
* @return the (found, expected, changePercentage) with coloring to
246+
* highlight the difference
246247
*/
247248
def typeDiff(found: Type, expected: Type)(implicit ctx: Context): (String, String) = {
248249
val fnd = wrapNonSensical(found, found.show)
249250
val exp = wrapNonSensical(expected, expected.show)
250251

251-
(found, expected) match {
252-
case (_: RefinedType, _: RefinedType) if ctx.settings.color.value != "never" =>
253-
DiffUtil.mkColoredTypeDiff(fnd, exp)
254-
case _ =>
255-
(hl"$fnd", hl"$exp")
252+
DiffUtil.mkColoredTypeDiff(fnd, exp) match {
253+
case _ if ctx.settings.color.value == "never" => (fnd, exp)
254+
case (fnd, exp, change) if change < 0.5 => (fnd, exp)
255+
case _ => (fnd, exp)
256256
}
257257
}
258258
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ object DiffUtil {
3232
}
3333

3434

35-
/** @return a tuple of the (found, expected) diffs as strings */
36-
def mkColoredTypeDiff(found: String, expected: String): (String, String) = {
35+
/** @return a tuple of the (found, expected, changedPercentage) diffs as strings */
36+
def mkColoredTypeDiff(found: String, expected: String): (String, String, Double) = {
37+
var totalChange = 0
3738
val foundTokens = splitTokens(found, Nil).toArray
3839
val expectedTokens = splitTokens(expected, Nil).toArray
3940

@@ -42,15 +43,19 @@ object DiffUtil {
4243

4344
val exp = diffExp.collect {
4445
case Unmodified(str) => str
45-
case Inserted(str) => ADDITION_COLOR + str + ANSI_DEFAULT
46+
case Inserted(str) =>
47+
totalChange += str.length
48+
ADDITION_COLOR + str + ANSI_DEFAULT
4649
}.mkString
4750

4851
val fnd = diffAct.collect {
4952
case Unmodified(str) => str
50-
case Inserted(str) => DELETION_COLOR + str + ANSI_DEFAULT
53+
case Inserted(str) =>
54+
totalChange += str.length
55+
DELETION_COLOR + str + ANSI_DEFAULT
5156
}.mkString
5257

53-
(fnd, exp)
58+
(fnd, exp, totalChange.toDouble / (expected.length + found.length))
5459
}
5560

5661
def mkColoredCodeDiff(code: String, lastCode: String, printDiffDel: Boolean): String = {

0 commit comments

Comments
 (0)