Skip to content

Commit a145ac0

Browse files
authored
Merge branch 'main' into petertrr/add-gofmt#861
2 parents 9d71e6d + 13d9627 commit a145ac0

File tree

15 files changed

+237
-30
lines changed

15 files changed

+237
-30
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1111

1212
## [Unreleased]
1313
### Added
14-
* Maven - Support for formatting shell scripts via [shfmt](https://github.com/mvdan/sh). ([#1998](https://github.com/diffplug/spotless/pull/1998))
1514
* Support for `gofmt` ([#2001](https://github.com/diffplug/spotless/pull/2001))
15+
* Support for formatting Java Docs for the Palantir formatter ([#2009](https://github.com/diffplug/spotless/pull/2009))
1616

1717
## [2.44.0] - 2024-01-15
1818
### Added

CONTRIBUTING.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ gradlew :plugin-maven:test --tests com.diffplug.spotless.maven.pom.SortPomMavenT
171171
gradlew :plugin-gradle:test --tests com.diffplug.gradle.spotless.FreshMarkExtensionTest
172172
```
173173

174+
## Check and format code
175+
176+
Before creating a pull request, you might want to format (yes, spotless is formatted by spotless)
177+
the code and check for possible bugs
178+
179+
* `./gradlew spotlessApply`
180+
* `./gradlew spotbugsMain`
181+
182+
These checks are also run by the automated pipeline when you submit a pull request, if
183+
the pipeline fails, first check if the code is formatted and no bugs were found.
184+
174185
## Integration testing
175186

176187
### Gradle - locally

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class PalantirJavaFormatStep {
2727
// prevent direct instantiation
2828
private PalantirJavaFormatStep() {}
2929

30+
private static final boolean DEFAULT_FORMAT_JAVADOC = false;
3031
private static final String DEFAULT_STYLE = "PALANTIR";
3132
private static final String NAME = "palantir-java-format";
3233
public static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:";
@@ -42,14 +43,25 @@ public static FormatterStep create(String version, Provisioner provisioner) {
4243
return create(version, defaultStyle(), provisioner);
4344
}
4445

45-
/** Creates a step which formats everything - code, import order, and unused imports. And with the style input. */
46+
/**
47+
* Creates a step which formats code, import order, and unused imports, but not Java docs. And with the given format
48+
* style.
49+
*/
4650
public static FormatterStep create(String version, String style, Provisioner provisioner) {
51+
return create(version, style, DEFAULT_FORMAT_JAVADOC, provisioner);
52+
}
53+
54+
/**
55+
* Creates a step which formats everything - code, import order, unused imports, and Java docs. And with the given
56+
* format style.
57+
*/
58+
public static FormatterStep create(String version, String style, boolean formatJavadoc, Provisioner provisioner) {
4759
Objects.requireNonNull(version, "version");
4860
Objects.requireNonNull(style, "style");
4961
Objects.requireNonNull(provisioner, "provisioner");
5062

5163
return FormatterStep.createLazy(NAME,
52-
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version, style),
64+
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version, style, formatJavadoc),
5365
State::createFormat);
5466
}
5567

@@ -63,6 +75,11 @@ public static String defaultStyle() {
6375
return DEFAULT_STYLE;
6476
}
6577

78+
/** Get default for whether Java docs should be formatted */
79+
public static boolean defaultFormatJavadoc() {
80+
return DEFAULT_FORMAT_JAVADOC;
81+
}
82+
6683
private static final class State implements Serializable {
6784
private static final long serialVersionUID = 1L;
6885

@@ -71,23 +88,23 @@ private static final class State implements Serializable {
7188
/** Version of the formatter jar. */
7289
private final String formatterVersion;
7390
private final String style;
91+
/** Whether to format Java docs. */
92+
private final boolean formatJavadoc;
7493

75-
State(JarState jarState, String formatterVersion) {
76-
this(jarState, formatterVersion, DEFAULT_STYLE);
77-
}
78-
79-
State(JarState jarState, String formatterVersion, String style) {
94+
State(JarState jarState, String formatterVersion, String style, boolean formatJavadoc) {
8095
ModuleHelper.doOpenInternalPackagesIfRequired();
8196
this.jarState = jarState;
8297
this.formatterVersion = formatterVersion;
8398
this.style = style;
99+
this.formatJavadoc = formatJavadoc;
84100
}
85101

86102
FormatterFunc createFormat() throws Exception {
87103
final ClassLoader classLoader = jarState.getClassLoader();
88104
final Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc");
89-
final Constructor<?> constructor = formatterFunc.getConstructor(String.class); // style
90-
return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance(style));
105+
// 1st arg is "style", 2nd arg is "formatJavadoc"
106+
final Constructor<?> constructor = formatterFunc.getConstructor(String.class, boolean.class);
107+
return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance(style, formatJavadoc));
91108
}
92109
}
93110
}

lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 DiffPlug
2+
* Copyright 2022-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,9 @@
1515
*/
1616
package com.diffplug.spotless.glue.pjf;
1717

18+
import java.lang.reflect.InvocationTargetException;
19+
import java.lang.reflect.Method;
20+
1821
import com.palantir.javaformat.java.Formatter;
1922
import com.palantir.javaformat.java.ImportOrderer;
2023
import com.palantir.javaformat.java.JavaFormatterOptions;
@@ -28,11 +31,20 @@ public class PalantirJavaFormatFormatterFunc implements FormatterFunc {
2831

2932
private final JavaFormatterOptions.Style formatterStyle;
3033

31-
public PalantirJavaFormatFormatterFunc(String style) {
34+
/**
35+
* Creates a new formatter func that formats code via Palantir.
36+
* @param style The style to use for formatting.
37+
* @param formatJavadoc Whether to format Java docs. Requires at least Palantir 2.36.0 or later, otherwise the
38+
* constructor will throw.
39+
*/
40+
public PalantirJavaFormatFormatterFunc(String style, boolean formatJavadoc) {
3241
this.formatterStyle = JavaFormatterOptions.Style.valueOf(style);
33-
formatter = Formatter.createFormatter(JavaFormatterOptions.builder()
34-
.style(formatterStyle)
35-
.build());
42+
JavaFormatterOptions.Builder builder = JavaFormatterOptions.builder();
43+
builder.style(formatterStyle);
44+
if (formatJavadoc) {
45+
applyFormatJavadoc(builder);
46+
}
47+
formatter = Formatter.createFormatter(builder.build());
3648
}
3749

3850
@Override
@@ -47,4 +59,15 @@ public String apply(String input) throws Exception {
4759
public String toString() {
4860
return "PalantirJavaFormatFormatterFunc{formatter=" + formatter + '}';
4961
}
62+
63+
private static void applyFormatJavadoc(JavaFormatterOptions.Builder builder) {
64+
// The formatJavadoc option is available since Palantir 2.36.0
65+
// To support older versions for now, attempt to invoke the builder method via reflection.
66+
try {
67+
Method formatJavadoc = JavaFormatterOptions.Builder.class.getMethod("formatJavadoc", boolean.class);
68+
formatJavadoc.invoke(builder, true);
69+
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
70+
throw new IllegalStateException("Cannot enable formatJavadoc option, make sure you are using Palantir with version 2.36.0 or later", e);
71+
}
72+
}
5073
}

plugin-gradle/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
* Support for `gofmt` ([#2001](https://github.com/diffplug/spotless/pull/2001))
77

8+
### Added
9+
* Maven / Gradle - Support for formatting Java Docs for the Palantir formatter ([#2009](https://github.com/diffplug/spotless/pull/2009))
10+
811
## [6.24.0] - 2024-01-15
912
### Added
1013
* Support for shell formatting via [shfmt](https://github.com/mvdan/sh). ([#1994](https://github.com/diffplug/spotless/pull/1994))

plugin-gradle/README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ spotless {
220220
palantirJavaFormat()
221221
// optional: you can specify a specific version and/or switch to AOSP/GOOGLE style
222222
palantirJavaFormat('2.9.0').style("GOOGLE")
223+
// optional: you can also format Javadocs, requires at least Palantir 2.39.0
224+
palantirJavaFormat('2.39.0').formatJavadoc(true)
223225
```
224226

225227
### eclipse jdt
@@ -1086,16 +1088,34 @@ To apply prettier to more kinds of files, just add more formats
10861088
Since spotless uses the actual npm prettier package behind the scenes, it is possible to use prettier with
10871089
[plugins](https://prettier.io/docs/en/plugins.html#official-plugins) or [community-plugins](https://www.npmjs.com/search?q=prettier-plugin) in order to support even more file types.
10881090
1091+
#### prettier version below 3
1092+
10891093
```gradle
10901094
spotless {
10911095
java {
10921096
prettier(['prettier': '2.8.8', 'prettier-plugin-java': '2.2.0']).config(['parser': 'java', 'tabWidth': 4])
1093-
// prettier(['prettier': '3.0.3', 'prettier-plugin-java': '2.3.0']).config(['parser': 'java', 'tabWidth': 4, 'plugins': ['prettier-plugin-java']]) // Prettier v3 requires additional 'plugins' config
10941097
}
10951098
format 'php', {
10961099
target 'src/**/*.php'
10971100
prettier(['prettier': '2.8.8', '@prettier/plugin-php': '0.19.6']).config(['parser': 'php', 'tabWidth': 3])
1098-
// prettier(['prettier': '3.0.3', '@prettier/plugin-php': '0.20.1']).config(['parser': 'php', 'tabWidth': 3, 'plugins': ['@prettier/plugin-php']]) // Prettier v3 requires additional 'plugins' config
1101+
}
1102+
}
1103+
```
1104+
1105+
#### prettier version 3+
1106+
1107+
With version 3 prettier it is required to pass in an additional 'plugins' parameter to the config block with a list of plugins you want to use.
1108+
1109+
```gradle
1110+
spotless {
1111+
java {
1112+
prettier(['prettier': '3.0.3', 'prettier-plugin-java': '2.3.0'])
1113+
.config(['parser': 'java', 'tabWidth': 4, 'plugins': ['prettier-plugin-java']])
1114+
}
1115+
format 'php', {
1116+
target 'src/**/*.php'
1117+
prettier(['prettier': '3.0.3', '@prettier/plugin-php': '0.20.1'])
1118+
.config(['parser': 'php', 'tabWidth': 3, 'plugins': ['@prettier/plugin-php']])
10991119
}
11001120
}
11011121
```

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -256,6 +256,7 @@ public PalantirJavaFormatConfig palantirJavaFormat(String version) {
256256
public class PalantirJavaFormatConfig {
257257
final String version;
258258
String style;
259+
boolean formatJavadoc;
259260

260261
PalantirJavaFormatConfig(String version) {
261262
this.version = Objects.requireNonNull(version);
@@ -269,8 +270,14 @@ public PalantirJavaFormatConfig style(String style) {
269270
return this;
270271
}
271272

273+
public PalantirJavaFormatConfig formatJavadoc(boolean formatJavadoc) {
274+
this.formatJavadoc = formatJavadoc;
275+
replaceStep(createStep());
276+
return this;
277+
}
278+
272279
private FormatterStep createStep() {
273-
return PalantirJavaFormatStep.create(version, style, provisioner());
280+
return PalantirJavaFormatStep.create(version, style, formatJavadoc, provisioner());
274281
}
275282
}
276283

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,4 +45,26 @@ void integration() throws IOException {
4545
"palantirJavaFormat('1.0.1')");
4646
checkRunsThenUpToDate();
4747
}
48+
49+
@Test
50+
void formatJavaDoc() 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+
" palantirJavaFormat('2.39.0').formatJavadoc(true)",
61+
" }",
62+
"}");
63+
64+
setFile("test.java").toResource("java/palantirjavaformat/JavaCodeWithJavaDocUnformatted.test");
65+
gradleRunner().withArguments("spotlessApply").build();
66+
assertFile("test.java").sameAsResource("java/palantirjavaformat/JavaCodeWithJavaDocFormatted.test");
67+
68+
checkRunsThenUpToDate();
69+
}
4870
}

plugin-maven/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
66
### Added
77
* Support for formatting shell scripts via [shfmt](https://github.com/mvdan/sh). ([#1998](https://github.com/diffplug/spotless/issues/1998))
88
* Support for `gofmt` ([#2001](https://github.com/diffplug/spotless/pull/2001))
9+
* Support for formatting Java Docs for the Palantir formatter ([#2009](https://github.com/diffplug/spotless/pull/2009))
910

1011
## [2.42.0] - 2024-01-15
1112
### Added

plugin-maven/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,9 @@ any other maven phase (i.e. compile) then it can be configured as below;
247247

248248
```xml
249249
<palantirJavaFormat>
250-
<version>2.10.0</version> <!-- optional -->
250+
<version>2.39.0</version> <!-- optional -->
251251
<style>PALANTIR</style> <!-- or AOSP/GOOGLE (optional) -->
252+
<formatJavadoc>false</formatJavadoc> <!-- defaults to false (optional, requires at least Palantir 2.39.0) -->
252253
</palantirJavaFormat>
253254
```
254255

0 commit comments

Comments
 (0)