Skip to content

Commit 0fd38db

Browse files
committed
fixes #129 - added the possibility to skip delta decompression
1 parent 3663cb5 commit 0fd38db

File tree

2 files changed

+106
-5
lines changed

2 files changed

+106
-5
lines changed

java-diff-utils/src/main/java/com/github/difflib/text/DiffRowGenerator.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
173173

174174
private final boolean showInlineDiffs;
175175
private final boolean replaceOriginalLinefeedInChangesWithSpaces;
176+
private final boolean decompressDeltas;
176177

177178
private DiffRowGenerator(Builder builder) {
178179
showInlineDiffs = builder.showInlineDiffs;
@@ -182,6 +183,7 @@ private DiffRowGenerator(Builder builder) {
182183
columnWidth = builder.columnWidth;
183184
mergeOriginalRevised = builder.mergeOriginalRevised;
184185
inlineDiffSplitter = builder.inlineDiffSplitter;
186+
decompressDeltas = builder.decompressDeltas;
185187

186188
if (builder.equalizer != null) {
187189
equalizer = builder.equalizer;
@@ -225,8 +227,14 @@ public List<DiffRow> generateDiffRows(final List<String> original, Patch<String>
225227
int endPos = 0;
226228
final List<AbstractDelta<String>> deltaList = patch.getDeltas();
227229

228-
for (AbstractDelta<String> originalDelta : deltaList) {
229-
for (AbstractDelta<String> delta : decompressDeltas(originalDelta)) {
230+
if (decompressDeltas) {
231+
for (AbstractDelta<String> originalDelta : deltaList) {
232+
for (AbstractDelta<String> delta : decompressDeltas(originalDelta)) {
233+
endPos = transformDeltaIntoDiffRow(original, endPos, diffRows, delta);
234+
}
235+
}
236+
} else {
237+
for (AbstractDelta<String> delta : deltaList) {
230238
endPos = transformDeltaIntoDiffRow(original, endPos, diffRows, delta);
231239
}
232240
}
@@ -442,6 +450,7 @@ public static class Builder {
442450

443451
private boolean showInlineDiffs = false;
444452
private boolean ignoreWhiteSpaces = false;
453+
private boolean decompressDeltas = true;
445454

446455
private BiFunction<Tag, Boolean, String> oldTag
447456
= (tag, f) -> f ? "<span class=\"editOldInline\">" : "</span>";
@@ -554,7 +563,8 @@ public Builder processDiffs(Function<String, String> processDiffs) {
554563
* Set the column width of generated lines of original and revised
555564
* texts.
556565
*
557-
* @param width the width to set. Making it &lt; 0 doesn't make any sense. Default 80.
566+
* @param width the width to set. Making it &lt; 0 doesn't make any
567+
* sense. Default 80.
558568
* @return builder with config of column width
559569
*/
560570
public Builder columnWidth(int width) {
@@ -586,6 +596,19 @@ public Builder mergeOriginalRevised(boolean mergeOriginalRevised) {
586596
return this;
587597
}
588598

599+
/**
600+
* Deltas could be in a state, that would produce some unreasonable
601+
* results within an inline diff. So the deltas are decompressed into
602+
* smaller parts and rebuild. But this could result in more differences.
603+
*
604+
* @param decompressDeltas
605+
* @return
606+
*/
607+
public Builder decompressDeltas(boolean decompressDeltas) {
608+
this.decompressDeltas = decompressDeltas;
609+
return this;
610+
}
611+
589612
/**
590613
* Per default each character is separatly processed. This variant
591614
* introduces processing by word, which does not deliver in word

java-diff-utils/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ public void testCorrectChangeIssue114() throws IOException {
641641

642642
assertThat(rows).extracting(item -> item.getTag().name()).containsExactly("CHANGE", "DELETE", "EQUAL", "CHANGE", "EQUAL");
643643
}
644-
644+
645645
@Test
646646
public void testCorrectChangeIssue114_2() throws IOException {
647647
List<String> original = Arrays.asList("A", "B", "C", "D", "E");
@@ -662,7 +662,7 @@ public void testCorrectChangeIssue114_2() throws IOException {
662662
assertThat(rows).extracting(item -> item.getTag().name()).containsExactly("CHANGE", "DELETE", "EQUAL", "CHANGE", "EQUAL");
663663
assertThat(rows.get(1).toString()).isEqualTo("[DELETE,~B~,]");
664664
}
665-
665+
666666
@Test
667667
public void testIssue119WrongContextLength() throws IOException {
668668
String original = Files.lines(Paths.get("target/test-classes/com/github/difflib/text/issue_119_original.txt")).collect(joining("\n"));
@@ -683,4 +683,82 @@ public void testIssue119WrongContextLength() throws IOException {
683683
.filter(item -> item.getTag() != DiffRow.Tag.EQUAL)
684684
.forEach(System.out::println);
685685
}
686+
687+
@Test
688+
public void testIssue129WithDeltaDecompression() {
689+
List<String> lines1 = Arrays.asList(
690+
"apple1",
691+
"apple2",
692+
"apple3",
693+
"A man named Frankenstein abc to Switzerland for cookies!",
694+
"banana1",
695+
"banana2",
696+
"banana3");
697+
List<String> lines2 = Arrays.asList(
698+
"apple1",
699+
"apple2",
700+
"apple3",
701+
"A man named Frankenstein",
702+
"xyz",
703+
"to Switzerland for cookies!",
704+
"banana1",
705+
"banana2",
706+
"banana3");
707+
int[] entry = {1};
708+
String txt = DiffRowGenerator.create()
709+
.showInlineDiffs(true)
710+
.oldTag((tag, isOpening) -> isOpening ? "==old" + tag + "==>" : "<==old==")
711+
.newTag((tag, isOpening) -> isOpening ? "==new" + tag + "==>" : "<==new==")
712+
.build()
713+
.generateDiffRows(lines1, lines2)
714+
.stream()
715+
.map(row -> row.getTag().toString())
716+
.collect(joining(" "));
717+
// .forEachOrdered(row -> {
718+
// System.out.printf("%4d %-8s %-80s %-80s\n", entry[0]++,
719+
// row.getTag(), row.getOldLine(), row.getNewLine());
720+
// });
721+
722+
assertThat(txt).isEqualTo("EQUAL EQUAL EQUAL CHANGE INSERT INSERT EQUAL EQUAL EQUAL");
723+
}
724+
725+
@Test
726+
public void testIssue129SkipDeltaDecompression() {
727+
List<String> lines1 = Arrays.asList(
728+
"apple1",
729+
"apple2",
730+
"apple3",
731+
"A man named Frankenstein abc to Switzerland for cookies!",
732+
"banana1",
733+
"banana2",
734+
"banana3");
735+
List<String> lines2 = Arrays.asList(
736+
"apple1",
737+
"apple2",
738+
"apple3",
739+
"A man named Frankenstein",
740+
"xyz",
741+
"to Switzerland for cookies!",
742+
"banana1",
743+
"banana2",
744+
"banana3");
745+
int[] entry = {1};
746+
String txt =
747+
DiffRowGenerator.create()
748+
.showInlineDiffs(true)
749+
.decompressDeltas(false)
750+
.oldTag((tag, isOpening) -> isOpening ? "==old" + tag + "==>" : "<==old==")
751+
.newTag((tag, isOpening) -> isOpening ? "==new" + tag + "==>" : "<==new==")
752+
.build()
753+
.generateDiffRows(lines1, lines2)
754+
.stream()
755+
.map(row -> row.getTag().toString())
756+
.collect(joining(" "));
757+
// .forEachOrdered(row -> {
758+
// System.out.printf("%4d %-8s %-80s %-80s\n", entry[0]++,
759+
// row.getTag(), row.getOldLine(), row.getNewLine());
760+
// });
761+
762+
assertThat(txt).isEqualTo("EQUAL EQUAL EQUAL CHANGE CHANGE CHANGE EQUAL EQUAL EQUAL");
763+
}
686764
}

0 commit comments

Comments
 (0)