Skip to content

Commit 121ee31

Browse files
committed
Support reorderModifiers = false via google-java-format CLI
1 parent 6baceea commit 121ee31

10 files changed

Lines changed: 30 additions & 25 deletions

File tree

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ For available versions see [releases](https://github.com/sbt/sbt-java-formatter/
3838
* The `javafmtRemoveUnusedImports` setting controls whether unused imports are removed (`true` by default).
3939
* The `javafmtReflowLongStrings` setting controls whether long string literals are reflowed (`true` by default).
4040
* The `javafmtFormatJavadoc` setting controls whether Javadoc comments are reformatted (`true` by default).
41+
* The `javafmtReorderModifiers` setting controls whether modifiers are reordered into JLS order (`true` by default).
4142
* The `javafmtFormatterCompatibleJavaVersion` setting selects which `google-java-format` runtime line to use (`21` by default).
4243
* The `javafmtJavaMaxHeap` setting controls the maximum heap passed to the forked `google-java-format` JVM (`Some("256m")` by default).
4344

@@ -114,6 +115,7 @@ ThisBuild / javafmtSortImports := true
114115
ThisBuild / javafmtRemoveUnusedImports := true
115116
ThisBuild / javafmtReflowLongStrings := true
116117
ThisBuild / javafmtFormatJavadoc := true
118+
ThisBuild / javafmtReorderModifiers := true
117119
```
118120

119121
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
129131
- lower `ThisBuild / javafmtFormatterCompatibleJavaVersion`
130132
- or point the formatter to a newer JDK via `SBT_JAVAFMT_JAVA_HOME` or `-Dsbt-javafmt.java.home=...`
131133

132-
`javafmtOptions` is still available for compatibility, but the preferred sbt-facing configuration is through the dedicated `javafmt...` settings above.
133-
134-
`JavaFormatterOptions.reorderModifiers()` currently has no effect in this plugin.
135-
136-
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).
134+
`javafmtOptions` is still available for compatibility with upstream `JavaFormatterOptions`, but the preferred sbt-facing configuration is through the dedicated `javafmt...` settings above.
137135

138136
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).
139137
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)

plugin/src/main/scala/com/github/sbt/JavaFormatterPlugin.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ object JavaFormatterPlugin extends AutoPlugin {
8282
settingKey[Boolean]("Whether google-java-format should reflow long string literals. Enabled by default.")
8383
val javafmtFormatJavadoc =
8484
settingKey[Boolean]("Whether google-java-format should format Javadoc comments. Enabled by default.")
85+
val javafmtReorderModifiers =
86+
settingKey[Boolean]("Whether google-java-format should reorder modifiers into JLS order. Enabled by default.")
8587
val javafmtOptions = settingKey[JavaFormatterOptions](
86-
"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.")
88+
"Compatibility setting for upstream JavaFormatterOptions. Prefer the dedicated javafmt... settings where available.")
8789
}
8890

8991
import autoImport._
@@ -121,7 +123,8 @@ object JavaFormatterPlugin extends AutoPlugin {
121123
javafmtSortImports := true,
122124
javafmtRemoveUnusedImports := true,
123125
javafmtReflowLongStrings := true,
124-
javafmtFormatJavadoc := true)
126+
javafmtFormatJavadoc := true,
127+
javafmtReorderModifiers := true)
125128

126129
def toBeScopedSettings: Seq[Setting[?]] =
127130
List(
@@ -130,6 +133,7 @@ object JavaFormatterPlugin extends AutoPlugin {
130133
.builder()
131134
.style(javafmtStyle.value)
132135
.formatJavadoc(javafmtFormatJavadoc.value)
136+
.reorderModifiers(javafmtReorderModifiers.value)
133137
.build(),
134138
javafmt := {
135139
val streamz = streams.value

plugin/src/main/scala/com/github/sbt/javaformatter/JavaFormatter.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,17 +363,15 @@ object JavaFormatter {
363363
sortImports: Boolean,
364364
removeUnusedImports: Boolean,
365365
reflowLongStrings: Boolean): Seq[String] = {
366-
if (!options.reorderModifiers()) {
367-
throw new MessageOnlyException(
368-
"The forked google-java-format CLI does not support reorderModifiers = false. " +
369-
"Please use the default reorderModifiers setting.")
370-
}
371366
val styleFlags =
372367
if (options.style() == JavaFormatterOptions.Style.AOSP) Seq("--aosp")
373368
else Nil
374369
val javadocFlags =
375370
if (options.formatJavadoc()) Nil
376371
else Seq("--skip-javadoc-formatting")
372+
val reorderModifiersFlags =
373+
if (options.reorderModifiers()) Nil
374+
else Seq("--skip-reordering-modifiers")
377375
val fixImportsOnlyFlags =
378376
if (fixImportsOnly) Seq("--fix-imports-only")
379377
else Nil
@@ -386,7 +384,7 @@ object JavaFormatter {
386384
val reflowLongStringsFlags =
387385
if (reflowLongStrings) Nil
388386
else Seq("--skip-reflowing-long-strings")
389-
styleFlags ++ javadocFlags ++ fixImportsOnlyFlags ++ sortImportsFlags ++ removeUnusedImportsFlags ++ reflowLongStringsFlags
387+
styleFlags ++ javadocFlags ++ reorderModifiersFlags ++ fixImportsOnlyFlags ++ sortImportsFlags ++ removeUnusedImportsFlags ++ reflowLongStringsFlags
390388
}
391389

392390
private case class CliResult(exitCode: Int, stdout: Vector[String], stderr: Vector[String])

plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/build.sbt

Lines changed: 0 additions & 10 deletions
This file was deleted.

plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/test

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ThisBuild / javafmtFormatterCompatibleJavaVersion := 11
2+
ThisBuild / javafmtReorderModifiers := false

plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers-unsupported/project/plugins.sbt renamed to plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/project/plugins.sbt

File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.lightbend;
2+
3+
final public class BadFormatting {
4+
static public void main(String[] args) {}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.lightbend;
2+
3+
final public class BadFormatting{
4+
static public void main(String[] args) {}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-> javafmtCheck
2+
> javafmt
3+
> javafmtCheck
4+
5+
$ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java

0 commit comments

Comments
 (0)