Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb5b7dac

Browse files
epictecchwumpz
andauthored
added support for merging inline deltas split by whitespace or a minor equality (#168) (#191)
Co-authored-by: Tobias <t.warneke@gmx.net>
1 parent0f8365b commitb5b7dac

File tree

4 files changed

+206
-1
lines changed

4 files changed

+206
-1
lines changed

‎java-diff-utils/src/main/java/com/github/difflib/text/DiffRowGenerator.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
importcom.github.difflib.patch.InsertDelta;
2525
importcom.github.difflib.patch.Patch;
2626
importcom.github.difflib.text.DiffRow.Tag;
27+
importcom.github.difflib.text.deltamerge.DeltaMergeUtils;
28+
importcom.github.difflib.text.deltamerge.InlineDeltaMergeInfo;
2729
importjava.util.*;
2830
importjava.util.function.BiFunction;
2931
importjava.util.function.BiPredicate;
@@ -75,6 +77,14 @@ public final class DiffRowGenerator {
7577
publicstaticfinalFunction<String,List<String>>SPLITTER_BY_WORD =line ->splitStringPreserveDelimiter(line,SPLIT_BY_WORD_PATTERN);
7678
publicstaticfinalPatternWHITESPACE_PATTERN =Pattern.compile("\\s+");
7779

80+
publicstaticfinalFunction<InlineDeltaMergeInfo,List<AbstractDelta<String>>>DEFAULT_INLINE_DELTA_MERGER =InlineDeltaMergeInfo::getDeltas;
81+
82+
/**
83+
* Merge diffs which are separated by equalities consisting of whitespace only.
84+
*/
85+
publicstaticfinalFunction<InlineDeltaMergeInfo,List<AbstractDelta<String>>>WHITESPACE_EQUALITIES_MERGER =deltaMergeInfo ->DeltaMergeUtils
86+
.mergeInlineDeltas(deltaMergeInfo, (equalities ->equalities.stream().allMatch(String::isBlank)));
87+
7888
publicstaticBuildercreate() {
7989
returnnewBuilder();
8090
}
@@ -170,6 +180,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
170180
privatefinalbooleanreportLinesUnchanged;
171181
privatefinalFunction<String,String>lineNormalizer;
172182
privatefinalFunction<String,String>processDiffs;
183+
privatefinalFunction<InlineDeltaMergeInfo,List<AbstractDelta<String>>>inlineDeltaMerger;
173184

174185
privatefinalbooleanshowInlineDiffs;
175186
privatefinalbooleanreplaceOriginalLinefeedInChangesWithSpaces;
@@ -194,11 +205,13 @@ private DiffRowGenerator(Builder builder) {
194205
reportLinesUnchanged =builder.reportLinesUnchanged;
195206
lineNormalizer =builder.lineNormalizer;
196207
processDiffs =builder.processDiffs;
208+
inlineDeltaMerger =builder.inlineDeltaMerger;
197209

198210
replaceOriginalLinefeedInChangesWithSpaces =builder.replaceOriginalLinefeedInChangesWithSpaces;
199211

200212
Objects.requireNonNull(inlineDiffSplitter);
201213
Objects.requireNonNull(lineNormalizer);
214+
Objects.requireNonNull(inlineDeltaMerger);
202215
}
203216

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

373-
List<AbstractDelta<String>>inlineDeltas =DiffUtils.diff(origList,revList,equalizer).getDeltas();
386+
List<AbstractDelta<String>>originalInlineDeltas =DiffUtils.diff(origList,revList,equalizer)
387+
.getDeltas();
388+
List<AbstractDelta<String>>inlineDeltas =inlineDeltaMerger
389+
.apply(newInlineDeltaMergeInfo(originalInlineDeltas,origList,revList));
374390

375391
Collections.reverse(inlineDeltas);
376392
for (AbstractDelta<String>inlineDelta :inlineDeltas) {
@@ -465,6 +481,7 @@ public static class Builder {
465481
privateFunction<String,String>processDiffs =null;
466482
privateBiPredicate<String,String>equalizer =null;
467483
privatebooleanreplaceOriginalLinefeedInChangesWithSpaces =false;
484+
privateFunction<InlineDeltaMergeInfo,List<AbstractDelta<String>>>inlineDeltaMerger =DEFAULT_INLINE_DELTA_MERGER;
468485

469486
privateBuilder() {
470487
}
@@ -673,5 +690,17 @@ public Builder replaceOriginalLinefeedInChangesWithSpaces(boolean replace) {
673690
this.replaceOriginalLinefeedInChangesWithSpaces =replace;
674691
returnthis;
675692
}
693+
694+
/**
695+
* Provide an inline delta merger for use case specific delta optimizations.
696+
*
697+
* @param inlineDeltaMerger
698+
* @return
699+
*/
700+
publicBuilderinlineDeltaMerger(
701+
Function<InlineDeltaMergeInfo,List<AbstractDelta<String>>>inlineDeltaMerger) {
702+
this.inlineDeltaMerger =inlineDeltaMerger;
703+
returnthis;
704+
}
676705
}
677706
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2009-2024 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
packagecom.github.difflib.text.deltamerge;
17+
18+
importjava.util.ArrayList;
19+
importjava.util.List;
20+
importjava.util.function.Predicate;
21+
22+
importcom.github.difflib.patch.AbstractDelta;
23+
importcom.github.difflib.patch.ChangeDelta;
24+
importcom.github.difflib.patch.Chunk;
25+
26+
/**
27+
* Provides utility features for merge inline deltas
28+
*
29+
* @author <a href="christian.meier@epictec.ch">Christian Meier</a>
30+
*/
31+
finalpublicclassDeltaMergeUtils {
32+
33+
publicstaticList<AbstractDelta<String>>mergeInlineDeltas(InlineDeltaMergeInfodeltaMergeInfo,
34+
Predicate<List<String>>replaceEquality) {
35+
finalList<AbstractDelta<String>>originalDeltas =deltaMergeInfo.getDeltas();
36+
if (originalDeltas.size() <2) {
37+
returnoriginalDeltas;
38+
}
39+
40+
finalList<AbstractDelta<String>>newDeltas =newArrayList<>();
41+
newDeltas.add(originalDeltas.get(0));
42+
for (inti =1;i <originalDeltas.size();i++) {
43+
finalAbstractDelta<String>previousDelta =newDeltas.getLast();
44+
finalAbstractDelta<String>currentDelta =originalDeltas.get(i);
45+
46+
finalList<String>equalities =deltaMergeInfo.getOrigList().subList(
47+
previousDelta.getSource().getPosition() +previousDelta.getSource().size(),
48+
currentDelta.getSource().getPosition());
49+
50+
if (replaceEquality.test(equalities)) {
51+
// Merge the previous delta, the equality and the current delta into one
52+
// ChangeDelta and replace the previous delta by this new ChangeDelta.
53+
finalList<String>allSourceLines =newArrayList<>();
54+
allSourceLines.addAll(previousDelta.getSource().getLines());
55+
allSourceLines.addAll(equalities);
56+
allSourceLines.addAll(currentDelta.getSource().getLines());
57+
58+
finalList<String>allTargetLines =newArrayList<>();
59+
allTargetLines.addAll(previousDelta.getTarget().getLines());
60+
allTargetLines.addAll(equalities);
61+
allTargetLines.addAll(currentDelta.getTarget().getLines());
62+
63+
finalChangeDelta<String>replacement =newChangeDelta<>(
64+
newChunk<>(previousDelta.getSource().getPosition(),allSourceLines),
65+
newChunk<>(previousDelta.getTarget().getPosition(),allTargetLines));
66+
67+
newDeltas.removeLast();
68+
newDeltas.add(replacement);
69+
}else {
70+
newDeltas.add(currentDelta);
71+
}
72+
}
73+
74+
returnnewDeltas;
75+
}
76+
77+
privateDeltaMergeUtils() {
78+
}
79+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2009-2024 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
packagecom.github.difflib.text.deltamerge;
17+
18+
importjava.util.List;
19+
20+
importcom.github.difflib.patch.AbstractDelta;
21+
22+
/**
23+
* Holds the information required to merge deltas originating from an inline
24+
* diff
25+
*
26+
* @author <a href="christian.meier@epictec.ch">Christian Meier</a>
27+
*/
28+
publicfinalclassInlineDeltaMergeInfo {
29+
30+
privatefinalList<AbstractDelta<String>>deltas;
31+
privatefinalList<String>origList;
32+
privatefinalList<String>revList;
33+
34+
publicInlineDeltaMergeInfo(List<AbstractDelta<String>>deltas,List<String>origList,List<String>revList) {
35+
this.deltas =deltas;
36+
this.origList =origList;
37+
this.revList =revList;
38+
}
39+
40+
publicList<AbstractDelta<String>>getDeltas() {
41+
returndeltas;
42+
}
43+
44+
publicList<String>getOrigList() {
45+
returnorigList;
46+
}
47+
48+
publicList<String>getRevList() {
49+
returnrevList;
50+
}
51+
}

‎java-diff-utils/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
importjava.util.Arrays;
1313
importjava.util.Collections;
1414
importjava.util.List;
15+
importjava.util.function.Function;
1516
importjava.util.regex.Pattern;
1617
importstaticjava.util.stream.Collectors.joining;
1718
importstaticjava.util.stream.Collectors.toList;
@@ -20,6 +21,10 @@
2021
importstaticorg.junit.jupiter.api.Assertions.assertTrue;
2122
importorg.junit.jupiter.api.Test;
2223

24+
importcom.github.difflib.patch.AbstractDelta;
25+
importcom.github.difflib.text.deltamerge.DeltaMergeUtils;
26+
importcom.github.difflib.text.deltamerge.InlineDeltaMergeInfo;
27+
2328
publicclassDiffRowGeneratorTest {
2429

2530
@Test
@@ -791,6 +796,47 @@ public void testIssue129SkipWhitespaceChanges() throws IOException {
791796
.forEach(System.out::println);
792797
}
793798

799+
@Test
800+
publicvoidtestGeneratorWithWhitespaceDeltaMerge() {
801+
finalDiffRowGeneratorgenerator =DiffRowGenerator.create().showInlineDiffs(true).mergeOriginalRevised(true)
802+
.inlineDiffByWord(true).oldTag(f ->"~").newTag(f ->"**")//
803+
.lineNormalizer(StringUtils::htmlEntites)// do not replace tabs
804+
.inlineDeltaMerger(DiffRowGenerator.WHITESPACE_EQUALITIES_MERGER).build();
805+
806+
assertInlineDiffResult(generator,"No diff","No diff","No diff");
807+
assertInlineDiffResult(generator," x whitespace before diff"," y whitespace before diff",
808+
" ~x~**y** whitespace before diff");
809+
assertInlineDiffResult(generator,"Whitespace after diff x ","Whitespace after diff y ",
810+
"Whitespace after diff ~x~**y** ");
811+
assertInlineDiffResult(generator,"Diff x x between","Diff y y between","Diff ~x x~**y y** between");
812+
assertInlineDiffResult(generator,"Hello\t world","Hi\t universe","~Hello\t world~**Hi\t universe**");
813+
assertInlineDiffResult(generator,"The quick brown fox jumps over the lazy dog","A lazy dog jumps over a fox",
814+
"~The quick brown fox ~**A lazy dog **jumps over ~the lazy dog~**a fox**");
815+
}
816+
817+
@Test
818+
publicvoidtestGeneratorWithMergingDeltasForShortEqualities() {
819+
finalFunction<InlineDeltaMergeInfo,List<AbstractDelta<String>>>shortEqualitiesMerger =deltaMergeInfo ->DeltaMergeUtils
820+
.mergeInlineDeltas(deltaMergeInfo,
821+
(equalities ->equalities.stream().mapToInt(String::length).sum() <6));
822+
823+
finalDiffRowGeneratorgenerator =DiffRowGenerator.create().showInlineDiffs(true).mergeOriginalRevised(true)
824+
.inlineDiffByWord(true).oldTag(f ->"~").newTag(f ->"**").inlineDeltaMerger(shortEqualitiesMerger)
825+
.build();
826+
827+
assertInlineDiffResult(generator,"No diff","No diff","No diff");
828+
assertInlineDiffResult(generator,"aaa bbb ccc","xxx bbb zzz","~aaa bbb ccc~**xxx bbb zzz**");
829+
assertInlineDiffResult(generator,"aaa bbbb ccc","xxx bbbb zzz","~aaa~**xxx** bbbb ~ccc~**zzz**");
830+
}
831+
832+
privatevoidassertInlineDiffResult(DiffRowGeneratorgenerator,Stringoriginal,Stringrevised,Stringexpected) {
833+
finalList<DiffRow>rows =generator.generateDiffRows(Arrays.asList(original),Arrays.asList(revised));
834+
print(rows);
835+
836+
assertEquals(1,rows.size());
837+
assertEquals(expected,rows.get(0).getOldLine().toString());
838+
}
839+
794840
@Test
795841
publicvoidtestIssue188HangOnExamples()throwsIOException,URISyntaxException {
796842
try (FileSystemzipFs =FileSystems.newFileSystem(Paths.get("target/test-classes/com/github/difflib/text/test.zip"),null);) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp