From 83994fa9e4ec58dea7c3ffb059323bdbf9e056af Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Thu, 10 Aug 2017 18:52:52 +1200 Subject: [PATCH 1/3] optionally show diff --- README.md | 8 +++++ sbt-scalafmt/build.sbt | 4 ++- .../sbt/scalafmt/ScalafmtCorePlugin.scala | 35 +++++++++++++------ 3 files changed, 36 insertions(+), 11 deletions(-) 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..f786609 100644 --- a/sbt-scalafmt/build.sbt +++ b/sbt-scalafmt/build.sbt @@ -6,9 +6,11 @@ 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" ) +resolvers += Resolver.mavenLocal sbtPlugin := true scalacOptions ++= Seq( 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 2cfb871..cd60c72 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 @@ -163,15 +170,22 @@ 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" + logger.warn(msg) + 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" @@ -223,7 +237,8 @@ object ScalafmtCorePlugin extends AutoPlugin { scalafmtOnCompile := false, scalafmtTestOnCompile := false, scalafmtVersion := "0.6.8", - scalafmtFailTest := true + scalafmtFailTest := true, + scalafmtShowDiff := false ) override val projectSettings = Seq( From 00bbcd3cd736741e5edb09dd058c7e3c95aad13e Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Thu, 31 Aug 2017 18:50:26 +1200 Subject: [PATCH 2/3] remove duplicate logger.warn --- .../scala/com/lucidchart/sbt/scalafmt/ScalafmtCorePlugin.scala | 1 - 1 file changed, 1 deletion(-) 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 cd60c72..d699daa 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 @@ -181,7 +181,6 @@ object ScalafmtCorePlugin extends AutoPlugin { 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" - logger.warn(msg) if (failForStyleIssues) logger.error(msg) else logger.warn(msg) } From 7e6dc79fcdbf1cbd9795b9c72e48e8e62ddf7b5a Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Fri, 1 Sep 2017 12:16:46 +1200 Subject: [PATCH 3/3] remove mavenLocal resolver --- sbt-scalafmt/build.sbt | 1 - 1 file changed, 1 deletion(-) diff --git a/sbt-scalafmt/build.sbt b/sbt-scalafmt/build.sbt index f786609..f9fbb3f 100644 --- a/sbt-scalafmt/build.sbt +++ b/sbt-scalafmt/build.sbt @@ -10,7 +10,6 @@ libraryDependencies ++= Seq( "com.michaelpollmeier" %% "colordiff" % "0.8" ) -resolvers += Resolver.mavenLocal sbtPlugin := true scalacOptions ++= Seq(