-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
java-diff-utils/src/main/java/com/github/difflib/text/deltamerge/DeltaMergeUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
java-diff-utils/src/main/java/com/github/difflib/text/deltamerge/InlineDeltaMergeInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 toDEFAULT_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 theinlineDeltaMerger
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 theequalizer
property is null.Please advise how to continue. Shall I:
DEFAULT_INLINE_DELTA_MERGER
and checking theinlineDeltaMerger
for nullThere was a problem hiding this comment.
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.