Skip to content
Draft
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
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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).

Expand Down Expand Up @@ -103,6 +104,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`.
Expand All @@ -118,11 +120,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,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._
Expand Down Expand Up @@ -118,7 +120,8 @@ object JavaFormatterPlugin extends AutoPlugin {
javafmtSortImports := true,
javafmtRemoveUnusedImports := true,
javafmtReflowLongStrings := true,
javafmtFormatJavadoc := true)
javafmtFormatJavadoc := true,
javafmtReorderModifiers := true)

def toBeScopedSettings: Seq[Setting[?]] =
List(
Expand All @@ -127,6 +130,7 @@ object JavaFormatterPlugin extends AutoPlugin {
.builder()
.style(javafmtStyle.value)
.formatJavadoc(javafmtFormatJavadoc.value)
.reorderModifiers(javafmtReorderModifiers.value)
.build(),
javafmt := {
val streamz = streams.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,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
Expand All @@ -352,7 +350,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])
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ThisBuild / javafmtFormatterCompatibleJavaVersion := 11
ThisBuild / javafmtReorderModifiers := false
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.lightbend;

final public class BadFormatting {
static public void main(String[] args) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.lightbend;

final public class BadFormatting{
static public void main(String[] args) {}
}
5 changes: 5 additions & 0 deletions plugin/src/sbt-test/sbt-java-formatter/reorder-modifiers/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-> javafmtCheck
> javafmt
> javafmtCheck

$ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java
Loading