18
18
import java .io .ByteArrayOutputStream ;
19
19
import java .io .File ;
20
20
import java .io .IOException ;
21
+ import java .nio .charset .Charset ;
21
22
import java .nio .charset .StandardCharsets ;
22
23
import java .nio .file .Files ;
24
+ import java .nio .file .Path ;
23
25
import java .util .List ;
24
26
import java .util .ListIterator ;
25
27
import java .util .Objects ;
@@ -44,11 +46,44 @@ public static Builder builder() {
44
46
return new Builder ();
45
47
}
46
48
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
+
47
82
public static class Builder {
48
83
private Builder () {}
49
84
50
85
private String runToFix ;
51
- private Formatter formatter ;
86
+ private CleanProvider formatter ;
52
87
private List <File > problemFiles ;
53
88
54
89
/** "Run 'gradlew spotlessApply' to fix these violations." */
@@ -58,7 +93,7 @@ public Builder runToFix(String runToFix) {
58
93
}
59
94
60
95
public Builder formatter (Formatter formatter ) {
61
- this .formatter = Objects . requireNonNull (formatter );
96
+ this .formatter = new CleanProviderFormatter (formatter );
62
97
return this ;
63
98
}
64
99
@@ -164,11 +199,11 @@ private void addIntendedLine(String indent, String line) {
164
199
private static String diff (Builder builder , File file ) throws IOException {
165
200
String raw = new String (Files .readAllBytes (file .toPath ()), builder .formatter .getEncoding ());
166
201
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 );
168
204
169
205
if (rawUnix .equals (formattedUnix )) {
170
206
// the formatting is fine, so it's a line-ending issue
171
- String formatted = builder .formatter .computeLineEndings (formattedUnix , file );
172
207
return diffWhitespaceLineEndings (raw , formatted , false , true );
173
208
} else {
174
209
return diffWhitespaceLineEndings (rawUnix , formattedUnix , true , false );
0 commit comments