Skip to content

Commit

Permalink
Support google-java-format's skip-javadoc-formatting option (#1793)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Sep 28, 2023
2 parents 26d0542 + e0b0229 commit 2d23db2
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 20 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Add support for biome. The Rome project [was renamed to Biome](https://biomejs.dev/blog/annoucing-biome/).
The configuration is still the same, but you should switch to the new `biome` tag / function and adjust
the version accordingly. ([#1804](https://github.com/diffplug/spotless/issues/1804)).
* Add support for `google-java-format`'s `skip-javadoc-formatting` option. ([#1793](https://github.com/diffplug/spotless/pull/1793))
### Fixed
* Added support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
* Fix support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
### Changes
* Bump default `flexmark` version to latest `0.64.0` -> `0.64.8`. ([#1801](https://github.com/diffplug/spotless/pull/1801))
* Bump default `ktlint` version to latest `0.50.0` -> `1.0.0`. ([#1808](https://github.com/diffplug/spotless/pull/1808))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ public class GoogleJavaFormatFormatterFunc implements FormatterFunc {

private final boolean reorderImports;

public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings, boolean reorderImports) {
public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings, boolean reorderImports, boolean formatJavadoc) {
this.version = Objects.requireNonNull(version);
this.formatterStyle = Style.valueOf(Objects.requireNonNull(style));
this.reflowStrings = reflowStrings;
this.reorderImports = reorderImports;

this.formatter = new Formatter(JavaFormatterOptions.builder()
.style(formatterStyle)
.build());
JavaFormatterOptions.Builder builder = JavaFormatterOptions.builder().style(formatterStyle);
if (!formatJavadoc) {
builder = builder.formatJavadoc(false);
}
this.formatter = new Formatter(builder.build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private GoogleJavaFormatStep() {}
private static final String DEFAULT_STYLE = "GOOGLE";
private static final boolean DEFAULT_REFLOW_LONG_STRINGS = false;
private static final boolean DEFAULT_REORDER_IMPORTS = false;
private static final boolean DEFAULT_FORMAT_JAVADOC = true;
static final String NAME = "google-java-format";
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";

Expand All @@ -57,11 +58,11 @@ public static FormatterStep create(String version, String style, Provisioner pro
}

public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
return create(groupArtifact, version, style, provisioner, reflowLongStrings, false);
return create(groupArtifact, version, style, provisioner, reflowLongStrings, false, DEFAULT_FORMAT_JAVADOC);
}

/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) {
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports, boolean formatJavadoc) {
Objects.requireNonNull(groupArtifact, "groupArtifact");
if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) {
throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'");
Expand All @@ -70,7 +71,7 @@ public static FormatterStep create(String groupArtifact, String version, String
Objects.requireNonNull(style, "style");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(NAME,
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings, reorderImports),
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings, reorderImports, formatJavadoc),
State::createFormat);
}

Expand Down Expand Up @@ -101,6 +102,10 @@ public static boolean defaultReorderImports() {
return DEFAULT_REORDER_IMPORTS;
}

public static boolean defaultFormatJavadoc() {
return DEFAULT_FORMAT_JAVADOC;
}

static final class State implements Serializable {
private static final long serialVersionUID = 1L;

Expand All @@ -111,6 +116,7 @@ static final class State implements Serializable {
final String style;
final boolean reflowLongStrings;
final boolean reorderImports;
final boolean formatJavadoc;

State(String stepName, String version, Provisioner provisioner) throws Exception {
this(stepName, version, DEFAULT_STYLE, provisioner);
Expand All @@ -121,10 +127,10 @@ static final class State implements Serializable {
}

State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS);
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS, DEFAULT_FORMAT_JAVADOC);
}

State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) throws Exception {
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports, boolean formatJavadoc) throws Exception {
JVM_SUPPORT.assertFormatterSupported(version);
ModuleHelper.doOpenInternalPackagesIfRequired();
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner);
Expand All @@ -133,13 +139,14 @@ static final class State implements Serializable {
this.style = style;
this.reflowLongStrings = reflowLongStrings;
this.reorderImports = reorderImports;
this.formatJavadoc = formatJavadoc;
}

FormatterFunc createFormat() throws Exception {
final ClassLoader classLoader = jarState.getClassLoader();
Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.java.GoogleJavaFormatFormatterFunc");
Constructor<?> constructor = formatterFunc.getConstructor(String.class, String.class, boolean.class, boolean.class);
FormatterFunc googleJavaFormatFormatterFunc = (FormatterFunc) constructor.newInstance(version, style, reflowLongStrings, reorderImports);
Constructor<?> constructor = formatterFunc.getConstructor(String.class, String.class, boolean.class, boolean.class, boolean.class);
FormatterFunc googleJavaFormatFormatterFunc = (FormatterFunc) constructor.newInstance(version, style, reflowLongStrings, reorderImports, formatJavadoc);

return JVM_SUPPORT.suggestLaterVersionOnError(version, googleJavaFormatFormatterFunc);
}
Expand Down
5 changes: 3 additions & 2 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
### Added
* Support `flexmark` in gradle. Previously only Maven was supported. ([#1801](https://github.com/diffplug/spotless/pull/1801))
* Added support for `google-java-format`'s `skip-javadoc-formatting` option ([#1793](https://github.com/diffplug/spotless/pull/1793))
* Add support for `flexmark` in gradle. Previously only Maven was supported. ([#1801](https://github.com/diffplug/spotless/pull/1801))
* Add support for biome. The Rome project [was renamed to Biome](https://biomejs.dev/blog/annoucing-biome/).
The configuration is still the same, but you should switch to the new `biome(...)` function and adjust
the version accordingly. ([#1804](https://github.com/diffplug/spotless/issues/1804)).
### Fixed
* Added support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
* Fixed support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
### Changes
* Bump default `ktlint` version to latest `0.50.0` -> `1.0.0`. ([#1808](https://github.com/diffplug/spotless/pull/1808))

Expand Down
4 changes: 2 additions & 2 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ spotless {
// don't need to set target, it is inferred from java
// apply a specific flavor of google-java-format
googleJavaFormat('1.8').aosp().reflowLongStrings()
googleJavaFormat('1.8').aosp().reflowLongStrings().skipJavadocFormatting()
// fix formatting of type annotations
formatAnnotations()
// make sure every file has the following copyright header.
Expand Down Expand Up @@ -207,7 +207,7 @@ spotless {
// optional: you can specify a specific version (>= 1.8) and/or switch to AOSP style
// and/or reflow long strings
// and/or use custom group artifact (you probably don't need this)
googleJavaFormat('1.8').aosp().reflowLongStrings().reorderImports(false).groupArtifact('com.google.googlejavaformat:google-java-format')
googleJavaFormat('1.8').aosp().reflowLongStrings().formatJavadoc(false).reorderImports(false).groupArtifact('com.google.googlejavaformat:google-java-format')
```

### palantir-java-format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public class GoogleJavaFormatConfig {
String style;
boolean reflowLongStrings;
boolean reorderImports;
boolean formatJavadoc = true;

GoogleJavaFormatConfig(String version) {
this.version = Objects.requireNonNull(version);
Expand Down Expand Up @@ -213,14 +214,25 @@ public GoogleJavaFormatConfig reorderImports(boolean reorderImports) {
return this;
}

public GoogleJavaFormatConfig skipJavadocFormatting() {
return formatJavadoc(false);
}

public GoogleJavaFormatConfig formatJavadoc(boolean formatJavadoc) {
this.formatJavadoc = formatJavadoc;
replaceStep(createStep());
return this;
}

private FormatterStep createStep() {
return GoogleJavaFormatStep.create(
groupArtifact,
version,
style,
provisioner(),
reflowLongStrings,
reorderImports);
reorderImports,
formatJavadoc);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,30 @@ void integration() throws IOException {
"googleJavaFormat()");
checkRunsThenUpToDate();
}

@Test
void integrationWithSkipJavadocFormatting() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"",
"spotless {",
" java {",
" target file('test.java')",
" googleJavaFormat('1.12.0').skipJavadocFormatting()",
" }",
"}");

setFile("test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test");
gradleRunner().withArguments("spotlessApply").build();
assertFile("test.java").sameAsResource("java/googlejavaformat/JavaCodeFormattedSkipJavadocFormatting.test");

checkRunsThenUpToDate();
replace("build.gradle",
"googleJavaFormat('1.12.0')",
"googleJavaFormat()");
checkRunsThenUpToDate();
}
}
5 changes: 3 additions & 2 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
### Added
* Add support for biome. The Rome project [was renamed to Biome](https://biomejs.dev/blog/annoucing-biome/).
* Added support for `google-java-format`'s `skip-javadoc-formatting` option ([#1793](https://github.com/diffplug/spotless/pull/1793))
* Added support for biome. The Rome project [was renamed to Biome](https://biomejs.dev/blog/annoucing-biome/).
The configuration is still the same, but you should switch to the new `<biome>` tag and adjust
the version accordingly. ([#1804](https://github.com/diffplug/spotless/issues/1804)).
### Fixed
* Added support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
* Fixed support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
### Changes
* Bump default `flexmark` version to latest `0.64.0` -> `0.64.8`. ([#1801](https://github.com/diffplug/spotless/pull/1801))
* Bump default `ktlint` version to latest `0.50.0` -> `1.0.0`. ([#1808](https://github.com/diffplug/spotless/pull/1808))
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ To use it in your pom, just [add the Spotless dependency](https://search.maven.o
<version>1.8</version>
<style>AOSP</style>
<reflowLongStrings>true</reflowLongStrings>
<formatJavadoc>false</formatJavadoc>
</googleJavaFormat>

<!-- make sure every file has the following copyright header.
Expand Down Expand Up @@ -233,6 +234,7 @@ any other maven phase (i.e. compile) then it can be configured as below;
<version>1.8</version> <!-- optional, 1.8 is minimum supported version -->
<style>GOOGLE</style> <!-- or AOSP (optional) -->
<reflowLongStrings>true</reflowLongStrings> <!-- optional -->
<formatJavadoc>false</formatJavadoc> <!-- optional -->
<!-- optional: custom group artifact (you probably don't need this) -->
<groupArtifact>com.google.googlejavaformat:google-java-format</groupArtifact>
</googleJavaFormat>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ public class GoogleJavaFormat implements FormatterStepFactory {
@Parameter
private Boolean reorderImports;

@Parameter
private Boolean formatJavadoc;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
String groupArtifact = this.groupArtifact != null ? this.groupArtifact : GoogleJavaFormatStep.defaultGroupArtifact();
String version = this.version != null ? this.version : GoogleJavaFormatStep.defaultVersion();
String style = this.style != null ? this.style : GoogleJavaFormatStep.defaultStyle();
boolean reflowLongStrings = this.reflowLongStrings != null ? this.reflowLongStrings : GoogleJavaFormatStep.defaultReflowLongStrings();
boolean reorderImports = this.reorderImports != null ? this.reorderImports : GoogleJavaFormatStep.defaultReorderImports();
return GoogleJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings, reorderImports);
boolean formatJavadoc = this.formatJavadoc != null ? this.formatJavadoc : GoogleJavaFormatStep.defaultFormatJavadoc();
return GoogleJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings, reorderImports, formatJavadoc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ void specificVersionReflowLongStrings() throws Exception {
runTest("java/googlejavaformat/JavaCodeFormattedReflowLongStrings.test");
}

@Test
void specificVersionSkipJavadocFormatting() throws Exception {
writePomWithJavaSteps(
"<googleJavaFormat>",
" <version>1.12.0</version>",
" <formatJavadoc>false</formatJavadoc>",
"</googleJavaFormat>");

runTest("java/googlejavaformat/JavaCodeFormattedSkipJavadocFormatting.test");
}

private void runTest(String targetResource) throws Exception {
String path = "src/main/java/test.java";
setFile(path).toResource("java/googlejavaformat/JavaCodeUnformatted.test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mylib.UsedA;
import mylib.UsedB;

public class Java {
/** Some javadoc. */
public static void main(String[] args) {
System.out.println(
"A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mylib.UsedA;
import mylib.UsedB;

public class Java {
/** Some javadoc. */
public static void main(String[] args) {
System.out.println(
"A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mylib.UsedA;
import mylib.UsedB;

public class Java {
/** Some javadoc. */
public static void main(String[] args) {
System.out.println(
"A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mylib.UsedA;
import mylib.UsedB;

public class Java {
/** Some javadoc. */
public static void main(String[] args) {
System.out.println(
"A very very very very very very very very very very very very very very very very very"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import mylib.UsedA;
import mylib.UsedB;

public class Java {
/**
* Some javadoc.
*/
public static void main(String[] args) {
System.out.println(
"A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length.");
UsedB.someMethod();
UsedA.someMethod();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import mylib.UsedB;
import mylib.UsedA;

public class Java {
/**
* Some javadoc.
*/
public static void main(String[] args) {
System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length.");
UsedB.someMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ void behaviorWithReflowLongStrings() throws Exception {
}
}

@Test
void behaviorWithSkipFormatJavadoc() throws Exception {
try (StepHarness step = StepHarness.forStep(GoogleJavaFormatStep.create(GoogleJavaFormatStep.defaultGroupArtifact(), GoogleJavaFormatStep.defaultVersion(), GoogleJavaFormatStep.defaultStyle(), TestProvisioner.mavenCentral(), GoogleJavaFormatStep.defaultReflowLongStrings(), GoogleJavaFormatStep.defaultReorderImports(), false))) {
step.testResource("java/googlejavaformat/JavaCodeUnformatted.test", "java/googlejavaformat/JavaCodeFormattedSkipJavadocFormatting.test")
.testResource("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test", "java/googlejavaformat/JavaCodeWithLicenseFormatted.test")
.testResource("java/googlejavaformat/JavaCodeWithLicensePackageUnformatted.test", "java/googlejavaformat/JavaCodeWithLicensePackageFormatted.test")
.testResource("java/googlejavaformat/JavaCodeWithPackageUnformatted.test", "java/googlejavaformat/JavaCodeWithPackageFormatted.test");
}
}

@Test
void behaviorWithCustomGroupArtifact() throws Exception {
FormatterStep step = GoogleJavaFormatStep.create(GoogleJavaFormatStep.defaultGroupArtifact(), "1.10.0", GoogleJavaFormatStep.defaultStyle(), TestProvisioner.mavenCentral(), false);
Expand Down

0 comments on commit 2d23db2

Please sign in to comment.