Thanks to visit codestin.com
Credit goes to github.com

Skip to content

support merging inline deltas split by whitespace or a minor equality (#168) #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.github.difflib.patch.InsertDelta;
import com.github.difflib.patch.Patch;
import com.github.difflib.text.DiffRow.Tag;
import com.github.difflib.text.deltamerge.DeltaMergeUtils;
import com.github.difflib.text.deltamerge.InlineDeltaMergeInfo;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
Expand Down Expand Up @@ -75,6 +77,14 @@ public final class DiffRowGenerator {
public static final Function<String, List<String>> SPLITTER_BY_WORD = line -> splitStringPreserveDelimiter(line, SPLIT_BY_WORD_PATTERN);
public static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");

public static final Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> DEFAULT_INLINE_DELTA_MERGER = InlineDeltaMergeInfo::getDeltas;

/**
* Merge diffs which are separated by equalities consisting of whitespace only.
*/
public static final Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> WHITESPACE_EQUALITIES_MERGER = deltaMergeInfo -> DeltaMergeUtils
.mergeInlineDeltas(deltaMergeInfo, (equalities -> equalities.stream().allMatch(String::isBlank)));

public static Builder create() {
return new Builder();
}
Expand Down Expand Up @@ -170,6 +180,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
private final boolean reportLinesUnchanged;
private final Function<String, String> lineNormalizer;
private final Function<String, String> processDiffs;
private final Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> inlineDeltaMerger;

private final boolean showInlineDiffs;
private final boolean replaceOriginalLinefeedInChangesWithSpaces;
Expand All @@ -194,11 +205,13 @@ private DiffRowGenerator(Builder builder) {
reportLinesUnchanged = builder.reportLinesUnchanged;
lineNormalizer = builder.lineNormalizer;
processDiffs = builder.processDiffs;
inlineDeltaMerger = builder.inlineDeltaMerger;

replaceOriginalLinefeedInChangesWithSpaces = builder.replaceOriginalLinefeedInChangesWithSpaces;

Objects.requireNonNull(inlineDiffSplitter);
Objects.requireNonNull(lineNormalizer);
Objects.requireNonNull(inlineDeltaMerger);
}

/**
Expand Down Expand Up @@ -370,7 +383,10 @@ private List<DiffRow> generateInlineDiffs(AbstractDelta<String> delta) {
origList = inlineDiffSplitter.apply(joinedOrig);
revList = inlineDiffSplitter.apply(joinedRev);

List<AbstractDelta<String>> inlineDeltas = DiffUtils.diff(origList, revList, equalizer).getDeltas();
List<AbstractDelta<String>> originalInlineDeltas = DiffUtils.diff(origList, revList, equalizer)
.getDeltas();
List<AbstractDelta<String>> inlineDeltas = inlineDeltaMerger
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change it so that the default configuration without a merger leaves the functionality as it was, so no default merger.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review and feedback.
The default behaviour is not changed by my pull request. By default, the inlineDeltaMerger is set to DEFAULT_INLINE_DELTA_MERGER which does not change the deltas. It just returns the original deltas.
By using the DEFAULT_INLINE_DELTA_MERGER, we can omit having null-checks on the inlineDeltaMerger and the code part that you've quoted stays cleaner.
Mainly, I used this approach compared to a null-check as it seems to be more in line with the other mechanisms. E.g. having a DEFAULT_EQUALIZER instead of having if-else cascades where Object::equals is used in case the equalizer property is null.

Please advise how to continue. Shall I:

  • leave my original code unchanged
  • add a comment above your quoted code part that explains that the default-delta-merger does not merge any deltas
  • Remove the DEFAULT_INLINE_DELTA_MERGER and checking the inlineDeltaMerger for null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wumpz: Please advise how to continue.

.apply(new InlineDeltaMergeInfo(originalInlineDeltas, origList, revList));

Collections.reverse(inlineDeltas);
for (AbstractDelta<String> inlineDelta : inlineDeltas) {
Expand Down Expand Up @@ -465,6 +481,7 @@ public static class Builder {
private Function<String, String> processDiffs = null;
private BiPredicate<String, String> equalizer = null;
private boolean replaceOriginalLinefeedInChangesWithSpaces = false;
private Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> inlineDeltaMerger = DEFAULT_INLINE_DELTA_MERGER;

private Builder() {
}
Expand Down Expand Up @@ -673,5 +690,17 @@ public Builder replaceOriginalLinefeedInChangesWithSpaces(boolean replace) {
this.replaceOriginalLinefeedInChangesWithSpaces = replace;
return this;
}

/**
* Provide an inline delta merger for use case specific delta optimizations.
*
* @param inlineDeltaMerger
* @return
*/
public Builder inlineDeltaMerger(
Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> inlineDeltaMerger) {
this.inlineDeltaMerger = inlineDeltaMerger;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2009-2024 java-diff-utils.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.difflib.text.deltamerge;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import com.github.difflib.patch.AbstractDelta;
import com.github.difflib.patch.ChangeDelta;
import com.github.difflib.patch.Chunk;

/**
* Provides utility features for merge inline deltas
*
* @author <a href="[email protected]">Christian Meier</a>
*/
final public class DeltaMergeUtils {

public static List<AbstractDelta<String>> mergeInlineDeltas(InlineDeltaMergeInfo deltaMergeInfo,
Predicate<List<String>> replaceEquality) {
final List<AbstractDelta<String>> originalDeltas = deltaMergeInfo.getDeltas();
if (originalDeltas.size() < 2) {
return originalDeltas;
}

final List<AbstractDelta<String>> newDeltas = new ArrayList<>();
newDeltas.add(originalDeltas.get(0));
for (int i = 1; i < originalDeltas.size(); i++) {
final AbstractDelta<String> previousDelta = newDeltas.getLast();
final AbstractDelta<String> currentDelta = originalDeltas.get(i);

final List<String> equalities = deltaMergeInfo.getOrigList().subList(
previousDelta.getSource().getPosition() + previousDelta.getSource().size(),
currentDelta.getSource().getPosition());

if (replaceEquality.test(equalities)) {
// Merge the previous delta, the equality and the current delta into one
// ChangeDelta and replace the previous delta by this new ChangeDelta.
final List<String> allSourceLines = new ArrayList<>();
allSourceLines.addAll(previousDelta.getSource().getLines());
allSourceLines.addAll(equalities);
allSourceLines.addAll(currentDelta.getSource().getLines());

final List<String> allTargetLines = new ArrayList<>();
allTargetLines.addAll(previousDelta.getTarget().getLines());
allTargetLines.addAll(equalities);
allTargetLines.addAll(currentDelta.getTarget().getLines());

final ChangeDelta<String> replacement = new ChangeDelta<>(
new Chunk<>(previousDelta.getSource().getPosition(), allSourceLines),
new Chunk<>(previousDelta.getTarget().getPosition(), allTargetLines));

newDeltas.removeLast();
newDeltas.add(replacement);
} else {
newDeltas.add(currentDelta);
}
}

return newDeltas;
}

private DeltaMergeUtils() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2009-2024 java-diff-utils.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.difflib.text.deltamerge;

import java.util.List;

import com.github.difflib.patch.AbstractDelta;

/**
* Holds the information required to merge deltas originating from an inline
* diff
*
* @author <a href="[email protected]">Christian Meier</a>
*/
public final class InlineDeltaMergeInfo {

private final List<AbstractDelta<String>> deltas;
private final List<String> origList;
private final List<String> revList;

public InlineDeltaMergeInfo(List<AbstractDelta<String>> deltas, List<String> origList, List<String> revList) {
this.deltas = deltas;
this.origList = origList;
this.revList = revList;
}

public List<AbstractDelta<String>> getDeltas() {
return deltas;
}

public List<String> getOrigList() {
return origList;
}

public List<String> getRevList() {
return revList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Pattern;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
Expand All @@ -20,6 +21,10 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

import com.github.difflib.patch.AbstractDelta;
import com.github.difflib.text.deltamerge.DeltaMergeUtils;
import com.github.difflib.text.deltamerge.InlineDeltaMergeInfo;

public class DiffRowGeneratorTest {

@Test
Expand Down Expand Up @@ -791,6 +796,47 @@ public void testIssue129SkipWhitespaceChanges() throws IOException {
.forEach(System.out::println);
}

@Test
public void testGeneratorWithWhitespaceDeltaMerge() {
final DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).mergeOriginalRevised(true)
.inlineDiffByWord(true).oldTag(f -> "~").newTag(f -> "**") //
.lineNormalizer(StringUtils::htmlEntites) // do not replace tabs
.inlineDeltaMerger(DiffRowGenerator.WHITESPACE_EQUALITIES_MERGER).build();

assertInlineDiffResult(generator, "No diff", "No diff", "No diff");
assertInlineDiffResult(generator, " x whitespace before diff", " y whitespace before diff",
" ~x~**y** whitespace before diff");
assertInlineDiffResult(generator, "Whitespace after diff x ", "Whitespace after diff y ",
"Whitespace after diff ~x~**y** ");
assertInlineDiffResult(generator, "Diff x x between", "Diff y y between", "Diff ~x x~**y y** between");
assertInlineDiffResult(generator, "Hello \t world", "Hi \t universe", "~Hello \t world~**Hi \t universe**");
assertInlineDiffResult(generator, "The quick brown fox jumps over the lazy dog", "A lazy dog jumps over a fox",
"~The quick brown fox ~**A lazy dog **jumps over ~the lazy dog~**a fox**");
}

@Test
public void testGeneratorWithMergingDeltasForShortEqualities() {
final Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> shortEqualitiesMerger = deltaMergeInfo -> DeltaMergeUtils
.mergeInlineDeltas(deltaMergeInfo,
(equalities -> equalities.stream().mapToInt(String::length).sum() < 6));

final DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).mergeOriginalRevised(true)
.inlineDiffByWord(true).oldTag(f -> "~").newTag(f -> "**").inlineDeltaMerger(shortEqualitiesMerger)
.build();

assertInlineDiffResult(generator, "No diff", "No diff", "No diff");
assertInlineDiffResult(generator, "aaa bbb ccc", "xxx bbb zzz", "~aaa bbb ccc~**xxx bbb zzz**");
assertInlineDiffResult(generator, "aaa bbbb ccc", "xxx bbbb zzz", "~aaa~**xxx** bbbb ~ccc~**zzz**");
}

private void assertInlineDiffResult(DiffRowGenerator generator, String original, String revised, String expected) {
final List<DiffRow> rows = generator.generateDiffRows(Arrays.asList(original), Arrays.asList(revised));
print(rows);

assertEquals(1, rows.size());
assertEquals(expected, rows.get(0).getOldLine().toString());
}

@Test
public void testIssue188HangOnExamples() throws IOException, URISyntaxException {
try (FileSystem zipFs = FileSystems.newFileSystem(Paths.get("target/test-classes/com/github/difflib/text/test.zip"), null);) {
Expand Down
Loading