Skip to content

Commit 8977d9e

Browse files
committed
Make FreshMarkStep round-trippable
1 parent efc15b4 commit 8977d9e

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

lib/src/main/java/com/diffplug/spotless/markdown/FreshMarkStep.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 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.
@@ -35,12 +35,11 @@
3535
import com.diffplug.spotless.JarState;
3636
import com.diffplug.spotless.Jvm;
3737
import com.diffplug.spotless.Provisioner;
38-
import com.diffplug.spotless.ThrowingEx.Supplier;
38+
import com.diffplug.spotless.RoundedStep;
3939

4040
/** A step for <a href="https://github.com/diffplug/freshmark">FreshMark</a>. */
41-
public class FreshMarkStep {
42-
// prevent direct instantiation
43-
private FreshMarkStep() {}
41+
public class FreshMarkStep implements RoundedStep {
42+
private static final long serialVersionUID = 1L;
4443

4544
private static final String DEFAULT_VERSION = "1.3.1";
4645
private static final String NAME = "freshmark";
@@ -52,13 +51,21 @@ private FreshMarkStep() {}
5251
private static final String FORMATTER_CLASS = "com.diffplug.freshmark.FreshMark";
5352
private static final String FORMATTER_METHOD = "compile";
5453

54+
private final JarState.Promised jarState;
55+
private final Map<String, ?> properties;
56+
57+
private FreshMarkStep(JarState.Promised jarState, Map<String, ?> properties) {
58+
this.jarState = jarState;
59+
this.properties = properties;
60+
}
61+
5562
/** Creates a formatter step for the given version and settings file. */
56-
public static FormatterStep create(Supplier<Map<String, ?>> properties, Provisioner provisioner) {
63+
public static FormatterStep create(Map<String, ?> properties, Provisioner provisioner) {
5764
return create(defaultVersion(), properties, provisioner);
5865
}
5966

6067
/** Creates a formatter step for the given version and settings file. */
61-
public static FormatterStep create(String version, Supplier<Map<String, ?>> properties, Provisioner provisioner) {
68+
public static FormatterStep create(String version, Map<String, ?> properties, Provisioner provisioner) {
6269
Objects.requireNonNull(version, "version");
6370
Objects.requireNonNull(properties, "properties");
6471
Objects.requireNonNull(provisioner, "provisioner");
@@ -70,21 +77,25 @@ public static FormatterStep create(String version, Supplier<Map<String, ?>> prop
7077
mavenCoordinates.add(NASHORN_MAVEN_COORDINATE + NASHORN_VERSION);
7178
}
7279

73-
return FormatterStep.createLazy(NAME,
74-
() -> new State(JarState.from(mavenCoordinates, provisioner), properties.get()),
80+
return FormatterStep.create(NAME,
81+
new FreshMarkStep(JarState.promise(() -> JarState.from(mavenCoordinates, provisioner)), properties),
82+
FreshMarkStep::equalityState,
7583
State::createFormat);
7684
}
7785

7886
public static String defaultVersion() {
7987
return DEFAULT_VERSION;
8088
}
8189

90+
private State equalityState() throws Exception {
91+
return new State(jarState.get(), properties);
92+
}
93+
8294
private static class State implements Serializable {
8395
private static final long serialVersionUID = 1L;
8496

85-
/** The jar that contains the formatter. */
86-
final JarState jarState;
87-
final NavigableMap<String, ?> properties;
97+
private final JarState jarState;
98+
private final NavigableMap<String, ?> properties;
8899

89100
State(JarState jarState, Map<String, ?> properties) {
90101
this.jarState = jarState;

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

Lines changed: 6 additions & 8 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.
@@ -38,13 +38,11 @@ public class FreshMarkExtension extends FormatExtension {
3838
@Inject
3939
public FreshMarkExtension(SpotlessExtension spotless) {
4040
super(spotless);
41-
addStep(FreshMarkStep.create(() -> {
42-
Map<String, Object> map = new HashMap<>();
43-
for (Action<Map<String, Object>> action : propertyActions) {
44-
action.execute(map);
45-
}
46-
return map;
47-
}, provisioner()));
41+
Map<String, Object> map = new HashMap<>();
42+
for (Action<Map<String, Object>> action : propertyActions) {
43+
action.execute(map);
44+
}
45+
addStep(FreshMarkStep.create(map, provisioner()));
4846
}
4947

5048
public void properties(Action<Map<String, Object>> action) {

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

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

testlib/src/test/java/com/diffplug/spotless/markdown/FreshMarkStepTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 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.
@@ -27,19 +27,19 @@
2727

2828
class FreshMarkStepTest {
2929
@Test
30-
void behavior() throws Exception {
30+
void behavior() {
3131
HashMap<String, String> map = new HashMap<>();
3232
map.put("lib", "MyLib");
3333
map.put("author", "Me");
34-
StepHarness.forStep(FreshMarkStep.create(() -> map, TestProvisioner.mavenCentral()))
34+
StepHarness.forStep(FreshMarkStep.create(map, TestProvisioner.mavenCentral()))
3535
.testResource("freshmark/FreshMarkUnformatted.test", "freshmark/FreshMarkFormatted.test");
3636
}
3737

3838
@Test
39-
void equality() throws Exception {
39+
void equality() {
4040
new SerializableEqualityTester() {
4141
String version = "1.3.1";
42-
Map<String, Object> props = new HashMap<>();
42+
final Map<String, Object> props = new HashMap<>();
4343

4444
@Override
4545
protected void setupTest(API api) {
@@ -57,7 +57,7 @@ protected void setupTest(API api) {
5757
protected FormatterStep create() {
5858
String finalVersion = this.version;
5959
Map<String, ?> finalProps = new HashMap<>(props);
60-
return FreshMarkStep.create(finalVersion, () -> finalProps, TestProvisioner.mavenCentral());
60+
return FreshMarkStep.create(finalVersion, finalProps, TestProvisioner.mavenCentral());
6161
}
6262
}.testEquals();
6363
}

0 commit comments

Comments
 (0)