1
1
/*
2
- * Copyright 2016-2021 DiffPlug
2
+ * Copyright 2016-2023 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -143,18 +143,14 @@ public String getMessage() {
143
143
Objects .requireNonNull (runToFix , "runToFix" );
144
144
Objects .requireNonNull (formatter , "formatter" );
145
145
Objects .requireNonNull (problemFiles , "problemFiles" );
146
- DiffMessageFormatter diffFormater = new DiffMessageFormatter (this );
146
+ DiffMessageFormatter diffFormater = new DiffMessageFormatter (formatter , problemFiles );
147
147
return "The following files had format violations:\n "
148
148
+ diffFormater .buffer
149
149
+ runToFix ;
150
150
} catch (IOException e ) {
151
151
throw Errors .asRuntime (e );
152
152
}
153
153
}
154
-
155
- String relativePath (File file ) {
156
- return formatter .getRootDir ().relativize (file .toPath ()).toString ();
157
- }
158
154
}
159
155
160
156
private static final int MAX_CHECK_MESSAGE_LINES = 50 ;
@@ -163,25 +159,32 @@ String relativePath(File file) {
163
159
private final StringBuilder buffer = new StringBuilder (MAX_CHECK_MESSAGE_LINES * 64 );
164
160
private int numLines = 0 ;
165
161
166
- private DiffMessageFormatter (Builder builder ) throws IOException {
167
- ListIterator <File > problemIter = builder .problemFiles .listIterator ();
162
+ private final CleanProvider formatter ;
163
+
164
+ private DiffMessageFormatter (CleanProvider formatter , List <File > problemFiles ) throws IOException {
165
+ this .formatter = Objects .requireNonNull (formatter , "formatter" );
166
+ ListIterator <File > problemIter = problemFiles .listIterator ();
168
167
while (problemIter .hasNext () && numLines < MAX_CHECK_MESSAGE_LINES ) {
169
168
File file = problemIter .next ();
170
- addFile (builder . relativePath (file ) + "\n " + DiffMessageFormatter . diff (builder , file ));
169
+ addFile (relativePath (file ) + "\n " + diff (file ));
171
170
}
172
171
if (problemIter .hasNext ()) {
173
- int remainingFiles = builder . problemFiles .size () - problemIter .nextIndex ();
172
+ int remainingFiles = problemFiles .size () - problemIter .nextIndex ();
174
173
if (remainingFiles >= MAX_FILES_TO_LIST ) {
175
174
buffer .append ("Violations also present in " ).append (remainingFiles ).append (" other files.\n " );
176
175
} else {
177
176
buffer .append ("Violations also present in:\n " );
178
177
while (problemIter .hasNext ()) {
179
- addIntendedLine (NORMAL_INDENT , builder . relativePath (problemIter .next ()));
178
+ addIntendedLine (NORMAL_INDENT , relativePath (problemIter .next ()));
180
179
}
181
180
}
182
181
}
183
182
}
184
183
184
+ private String relativePath (File file ) {
185
+ return formatter .getRootDir ().relativize (file .toPath ()).toString ();
186
+ }
187
+
185
188
private static final int MIN_LINES_PER_FILE = 4 ;
186
189
private static final Splitter NEWLINE_SPLITTER = Splitter .on ('\n' );
187
190
@@ -230,10 +233,10 @@ private void addIntendedLine(String indent, String line) {
230
233
* look like if formatted using the given formatter. Does not end with any newline
231
234
* sequence (\n, \r, \r\n).
232
235
*/
233
- private static String diff (Builder builder , File file ) throws IOException {
234
- String raw = new String (Files .readAllBytes (file .toPath ()), builder . formatter .getEncoding ());
236
+ private String diff (File file ) throws IOException {
237
+ String raw = new String (Files .readAllBytes (file .toPath ()), formatter .getEncoding ());
235
238
String rawUnix = LineEnding .toUnix (raw );
236
- String formatted = builder . formatter .getFormatted (file , rawUnix );
239
+ String formatted = formatter .getFormatted (file , rawUnix );
237
240
String formattedUnix = LineEnding .toUnix (formatted );
238
241
239
242
if (rawUnix .equals (formattedUnix )) {
0 commit comments