Skip to content

Commit a704a68

Browse files
committed
Make Antlr4FormatterStep round-trippable
1 parent fec005d commit a704a68

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

lib/src/main/java/com/diffplug/spotless/antlr4/Antlr4FormatterStep.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 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.
@@ -15,44 +15,54 @@
1515
*/
1616
package com.diffplug.spotless.antlr4;
1717

18-
import java.io.IOException;
1918
import java.io.Serializable;
2019
import java.lang.reflect.InvocationTargetException;
2120
import java.lang.reflect.Method;
2221

23-
import com.diffplug.spotless.*;
24-
25-
public class Antlr4FormatterStep {
22+
import com.diffplug.spotless.FormatterFunc;
23+
import com.diffplug.spotless.FormatterStep;
24+
import com.diffplug.spotless.JarState;
25+
import com.diffplug.spotless.Provisioner;
26+
import com.diffplug.spotless.RoundedStep;
27+
import com.diffplug.spotless.ThrowingEx;
2628

29+
public class Antlr4FormatterStep implements RoundedStep {
30+
private static final long serialVersionUID = 1L;
31+
private static final String MAVEN_COORDINATE = "com.khubla.antlr4formatter:antlr4-formatter:";
32+
private static final String DEFAULT_VERSION = "1.2.1";
2733
public static final String NAME = "antlr4Formatter";
2834

29-
private Antlr4FormatterStep() {}
35+
private final JarState.Promised jarState;
3036

31-
private static final String MAVEN_COORDINATE = "com.khubla.antlr4formatter:antlr4-formatter:";
32-
private static final String DEFAULT_VERSION = "1.2.1";
37+
private Antlr4FormatterStep(JarState.Promised jarState) {
38+
this.jarState = jarState;
39+
}
3340

3441
public static FormatterStep create(Provisioner provisioner) {
3542
return create(defaultVersion(), provisioner);
3643
}
3744

3845
public static FormatterStep create(String version, Provisioner provisioner) {
39-
return FormatterStep.createLazy(NAME, () -> new State(version, provisioner), State::createFormat);
46+
return FormatterStep.create(NAME,
47+
new Antlr4FormatterStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + version, provisioner))),
48+
Antlr4FormatterStep::equalityState,
49+
State::createFormat);
4050
}
4151

4252
public static String defaultVersion() {
4353
return DEFAULT_VERSION;
4454
}
4555

46-
static final class State implements Serializable {
47-
private static final long serialVersionUID = 1L;
56+
private State equalityState() {
57+
return new State(jarState.get());
58+
}
4859

49-
/**
50-
* The jar that contains the formatter.
51-
*/
52-
final JarState jarState;
60+
private static final class State implements Serializable {
61+
private static final long serialVersionUID = 1L;
62+
private final JarState jarState;
5363

54-
State(String version, Provisioner provisioner) throws IOException {
55-
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
64+
State(JarState jarState) {
65+
this.jarState = jarState;
5666
}
5767

5868
FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException {

testlib/src/main/java/com/diffplug/spotless/StepHarnessBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected StepHarnessBase(Formatter formatter) {
5050
} else if (onlyStepName.equals("fence")) {
5151
supportsRoundTrip = true;
5252
} else if (Set.of("black", "buf", "clang", "ktlint", "ktfmt", "scalafmt", "palantir-java-format", "google-java-format",
53-
"removeUnusedImports", "cleanthat", "No line break between type annotation and type",
53+
"removeUnusedImports", "cleanthat", "No line break between type annotation and type", "antlr4Formatter",
5454
"gson", "jacksonJson", "apply-json-patch", "jsonSimple", "sortPom", "jacksonYaml", "gherkinUtils",
5555
"importOrder", "Remove unnecessary semicolons").contains(onlyStepName)) {
5656
supportsRoundTrip = true;

0 commit comments

Comments
 (0)