Skip to content

Commit 2d23db2

Browse files
authored
Support google-java-format's skip-javadoc-formatting option (#1793)
2 parents 26d0542 + e0b0229 commit 2d23db2

File tree

18 files changed

+118
-20
lines changed

18 files changed

+118
-20
lines changed

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1414
* Add support for biome. The Rome project [was renamed to Biome](https://biomejs.dev/blog/annoucing-biome/).
1515
The configuration is still the same, but you should switch to the new `biome` tag / function and adjust
1616
the version accordingly. ([#1804](https://github.com/diffplug/spotless/issues/1804)).
17+
* Add support for `google-java-format`'s `skip-javadoc-formatting` option. ([#1793](https://github.com/diffplug/spotless/pull/1793))
1718
### Fixed
18-
* Added support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
19+
* Fix support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
1920
### Changes
2021
* Bump default `flexmark` version to latest `0.64.0` -> `0.64.8`. ([#1801](https://github.com/diffplug/spotless/pull/1801))
2122
* Bump default `ktlint` version to latest `0.50.0` -> `1.0.0`. ([#1808](https://github.com/diffplug/spotless/pull/1808))

lib/src/googleJavaFormat/java/com/diffplug/spotless/glue/java/GoogleJavaFormatFormatterFunc.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,17 @@ public class GoogleJavaFormatFormatterFunc implements FormatterFunc {
4646

4747
private final boolean reorderImports;
4848

49-
public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings, boolean reorderImports) {
49+
public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings, boolean reorderImports, boolean formatJavadoc) {
5050
this.version = Objects.requireNonNull(version);
5151
this.formatterStyle = Style.valueOf(Objects.requireNonNull(style));
5252
this.reflowStrings = reflowStrings;
5353
this.reorderImports = reorderImports;
5454

55-
this.formatter = new Formatter(JavaFormatterOptions.builder()
56-
.style(formatterStyle)
57-
.build());
55+
JavaFormatterOptions.Builder builder = JavaFormatterOptions.builder().style(formatterStyle);
56+
if (!formatJavadoc) {
57+
builder = builder.formatJavadoc(false);
58+
}
59+
this.formatter = new Formatter(builder.build());
5860
}
5961

6062
@Override

lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ private GoogleJavaFormatStep() {}
3333
private static final String DEFAULT_STYLE = "GOOGLE";
3434
private static final boolean DEFAULT_REFLOW_LONG_STRINGS = false;
3535
private static final boolean DEFAULT_REORDER_IMPORTS = false;
36+
private static final boolean DEFAULT_FORMAT_JAVADOC = true;
3637
static final String NAME = "google-java-format";
3738
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";
3839

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

5960
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
60-
return create(groupArtifact, version, style, provisioner, reflowLongStrings, false);
61+
return create(groupArtifact, version, style, provisioner, reflowLongStrings, false, DEFAULT_FORMAT_JAVADOC);
6162
}
6263

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

@@ -101,6 +102,10 @@ public static boolean defaultReorderImports() {
101102
return DEFAULT_REORDER_IMPORTS;
102103
}
103104

105+
public static boolean defaultFormatJavadoc() {
106+
return DEFAULT_FORMAT_JAVADOC;
107+
}
108+
104109
static final class State implements Serializable {
105110
private static final long serialVersionUID = 1L;
106111

@@ -111,6 +116,7 @@ static final class State implements Serializable {
111116
final String style;
112117
final boolean reflowLongStrings;
113118
final boolean reorderImports;
119+
final boolean formatJavadoc;
114120

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

123129
State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
124-
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS);
130+
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS, DEFAULT_FORMAT_JAVADOC);
125131
}
126132

127-
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) throws Exception {
133+
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports, boolean formatJavadoc) throws Exception {
128134
JVM_SUPPORT.assertFormatterSupported(version);
129135
ModuleHelper.doOpenInternalPackagesIfRequired();
130136
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner);
@@ -133,13 +139,14 @@ static final class State implements Serializable {
133139
this.style = style;
134140
this.reflowLongStrings = reflowLongStrings;
135141
this.reorderImports = reorderImports;
142+
this.formatJavadoc = formatJavadoc;
136143
}
137144

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

144151
return JVM_SUPPORT.suggestLaterVersionOnError(version, googleJavaFormatFormatterFunc);
145152
}

plugin-gradle/CHANGES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
44

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

plugin-gradle/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ spotless {
116116
// don't need to set target, it is inferred from java
117117
118118
// apply a specific flavor of google-java-format
119-
googleJavaFormat('1.8').aosp().reflowLongStrings()
119+
googleJavaFormat('1.8').aosp().reflowLongStrings().skipJavadocFormatting()
120120
// fix formatting of type annotations
121121
formatAnnotations()
122122
// make sure every file has the following copyright header.
@@ -207,7 +207,7 @@ spotless {
207207
// optional: you can specify a specific version (>= 1.8) and/or switch to AOSP style
208208
// and/or reflow long strings
209209
// and/or use custom group artifact (you probably don't need this)
210-
googleJavaFormat('1.8').aosp().reflowLongStrings().reorderImports(false).groupArtifact('com.google.googlejavaformat:google-java-format')
210+
googleJavaFormat('1.8').aosp().reflowLongStrings().formatJavadoc(false).reorderImports(false).groupArtifact('com.google.googlejavaformat:google-java-format')
211211
```
212212

213213
### palantir-java-format

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public class GoogleJavaFormatConfig {
174174
String style;
175175
boolean reflowLongStrings;
176176
boolean reorderImports;
177+
boolean formatJavadoc = true;
177178

178179
GoogleJavaFormatConfig(String version) {
179180
this.version = Objects.requireNonNull(version);
@@ -213,14 +214,25 @@ public GoogleJavaFormatConfig reorderImports(boolean reorderImports) {
213214
return this;
214215
}
215216

217+
public GoogleJavaFormatConfig skipJavadocFormatting() {
218+
return formatJavadoc(false);
219+
}
220+
221+
public GoogleJavaFormatConfig formatJavadoc(boolean formatJavadoc) {
222+
this.formatJavadoc = formatJavadoc;
223+
replaceStep(createStep());
224+
return this;
225+
}
226+
216227
private FormatterStep createStep() {
217228
return GoogleJavaFormatStep.create(
218229
groupArtifact,
219230
version,
220231
style,
221232
provisioner(),
222233
reflowLongStrings,
223-
reorderImports);
234+
reorderImports,
235+
formatJavadoc);
224236
}
225237
}
226238

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GoogleJavaFormatIntegrationTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,30 @@ void integration() throws IOException {
4545
"googleJavaFormat()");
4646
checkRunsThenUpToDate();
4747
}
48+
49+
@Test
50+
void integrationWithSkipJavadocFormatting() throws IOException {
51+
setFile("build.gradle").toLines(
52+
"plugins {",
53+
" id 'com.diffplug.spotless'",
54+
"}",
55+
"repositories { mavenCentral() }",
56+
"",
57+
"spotless {",
58+
" java {",
59+
" target file('test.java')",
60+
" googleJavaFormat('1.12.0').skipJavadocFormatting()",
61+
" }",
62+
"}");
63+
64+
setFile("test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test");
65+
gradleRunner().withArguments("spotlessApply").build();
66+
assertFile("test.java").sameAsResource("java/googlejavaformat/JavaCodeFormattedSkipJavadocFormatting.test");
67+
68+
checkRunsThenUpToDate();
69+
replace("build.gradle",
70+
"googleJavaFormat('1.12.0')",
71+
"googleJavaFormat()");
72+
checkRunsThenUpToDate();
73+
}
4874
}

plugin-maven/CHANGES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
44

55
## [Unreleased]
66
### Added
7-
* Add support for biome. The Rome project [was renamed to Biome](https://biomejs.dev/blog/annoucing-biome/).
7+
* Added support for `google-java-format`'s `skip-javadoc-formatting` option ([#1793](https://github.com/diffplug/spotless/pull/1793))
8+
* Added support for biome. The Rome project [was renamed to Biome](https://biomejs.dev/blog/annoucing-biome/).
89
The configuration is still the same, but you should switch to the new `<biome>` tag and adjust
910
the version accordingly. ([#1804](https://github.com/diffplug/spotless/issues/1804)).
1011
### Fixed
11-
* Added support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
12+
* Fixed support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
1213
### Changes
1314
* Bump default `flexmark` version to latest `0.64.0` -> `0.64.8`. ([#1801](https://github.com/diffplug/spotless/pull/1801))
1415
* Bump default `ktlint` version to latest `0.50.0` -> `1.0.0`. ([#1808](https://github.com/diffplug/spotless/pull/1808))

plugin-maven/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ To use it in your pom, just [add the Spotless dependency](https://search.maven.o
109109
<version>1.8</version>
110110
<style>AOSP</style>
111111
<reflowLongStrings>true</reflowLongStrings>
112+
<formatJavadoc>false</formatJavadoc>
112113
</googleJavaFormat>
113114

114115
<!-- make sure every file has the following copyright header.
@@ -233,6 +234,7 @@ any other maven phase (i.e. compile) then it can be configured as below;
233234
<version>1.8</version> <!-- optional, 1.8 is minimum supported version -->
234235
<style>GOOGLE</style> <!-- or AOSP (optional) -->
235236
<reflowLongStrings>true</reflowLongStrings> <!-- optional -->
237+
<formatJavadoc>false</formatJavadoc> <!-- optional -->
236238
<!-- optional: custom group artifact (you probably don't need this) -->
237239
<groupArtifact>com.google.googlejavaformat:google-java-format</groupArtifact>
238240
</googleJavaFormat>

plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ public class GoogleJavaFormat implements FormatterStepFactory {
3838
@Parameter
3939
private Boolean reorderImports;
4040

41+
@Parameter
42+
private Boolean formatJavadoc;
43+
4144
@Override
4245
public FormatterStep newFormatterStep(FormatterStepConfig config) {
4346
String groupArtifact = this.groupArtifact != null ? this.groupArtifact : GoogleJavaFormatStep.defaultGroupArtifact();
4447
String version = this.version != null ? this.version : GoogleJavaFormatStep.defaultVersion();
4548
String style = this.style != null ? this.style : GoogleJavaFormatStep.defaultStyle();
4649
boolean reflowLongStrings = this.reflowLongStrings != null ? this.reflowLongStrings : GoogleJavaFormatStep.defaultReflowLongStrings();
4750
boolean reorderImports = this.reorderImports != null ? this.reorderImports : GoogleJavaFormatStep.defaultReorderImports();
48-
return GoogleJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings, reorderImports);
51+
boolean formatJavadoc = this.formatJavadoc != null ? this.formatJavadoc : GoogleJavaFormatStep.defaultFormatJavadoc();
52+
return GoogleJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings, reorderImports, formatJavadoc);
4953
}
5054
}

0 commit comments

Comments
 (0)