Skip to content

Commit

Permalink
Set javafmtOnCompile := false by default (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge authored Nov 9, 2021
1 parent 7bcf57a commit 1fc9fcc
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ addSbtPlugin("com.lightbend.sbt" % "sbt-java-formatter" % --latest version---)

For available versions see [releases](https://github.com/sbt/sbt-java-formatter/releases).

* `javafmt` formats Java files (done automatically on `compile` for `Compile` and `Test` configurations, unless `javafmtOnCompile := false` is set)
* `javafmt` formats Java files
* `javafmtAll` formats Java files for all configurations (`Compile` and `Test` by default)
* `javafmtCheck` fails if files need reformatting
* `javafmtCheckAll` fails if files need reformatting in any configuration (`Compile` and `Test` by default)

* The `javafmtOnCompile` setting controls whether the formatter kicks in on compile (`true` by default as the `AutomateJavaFormatterPlugin` is triggered if not disabled explicitly).
* The `javafmtOnCompile` setting controls whether the formatter kicks in on compile (`false` by default).
* The `javafmtStyle` setting defines the formatting style: Google Java Style (by default) or AOSP style.

This plugin requires sbt 1.3.0+.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object AutomateJavaFormatterPlugin extends AutoPlugin {

override def `requires` = plugins.JvmPlugin && JavaFormatterPlugin

override def globalSettings: Seq[Def.Setting[_]] = Seq(JavaFormatterPlugin.autoImport.javafmtOnCompile := true)
override def globalSettings: Seq[Def.Setting[_]] = Seq(JavaFormatterPlugin.autoImport.javafmtOnCompile := false)
}

object JavaFormatterPlugin extends AutoPlugin {
Expand All @@ -41,7 +41,7 @@ object JavaFormatterPlugin extends AutoPlugin {
val javafmtCheckAll: TaskKey[Unit] = taskKey(
"Execute the javafmtCheck task for all configurations in which it is enabled. " +
"(By default this means the Compile and Test configurations.)")
val javafmtOnCompile = settingKey[Boolean]("Format Java source files on compile, on by default.")
val javafmtOnCompile = settingKey[Boolean]("Format Java source files on compile, off by default.")
val javafmtStyle =
settingKey[JavaFormatterOptions.Style]("Define formatting style, Google Java Style (default) or AOSP")
val javafmtOptions = settingKey[JavaFormatterOptions](
Expand Down Expand Up @@ -91,12 +91,12 @@ object JavaFormatterPlugin extends AutoPlugin {
JavaFormatter.check(baseDir, sD, iF, eF, streamz, cache, options)
},
javafmtDoFormatOnCompile := Def.settingDyn {
if (javafmtOnCompile.value) {
javafmt in resolvedScoped.value.scope
} else {
Def.task(())
}
}.value,
if (javafmtOnCompile.value) {
javafmt in resolvedScoped.value.scope
} else {
Def.task(())
}
}.value,
compile / compileInputs := (compile / compileInputs).dependsOn(javafmtDoFormatOnCompile).value)

def notToBeScopedSettings: Seq[Setting[_]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ object JavaFormatter {

import sjsonnew.{ :*:, LList, LNil }

implicit val analysisIso = LList.iso({ a: Analysis => ("failedCheck", a.failedCheck) :*: LNil }, {
in: (Set[File] :*: LNil) => Analysis(in.head)
})
implicit val analysisIso = LList.iso(
{ a: Analysis => ("failedCheck", a.failedCheck) :*: LNil },
{ in: (Set[File] :*: LNil) =>
Analysis(in.head)
})
}

private def cachedCheckSources(cacheStoreFactory: CacheStoreFactory, baseDir: File, sources: Seq[File], log: Logger)(
Expand Down Expand Up @@ -101,8 +103,8 @@ object JavaFormatter {
Analysis(failedCheck = unformatted)
}

private def cachedFormatSources(cacheStoreFactory: CacheStoreFactory, sources: Seq[File], log: Logger)(
implicit formatter: Formatter): Unit = {
private def cachedFormatSources(cacheStoreFactory: CacheStoreFactory, sources: Seq[File], log: Logger)(implicit
formatter: Formatter): Unit = {
trackSourcesViaCache(cacheStoreFactory, sources) { (outDiff, prev) =>
log.debug(outDiff.toString)
val updatedOrAdded = outDiff.modified & outDiff.checked
Expand Down Expand Up @@ -139,8 +141,8 @@ object JavaFormatter {
prevTracker(())
}

private def withFormattedSources[T](sources: Seq[File], log: Logger)(onFormat: (File, String, String) => T)(
implicit formatter: Formatter): Seq[Option[T]] = {
private def withFormattedSources[T](sources: Seq[File], log: Logger)(onFormat: (File, String, String) => T)(implicit
formatter: Formatter): Seq[Option[T]] = {
sources.map { file =>
val input = IO.read(file)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import com.google.googlejavaformat.java.JavaFormatterOptions
// no settings needed

ThisBuild / javafmtStyle := JavaFormatterOptions.Style.AOSP
ThisBuild / javafmtOnCompile := true
3 changes: 1 addition & 2 deletions plugin/src/sbt-test/sbt-java-formatter/default/test
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# compile should trigger formatting
> test:compile
> javafmtAll

#$ exec echo "====== FORMATTED ======"
#$ exec cat src/main/java/com/lightbend/BadFormatting.java
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// no settings needed
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# compile should trigger formatting
# compile should not trigger formatting
> compile

#$ exec echo "====== FORMATTED ======"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ThisBuild / javafmtOnCompile := true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.3.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.lightbend.sbt" % "sbt-java-formatter" % System.getProperty("plugin.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lightbend;

public class BadFormatting {
BadFormatting() {
example();
}

public void example() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.lightbend;

public class BadFormatting {
BadFormatting () {example();}
public void example () {}
}
10 changes: 10 additions & 0 deletions plugin/src/sbt-test/sbt-java-formatter/on-compile-true/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# compile should not trigger formatting
> compile

#$ exec echo "====== FORMATTED ======"
#$ exec cat src/main/java/com/lightbend/BadFormatting.java
#$ exec echo "====== EXPECTED ======"
#$ exec cat src/main/java-expected/com/lightbend/BadFormatting.java

#$ exec echo "====== DIFF ======"
$ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java
3 changes: 0 additions & 3 deletions plugin/src/sbt-test/sbt-java-formatter/on-compile/build.sbt

This file was deleted.

0 comments on commit 1fc9fcc

Please sign in to comment.