fixes #129 - added the possibility to skip delta decompression

This commit is contained in:
Tobias Warneke
2021-10-21 23:17:04 +02:00
parent 3663cb5d87
commit 0fd38db8ae
2 changed files with 106 additions and 5 deletions

View File

@@ -173,6 +173,7 @@ public final class DiffRowGenerator {
private final boolean showInlineDiffs;
private final boolean replaceOriginalLinefeedInChangesWithSpaces;
private final boolean decompressDeltas;
private DiffRowGenerator(Builder builder) {
showInlineDiffs = builder.showInlineDiffs;
@@ -182,6 +183,7 @@ public final class DiffRowGenerator {
columnWidth = builder.columnWidth;
mergeOriginalRevised = builder.mergeOriginalRevised;
inlineDiffSplitter = builder.inlineDiffSplitter;
decompressDeltas = builder.decompressDeltas;
if (builder.equalizer != null) {
equalizer = builder.equalizer;
@@ -225,8 +227,14 @@ public final class DiffRowGenerator {
int endPos = 0;
final List<AbstractDelta<String>> deltaList = patch.getDeltas();
for (AbstractDelta<String> originalDelta : deltaList) {
for (AbstractDelta<String> delta : decompressDeltas(originalDelta)) {
if (decompressDeltas) {
for (AbstractDelta<String> originalDelta : deltaList) {
for (AbstractDelta<String> delta : decompressDeltas(originalDelta)) {
endPos = transformDeltaIntoDiffRow(original, endPos, diffRows, delta);
}
}
} else {
for (AbstractDelta<String> delta : deltaList) {
endPos = transformDeltaIntoDiffRow(original, endPos, diffRows, delta);
}
}
@@ -442,6 +450,7 @@ public final class DiffRowGenerator {
private boolean showInlineDiffs = false;
private boolean ignoreWhiteSpaces = false;
private boolean decompressDeltas = true;
private BiFunction<Tag, Boolean, String> oldTag
= (tag, f) -> f ? "<span class=\"editOldInline\">" : "</span>";
@@ -554,7 +563,8 @@ public final class DiffRowGenerator {
* Set the column width of generated lines of original and revised
* texts.
*
* @param width the width to set. Making it &lt; 0 doesn't make any sense. Default 80.
* @param width the width to set. Making it &lt; 0 doesn't make any
* sense. Default 80.
* @return builder with config of column width
*/
public Builder columnWidth(int width) {
@@ -586,6 +596,19 @@ public final class DiffRowGenerator {
return this;
}
/**
* Deltas could be in a state, that would produce some unreasonable
* results within an inline diff. So the deltas are decompressed into
* smaller parts and rebuild. But this could result in more differences.
*
* @param decompressDeltas
* @return
*/
public Builder decompressDeltas(boolean decompressDeltas) {
this.decompressDeltas = decompressDeltas;
return this;
}
/**
* Per default each character is separatly processed. This variant
* introduces processing by word, which does not deliver in word

View File

@@ -641,7 +641,7 @@ public class DiffRowGeneratorTest {
assertThat(rows).extracting(item -> item.getTag().name()).containsExactly("CHANGE", "DELETE", "EQUAL", "CHANGE", "EQUAL");
}
@Test
public void testCorrectChangeIssue114_2() throws IOException {
List<String> original = Arrays.asList("A", "B", "C", "D", "E");
@@ -662,7 +662,7 @@ public class DiffRowGeneratorTest {
assertThat(rows).extracting(item -> item.getTag().name()).containsExactly("CHANGE", "DELETE", "EQUAL", "CHANGE", "EQUAL");
assertThat(rows.get(1).toString()).isEqualTo("[DELETE,~B~,]");
}
@Test
public void testIssue119WrongContextLength() throws IOException {
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 class DiffRowGeneratorTest {
.filter(item -> item.getTag() != DiffRow.Tag.EQUAL)
.forEach(System.out::println);
}
@Test
public void testIssue129WithDeltaDecompression() {
List<String> lines1 = Arrays.asList(
"apple1",
"apple2",
"apple3",
"A man named Frankenstein abc to Switzerland for cookies!",
"banana1",
"banana2",
"banana3");
List<String> lines2 = Arrays.asList(
"apple1",
"apple2",
"apple3",
"A man named Frankenstein",
"xyz",
"to Switzerland for cookies!",
"banana1",
"banana2",
"banana3");
int[] entry = {1};
String txt = DiffRowGenerator.create()
.showInlineDiffs(true)
.oldTag((tag, isOpening) -> isOpening ? "==old" + tag + "==>" : "<==old==")
.newTag((tag, isOpening) -> isOpening ? "==new" + tag + "==>" : "<==new==")
.build()
.generateDiffRows(lines1, lines2)
.stream()
.map(row -> row.getTag().toString())
.collect(joining(" "));
// .forEachOrdered(row -> {
// System.out.printf("%4d %-8s %-80s %-80s\n", entry[0]++,
// row.getTag(), row.getOldLine(), row.getNewLine());
// });
assertThat(txt).isEqualTo("EQUAL EQUAL EQUAL CHANGE INSERT INSERT EQUAL EQUAL EQUAL");
}
@Test
public void testIssue129SkipDeltaDecompression() {
List<String> lines1 = Arrays.asList(
"apple1",
"apple2",
"apple3",
"A man named Frankenstein abc to Switzerland for cookies!",
"banana1",
"banana2",
"banana3");
List<String> lines2 = Arrays.asList(
"apple1",
"apple2",
"apple3",
"A man named Frankenstein",
"xyz",
"to Switzerland for cookies!",
"banana1",
"banana2",
"banana3");
int[] entry = {1};
String txt =
DiffRowGenerator.create()
.showInlineDiffs(true)
.decompressDeltas(false)
.oldTag((tag, isOpening) -> isOpening ? "==old" + tag + "==>" : "<==old==")
.newTag((tag, isOpening) -> isOpening ? "==new" + tag + "==>" : "<==new==")
.build()
.generateDiffRows(lines1, lines2)
.stream()
.map(row -> row.getTag().toString())
.collect(joining(" "));
// .forEachOrdered(row -> {
// System.out.printf("%4d %-8s %-80s %-80s\n", entry[0]++,
// row.getTag(), row.getOldLine(), row.getNewLine());
// });
assertThat(txt).isEqualTo("EQUAL EQUAL EQUAL CHANGE CHANGE CHANGE EQUAL EQUAL EQUAL");
}
}