From 4cd9cc1dc50a7c39bfeb9b5ebb5c3872afcf4600 Mon Sep 17 00:00:00 2001 From: Chao-Yue Lai Date: Mon, 3 Apr 2023 18:07:32 -0400 Subject: [PATCH 1/2] Adding back the "style" option in Palantir Java Format --- .../spotless/java/PalantirJavaFormatStep.java | 26 ++++++++++++++++--- .../pjf/PalantirJavaFormatFormatterFunc.java | 11 +++++--- plugin-gradle/README.md | 4 +-- .../gradle/spotless/JavaExtension.java | 10 ++++++- plugin-maven/README.md | 1 + .../JavaCodeFormattedGoogle.test | 11 ++++++++ .../JavaCodeWithLicenseFormattedGoogle.test | 16 ++++++++++++ .../JavaCodeWithPackageFormattedGoogle.test | 13 ++++++++++ .../java/PalantirJavaFormatStepTest.java | 15 ++++++++++- 9 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedGoogle.test create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedGoogle.test create mode 100644 testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedGoogle.test diff --git a/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java b/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java index 67c55c2e86..f769ad09ec 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java @@ -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. @@ -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 JVM_SUPPORT = Jvm. support(NAME).add(8, "1.1.0").add(11, "2.28.0"); @@ -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); } @@ -51,6 +58,11 @@ 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; @@ -58,18 +70,24 @@ private static final class State implements Serializable { 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)); } } } diff --git a/lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java b/lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java index 6824cdbc48..189bbb54be 100644 --- a/lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java +++ b/lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java @@ -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. @@ -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); } diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 616aacdb1f..95ca3af5ff 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -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 diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index e67bb6d076..d85cb8de5e 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -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()); } } diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 5971721e24..12ade2ccf5 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -228,6 +228,7 @@ any other maven phase (i.e. compile) then it can be configured as below; ```xml 2.10.0 + ``` diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedGoogle.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedGoogle.test new file mode 100644 index 0000000000..7a83a0a12c --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormattedGoogle.test @@ -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(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedGoogle.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedGoogle.test new file mode 100644 index 0000000000..9ee9bcbad6 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormattedGoogle.test @@ -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(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedGoogle.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedGoogle.test new file mode 100644 index 0000000000..4992ade147 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormattedGoogle.test @@ -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(); + } +} diff --git a/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java index dfd59c2655..81d03b6c32 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java @@ -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) { @@ -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(); } From e12973a37a648a9e84806dfd94ccbe30073b1d5f Mon Sep 17 00:00:00 2001 From: Chao-Yue Lai Date: Mon, 3 Apr 2023 18:45:28 -0400 Subject: [PATCH 2/2] Adding the commit in CHANGES.md's --- CHANGES.md | 1 + plugin-gradle/CHANGES.md | 1 + plugin-maven/CHANGES.md | 2 ++ 3 files changed, 4 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 77c49efe2a..89c99b7841 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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)) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 0a0cabcca0..15ee39e1f8 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -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 < `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)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 421b5e56e7..72d235a38a 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -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 < `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))