Skip to content

Commit f95d093

Browse files
authored
Adding back the "style" option in Palantir Java Format (#1654)
2 parents 504f8c6 + e12973a commit f95d093

File tree

12 files changed

+99
-12
lines changed

12 files changed

+99
-12
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Added
1414
* Support configuration of mirrors for P2 repositories in `EquoBasedStepBuilder` ([#1629](https://github.com/diffplug/spotless/issues/1629)).
15+
* The `style` option in Palantir Java Format ([#1654](https://github.com/diffplug/spotless/pull/1654)).
1516
### Changes
1617
* **POTENTIALLY BREAKING** Converted `googleJavaFormat` to a compile-only dependency and drop support for versions < `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
1718
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))

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

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -27,6 +27,7 @@ public class PalantirJavaFormatStep {
2727
// prevent direct instantiation
2828
private PalantirJavaFormatStep() {}
2929

30+
private static final String DEFAULT_STYLE = "PALANTIR";
3031
private static final String NAME = "palantir-java-format";
3132
private static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:";
3233
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.1.0").add(11, "2.28.0");
@@ -38,11 +39,17 @@ public static FormatterStep create(Provisioner provisioner) {
3839

3940
/** Creates a step which formats everything - code, import order, and unused imports. */
4041
public static FormatterStep create(String version, Provisioner provisioner) {
42+
return create(version, defaultStyle(), provisioner);
43+
}
44+
45+
/** Creates a step which formats everything - code, import order, and unused imports. And with the style input. */
46+
public static FormatterStep create(String version, String style, Provisioner provisioner) {
4147
Objects.requireNonNull(version, "version");
48+
Objects.requireNonNull(style, "style");
4249
Objects.requireNonNull(provisioner, "provisioner");
4350

4451
return FormatterStep.createLazy(NAME,
45-
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version),
52+
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version, style),
4653
State::createFormat);
4754
}
4855

@@ -51,25 +58,36 @@ public static String defaultVersion() {
5158
return JVM_SUPPORT.getRecommendedFormatterVersion();
5259
}
5360

61+
/** Get default style */
62+
public static String defaultStyle() {
63+
return DEFAULT_STYLE;
64+
}
65+
5466
private static final class State implements Serializable {
5567
private static final long serialVersionUID = 1L;
5668

5769
/** The jar that contains the formatter. */
5870
private final JarState jarState;
5971
/** Version of the formatter jar. */
6072
private final String formatterVersion;
73+
private final String style;
6174

6275
State(JarState jarState, String formatterVersion) {
76+
this(jarState, formatterVersion, DEFAULT_STYLE);
77+
}
78+
79+
State(JarState jarState, String formatterVersion, String style) {
6380
ModuleHelper.doOpenInternalPackagesIfRequired();
6481
this.jarState = jarState;
6582
this.formatterVersion = formatterVersion;
83+
this.style = style;
6684
}
6785

6886
FormatterFunc createFormat() throws Exception {
6987
final ClassLoader classLoader = jarState.getClassLoader();
7088
final Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc");
71-
final Constructor<?> constructor = formatterFunc.getConstructor();
72-
return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance());
89+
final Constructor<?> constructor = formatterFunc.getConstructor(String.class); // style
90+
return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance(style));
7391
}
7492
}
7593
}

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2023 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.
@@ -26,16 +26,19 @@ public class PalantirJavaFormatFormatterFunc implements FormatterFunc {
2626

2727
private final Formatter formatter;
2828

29-
public PalantirJavaFormatFormatterFunc() {
29+
private final JavaFormatterOptions.Style formatterStyle;
30+
31+
public PalantirJavaFormatFormatterFunc(String style) {
32+
this.formatterStyle = JavaFormatterOptions.Style.valueOf(style);
3033
formatter = Formatter.createFormatter(JavaFormatterOptions.builder()
31-
.style(JavaFormatterOptions.Style.PALANTIR)
34+
.style(formatterStyle)
3235
.build());
3336
}
3437

3538
@Override
3639
public String apply(String input) throws Exception {
3740
String source = input;
38-
source = ImportOrderer.reorderImports(source, JavaFormatterOptions.Style.PALANTIR);
41+
source = ImportOrderer.reorderImports(source, formatterStyle);
3942
source = RemoveUnusedImports.removeUnusedImports(source);
4043
return formatter.formatSource(source);
4144
}

plugin-gradle/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1414
```
1515
Mirrors are selected by prefix match, for example `https://download.eclipse.org/eclipse/updates/4.26/` will be redirected to `https://some.internal.mirror/eclipse/eclipse/updates/4.26/`.
1616
The same configuration exists for `greclipse` and `eclipseCdt`.
17+
* The `style` option in Palantir Java Format ([#1654](https://github.com/diffplug/spotless/pull/1654)).
1718
### Changes
1819
* **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions &lt; `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
1920
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))

plugin-gradle/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ spotless {
200200
spotless {
201201
java {
202202
palantirJavaFormat()
203-
// optional: you can specify a specific version
204-
palantirJavaFormat('2.9.0')
203+
// optional: you can specify a specific version and/or switch to AOSP/GOOGLE style
204+
palantirJavaFormat('2.9.0').style("GOOGLE")
205205
```
206206

207207
### eclipse jdt

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,22 @@ public PalantirJavaFormatConfig palantirJavaFormat(String version) {
198198

199199
public class PalantirJavaFormatConfig {
200200
final String version;
201+
String style;
201202

202203
PalantirJavaFormatConfig(String version) {
203204
this.version = Objects.requireNonNull(version);
205+
this.style = PalantirJavaFormatStep.defaultStyle();
204206
addStep(createStep());
205207
}
206208

209+
public PalantirJavaFormatConfig style(String style) {
210+
this.style = Objects.requireNonNull(style);
211+
replaceStep(createStep());
212+
return this;
213+
}
214+
207215
private FormatterStep createStep() {
208-
return PalantirJavaFormatStep.create(version, provisioner());
216+
return PalantirJavaFormatStep.create(version, style, provisioner());
209217
}
210218
}
211219

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* The `style` option in Palantir Java Format ([#1654](https://github.com/diffplug/spotless/pull/1654)).
68
### Changes
79
* **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions &lt; `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
810
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))

plugin-maven/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ any other maven phase (i.e. compile) then it can be configured as below;
228228
```xml
229229
<palantirJavaFormat>
230230
<version>2.10.0</version> <!-- optional -->
231+
<style>PALANTIR</style> <!-- or AOSP/GOOGLE (optional) -->
231232
</palantirJavaFormat>
232233
```
233234

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import mylib.UsedA;
2+
import mylib.UsedB;
3+
4+
public class Java {
5+
public static void main(String[] args) {
6+
System.out.println(
7+
"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.");
8+
UsedB.someMethod();
9+
UsedA.someMethod();
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Some license stuff.
3+
* Very official.
4+
*/
5+
6+
import mylib.UsedA;
7+
import mylib.UsedB;
8+
9+
public class Java {
10+
public static void main(String[] args) {
11+
System.out.println(
12+
"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.");
13+
UsedB.someMethod();
14+
UsedA.someMethod();
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package hello.world;
2+
3+
import mylib.UsedA;
4+
import mylib.UsedB;
5+
6+
public class Java {
7+
public static void main(String[] args) {
8+
System.out.println(
9+
"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.");
10+
UsedB.someMethod();
11+
UsedA.someMethod();
12+
}
13+
}

testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,20 @@ void behavior() throws Exception {
5454
.testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormatted.test");
5555
}
5656

57+
@Test
58+
void behaviorWithGoogleStyle() throws Exception {
59+
FormatterStep step = PalantirJavaFormatStep.create("1.1.0", "GOOGLE", TestProvisioner.mavenCentral());
60+
StepHarness.forStep(step)
61+
.testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormattedGoogle.test")
62+
.testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormattedGoogle.test")
63+
.testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormattedGoogle.test");
64+
}
65+
5766
@Test
5867
void equality() {
5968
new SerializableEqualityTester() {
6069
String version = "1.1.0";
70+
String style = "";
6171

6272
@Override
6373
protected void setupTest(API api) {
@@ -66,12 +76,15 @@ protected void setupTest(API api) {
6676
// change the version, and it's different
6777
version = "1.0.0";
6878
api.areDifferentThan();
79+
// change the style, and it's different
80+
style = "AOSP";
81+
api.areDifferentThan();
6982
}
7083

7184
@Override
7285
protected FormatterStep create() {
7386
String finalVersion = this.version;
74-
return PalantirJavaFormatStep.create(finalVersion, TestProvisioner.mavenCentral());
87+
return PalantirJavaFormatStep.create(finalVersion, style, TestProvisioner.mavenCentral());
7588
}
7689
}.testEquals();
7790
}

0 commit comments

Comments
 (0)