diff --git a/README.md b/README.md index 98368d4..952aa3f 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ For available versions see [releases](https://github.com/sbt/sbt-java-formatter/ * The `javafmtRemoveUnusedImports` setting controls whether unused imports are removed (`true` by default). * The `javafmtReflowLongStrings` setting controls whether long string literals are reflowed (`true` by default). * The `javafmtFormatJavadoc` setting controls whether Javadoc comments are reformatted (`true` by default). +* The `javafmtReorderModifiers` setting controls whether modifiers are reordered into JLS order (`true` by default). * The `javafmtFormatterCompatibleJavaVersion` setting selects which `google-java-format` runtime line to use (`21` by default). * The `javafmtJavaMaxHeap` setting controls the maximum heap passed to the forked `google-java-format` JVM (`Some("256m")` by default). @@ -114,6 +115,7 @@ ThisBuild / javafmtSortImports := true ThisBuild / javafmtRemoveUnusedImports := true ThisBuild / javafmtReflowLongStrings := true ThisBuild / javafmtFormatJavadoc := true +ThisBuild / javafmtReorderModifiers := true ``` Set any of them to `false` to pass the corresponding `--skip-...` flag to `google-java-format`. @@ -129,11 +131,7 @@ If the selected formatter runtime is newer than the Java used to launch the form - lower `ThisBuild / javafmtFormatterCompatibleJavaVersion` - or point the formatter to a newer JDK via `SBT_JAVAFMT_JAVA_HOME` or `-Dsbt-javafmt.java.home=...` -`javafmtOptions` is still available for compatibility, but the preferred sbt-facing configuration is through the dedicated `javafmt...` settings above. - -`JavaFormatterOptions.reorderModifiers()` currently has no effect in this plugin. - -The plugin now runs `google-java-format` via its CLI in a forked JVM, and the released `google-java-format` CLI used here [does not yet support a corresponding `--skip-reordering-modifiers` flag](https://github.com/google/google-java-format/pull/1373). +`javafmtOptions` is still available for compatibility with upstream `JavaFormatterOptions`, but the preferred sbt-facing configuration is through the dedicated `javafmt...` settings above. If you want to tweak the format, take a minute to consider whether it is really worth it, and have a look at the motivations in the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html). If you decide you really need more flexibility, you could consider other plugins such as the [sbt-checkstyle-plugin](https://github.com/etsy/sbt-checkstyle-plugin) diff --git a/plugin/src/main/scala/com/github/sbt/JavaFormatterPlugin.scala b/plugin/src/main/scala/com/github/sbt/JavaFormatterPlugin.scala index d5d62c2..7fbd5c8 100644 --- a/plugin/src/main/scala/com/github/sbt/JavaFormatterPlugin.scala +++ b/plugin/src/main/scala/com/github/sbt/JavaFormatterPlugin.scala @@ -82,8 +82,10 @@ object JavaFormatterPlugin extends AutoPlugin { settingKey[Boolean]("Whether google-java-format should reflow long string literals. Enabled by default.") val javafmtFormatJavadoc = settingKey[Boolean]("Whether google-java-format should format Javadoc comments. Enabled by default.") + val javafmtReorderModifiers = + settingKey[Boolean]("Whether google-java-format should reorder modifiers into JLS order. Enabled by default.") val javafmtOptions = settingKey[JavaFormatterOptions]( - "Compatibility setting for upstream JavaFormatterOptions. Prefer the dedicated javafmt... settings; reorderModifiers() currently has no effect with the released google-java-format CLI used by this plugin.") + "Compatibility setting for upstream JavaFormatterOptions. Prefer the dedicated javafmt... settings where available.") } import autoImport._ @@ -121,7 +123,8 @@ object JavaFormatterPlugin extends AutoPlugin { javafmtSortImports := true, javafmtRemoveUnusedImports := true, javafmtReflowLongStrings := true, - javafmtFormatJavadoc := true) + javafmtFormatJavadoc := true, + javafmtReorderModifiers := true) def toBeScopedSettings: Seq[Setting[?]] = List( @@ -130,6 +133,7 @@ object JavaFormatterPlugin extends AutoPlugin { .builder() .style(javafmtStyle.value) .formatJavadoc(javafmtFormatJavadoc.value) + .reorderModifiers(javafmtReorderModifiers.value) .build(), javafmt := { val streamz = streams.value diff --git a/plugin/src/main/scala/com/github/sbt/javaformatter/JavaFormatter.scala b/plugin/src/main/scala/com/github/sbt/javaformatter/JavaFormatter.scala index 7a73621..f0cfb22 100644 --- a/plugin/src/main/scala/com/github/sbt/javaformatter/JavaFormatter.scala +++ b/plugin/src/main/scala/com/github/sbt/javaformatter/JavaFormatter.scala @@ -363,17 +363,15 @@ object JavaFormatter { sortImports: Boolean, removeUnusedImports: Boolean, reflowLongStrings: Boolean): Seq[String] = { - if (!options.reorderModifiers()) { - throw new MessageOnlyException( - "The forked google-java-format CLI does not support reorderModifiers = false. " + - "Please use the default reorderModifiers setting.") - } val styleFlags = if (options.style() == JavaFormatterOptions.Style.AOSP) Seq("--aosp") else Nil val javadocFlags = if (options.formatJavadoc()) Nil else Seq("--skip-javadoc-formatting") + val reorderModifiersFlags = + if (options.reorderModifiers()) Nil + else Seq("--skip-reordering-modifiers") val fixImportsOnlyFlags = if (fixImportsOnly) Seq("--fix-imports-only") else Nil @@ -386,7 +384,7 @@ object JavaFormatter { val reflowLongStringsFlags = if (reflowLongStrings) Nil else Seq("--skip-reflowing-long-strings") - styleFlags ++ javadocFlags ++ fixImportsOnlyFlags ++ sortImportsFlags ++ removeUnusedImportsFlags ++ reflowLongStringsFlags + styleFlags ++ javadocFlags ++ reorderModifiersFlags ++ fixImportsOnlyFlags ++ sortImportsFlags ++ removeUnusedImportsFlags ++ reflowLongStringsFlags } private case class CliResult(exitCode: Int, stdout: Vector[String], stderr: Vector[String]) diff --git a/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/build.sbt b/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/build.sbt deleted file mode 100644 index eda3e22..0000000 --- a/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/build.sbt +++ /dev/null @@ -1,10 +0,0 @@ -import com.google.googlejavaformat.java.JavaFormatterOptions - -ThisBuild / javafmtFormatterCompatibleJavaVersion := 11 - -Compile / javafmtOptions := JavaFormatterOptions - .builder() - .style(javafmtStyle.value) - .formatJavadoc(javafmtFormatJavadoc.value) - .reorderModifiers(false) - .build() diff --git a/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/test b/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/test deleted file mode 100644 index 329f06f..0000000 --- a/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/test +++ /dev/null @@ -1,2 +0,0 @@ --> javafmt --> javafmtCheck diff --git a/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/build.sbt b/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/build.sbt new file mode 100644 index 0000000..b255b38 --- /dev/null +++ b/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/build.sbt @@ -0,0 +1,2 @@ +ThisBuild / javafmtFormatterCompatibleJavaVersion := 11 +ThisBuild / javafmtReorderModifiers := false diff --git a/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/project/plugins.sbt b/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/project/plugins.sbt similarity index 100% rename from plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/project/plugins.sbt rename to plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/project/plugins.sbt diff --git a/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/src/main/java-expected/com/lightbend/BadFormatting.java b/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/src/main/java-expected/com/lightbend/BadFormatting.java new file mode 100644 index 0000000..94a989e --- /dev/null +++ b/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/src/main/java-expected/com/lightbend/BadFormatting.java @@ -0,0 +1,5 @@ +package com.lightbend; + +final public class BadFormatting { + static public void main(String[] args) {} +} diff --git a/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/src/main/java/com/lightbend/BadFormatting.java b/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/src/main/java/com/lightbend/BadFormatting.java new file mode 100644 index 0000000..e937c98 --- /dev/null +++ b/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/src/main/java/com/lightbend/BadFormatting.java @@ -0,0 +1,5 @@ +package com.lightbend; + +final public class BadFormatting{ + static public void main(String[] args) {} +} diff --git a/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/test b/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/test new file mode 100644 index 0000000..0463565 --- /dev/null +++ b/plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/test @@ -0,0 +1,5 @@ +-> javafmtCheck +> javafmt +> javafmtCheck + +$ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java