diff --git a/README.md b/README.md index 496ddbe..5568650 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,14 @@ ignoreErrors in scalafmt := false // current project ignoreErrors in (Compile, scalafmt) := false // current project, specific configuration ``` +By default, scalafmt just lists the files that have differences. You can configure it to show the actual diff like this: + +```scala +scalafmtShowDiff in (ThisBuild, scalafmt) := false // all projects +scalafmtShowDiff in scalafmt := false // current project +scalafmtShowDiff in (Compile, scalafmt) := false // current project, specific configuration +``` + ## Additional configuration The scalafmt task is defined by default for the compile and test configurations. To define it for additional diff --git a/sbt-scalafmt/build.sbt b/sbt-scalafmt/build.sbt index 0be5ada..f9fbb3f 100644 --- a/sbt-scalafmt/build.sbt +++ b/sbt-scalafmt/build.sbt @@ -6,7 +6,8 @@ buildInfoPackage := "com.lucidchart.sbt.scalafmt" libraryDependencies ++= Seq( "com.google.code.findbugs" % "jsr305" % "3.0.2" % Provided, // fixes warnings about javax annotations - "com.google.guava" % "guava" % "19.0" + "com.google.guava" % "guava" % "19.0", + "com.michaelpollmeier" %% "colordiff" % "0.8" ) sbtPlugin := true diff --git a/sbt-scalafmt/src/main/scala/com/lucidchart/sbt/scalafmt/ScalafmtCorePlugin.scala b/sbt-scalafmt/src/main/scala/com/lucidchart/sbt/scalafmt/ScalafmtCorePlugin.scala index c2a7e28..be4206e 100644 --- a/sbt-scalafmt/src/main/scala/com/lucidchart/sbt/scalafmt/ScalafmtCorePlugin.scala +++ b/sbt-scalafmt/src/main/scala/com/lucidchart/sbt/scalafmt/ScalafmtCorePlugin.scala @@ -1,5 +1,6 @@ package com.lucidchart.sbt.scalafmt +import colordiff.ColorDiff import com.google.common.cache._ import com.lucidchart.scalafmt.api.{Dialect, ScalafmtFactory, Scalafmtter} import java.io.FileNotFoundException @@ -9,6 +10,7 @@ import sbt.Keys._ import sbt._ import sbt.plugins.{IvyPlugin, JvmPlugin} import scala.collection.breakOut +import scala.io.Source import scala.util.control.NonFatal import scala.util.control.Exception.catching @@ -45,6 +47,11 @@ object ScalafmtCorePlugin extends AutoPlugin { "Fail build when one or more style issues are found", CSetting ) + val scalafmtShowDiff = SettingKey[Boolean]( + "scalafmt-show-diff", + "show differences between original and formatted version", + CSetting + ) private[this] val scalafmtFn = Def.task { val ignoreErrors = this.ignoreErrors.value @@ -167,15 +174,21 @@ object ScalafmtCorePlugin extends AutoPlugin { val scalafmtter = scalafmtFn.value val failForStyleIssues = scalafmtFailTest.value - val differentCount = sources.value.count { file => - val content = IO.read(file) - val hasChanges = content != scalafmtter(file.toString, content) - if (hasChanges) { - val msg = s"$file has changes after scalafmt" - if (failForStyleIssues) logger.error(msg) - else logger.warn(msg) - } - hasChanges + val showDiff = scalafmtShowDiff.value + val differentCount = sources.value.count { + file => + val original = IO.read(file) + val formatted = scalafmtter(file.toString, original) + val hasChanges = original != formatted + if (hasChanges) { + val msg = if (showDiff) { + val diff = ColorDiff(original.split('\n').toList, formatted.split('\n').toList) + s"$file has changes after scalafmt:\n$diff" + } else s"$file has changes after scalafmt" + if (failForStyleIssues) logger.error(msg) + else logger.warn(msg) + } + hasChanges } AnalysisPlatform.counted("Scala source", "", "s", differentCount).foreach { message => val msg = s"$message not formatted in $display" @@ -227,7 +240,8 @@ object ScalafmtCorePlugin extends AutoPlugin { scalafmtOnCompile := false, scalafmtTestOnCompile := false, scalafmtVersion := "0.6.8", - scalafmtFailTest := true + scalafmtFailTest := true, + scalafmtShowDiff := false ) override val projectSettings = Seq(