Skip to content

Commit

Permalink
Only show changed lines in diff
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszwawrzyk committed Aug 16, 2018
1 parent 746d474 commit 0bc115a
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion internal/zinc-core/src/main/scala/sbt/internal/inc/APIDiff.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ private[inc] class APIDiff {
val lastTokens = splitTokens(lastCode, Nil).toArray

val diff = hirschberg(lastTokens, tokens)
val cleanDiff = dropNonChangedLines(diff)

diff.collect {
cleanDiff.collect {
case Unmodified(str) => str
case Inserted(str) => ADDITION_COLOR + str + ANSI_DEFAULT
case Modified(old, str) if printDiffDel =>
Expand All @@ -155,6 +156,24 @@ private[inc] class APIDiff {
}.mkString
}

private def dropNonChangedLines(patches: Array[Patch]): Seq[Patch] = {
def recur(patches: List[Patch], result: List[List[Patch]]): List[List[Patch]] = {
if (patches.isEmpty) result.reverse
else {
val (line, rest) = patches.span {
case Modified(_, "\n") | Deleted("\n") | Inserted("\n") | Unmodified("\n") => false
case _ => true
}
val hasChanges = line.exists {
case _: Modified | _: Deleted | _: Inserted => true
case _: Unmodified => false
}
recur(rest.drop(1), if (hasChanges) (line ++ rest.take(1)) :: result else result)
}
}
recur(patches.toList, Nil).flatten
}

private sealed trait Patch
private case class Unmodified(str: String) extends Patch
private case class Modified(original: String, str: String) extends Patch
Expand Down

0 comments on commit 0bc115a

Please sign in to comment.