Skip to content

Adding back the "style" option in Palantir Java Format #1654

New issue

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

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

Already on GitHub? # to your account

Merged
merged 2 commits into from
Apr 5, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* Support configuration of mirrors for P2 repositories in `EquoBasedStepBuilder` ([#1629](https://github.com/diffplug/spotless/issues/1629)).
* The `style` option in Palantir Java Format ([#1654](https://github.com/diffplug/spotless/pull/1654)).
### Changes
* **POTENTIALLY BREAKING** Converted `googleJavaFormat` to a compile-only dependency and drop support for versions < `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@ public class PalantirJavaFormatStep {
// prevent direct instantiation
private PalantirJavaFormatStep() {}

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

/** Creates a step which formats everything - code, import order, and unused imports. */
public static FormatterStep create(String version, Provisioner provisioner) {
return create(version, defaultStyle(), provisioner);
}

/** Creates a step which formats everything - code, import order, and unused imports. And with the style input. */
public static FormatterStep create(String version, String style, Provisioner provisioner) {
Objects.requireNonNull(version, "version");
Objects.requireNonNull(style, "style");
Objects.requireNonNull(provisioner, "provisioner");

return FormatterStep.createLazy(NAME,
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version),
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version, style),
State::createFormat);
}

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

/** Get default style */
public static String defaultStyle() {
return DEFAULT_STYLE;
}

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

/** The jar that contains the formatter. */
private final JarState jarState;
/** Version of the formatter jar. */
private final String formatterVersion;
private final String style;

State(JarState jarState, String formatterVersion) {
this(jarState, formatterVersion, DEFAULT_STYLE);
}

State(JarState jarState, String formatterVersion, String style) {
ModuleHelper.doOpenInternalPackagesIfRequired();
this.jarState = jarState;
this.formatterVersion = formatterVersion;
this.style = style;
}

FormatterFunc createFormat() throws Exception {
final ClassLoader classLoader = jarState.getClassLoader();
final Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc");
final Constructor<?> constructor = formatterFunc.getConstructor();
return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance());
final Constructor<?> constructor = formatterFunc.getConstructor(String.class); // style
return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance(style));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 DiffPlug
* Copyright 2022-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,16 +26,19 @@ public class PalantirJavaFormatFormatterFunc implements FormatterFunc {

private final Formatter formatter;

public PalantirJavaFormatFormatterFunc() {
private final JavaFormatterOptions.Style formatterStyle;

public PalantirJavaFormatFormatterFunc(String style) {
this.formatterStyle = JavaFormatterOptions.Style.valueOf(style);
formatter = Formatter.createFormatter(JavaFormatterOptions.builder()
.style(JavaFormatterOptions.Style.PALANTIR)
.style(formatterStyle)
.build());
}

@Override
public String apply(String input) throws Exception {
String source = input;
source = ImportOrderer.reorderImports(source, JavaFormatterOptions.Style.PALANTIR);
source = ImportOrderer.reorderImports(source, formatterStyle);
source = RemoveUnusedImports.removeUnusedImports(source);
return formatter.formatSource(source);
}
Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
```
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/`.
The same configuration exists for `greclipse` and `eclipseCdt`.
* The `style` option in Palantir Java Format ([#1654](https://github.com/diffplug/spotless/pull/1654)).
### Changes
* **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions &lt; `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
Expand Down
4 changes: 2 additions & 2 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ spotless {
spotless {
java {
palantirJavaFormat()
// optional: you can specify a specific version
palantirJavaFormat('2.9.0')
// optional: you can specify a specific version and/or switch to AOSP/GOOGLE style
palantirJavaFormat('2.9.0').style("GOOGLE")
```

### eclipse jdt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,22 @@ public PalantirJavaFormatConfig palantirJavaFormat(String version) {

public class PalantirJavaFormatConfig {
final String version;
String style;

PalantirJavaFormatConfig(String version) {
this.version = Objects.requireNonNull(version);
this.style = PalantirJavaFormatStep.defaultStyle();
addStep(createStep());
}

public PalantirJavaFormatConfig style(String style) {
this.style = Objects.requireNonNull(style);
replaceStep(createStep());
return this;
}

private FormatterStep createStep() {
return PalantirJavaFormatStep.create(version, provisioner());
return PalantirJavaFormatStep.create(version, style, provisioner());
}
}

Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Added
* The `style` option in Palantir Java Format ([#1654](https://github.com/diffplug/spotless/pull/1654)).
### Changes
* **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions &lt; `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ any other maven phase (i.e. compile) then it can be configured as below;
```xml
<palantirJavaFormat>
<version>2.10.0</version> <!-- optional -->
<style>PALANTIR</style> <!-- or AOSP/GOOGLE (optional) -->
</palantirJavaFormat>
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import mylib.UsedA;
import mylib.UsedB;

public class Java {
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
@@ -0,0 +1,16 @@
/*
* Some license stuff.
* Very official.
*/

import mylib.UsedA;
import mylib.UsedB;

public class Java {
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
@@ -0,0 +1,13 @@
package hello.world;

import mylib.UsedA;
import mylib.UsedB;

public class Java {
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 @@ -54,10 +54,20 @@ void behavior() throws Exception {
.testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormatted.test");
}

@Test
void behaviorWithGoogleStyle() throws Exception {
FormatterStep step = PalantirJavaFormatStep.create("1.1.0", "GOOGLE", TestProvisioner.mavenCentral());
StepHarness.forStep(step)
.testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormattedGoogle.test")
.testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormattedGoogle.test")
.testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormattedGoogle.test");
}

@Test
void equality() {
new SerializableEqualityTester() {
String version = "1.1.0";
String style = "";

@Override
protected void setupTest(API api) {
Expand All @@ -66,12 +76,15 @@ protected void setupTest(API api) {
// change the version, and it's different
version = "1.0.0";
api.areDifferentThan();
// change the style, and it's different
style = "AOSP";
api.areDifferentThan();
}

@Override
protected FormatterStep create() {
String finalVersion = this.version;
return PalantirJavaFormatStep.create(finalVersion, TestProvisioner.mavenCentral());
return PalantirJavaFormatStep.create(finalVersion, style, TestProvisioner.mavenCentral());
}
}.testEquals();
}
Expand Down