@@ -61,6 +61,7 @@ public final class DiffRowGenerator {
61
61
}
62
62
return list ;
63
63
};
64
+
64
65
public static final Pattern SPLIT_BY_WORD_PATTERN = Pattern .compile ("\\ s+|[,.\\ [\\ ](){}/\\ \\ *+\\ -#]" );
65
66
66
67
/**
@@ -106,7 +107,7 @@ protected final static List<String> splitStringPreserveDelimiter(String str, Pat
106
107
*/
107
108
static void wrapInTag (List <String > sequence , int startPosition ,
108
109
int endPosition , Tag tag , BiFunction <Tag , Boolean , String > tagGenerator ,
109
- Function <String , String > processDiffs ) {
110
+ Function <String , String > processDiffs , boolean replaceLinefeedWithSpace ) {
110
111
int endPos = endPosition ;
111
112
112
113
while (endPos >= startPosition ) {
@@ -115,6 +116,9 @@ static void wrapInTag(List<String> sequence, int startPosition,
115
116
while (endPos > startPosition ) {
116
117
if (!"\n " .equals (sequence .get (endPos - 1 ))) {
117
118
break ;
119
+ } else if (replaceLinefeedWithSpace ) {
120
+ sequence .set (endPos - 1 , " " );
121
+ break ;
118
122
}
119
123
endPos --;
120
124
}
@@ -133,7 +137,11 @@ static void wrapInTag(List<String> sequence, int startPosition,
133
137
//search position for end tag
134
138
while (endPos > startPosition ) {
135
139
if ("\n " .equals (sequence .get (endPos - 1 ))) {
136
- break ;
140
+ if (replaceLinefeedWithSpace ) {
141
+ sequence .set (endPos - 1 , " " );
142
+ } else {
143
+ break ;
144
+ }
137
145
}
138
146
if (processDiffs != null ) {
139
147
sequence .set (endPos - 1 ,
@@ -159,6 +167,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
159
167
private final Function <String , String > processDiffs ;
160
168
161
169
private final boolean showInlineDiffs ;
170
+ private final boolean replaceOriginalLinefeedInChangesWithSpaces ;
162
171
163
172
private DiffRowGenerator (Builder builder ) {
164
173
showInlineDiffs = builder .showInlineDiffs ;
@@ -178,6 +187,8 @@ private DiffRowGenerator(Builder builder) {
178
187
reportLinesUnchanged = builder .reportLinesUnchanged ;
179
188
lineNormalizer = builder .lineNormalizer ;
180
189
processDiffs = builder .processDiffs ;
190
+
191
+ replaceOriginalLinefeedInChangesWithSpaces = builder .replaceOriginalLinefeedInChangesWithSpaces ;
181
192
182
193
Objects .requireNonNull (inlineDiffSplitter );
183
194
Objects .requireNonNull (lineNormalizer );
@@ -313,19 +324,19 @@ private List<DiffRow> generateInlineDiffs(AbstractDelta<String> delta) {
313
324
if (inlineDelta .getType () == DeltaType .DELETE ) {
314
325
wrapInTag (origList , inlineOrig .getPosition (), inlineOrig
315
326
.getPosition ()
316
- + inlineOrig .size (), Tag .DELETE , oldTag , processDiffs );
327
+ + inlineOrig .size (), Tag .DELETE , oldTag , processDiffs , replaceOriginalLinefeedInChangesWithSpaces && mergeOriginalRevised );
317
328
} else if (inlineDelta .getType () == DeltaType .INSERT ) {
318
329
if (mergeOriginalRevised ) {
319
330
origList .addAll (inlineOrig .getPosition (),
320
331
revList .subList (inlineRev .getPosition (),
321
332
inlineRev .getPosition () + inlineRev .size ()));
322
333
wrapInTag (origList , inlineOrig .getPosition (),
323
334
inlineOrig .getPosition () + inlineRev .size (),
324
- Tag .INSERT , newTag , processDiffs );
335
+ Tag .INSERT , newTag , processDiffs , false );
325
336
} else {
326
337
wrapInTag (revList , inlineRev .getPosition (),
327
338
inlineRev .getPosition () + inlineRev .size (),
328
- Tag .INSERT , newTag , processDiffs );
339
+ Tag .INSERT , newTag , processDiffs , false );
329
340
}
330
341
} else if (inlineDelta .getType () == DeltaType .CHANGE ) {
331
342
if (mergeOriginalRevised ) {
@@ -334,15 +345,15 @@ private List<DiffRow> generateInlineDiffs(AbstractDelta<String> delta) {
334
345
inlineRev .getPosition () + inlineRev .size ()));
335
346
wrapInTag (origList , inlineOrig .getPosition () + inlineOrig .size (),
336
347
inlineOrig .getPosition () + inlineOrig .size () + inlineRev .size (),
337
- Tag .CHANGE , newTag , processDiffs );
348
+ Tag .CHANGE , newTag , processDiffs , false );
338
349
} else {
339
350
wrapInTag (revList , inlineRev .getPosition (),
340
351
inlineRev .getPosition () + inlineRev .size (),
341
- Tag .CHANGE , newTag , processDiffs );
352
+ Tag .CHANGE , newTag , processDiffs , false );
342
353
}
343
354
wrapInTag (origList , inlineOrig .getPosition (),
344
355
inlineOrig .getPosition () + inlineOrig .size (),
345
- Tag .CHANGE , oldTag , processDiffs );
356
+ Tag .CHANGE , oldTag , processDiffs , replaceOriginalLinefeedInChangesWithSpaces && mergeOriginalRevised );
346
357
}
347
358
}
348
359
StringBuilder origResult = new StringBuilder ();
@@ -385,10 +396,10 @@ public static class Builder {
385
396
private boolean showInlineDiffs = false ;
386
397
private boolean ignoreWhiteSpaces = false ;
387
398
388
- private BiFunction <Tag , Boolean , String > oldTag =
389
- (tag , f ) -> f ? "<span class=\" editOldInline\" >" : "</span>" ;
390
- private BiFunction <Tag , Boolean , String > newTag =
391
- (tag , f ) -> f ? "<span class=\" editNewInline\" >" : "</span>" ;
399
+ private BiFunction <Tag , Boolean , String > oldTag
400
+ = (tag , f ) -> f ? "<span class=\" editOldInline\" >" : "</span>" ;
401
+ private BiFunction <Tag , Boolean , String > newTag
402
+ = (tag , f ) -> f ? "<span class=\" editNewInline\" >" : "</span>" ;
392
403
393
404
private int columnWidth = 0 ;
394
405
private boolean mergeOriginalRevised = false ;
@@ -397,6 +408,7 @@ public static class Builder {
397
408
private Function <String , String > lineNormalizer = LINE_NORMALIZER_FOR_HTML ;
398
409
private Function <String , String > processDiffs = null ;
399
410
private BiPredicate <String , String > equalizer = null ;
411
+ private boolean replaceOriginalLinefeedInChangesWithSpaces = false ;
400
412
401
413
private Builder () {
402
414
}
@@ -445,7 +457,7 @@ public Builder oldTag(BiFunction<Tag, Boolean, String> generator) {
445
457
this .oldTag = generator ;
446
458
return this ;
447
459
}
448
-
460
+
449
461
/**
450
462
* Generator for Old-Text-Tags.
451
463
*
@@ -467,7 +479,7 @@ public Builder newTag(BiFunction<Tag, Boolean, String> generator) {
467
479
this .newTag = generator ;
468
480
return this ;
469
481
}
470
-
482
+
471
483
/**
472
484
* Generator for New-Text-Tags.
473
485
*
@@ -575,5 +587,17 @@ public Builder equalizer(BiPredicate<String, String> equalizer) {
575
587
this .equalizer = equalizer ;
576
588
return this ;
577
589
}
590
+
591
+ /**
592
+ * Sometimes it happens that a change contains multiple lines. If there is no correspondence
593
+ * in old and new. To keep the merged line more readable the linefeeds could be replaced
594
+ * by spaces.
595
+ * @param replace
596
+ * @return
597
+ */
598
+ public Builder replaceOriginalLinefeedInChangesWithSpaces (boolean replace ) {
599
+ this .replaceOriginalLinefeedInChangesWithSpaces = replace ;
600
+ return this ;
601
+ }
578
602
}
579
603
}
0 commit comments