Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7fe3dc9

Browse files
committed
Show tree diff
1 parent 62088cc commit 7fe3dc9

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

build.sbt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ lazy val compiler = configureAsSubproject(project)
444444
name := "scala-compiler",
445445
description := "Scala Compiler",
446446
libraryDependencies += asmDep,
447+
libraryDependencies += diffUtilsDep,
447448
// These are only needed for the POM:
448449
// TODO: jline dependency is only needed for the REPL shell, which should move to its own jar
449450
libraryDependencies ++= jlineDeps,
@@ -454,7 +455,11 @@ lazy val compiler = configureAsSubproject(project)
454455
// (with strings) to deal with mutual recursion
455456
Compile / packageBin / products :=
456457
(Compile / packageBin / products).value ++
457-
Seq((Compile / dependencyClasspath).value.find(_.get(moduleID.key).map(id => (id.organization, id.name, id.revision)).contains((asmDep.organization, asmDep.name, asmDep.revision))).get.data) ++
458+
(Compile / dependencyClasspath).value.filter(_.get(moduleID.key).map(id => (id.organization, id.name, id.revision)) match {
459+
case Some((diffUtilsDep.organization, diffUtilsDep.name, diffUtilsDep.revision)) => true
460+
case Some((asmDep.organization, asmDep.name, asmDep.revision)) => true
461+
case _ => false
462+
}).map(_.data) ++
458463
(LocalProject("interactive") / Compile / packageBin / products).value ++
459464
(LocalProject("scaladoc") / Compile / packageBin / products).value ++
460465
(LocalProject("repl") / Compile / packageBin / products).value ++

src/compiler/scala/tools/nsc/Global.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,22 @@ class Global(var currentSettings: Settings, reporter0: Reporter)
210210
print(" // " + unit.source)
211211
if (unit.body == null) println(": tree is null")
212212
else {
213-
val source = util.stringFromWriter(w => newTreePrinter(w) print unit.body)
213+
val source = util.stringFromWriter(w => newTreePrinter(w).print(unit.body))
214214

215215
// treePrinter show unit.body
216216
if (lastPrintedSource == source)
217-
println(": tree is unchanged since " + lastPrintedPhase)
217+
println(s": tree is unchanged since $lastPrintedPhase")
218+
else if (settings.showTreeDiff) {
219+
import scala.jdk.CollectionConverters._
220+
import com.github.difflib.{DiffUtils, UnifiedDiffUtils}
221+
val diff = DiffUtils.diff(lastPrintedSource.linesIterator.toList.asJava, source.linesIterator.toList.asJava)
222+
val unified = UnifiedDiffUtils.generateUnifiedDiff(lastPrintedPhase.name, phase.prev.name, lastPrintedSource.linesIterator.toList.asJava, diff, 1).asScala
223+
lastPrintedPhase = phase.prev
224+
lastPrintedSource = source
225+
println("")
226+
unified.foreach(println)
227+
println("")
228+
}
218229
else {
219230
lastPrintedPhase = phase.prev // since we're running inside "exitingPhase"
220231
lastPrintedSource = source

src/compiler/scala/tools/nsc/settings/ScalaSettings.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,16 @@ trait ScalaSettings extends StandardScalaSettings with Warnings { _: MutableSett
227227
name = "-Yprint-trees",
228228
helpArg = "style",
229229
descr = "How to print trees when -Vprint is enabled.",
230-
choices = List("text", "compact", "format", "text+format"),
230+
choices = List("text", "compact", "format", "text+format", "diff"),
231231
default = "text"
232232
).withPostSetHook(pt => pt.value match {
233-
case "text" =>
234233
case "compact" => XshowtreesCompact.value = true
235234
case "format" => Xshowtrees.value = true
236235
case "text+format" => XshowtreesStringified.value = true
236+
case _ =>
237237
})
238238

239+
def showTreeDiff: Boolean = YprintTrees.value == "diff"
239240
val Xshowtrees = BooleanSetting ("-Yshow-trees", "(Requires -Vprint:) Print detailed ASTs in formatted form.").internalOnly()
240241
val XshowtreesCompact
241242
= BooleanSetting ("-Yshow-trees-compact", "(Requires -Vprint:) Print detailed ASTs in compact form.").internalOnly()

0 commit comments

Comments
 (0)