Skip to content

Commit 1f253fe

Browse files
committed
chore: add palantir java formatter
1 parent c7b20ef commit 1f253fe

File tree

9 files changed

+380
-7
lines changed

9 files changed

+380
-7
lines changed

app/src/main/java/com/diffplug/spotless/cli/SpotlessCLI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.diffplug.spotless.cli.logging.output.Output;
4646
import com.diffplug.spotless.cli.steps.GoogleJavaFormat;
4747
import com.diffplug.spotless.cli.steps.LicenseHeader;
48+
import com.diffplug.spotless.cli.steps.PalantirJavaFormat;
4849
import com.diffplug.spotless.cli.steps.Prettier;
4950
import com.diffplug.spotless.cli.version.SpotlessCLIVersionProvider;
5051

@@ -89,7 +90,7 @@
8990
"-2:An exception occurred during execution."
9091
},
9192
subcommandsRepeatable = true,
92-
subcommands = {LicenseHeader.class, GoogleJavaFormat.class, Prettier.class})
93+
subcommands = {LicenseHeader.class, GoogleJavaFormat.class, PalantirJavaFormat.class, Prettier.class})
9394
public class SpotlessCLI implements SpotlessAction, SpotlessCommand, SpotlessActionContextProvider {
9495

9596
private static final Logger LOGGER = LoggerFactory.getLogger(SpotlessCLI.class);

app/src/main/java/com/diffplug/spotless/cli/steps/GoogleJavaFormat.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public class GoogleJavaFormat extends SpotlessFormatterStep {
3434
"The style to use for the google java format." + OptionConstants.VALID_AND_DEFAULT_VALUES_SUFFIX)
3535
Style style;
3636

37+
public enum Style {
38+
AOSP,
39+
GOOGLE
40+
}
41+
3742
@CommandLine.Option(
3843
names = {"--reflow-long-strings", "-r"},
3944
defaultValue = "false",
@@ -63,9 +68,4 @@ public List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context)
6368
reorderImports,
6469
formatJavadoc));
6570
}
66-
67-
public enum Style {
68-
AOSP,
69-
GOOGLE
70-
}
7171
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.cli.steps;
17+
18+
import java.util.List;
19+
20+
import org.jetbrains.annotations.NotNull;
21+
22+
import com.diffplug.spotless.FormatterStep;
23+
import com.diffplug.spotless.cli.core.SpotlessActionContext;
24+
import com.diffplug.spotless.cli.help.OptionConstants;
25+
import com.diffplug.spotless.java.PalantirJavaFormatStep;
26+
27+
import picocli.CommandLine;
28+
29+
@CommandLine.Command(name = "palantir-java-format", description = "Runs palantir java format")
30+
public class PalantirJavaFormat extends SpotlessFormatterStep {
31+
32+
@CommandLine.Option(
33+
names = {"--style", "-s"},
34+
defaultValue = "PALANTIR",
35+
description =
36+
"The style to use for the palantir java format." + OptionConstants.VALID_AND_DEFAULT_VALUES_SUFFIX)
37+
Style style;
38+
39+
@CommandLine.Option(
40+
names = {"--format-javadoc", "-j"},
41+
defaultValue = "false",
42+
description = "Format javadoc." + OptionConstants.DEFAULT_VALUE_SUFFIX)
43+
boolean formatJavadoc;
44+
45+
public enum Style {
46+
PALANTIR,
47+
AOSP,
48+
GOOGLE
49+
}
50+
51+
@Override
52+
public @NotNull List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context) {
53+
return List.of(PalantirJavaFormatStep.create(
54+
PalantirJavaFormatStep.defaultVersion(), style.name(), formatJavadoc, context.provisioner()));
55+
}
56+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.cli.steps;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.EnumSource;
21+
22+
import com.diffplug.spotless.cli.CLIIntegrationHarness;
23+
import com.diffplug.spotless.cli.SpotlessCLIRunner;
24+
import com.diffplug.spotless.tag.CliNativeTest;
25+
import com.diffplug.spotless.tag.CliProcessTest;
26+
27+
@CliProcessTest
28+
@CliNativeTest
29+
public class PalantirJavaFormatTest extends CLIIntegrationHarness {
30+
31+
@Test
32+
void itFormatsWithDefaultOptions() {
33+
setFile("Java.java").toResource("java/palantirjavaformat/JavaCodeWithJavaDocUnformatted.test");
34+
35+
SpotlessCLIRunner.Result result = cliRunner()
36+
.withTargets("*.java")
37+
.withStep(PalantirJavaFormat.class)
38+
.run();
39+
40+
selfie().expectResource("Java.java").toMatchDisk();
41+
}
42+
43+
@ParameterizedTest
44+
@EnumSource(PalantirJavaFormat.Style.class)
45+
void itFormatsWithSelectedStyle(PalantirJavaFormat.Style style) {
46+
setFile("Java.java").toResource("java/palantirjavaformat/JavaCodeWithJavaDocUnformatted.test");
47+
48+
SpotlessCLIRunner.Result result = cliRunner()
49+
.withTargets("*.java")
50+
.withStep(PalantirJavaFormat.class)
51+
.withOption("--style", style.name())
52+
.run();
53+
54+
selfie().expectResource("Java.java").toMatchDisk(style.name());
55+
}
56+
57+
@Test
58+
void itFormatsJavadoc() {
59+
setFile("Java.java").toResource("java/palantirjavaformat/JavaCodeWithJavaDocUnformatted.test");
60+
61+
SpotlessCLIRunner.Result result = cliRunner()
62+
.withTargets("*.java")
63+
.withStep(PalantirJavaFormat.class)
64+
.withOption("--format-javadoc", "true")
65+
.run();
66+
67+
selfie().expectResource("Java.java").toMatchDisk();
68+
}
69+
70+
@Test
71+
void itFormatsTextBlocks() {
72+
setFile("Java.java").toResource("java/palantirjavaformat/TextBlock.dirty");
73+
74+
SpotlessCLIRunner.Result result = cliRunner()
75+
.withTargets("*.java")
76+
.withStep(PalantirJavaFormat.class)
77+
.run();
78+
79+
selfie().expectResource("Java.java").toMatchDisk();
80+
}
81+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
╔═ itFormatsJavadoc ═╗
2+
import mylib.Unused;
3+
import mylib.UsedA;
4+
import mylib.UsedB;
5+
6+
/**
7+
* This is a test class with a long unformatted JavaDoc description. Lorem ipsum dolor sit amet, consectetur adipiscing
8+
* elit. Vestibulum pulvinar condimentum elit, eget mollis magna sollicitudin in. Aenean pharetra nunc nec luctus
9+
* consequat. Donec nec tincidunt quam, in auctor ipsum. Nam in sem orci. Maecenas interdum posuere orci a semper. Cras
10+
* vulputate blandit metus, nec semper urna porttitor at. Praesent velit turpis, consequat in cursus eget, posuere eget
11+
* magna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque ante eros, sagittis sed tempus nec,
12+
* rutrum ac arcu. Sed porttitor quam at enim commodo dictum. Sed fringilla tincidunt ex in aliquet.
13+
*
14+
* @author https://www.lipsum.com/
15+
* @since 0.0.2
16+
*/
17+
public class Java {
18+
/**
19+
* A very simple method that I really like a lot?
20+
*
21+
* <p>Care for more details?
22+
*
23+
* <ul>
24+
* <li>Too
25+
* <li>bad
26+
* <li>I
27+
* <li>don't
28+
* <li>have
29+
* <li>any
30+
* </ul>
31+
*
32+
* @param args Useless args, but see {@link Unused}, perhaps even {@link UsedA} or even {@link UsedB b }?
33+
*/
34+
public static void main(String[] args) {
35+
System.out.println(
36+
"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.");
37+
UsedB.someMethod();
38+
UsedA.someMethod();
39+
}
40+
}
41+
42+
╔═ itFormatsTextBlocks ═╗
43+
import mylib.UsedA;
44+
import mylib.UsedB;
45+
46+
public class Java {
47+
public static void main(String[] args) {
48+
var a = """
49+
Howdy
50+
Partner!
51+
""";
52+
System.out.println(a);
53+
UsedB.someMethod();
54+
UsedA.someMethod();
55+
}
56+
}
57+
58+
╔═ itFormatsWithDefaultOptions ═╗
59+
import mylib.Unused;
60+
import mylib.UsedA;
61+
import mylib.UsedB;
62+
63+
/** This is a test class with a long unformatted JavaDoc description. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pulvinar condimentum elit, eget mollis magna sollicitudin in. Aenean pharetra nunc nec luctus consequat. Donec nec tincidunt quam, in auctor ipsum. Nam in sem orci. Maecenas interdum posuere orci a semper. Cras vulputate blandit metus, nec semper urna porttitor at. Praesent velit turpis, consequat in cursus eget, posuere eget magna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque ante eros, sagittis sed tempus nec, rutrum ac arcu. Sed porttitor quam at enim commodo dictum. Sed fringilla tincidunt ex in aliquet.
64+
* @author https://www.lipsum.com/
65+
* @since 0.0.2
66+
*/
67+
public class Java {
68+
/**
69+
* A very simple method that I
70+
* really
71+
* like
72+
* a lot?
73+
*
74+
* Care for more details? <ul><li>Too</li><li>bad</li>
75+
* <li>I</li><li>don't</li><li>have</li>
76+
* <li>any</li>
77+
* </ul>
78+
*
79+
* @param args Useless args, but
80+
* see {@link Unused}, perhaps even {@link UsedA} or even {@link UsedB b }?
81+
*/
82+
public static void main(String[] args) {
83+
System.out.println(
84+
"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.");
85+
UsedB.someMethod();
86+
UsedA.someMethod();
87+
}
88+
}
89+
90+
╔═ itFormatsWithSelectedStyle/AOSP ═╗
91+
import mylib.Unused;
92+
import mylib.UsedA;
93+
import mylib.UsedB;
94+
95+
/** This is a test class with a long unformatted JavaDoc description. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pulvinar condimentum elit, eget mollis magna sollicitudin in. Aenean pharetra nunc nec luctus consequat. Donec nec tincidunt quam, in auctor ipsum. Nam in sem orci. Maecenas interdum posuere orci a semper. Cras vulputate blandit metus, nec semper urna porttitor at. Praesent velit turpis, consequat in cursus eget, posuere eget magna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque ante eros, sagittis sed tempus nec, rutrum ac arcu. Sed porttitor quam at enim commodo dictum. Sed fringilla tincidunt ex in aliquet.
96+
* @author https://www.lipsum.com/
97+
* @since 0.0.2
98+
*/
99+
public class Java {
100+
/**
101+
* A very simple method that I
102+
* really
103+
* like
104+
* a lot?
105+
*
106+
* Care for more details? <ul><li>Too</li><li>bad</li>
107+
* <li>I</li><li>don't</li><li>have</li>
108+
* <li>any</li>
109+
* </ul>
110+
*
111+
* @param args Useless args, but
112+
* see {@link Unused}, perhaps even {@link UsedA} or even {@link UsedB b }?
113+
*/
114+
public static void main(String[] args) {
115+
System.out.println(
116+
"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.");
117+
UsedB.someMethod();
118+
UsedA.someMethod();
119+
}
120+
}
121+
122+
╔═ itFormatsWithSelectedStyle/GOOGLE ═╗
123+
import mylib.Unused;
124+
import mylib.UsedA;
125+
import mylib.UsedB;
126+
127+
/** This is a test class with a long unformatted JavaDoc description. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pulvinar condimentum elit, eget mollis magna sollicitudin in. Aenean pharetra nunc nec luctus consequat. Donec nec tincidunt quam, in auctor ipsum. Nam in sem orci. Maecenas interdum posuere orci a semper. Cras vulputate blandit metus, nec semper urna porttitor at. Praesent velit turpis, consequat in cursus eget, posuere eget magna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque ante eros, sagittis sed tempus nec, rutrum ac arcu. Sed porttitor quam at enim commodo dictum. Sed fringilla tincidunt ex in aliquet.
128+
* @author https://www.lipsum.com/
129+
* @since 0.0.2
130+
*/
131+
public class Java {
132+
/**
133+
* A very simple method that I
134+
* really
135+
* like
136+
* a lot?
137+
*
138+
* Care for more details? <ul><li>Too</li><li>bad</li>
139+
* <li>I</li><li>don't</li><li>have</li>
140+
* <li>any</li>
141+
* </ul>
142+
*
143+
* @param args Useless args, but
144+
* see {@link Unused}, perhaps even {@link UsedA} or even {@link UsedB b }?
145+
*/
146+
public static void main(String[] args) {
147+
System.out.println(
148+
"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.");
149+
UsedB.someMethod();
150+
UsedA.someMethod();
151+
}
152+
}
153+
154+
╔═ itFormatsWithSelectedStyle/PALANTIR ═╗
155+
import mylib.Unused;
156+
import mylib.UsedA;
157+
import mylib.UsedB;
158+
159+
/** This is a test class with a long unformatted JavaDoc description. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pulvinar condimentum elit, eget mollis magna sollicitudin in. Aenean pharetra nunc nec luctus consequat. Donec nec tincidunt quam, in auctor ipsum. Nam in sem orci. Maecenas interdum posuere orci a semper. Cras vulputate blandit metus, nec semper urna porttitor at. Praesent velit turpis, consequat in cursus eget, posuere eget magna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque ante eros, sagittis sed tempus nec, rutrum ac arcu. Sed porttitor quam at enim commodo dictum. Sed fringilla tincidunt ex in aliquet.
160+
* @author https://www.lipsum.com/
161+
* @since 0.0.2
162+
*/
163+
public class Java {
164+
/**
165+
* A very simple method that I
166+
* really
167+
* like
168+
* a lot?
169+
*
170+
* Care for more details? <ul><li>Too</li><li>bad</li>
171+
* <li>I</li><li>don't</li><li>have</li>
172+
* <li>any</li>
173+
* </ul>
174+
*
175+
* @param args Useless args, but
176+
* see {@link Unused}, perhaps even {@link UsedA} or even {@link UsedB b }?
177+
*/
178+
public static void main(String[] args) {
179+
System.out.println(
180+
"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.");
181+
UsedB.someMethod();
182+
UsedA.someMethod();
183+
}
184+
}
185+
186+
╔═ [end of file] ═╗

build-logic/src/main/groovy/com/diffplug/spotless/cli/picocli/usage/DocumentedUsages.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ enum DocumentedUsages {
55
MAIN(""),
66
GOOGLE_JAVA_FORMAT(),
77
LICENSE_HEADER(),
8+
PALANTIR_JAVA_FORMAT(),
89
PRETTIER()
910

1011
private final String fileName

gradle/libs.versions.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ durian = "1.2.0"
66
junit = "5.8.1"
77
mockito = "5.17.0"
88
native-include-googleJavaFormat = "1.24.0"
9+
native-include-palantirJavaFormat = "2.61.0"
910
picocli = "4.7.6"
1011
selfie = "2.5.1"
1112
slf4j = "2.0.17"
@@ -22,6 +23,7 @@ junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.re
2223
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" }
2324
mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" }
2425
native-include-googleJavaFormat = { module = "com.google.googlejavaformat:google-java-format", version.ref = "native-include-googleJavaFormat" }
26+
native-include-palantirJavaFormat = { module = "com.palantir.javaformat:palantir-java-format", version.ref = "native-include-palantirJavaFormat" }
2527
picocli = { module = "info.picocli:picocli", version.ref = "picocli" }
2628
picocli-codegen = { module = "info.picocli:picocli-codegen", version.ref = "picocli" }
2729
selfie = { module = "com.diffplug.selfie:selfie-runner-junit5", version.ref = "selfie" }
@@ -33,7 +35,8 @@ spotless-lib-extra = { module = "com.diffplug.spotless:spotless-lib-extra", vers
3335
[bundles]
3436
durian-libs = ["durian-core", "durian-io", "durian-collect"]
3537
native-includes = [
36-
"native-include-googleJavaFormat"
38+
"native-include-googleJavaFormat",
39+
"native-include-palantirJavaFormat",
3740
]
3841
spotless-libs = ["spotless-lib", "spotless-lib-extra"]
3942
test-libs = ["assertj-core", "junit-jupiter-api", "selfie", "mockito"]

0 commit comments

Comments
 (0)