Skip to content
Merged
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: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ jobs:
- name: Build and test (sbt 1.x)
if: ${{ matrix.jobtype == 1 }}
shell: bash
run: sbt -v '++ 2.12.x' clean scalafmtSbtCheck scalafmtCheckAll test 'scripted sbt-java-formatter/*' 'scripted java-runtime/compatible-11'
run: sbt -v '++ 2.12.x' clean scalafmtSbtCheck scalafmtCheckAll test 'scripted sbt-java-formatter/*' 'scripted java-runtime/compatible-11' 'scripted java-runtime/cache-runtime-on-java-11'
- name: Build and test (sbt 2.x)
if: ${{ matrix.jobtype == 2 }}
shell: bash
run: sbt -v '++ 3.x' clean scalafmtSbtCheck scalafmtCheckAll test 'scripted sbt-java-formatter/*' 'scripted java-runtime/compatible-17' 'scripted java-runtime/incompatible-21-on-java-17'
- name: Build and test (sbt 2.x, Java 21)
if: ${{ matrix.jobtype == 3 }}
shell: bash
run: sbt -v '++ 3.x' clean scalafmtSbtCheck scalafmtCheckAll test 'scripted sbt-java-formatter/*' 'scripted java-runtime/compatible-21' 'scripted java-runtime/reorder-modifiers'
run: sbt -v '++ 3.x' clean scalafmtSbtCheck scalafmtCheckAll test 'scripted sbt-java-formatter/*' 'scripted java-runtime/compatible-21' 'scripted java-runtime/reorder-modifiers' 'scripted java-runtime/max-line-length'
- name: Build and test (sbt 2.x, Java 25)
if: ${{ matrix.jobtype == 4 }}
shell: bash
run: sbt -v '++ 3.x' clean scalafmtSbtCheck scalafmtCheckAll test 'scripted java-runtime/compatible-21' 'scripted java-runtime/reorder-modifiers'
run: sbt -v '++ 3.x' clean scalafmtSbtCheck scalafmtCheckAll test 'scripted java-runtime/compatible-21' 'scripted java-runtime/reorder-modifiers' 'scripted java-runtime/max-line-length'
- name: Build and test (sbt 1.x, Java 11 with formatter on Java 25)
if: ${{ matrix.jobtype == 5 }}
shell: bash
Expand All @@ -78,4 +78,4 @@ jobs:
SBT_JAVAFMT_JAVA_HOME: ${{ env.JAVA_HOME_25_X64 }}
run: |
export PATH="${JAVA_HOME}/bin:$PATH"
sbt -v '++ 2.12.x' clean scalafmtSbtCheck scalafmtCheckAll test 'scripted java-runtime/compatible-21' 'scripted java-runtime/reorder-modifiers'
sbt -v '++ 2.12.x' clean scalafmtSbtCheck scalafmtCheckAll test 'scripted java-runtime/compatible-21' 'scripted java-runtime/reorder-modifiers' 'scripted java-runtime/max-line-length'
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ For available versions see [releases](https://github.com/sbt/sbt-java-formatter/

* 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.
* The `javafmtMaxLineLength` setting controls the maximum line length (`100` by default). Custom values require `javafmtFormatterCompatibleJavaVersion := 21`.
* The `javafmtSortImports` setting controls whether imports are sorted (`true` by default).
* 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).
Expand Down Expand Up @@ -86,7 +87,7 @@ SBT_JAVAFMT_JAVA_HOME=/path/to/jdk-17 sbt javafmt
> [!NOTE]
> Many projects use Java 17 as their baseline today. If that is true for your build, `ThisBuild / javafmtFormatterCompatibleJavaVersion := 17` is often the simplest setup.
>
> The main Java language support changes in `google-java-format` [after `v1.28.0`](https://github.com/google/google-java-format/compare/v1.28.0...v1.36.1) are:
> The main Java language support changes in `google-java-format` [after `v1.28.0`](https://github.com/google/google-java-format/compare/v1.28.0...v1.37.0) are:
>
> - [Initial support for import module in google-java-format](https://github.com/google/google-java-format/commit/6afe380707ec16884ec2761763ccec998de403d1)
> - [Support Instance Main Methods in google-java-format](https://github.com/google/google-java-format/commit/737b0032b3a18eb6e458271ea440098c166f6c2d)
Expand Down Expand Up @@ -119,13 +120,35 @@ ThisBuild / javafmtFormatJavadoc := true
ThisBuild / javafmtReorderModifiers := true
```

Set any of them to `false` to pass the corresponding `--skip-...` flag to `google-java-format`.
Set the Boolean formatter options to `false` to pass the corresponding `--skip-...` flag to `google-java-format`.

Use `javafmtStyle` to select Google or AOSP style, which is equivalent to the CLI's `--style=google` / `--style=aosp` options (`--google-style` / `--aosp`):

```scala
import com.google.googlejavaformat.java.JavaFormatterOptions

ThisBuild / javafmtStyle := JavaFormatterOptions.Style.GOOGLE
// Or: ThisBuild / javafmtStyle := JavaFormatterOptions.Style.AOSP
```

Use `javafmtMaxLineLength` to customize the line width used for code, comments, and long string reflow:

```scala
ThisBuild / javafmtFormatterCompatibleJavaVersion := 21
ThisBuild / javafmtMaxLineLength := 120
```

The value must be positive. The default of `100` works with all formatter runtime lines; custom values require the Java 21 line because the Java 11 and Java 17 lines do not support `--max-line-length`.

Only `javafmtStyle`, `javafmtSortImports`, and `javafmtRemoveUnusedImports` affect the output of the `javafmtFixImports...` tasks. Full-formatting options (line length, Javadoc formatting, modifier reordering, and long string reflow) are ignored and not validated for import-only tasks, even on older formatter runtime lines.

Formatting and check caches are invalidated when effective formatter options or the formatter runtime change, even if source files have not changed. Import-only tasks use a separate cache from full-formatting tasks; changes to full-formatting options do not invalidate their cache.

`javafmtFormatterCompatibleJavaVersion` maps to these formatter versions:

- `11` -> `google-java-format 1.24.0`
- `17` -> `google-java-format 1.28.0`
- `21` -> `google-java-format 1.36.1` (default)
- `21` -> `google-java-format 1.37.0` (default)

If the selected formatter runtime is newer than the Java used to launch the formatter JVM, either:

Expand Down
51 changes: 33 additions & 18 deletions plugin/src/main/scala/com/github/sbt/JavaFormatterPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ object JavaFormatterPlugin extends AutoPlugin {
"Selects the google-java-format runtime line by compatible Java version. Supported values: 11, 17, 21.")
val javafmtStyle =
settingKey[JavaFormatterOptions.Style]("Define formatting style, Google Java Style (default) or AOSP")
val javafmtMaxLineLength =
settingKey[Int](
s"Maximum line length used by google-java-format. Defaults to ${JavaFormatter.DefaultMaxLineLength}; custom values require the Java 21 formatter runtime line.")
val javafmtJavaMaxHeap =
settingKey[Option[String]]("Maximum heap size passed to the forked google-java-format JVM, e.g. Some(\"256m\").")
val javafmtSortImports =
Expand Down Expand Up @@ -119,6 +122,7 @@ object JavaFormatterPlugin extends AutoPlugin {
javafmtOnCompile := false,
javafmtFormatterCompatibleJavaVersion := 21,
javafmtStyle := JavaFormatterOptions.Style.GOOGLE,
javafmtMaxLineLength := JavaFormatter.DefaultMaxLineLength,
javafmtJavaMaxHeap := Some("256m"),
javafmtSortImports := true,
javafmtRemoveUnusedImports := true,
Expand All @@ -143,8 +147,9 @@ object JavaFormatterPlugin extends AutoPlugin {
val iF = (javafmt / includeFilter).value
val eF = (javafmt / excludeFilter).value
val cache = streamz.cacheStoreFactory
val maxLineLength = javafmtMaxLineLength.value
val options =
validateOptions(javafmtOptions.value, javafmtFormatterCompatibleJavaVersion.value)
validateOptions(javafmtOptions.value, javafmtFormatterCompatibleJavaVersion.value, maxLineLength)
val formatterClasspath = javafmtFormatterClasspath.value.toVector
val javaMaxHeap = javafmtJavaMaxHeap.value
val sortImports = javafmtSortImports.value
Expand All @@ -163,7 +168,8 @@ object JavaFormatterPlugin extends AutoPlugin {
javaMaxHeap,
sortImports,
removeUnusedImports,
reflowLongStrings)
reflowLongStrings,
maxLineLength)
},
javafmtCheck := {
val streamz = streams.value
Expand All @@ -174,8 +180,9 @@ object JavaFormatterPlugin extends AutoPlugin {
val iF = (javafmt / includeFilter).value
val eF = (javafmt / excludeFilter).value
val cache = (javafmt / streams).value.cacheStoreFactory
val maxLineLength = javafmtMaxLineLength.value
val options =
validateOptions(javafmtOptions.value, javafmtFormatterCompatibleJavaVersion.value)
validateOptions(javafmtOptions.value, javafmtFormatterCompatibleJavaVersion.value, maxLineLength)
val formatterClasspath = javafmtFormatterClasspath.value.toVector
val javaMaxHeap = javafmtJavaMaxHeap.value
val sortImports = javafmtSortImports.value
Expand All @@ -195,7 +202,8 @@ object JavaFormatterPlugin extends AutoPlugin {
javaMaxHeap,
sortImports,
removeUnusedImports,
reflowLongStrings)
reflowLongStrings,
maxLineLength)
},
javafmtFixImports := {
val streamz = streams.value
Expand All @@ -204,14 +212,12 @@ object JavaFormatterPlugin extends AutoPlugin {
val sD = (javafmt / sourceDirectories).value.toList
val iF = (javafmt / includeFilter).value
val eF = (javafmt / excludeFilter).value
val cache = streamz.cacheStoreFactory
val options =
validateOptions(javafmtOptions.value, javafmtFormatterCompatibleJavaVersion.value)
val cache = (javafmt / streams).value.cacheStoreFactory
val options = javafmtOptions.value
val formatterClasspath = javafmtFormatterClasspath.value.toVector
val javaMaxHeap = javafmtJavaMaxHeap.value
val sortImports = javafmtSortImports.value
val removeUnusedImports = javafmtRemoveUnusedImports.value
val reflowLongStrings = javafmtReflowLongStrings.value
JavaFormatter.fixImports(
projectId,
invocationId,
Expand All @@ -224,8 +230,7 @@ object JavaFormatterPlugin extends AutoPlugin {
formatterClasspath,
javaMaxHeap,
sortImports,
removeUnusedImports,
reflowLongStrings)
removeUnusedImports)
},
javafmtFixImportsCheck := {
val streamz = streams.value
Expand All @@ -236,13 +241,11 @@ object JavaFormatterPlugin extends AutoPlugin {
val iF = (javafmt / includeFilter).value
val eF = (javafmt / excludeFilter).value
val cache = (javafmt / streams).value.cacheStoreFactory
val options =
validateOptions(javafmtOptions.value, javafmtFormatterCompatibleJavaVersion.value)
val options = javafmtOptions.value
val formatterClasspath = javafmtFormatterClasspath.value.toVector
val javaMaxHeap = javafmtJavaMaxHeap.value
val sortImports = javafmtSortImports.value
val removeUnusedImports = javafmtRemoveUnusedImports.value
val reflowLongStrings = javafmtReflowLongStrings.value
JavaFormatter.fixImportsCheck(
projectId,
invocationId,
Expand All @@ -256,8 +259,7 @@ object JavaFormatterPlugin extends AutoPlugin {
formatterClasspath,
javaMaxHeap,
sortImports,
removeUnusedImports,
reflowLongStrings)
removeUnusedImports)
},
javafmtDoFormatOnCompile := Def.settingDyn {
if (javafmtOnCompile.value) {
Expand Down Expand Up @@ -287,14 +289,27 @@ object JavaFormatterPlugin extends AutoPlugin {
compatibleJavaVersion match {
case 11 => "1.24.0"
case 17 => "1.28.0"
case 21 => "1.36.1"
case 21 => "1.37.0"
case other =>
throw new MessageOnlyException(
s"Unsupported javafmtFormatterCompatibleJavaVersion: $other. Expected one of: 11, 17, 21.")
}

private def validateOptions(options: JavaFormatterOptions, compatibleJavaVersion: Int): JavaFormatterOptions = {
if (!options.reorderModifiers() && compatibleJavaVersion != 21) {
private def validateOptions(
options: JavaFormatterOptions,
compatibleJavaVersion: Int,
maxLineLength: Int): JavaFormatterOptions = {
if (maxLineLength <= 0) {
throw new MessageOnlyException(s"javafmtMaxLineLength must be positive, but was $maxLineLength.")
}
if (maxLineLength != JavaFormatter.DefaultMaxLineLength && compatibleJavaVersion < 21) {
throw new MessageOnlyException(
"A custom javafmtMaxLineLength requires " +
"ThisBuild / javafmtFormatterCompatibleJavaVersion := 21 " +
"because the Java 11 and Java 17 formatter runtime lines do not support " +
"--max-line-length.")
}
if (!options.reorderModifiers() && compatibleJavaVersion < 21) {
throw new MessageOnlyException(
"Disabling modifier reordering requires " +
"ThisBuild / javafmtFormatterCompatibleJavaVersion := 21 " +
Expand Down
Loading
Loading