22
22
import java .nio .charset .StandardCharsets ;
23
23
import java .nio .file .Files ;
24
24
import java .nio .file .Path ;
25
+ import java .util .AbstractMap ;
25
26
import java .util .List ;
26
27
import java .util .ListIterator ;
28
+ import java .util .Map ;
27
29
import java .util .Objects ;
28
30
29
31
import org .eclipse .jgit .diff .DiffFormatter ;
32
+ import org .eclipse .jgit .diff .Edit ;
30
33
import org .eclipse .jgit .diff .EditList ;
31
34
import org .eclipse .jgit .diff .MyersDiff ;
32
35
import org .eclipse .jgit .diff .RawText ;
@@ -55,10 +58,10 @@ interface CleanProvider {
55
58
String getFormatted (File file , String rawUnix );
56
59
}
57
60
58
- public static class CleanProviderFormatter implements CleanProvider {
61
+ private static class CleanProviderFormatter implements CleanProvider {
59
62
private final Formatter formatter ;
60
63
61
- public CleanProviderFormatter (Formatter formatter ) {
64
+ CleanProviderFormatter (Formatter formatter ) {
62
65
this .formatter = Objects .requireNonNull (formatter );
63
66
}
64
67
@@ -234,10 +237,19 @@ private void addIntendedLine(String indent, String line) {
234
237
* sequence (\n, \r, \r\n).
235
238
*/
236
239
private String diff (File file ) throws IOException {
237
- return diff (formatter , file );
240
+ return diff (formatter , file ). getKey () ;
238
241
}
239
242
240
- public static String diff (CleanProvider formatter , File file ) throws IOException {
243
+ /**
244
+ * Returns a map entry with key being a git-style diff between the contents of the given file and what those contents would
245
+ * look like if formatted using the given formatter. Does not end with any newline
246
+ * sequence (\n, \r, \r\n). The value of the map entry is the line where the first difference occurred.
247
+ */
248
+ public static Map .Entry <String , Integer > diff (Formatter formatter , File file ) throws IOException {
249
+ return diff (new CleanProviderFormatter (formatter ), file );
250
+ }
251
+
252
+ private static Map .Entry <String , Integer > diff (CleanProvider formatter , File file ) throws IOException {
241
253
String raw = new String (Files .readAllBytes (file .toPath ()), formatter .getEncoding ());
242
254
String rawUnix = LineEnding .toUnix (raw );
243
255
String formatted = formatter .getFormatted (file , rawUnix );
@@ -252,13 +264,13 @@ public static String diff(CleanProvider formatter, File file) throws IOException
252
264
}
253
265
254
266
/**
255
- * Returns a git-style diff between the two unix strings.
267
+ * Returns a map entry with key being a git-style diff between the two unix strings and value being the line of the first difference (in the dirty string)
256
268
* <p>
257
269
* Output has no trailing newlines.
258
270
* <p>
259
271
* Boolean args determine whether whitespace or line endings will be visible.
260
272
*/
261
- private static String diffWhitespaceLineEndings (String dirty , String clean , boolean whitespace , boolean lineEndings ) throws IOException {
273
+ private static Map . Entry < String , Integer > diffWhitespaceLineEndings (String dirty , String clean , boolean whitespace , boolean lineEndings ) throws IOException {
262
274
dirty = visibleWhitespaceLineEndings (dirty , whitespace , lineEndings );
263
275
clean = visibleWhitespaceLineEndings (clean , whitespace , lineEndings );
264
276
@@ -275,7 +287,11 @@ private static String diffWhitespaceLineEndings(String dirty, String clean, bool
275
287
276
288
// we don't need the diff to show this, since we display newlines ourselves
277
289
formatted = formatted .replace ("\\ No newline at end of file\n " , "" );
278
- return NEWLINE_MATCHER .trimTrailingFrom (formatted );
290
+ return new AbstractMap .SimpleEntry <>(NEWLINE_MATCHER .trimTrailingFrom (formatted ), getLineOfFirstDifference (edits ));
291
+ }
292
+
293
+ private static int getLineOfFirstDifference (EditList edits ) {
294
+ return edits .stream ().mapToInt (Edit ::getBeginA ).min ().getAsInt ();
279
295
}
280
296
281
297
private static final CharMatcher NEWLINE_MATCHER = CharMatcher .is ('\n' );
0 commit comments