Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When checking, output unified diff if requested #188

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ lazy val plugin = project
.settings(
moduleName := "sbt-scalafmt",
libraryDependencies ++= List(
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0",
"org.scalameta" %% "scalafmt-sysops" % scalafmtVersion,
"org.scalameta" %% "scalafmt-dynamic" % scalafmtVersion
),
Expand Down
23 changes: 23 additions & 0 deletions plugin/src/main/scala/org/scalafmt/sbt/DiffUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.scalafmt.sbt

import org.scalafmt.CompatCollections.JavaConverters._

import difflib.DiffUtils._

object DiffUtils {

def unifiedDiff(file: String, input: String, output: String): String = {
def jList(code: String, addEol: Boolean) = {
val last = if (addEol) Iterator.single("") else Iterator.empty
(code.linesIterator ++ last).toIndexedSeq.asJava
}
val a = jList(input, false)
// output always has EOL; if input doesn't, pretend output has extra line
val inputNoEol = input.lastOption.forall(x => x != '\n' && x != '\r')
val b = jList(output, inputNoEol)
val patch =
generateUnifiedDiff(s"a$file", s"b$file", a, diff(a, b), 1)
if (patch.isEmpty) "" else patch.iterator().asScala.mkString("\n")
}

}
1 change: 1 addition & 0 deletions plugin/src/main/scala/org/scalafmt/sbt/ErrorHandling.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.scalafmt.sbt

private[sbt] class ErrorHandling(
val printDiff: Boolean,
val logOnEachError: Boolean,
val failOnErrors: Boolean,
val detailedErrorEnabled: Boolean
Expand Down
12 changes: 11 additions & 1 deletion plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ object ScalafmtPlugin extends AutoPlugin {
val scalafmtFailOnErrors = settingKey[Boolean](
"Controls whether to fail in case of formatting errors."
)
val scalafmtPrintDiff = settingKey[Boolean](
"Enables full diff output when running check."
)
}

import autoImport._
Expand Down Expand Up @@ -306,7 +309,11 @@ object ScalafmtPlugin extends AutoPlugin {
}
val unformatted = Set.newBuilder[File]
withFormattedSources(Unit, sources) { (_, file, input, output) =>
log.warn(s"$file isn't formatted properly!")
val diff = if (errorHandling.printDiff) {
DiffUtils.unifiedDiff("/" + asRelative(file).getPath, input, output)
} else ""
val suffix = if (diff.isEmpty) "" else '\n' + diff
log.warn(s"$file isn't formatted properly!$suffix")
unformatted += file
Unit
}
Expand Down Expand Up @@ -448,6 +455,7 @@ object ScalafmtPlugin extends AutoPlugin {
thisProject.value,
scalafmtFilter.value,
new ErrorHandling(
scalafmtPrintDiff.value,
scalafmtLogOnEachError.value,
scalafmtFailOnErrors.value,
scalafmtDetailedError.value
Expand Down Expand Up @@ -492,6 +500,7 @@ object ScalafmtPlugin extends AutoPlugin {
thisProject.value,
"",
new ErrorHandling(
scalafmtPrintDiff.value,
scalafmtLogOnEachError.value,
scalafmtFailOnErrors.value,
scalafmtDetailedError.value
Expand Down Expand Up @@ -524,6 +533,7 @@ object ScalafmtPlugin extends AutoPlugin {
scalafmtOnCompile := false,
scalafmtLogOnEachError := false,
scalafmtFailOnErrors := true,
scalafmtPrintDiff := false,
scalafmtDetailedError := false
)

Expand Down