From c61edfa39f962ab0f13d0c710cf499b105ac7517 Mon Sep 17 00:00:00 2001 From: Michael Pfeiffer Date: Wed, 11 Dec 2024 09:07:29 +0100 Subject: [PATCH 1/2] Support more options from google formatter - skipRemovingUnusedImports defaults to false - skipReflowingLongStrings defaults to true for backwards compatibility --- src/main/java/com/spotify/fmt/AbstractFMT.java | 14 ++++++++++++++ src/main/java/com/spotify/fmt/Formatter.java | 9 ++++++++- .../com/spotify/fmt/FormattingConfiguration.java | 4 ++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/spotify/fmt/AbstractFMT.java b/src/main/java/com/spotify/fmt/AbstractFMT.java index e8730ee..1a05735 100644 --- a/src/main/java/com/spotify/fmt/AbstractFMT.java +++ b/src/main/java/com/spotify/fmt/AbstractFMT.java @@ -82,6 +82,12 @@ public abstract class AbstractFMT extends AbstractMojo { @Parameter(defaultValue = "false", property = "skipSortingImports") private boolean skipSortingImports = false; + @Parameter(defaultValue = "false", property = "skipRemovingUnusedImports") + private boolean skipRemovingUnusedImports = false; + + @Parameter(defaultValue = "true", property = "skipReflowingLongStrings") + private boolean skipReflowingLongStrings = true; + @Parameter(defaultValue = "google", property = "style") private String style; @@ -121,6 +127,12 @@ public void execute() throws MojoFailureException { if (skipSortingImports) { getLog().info("Skipping sorting imports"); } + if (skipRemovingUnusedImports) { + getLog().info("Skipping removing unused imports"); + } + if (skipReflowingLongStrings) { + getLog().info("Skipping reflowing long strings"); + } List directoriesToFormat = new ArrayList<>(); if (sourceDirectory.exists() && !skipSourceDirectory) { directoriesToFormat.add(sourceDirectory); @@ -150,6 +162,8 @@ public void execute() throws MojoFailureException { .filesPathPattern(filesPathPattern) .verbose(verbose) .skipSortingImports(skipSortingImports) + .skipRemovingUnusedImports(skipRemovingUnusedImports) + .skipReflowingLongStrings(skipReflowingLongStrings) .writeReformattedFiles(shouldWriteReformattedFiles()) .processingLabel(getProcessingLabel()) .build(); diff --git a/src/main/java/com/spotify/fmt/Formatter.java b/src/main/java/com/spotify/fmt/Formatter.java index 2626a39..a4b9b93 100644 --- a/src/main/java/com/spotify/fmt/Formatter.java +++ b/src/main/java/com/spotify/fmt/Formatter.java @@ -33,6 +33,8 @@ import com.google.googlejavaformat.java.JavaFormatterOptions; import com.google.googlejavaformat.java.JavaFormatterOptions.Style; import com.google.googlejavaformat.java.RemoveUnusedImports; +import com.google.googlejavaformat.java.StringWrapper; + import java.io.File; import java.io.FileFilter; import java.io.IOException; @@ -154,10 +156,15 @@ private boolean formatSourceFile( try { String input = source.read(); String formatted = formatter.formatSource(input); - formatted = RemoveUnusedImports.removeUnusedImports(formatted); + if (!cfg.skipRemovingUnusedImports()) { + formatted = RemoveUnusedImports.removeUnusedImports(formatted); + } if (!cfg.skipSortingImports()) { formatted = ImportOrderer.reorderImports(formatted, style); } + if (!cfg.skipReflowingLongStrings()) { + formatted = StringWrapper.wrap(formatted, formatter); + } if (!input.equals(formatted)) { if (cfg.writeReformattedFiles()) { CharSink sink = com.google.common.io.Files.asCharSink(file, Charsets.UTF_8); diff --git a/src/main/java/com/spotify/fmt/FormattingConfiguration.java b/src/main/java/com/spotify/fmt/FormattingConfiguration.java index b81a937..4ab9c68 100644 --- a/src/main/java/com/spotify/fmt/FormattingConfiguration.java +++ b/src/main/java/com/spotify/fmt/FormattingConfiguration.java @@ -47,6 +47,10 @@ interface FormattingConfiguration extends Serializable { String filesPathPattern(); boolean skipSortingImports(); + + boolean skipRemovingUnusedImports(); + + boolean skipReflowingLongStrings(); boolean writeReformattedFiles(); From 6b367e716b6a71aabe1ac32266c49d2dfc7b61ac Mon Sep 17 00:00:00 2001 From: Michael Pfeiffer Date: Wed, 11 Dec 2024 09:21:21 +0100 Subject: [PATCH 2/2] Updated documentation --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 4374ec4..85a2cb1 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,10 @@ For example, you may prefer that the `check` goal is performed in an earlier pha `skip` is whether the plugin should skip the operation. +`skipReflowingLongStrings` is whether the plugin should skip reflowing long strings. It defaults to `false`. + +`skipRemovingUnusedImports` is whether the plugin should skip removing unused imports. It defaults to `true`. + `skipSortingImports` is whether the plugin should skip sorting imports. `skipSourceDirectory` is whether the plugin should skip formatting/checking the `sourceDirectory`. It defaults to `false`. @@ -126,6 +130,8 @@ example: false false false + true + false