Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more options from google formatter #196

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -126,6 +130,8 @@ example:
<skipSourceDirectory>false</skipSourceDirectory>
<skipTestSourceDirectory>false</skipTestSourceDirectory>
<skipSortingImports>false</skipSortingImports>
<skipRemovingUnusedImports>true</skipRemovingUnusedImports>
<skipReflowingLongStrings>false</skipReflowingLongStrings>
<style>google</style>
</configuration>
<executions>
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/spotify/fmt/AbstractFMT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<File> directoriesToFormat = new ArrayList<>();
if (sourceDirectory.exists() && !skipSourceDirectory) {
directoriesToFormat.add(sourceDirectory);
Expand Down Expand Up @@ -150,6 +162,8 @@ public void execute() throws MojoFailureException {
.filesPathPattern(filesPathPattern)
.verbose(verbose)
.skipSortingImports(skipSortingImports)
.skipRemovingUnusedImports(skipRemovingUnusedImports)
.skipReflowingLongStrings(skipReflowingLongStrings)
.writeReformattedFiles(shouldWriteReformattedFiles())
.processingLabel(getProcessingLabel())
.build();
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/spotify/fmt/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/spotify/fmt/FormattingConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ interface FormattingConfiguration extends Serializable {
String filesPathPattern();

boolean skipSortingImports();

boolean skipRemovingUnusedImports();

boolean skipReflowingLongStrings();

boolean writeReformattedFiles();

Expand Down