Skip to content

Commit 15960a8

Browse files
committed
Make ClangFormatStep serializable.
1 parent bf0d73a commit 15960a8

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

lib/src/main/java/com/diffplug/spotless/cpp/ClangFormatStep.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 DiffPlug
2+
* Copyright 2020-2023 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.
@@ -64,10 +64,10 @@ public ClangFormatStep withPathToExe(String pathToExe) {
6464
}
6565

6666
public FormatterStep create() {
67-
return FormatterStep.createLazy(name(), this::createState, State::toFunc);
67+
return FormatterStep.createLazy(name(), this::createState, RoundtripState::state, State::toFunc);
6868
}
6969

70-
private State createState() throws IOException, InterruptedException {
70+
private RoundtripState createState() throws IOException, InterruptedException {
7171
String howToInstall = "" +
7272
"You can download clang-format from https://releases.llvm.org and " +
7373
"then point Spotless to it with {@code pathToExe('/path/to/clang-format')} " +
@@ -82,7 +82,25 @@ private State createState() throws IOException, InterruptedException {
8282
.fixWrongVersion(
8383
"You can tell Spotless to use the version you already have with {@code clangFormat('{versionFound}')}" +
8484
"or you can download the currently specified version, {version}.\n" + howToInstall);
85-
return new State(this, exe);
85+
return new RoundtripState(this, exe);
86+
}
87+
88+
static class RoundtripState implements Serializable {
89+
private static final long serialVersionUID = 1L;
90+
91+
final String version;
92+
final @Nullable String style;
93+
final ForeignExe exe;
94+
95+
RoundtripState(ClangFormatStep step, ForeignExe exe) {
96+
this.version = step.version;
97+
this.style = step.style;
98+
this.exe = exe;
99+
}
100+
101+
private State state() {
102+
return new State(version, style, exe);
103+
}
86104
}
87105

88106
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
@@ -95,9 +113,9 @@ static class State implements Serializable {
95113
// used for executing
96114
private transient @Nullable List<String> args;
97115

98-
State(ClangFormatStep step, ForeignExe pathToExe) {
99-
this.version = step.version;
100-
this.style = step.style;
116+
State(String version, @Nullable String style, ForeignExe pathToExe) {
117+
this.version = version;
118+
this.style = style;
101119
this.exe = Objects.requireNonNull(pathToExe);
102120
}
103121

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Locale;
1919
import java.util.Objects;
20+
import java.util.Set;
2021

2122
import org.assertj.core.api.Assertions;
2223

@@ -48,7 +49,7 @@ protected StepHarnessBase(Formatter formatter) {
4849
supportsRoundTrip = true;
4950
} else if (onlyStepName.equals("fence")) {
5051
supportsRoundTrip = true;
51-
} else if (onlyStepName.equals("buf")) {
52+
} else if (Set.of("buf", "clang").contains(onlyStepName)) {
5253
supportsRoundTrip = true;
5354
}
5455
}

0 commit comments

Comments
 (0)