Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork201
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
@@ -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(); | ||
} | ||
@@ -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; | ||
@@ -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); | ||
} | ||
/** | ||
@@ -370,7 +383,10 @@ private List<DiffRow> generateInlineDiffs(AbstractDelta<String> delta) { | ||
origList = inlineDiffSplitter.apply(joinedOrig); | ||
revList = inlineDiffSplitter.apply(joinedRev); | ||
List<AbstractDelta<String>> originalInlineDeltas = DiffUtils.diff(origList, revList, equalizer) | ||
.getDeltas(); | ||
List<AbstractDelta<String>> inlineDeltas = inlineDeltaMerger | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Thanks for your review and feedback. Please advise how to continue. Shall I:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
@@ -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() { | ||
} | ||
@@ -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="christian.meier@epictec.ch">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="christian.meier@epictec.ch">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; | ||
} | ||
} |
Uh oh!
There was an error while loading.Please reload this page.