Skip to content

Commit 7d620c8

Browse files
committed
1 parent 16cd9c6 commit 7d620c8

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,36 +63,38 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
6363
List<AbstractDelta<String>> patchDeltas = new ArrayList<>(
6464
file.getPatch().getDeltas());
6565

66-
List<AbstractDelta<String>> deltas = new ArrayList<>();
67-
68-
AbstractDelta<String> delta = patchDeltas.get(0);
69-
deltas.add(delta); // add the first Delta to the current set
70-
// if there's more than 1 Delta, we may need to output them together
71-
if (patchDeltas.size() > 1) {
72-
for (int i = 1; i < patchDeltas.size(); i++) {
73-
int position = delta.getSource().getPosition();
74-
75-
// Check if the next Delta is too close to the current
76-
// position.
77-
// And if it is, add it to the current set
78-
AbstractDelta<String> nextDelta = patchDeltas.get(i);
79-
if ((position + delta.getSource().size() + contextSize) >= (nextDelta
80-
.getSource().getPosition() - contextSize)) {
81-
deltas.add(nextDelta);
82-
} else {
83-
// if it isn't, output the current set,
84-
// then create a new set and add the current Delta to
85-
// it.
86-
processDeltas(writer, originalLines, deltas, contextSize);
87-
deltas.clear();
88-
deltas.add(nextDelta);
66+
if (!patchDeltas.isEmpty()) {
67+
List<AbstractDelta<String>> deltas = new ArrayList<>();
68+
69+
AbstractDelta<String> delta = patchDeltas.get(0);
70+
deltas.add(delta); // add the first Delta to the current set
71+
// if there's more than 1 Delta, we may need to output them together
72+
if (patchDeltas.size() > 1) {
73+
for (int i = 1; i < patchDeltas.size(); i++) {
74+
int position = delta.getSource().getPosition();
75+
76+
// Check if the next Delta is too close to the current
77+
// position.
78+
// And if it is, add it to the current set
79+
AbstractDelta<String> nextDelta = patchDeltas.get(i);
80+
if ((position + delta.getSource().size() + contextSize) >= (nextDelta
81+
.getSource().getPosition() - contextSize)) {
82+
deltas.add(nextDelta);
83+
} else {
84+
// if it isn't, output the current set,
85+
// then create a new set and add the current Delta to
86+
// it.
87+
processDeltas(writer, originalLines, deltas, contextSize);
88+
deltas.clear();
89+
deltas.add(nextDelta);
90+
}
91+
delta = nextDelta;
8992
}
90-
delta = nextDelta;
91-
}
9293

94+
}
95+
// don't forget to process the last set of Deltas
96+
processDeltas(writer, originalLines, deltas, contextSize);
9397
}
94-
// don't forget to process the last set of Deltas
95-
processDeltas(writer, originalLines, deltas, contextSize);
9698

9799
}
98100
if (diff.getTail() != null) {

0 commit comments

Comments
 (0)