GeezerVison is showing github.com — fixes #129 - added the possibility to skip delta decompression · dandycheung/java-diff-utils@0fd38db
File treeExpand file treeCollapse file tree Open diff view settings main/java/com/github/difflib/text test/java/com/github/difflib/text
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/main/java/com/github/difflib/text/DiffRowGenerator.java +26-3Lines changed: 26 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -173,6 +173,7 @@ static void wrapInTag(List<String> sequence, int startPosition, | 173 | 173 |
| 174 | 174 | private final boolean showInlineDiffs; | 175 | 175 | private final boolean replaceOriginalLinefeedInChangesWithSpaces; | | 176 | + private final boolean decompressDeltas; | 176 | 177 |
| 177 | 178 | private DiffRowGenerator(Builder builder) { | 178 | 179 | showInlineDiffs = builder.showInlineDiffs; | @@ -182,6 +183,7 @@ private DiffRowGenerator(Builder builder) { | 182 | 183 | columnWidth = builder.columnWidth; | 183 | 184 | mergeOriginalRevised = builder.mergeOriginalRevised; | 184 | 185 | inlineDiffSplitter = builder.inlineDiffSplitter; | | 186 | + decompressDeltas = builder.decompressDeltas; | 185 | 187 |
| 186 | 188 | if (builder.equalizer != null) { | 187 | 189 | equalizer = builder.equalizer; | @@ -225,8 +227,14 @@ public List<DiffRow> generateDiffRows(final List<String> original, Patch<String> | 225 | 227 | int endPos = 0; | 226 | 228 | final List<AbstractDelta<String>> deltaList = patch.getDeltas(); | 227 | 229 |
| 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) { | 230 | 238 | endPos = transformDeltaIntoDiffRow(original, endPos, diffRows, delta); | 231 | 239 | } | 232 | 240 | } | @@ -442,6 +450,7 @@ public static class Builder { | 442 | 450 |
| 443 | 451 | private boolean showInlineDiffs = false; | 444 | 452 | private boolean ignoreWhiteSpaces = false; | | 453 | + private boolean decompressDeltas = true; | 445 | 454 |
| 446 | 455 | private BiFunction<Tag, Boolean, String> oldTag | 447 | 456 | = (tag, f) -> f ? "<span class=\"editOldInline\">" : "</span>"; | @@ -554,7 +563,8 @@ public Builder processDiffs(Function<String, String> processDiffs) { | 554 | 563 | * Set the column width of generated lines of original and revised | 555 | 564 | * texts. | 556 | 565 | * | 557 | | - * @param width the width to set. Making it < 0 doesn't make any sense. Default 80. | | 566 | + * @param width the width to set. Making it < 0 doesn't make any | | 567 | + * sense. Default 80. | 558 | 568 | * @return builder with config of column width | 559 | 569 | */ | 560 | 570 | public Builder columnWidth(int width) { | @@ -586,6 +596,19 @@ public Builder mergeOriginalRevised(boolean mergeOriginalRevised) { | 586 | 596 | return this; | 587 | 597 | } | 588 | 598 |
| | 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 | + | 589 | 612 | /** | 590 | 613 | * Per default each character is separatly processed. This variant | 591 | 614 | * introduces processing by word, which does not deliver in word | |
Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java +80-2Lines changed: 80 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -641,7 +641,7 @@ public void testCorrectChangeIssue114() throws IOException { | 641 | 641 |
| 642 | 642 | assertThat(rows).extracting(item -> item.getTag().name()).containsExactly("CHANGE", "DELETE", "EQUAL", "CHANGE", "EQUAL"); | 643 | 643 | } | 644 | | - | | 644 | + | 645 | 645 | @Test | 646 | 646 | public void testCorrectChangeIssue114_2() throws IOException { | 647 | 647 | List<String> original = Arrays.asList("A", "B", "C", "D", "E"); | @@ -662,7 +662,7 @@ public void testCorrectChangeIssue114_2() throws IOException { | 662 | 662 | assertThat(rows).extracting(item -> item.getTag().name()).containsExactly("CHANGE", "DELETE", "EQUAL", "CHANGE", "EQUAL"); | 663 | 663 | assertThat(rows.get(1).toString()).isEqualTo("[DELETE,~B~,]"); | 664 | 664 | } | 665 | | - | | 665 | + | 666 | 666 | @Test | 667 | 667 | public void testIssue119WrongContextLength() throws IOException { | 668 | 668 | 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 { | 683 | 683 | .filter(item -> item.getTag() != DiffRow.Tag.EQUAL) | 684 | 684 | .forEach(System.out::println); | 685 | 685 | } | | 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 | + } | 686 | 764 | } |
|