Skip to content

Commit

Permalink
Merge pull request #46 from mpollmeier/optionally-show-diff
Browse files Browse the repository at this point in the history
optionally show diff
  • Loading branch information
joeycozza authored Sep 7, 2017
2 parents fc86eb8 + 7e6dc79 commit 3d5c1a2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion sbt-scalafmt/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 3d5c1a2

Please sign in to comment.