Skip to content

Commit 13f70f1

Browse files
committed
Refactor DiffMessageFormatter to prepare for formatting from a folder of clean files.
1 parent 93e3102 commit 13f70f1

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
import java.io.ByteArrayOutputStream;
1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.nio.charset.Charset;
2122
import java.nio.charset.StandardCharsets;
2223
import java.nio.file.Files;
24+
import java.nio.file.Path;
2325
import java.util.List;
2426
import java.util.ListIterator;
2527
import java.util.Objects;
@@ -44,11 +46,44 @@ public static Builder builder() {
4446
return new Builder();
4547
}
4648

49+
interface CleanProvider {
50+
51+
Path getRootDir();
52+
53+
Charset getEncoding();
54+
55+
String getFormatted(File file, String rawUnix);
56+
}
57+
58+
private static class CleanProviderFormatter implements CleanProvider {
59+
private final Formatter formatter;
60+
61+
CleanProviderFormatter(Formatter formatter) {
62+
this.formatter = Objects.requireNonNull(formatter);
63+
}
64+
65+
@Override
66+
public Path getRootDir() {
67+
return formatter.getRootDir();
68+
}
69+
70+
@Override
71+
public Charset getEncoding() {
72+
return formatter.getEncoding();
73+
}
74+
75+
@Override
76+
public String getFormatted(File file, String rawUnix) {
77+
String unix = PaddedCell.check(formatter, file, rawUnix).canonical();
78+
return formatter.computeLineEndings(unix, file);
79+
}
80+
}
81+
4782
public static class Builder {
4883
private Builder() {}
4984

5085
private String runToFix;
51-
private Formatter formatter;
86+
private CleanProvider formatter;
5287
private List<File> problemFiles;
5388

5489
/** "Run 'gradlew spotlessApply' to fix these violations." */
@@ -58,7 +93,7 @@ public Builder runToFix(String runToFix) {
5893
}
5994

6095
public Builder formatter(Formatter formatter) {
61-
this.formatter = Objects.requireNonNull(formatter);
96+
this.formatter = new CleanProviderFormatter(formatter);
6297
return this;
6398
}
6499

@@ -164,11 +199,11 @@ private void addIntendedLine(String indent, String line) {
164199
private static String diff(Builder builder, File file) throws IOException {
165200
String raw = new String(Files.readAllBytes(file.toPath()), builder.formatter.getEncoding());
166201
String rawUnix = LineEnding.toUnix(raw);
167-
String formattedUnix = PaddedCell.check(builder.formatter, file, rawUnix).canonical();
202+
String formatted = builder.formatter.getFormatted(file, rawUnix);
203+
String formattedUnix = LineEnding.toUnix(formatted);
168204

169205
if (rawUnix.equals(formattedUnix)) {
170206
// the formatting is fine, so it's a line-ending issue
171-
String formatted = builder.formatter.computeLineEndings(formattedUnix, file);
172207
return diffWhitespaceLineEndings(raw, formatted, false, true);
173208
} else {
174209
return diffWhitespaceLineEndings(rawUnix, formattedUnix, true, false);

0 commit comments

Comments
 (0)