Skip to content

Commit d624e72

Browse files
committed
Support reorderModifiers = false via google-java-format CLI
1 parent 1fb4d2f commit d624e72

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
@@ -37,6 +37,7 @@ For available versions see [releases](https://github.com/sbt/sbt-java-formatter/
3737
* The `javafmtRemoveUnusedImports` setting controls whether unused imports are removed (`true` by default).
3838
* The `javafmtReflowLongStrings` setting controls whether long string literals are reflowed (`true` by default).
3939
* The `javafmtFormatJavadoc` setting controls whether Javadoc comments are reformatted (`true` by default).
40+
* The `javafmtReorderModifiers` setting controls whether modifiers are reordered into JLS order (`true` by default).
4041
* The `javafmtFormatterCompatibleJavaVersion` setting selects which `google-java-format` runtime line to use (`21` by default).
4142
* The `javafmtJavaMaxHeap` setting controls the maximum heap passed to the forked `google-java-format` JVM (`Some("256m")` by default).
4243

@@ -103,6 +104,7 @@ ThisBuild / javafmtSortImports := true
103104
ThisBuild / javafmtRemoveUnusedImports := true
104105
ThisBuild / javafmtReflowLongStrings := true
105106
ThisBuild / javafmtFormatJavadoc := true
107+
ThisBuild / javafmtReorderModifiers := true
106108
```
107109

108110
Set any of them to `false` to pass the corresponding `--skip-...` flag to `google-java-format`.
@@ -118,11 +120,7 @@ If the selected formatter runtime is newer than the Java used to launch the form
118120
- lower `ThisBuild / javafmtFormatterCompatibleJavaVersion`
119121
- or point the formatter to a newer JDK via `SBT_JAVAFMT_JAVA_HOME` or `-Dsbt-javafmt.java.home=...`
120122

121-
`javafmtOptions` is still available for compatibility, but the preferred sbt-facing configuration is through the dedicated `javafmt...` settings above.
122-
123-
`JavaFormatterOptions.reorderModifiers()` currently has no effect in this plugin.
124-
125-
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).
123+
`javafmtOptions` is still available for compatibility with upstream `JavaFormatterOptions`, but the preferred sbt-facing configuration is through the dedicated `javafmt...` settings above.
126124

127125
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).
128126
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
@@ -79,8 +79,10 @@ object JavaFormatterPlugin extends AutoPlugin {
7979
settingKey[Boolean]("Whether google-java-format should reflow long string literals. Enabled by default.")
8080
val javafmtFormatJavadoc =
8181
settingKey[Boolean]("Whether google-java-format should format Javadoc comments. Enabled by default.")
82+
val javafmtReorderModifiers =
83+
settingKey[Boolean]("Whether google-java-format should reorder modifiers into JLS order. Enabled by default.")
8284
val javafmtOptions = settingKey[JavaFormatterOptions](
83-
"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.")
85+
"Compatibility setting for upstream JavaFormatterOptions. Prefer the dedicated javafmt... settings where available.")
8486
}
8587

8688
import autoImport._
@@ -118,7 +120,8 @@ object JavaFormatterPlugin extends AutoPlugin {
118120
javafmtSortImports := true,
119121
javafmtRemoveUnusedImports := true,
120122
javafmtReflowLongStrings := true,
121-
javafmtFormatJavadoc := true)
123+
javafmtFormatJavadoc := true,
124+
javafmtReorderModifiers := true)
122125

123126
def toBeScopedSettings: Seq[Setting[?]] =
124127
List(
@@ -127,6 +130,7 @@ object JavaFormatterPlugin extends AutoPlugin {
127130
.builder()
128131
.style(javafmtStyle.value)
129132
.formatJavadoc(javafmtFormatJavadoc.value)
133+
.reorderModifiers(javafmtReorderModifiers.value)
130134
.build(),
131135
javafmt := {
132136
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
@@ -329,17 +329,15 @@ object JavaFormatter {
329329
sortImports: Boolean,
330330
removeUnusedImports: Boolean,
331331
reflowLongStrings: Boolean): Seq[String] = {
332-
if (!options.reorderModifiers()) {
333-
throw new MessageOnlyException(
334-
"The forked google-java-format CLI does not support reorderModifiers = false. " +
335-
"Please use the default reorderModifiers setting.")
336-
}
337332
val styleFlags =
338333
if (options.style() == JavaFormatterOptions.Style.AOSP) Seq("--aosp")
339334
else Nil
340335
val javadocFlags =
341336
if (options.formatJavadoc()) Nil
342337
else Seq("--skip-javadoc-formatting")
338+
val reorderModifiersFlags =
339+
if (options.reorderModifiers()) Nil
340+
else Seq("--skip-reordering-modifiers")
343341
val fixImportsOnlyFlags =
344342
if (fixImportsOnly) Seq("--fix-imports-only")
345343
else Nil
@@ -352,7 +350,7 @@ object JavaFormatter {
352350
val reflowLongStringsFlags =
353351
if (reflowLongStrings) Nil
354352
else Seq("--skip-reflowing-long-strings")
355-
styleFlags ++ javadocFlags ++ fixImportsOnlyFlags ++ sortImportsFlags ++ removeUnusedImportsFlags ++ reflowLongStringsFlags
353+
styleFlags ++ javadocFlags ++ reorderModifiersFlags ++ fixImportsOnlyFlags ++ sortImportsFlags ++ removeUnusedImportsFlags ++ reflowLongStringsFlags
356354
}
357355

358356
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)