Skip to content

Commit a3cad7b

Browse files
committed
Add a DiffMessageFormatter which works on a folder of clean files.
1 parent 13f70f1 commit a3cad7b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ public String getFormatted(File file, String rawUnix) {
7979
}
8080
}
8181

82+
private static class CleanProviderFolder implements CleanProvider {
83+
private final Path rootDir;
84+
private final Path cleanDir;
85+
private final Charset encoding;
86+
87+
CleanProviderFolder(Path rootDir, Path cleanDir, String encoding) {
88+
this.rootDir = rootDir;
89+
this.cleanDir = cleanDir;
90+
this.encoding = Charset.forName(encoding);
91+
}
92+
93+
@Override
94+
public Path getRootDir() {
95+
return rootDir;
96+
}
97+
98+
@Override
99+
public Charset getEncoding() {
100+
return encoding;
101+
}
102+
103+
@Override
104+
public String getFormatted(File file, String rawUnix) {
105+
Path relative = rootDir.relativize(file.toPath());
106+
Path clean = cleanDir.resolve(rootDir.relativize(file.toPath()));
107+
byte[] content = Errors.rethrow().get(() -> Files.readAllBytes(clean));
108+
return new String(content, encoding);
109+
}
110+
}
111+
82112
public static class Builder {
83113
private Builder() {}
84114

@@ -97,6 +127,11 @@ public Builder formatter(Formatter formatter) {
97127
return this;
98128
}
99129

130+
public Builder formatterFolder(Path rootDir, Path cleanDir, String encoding) {
131+
this.formatter = new CleanProviderFolder(rootDir, cleanDir, encoding);
132+
return this;
133+
}
134+
100135
public Builder problemFiles(List<File> problemFiles) {
101136
this.problemFiles = Objects.requireNonNull(problemFiles);
102137
Preconditions.checkArgument(!problemFiles.isEmpty(), "cannot be empty");

0 commit comments

Comments
 (0)