@@ -152,6 +152,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
152
152
private final Function <String , String > lineNormalizer ;
153
153
154
154
private final boolean showInlineDiffs ;
155
+ private final boolean rawValues ;
155
156
156
157
private DiffRowGenerator (Builder builder ) {
157
158
showInlineDiffs = builder .showInlineDiffs ;
@@ -164,6 +165,7 @@ private DiffRowGenerator(Builder builder) {
164
165
equalizer = ignoreWhiteSpaces ? IGNORE_WHITESPACE_EQUALIZER : DEFAULT_EQUALIZER ;
165
166
reportLinesUnchanged = builder .reportLinesUnchanged ;
166
167
lineNormalizer = builder .lineNormalizer ;
168
+ rawValues = builder .rawValues ;
167
169
168
170
Objects .requireNonNull (inlineDiffSplitter );
169
171
Objects .requireNonNull (lineNormalizer );
@@ -198,14 +200,14 @@ public List<DiffRow> generateDiffRows(final List<String> original, Patch<String>
198
200
Chunk <String > rev = delta .getTarget ();
199
201
200
202
for (String line : original .subList (endPos , orig .getPosition ())) {
201
- diffRows .add (buildDiffRow (Tag .EQUAL , line , line ));
203
+ diffRows .add (buildDiffRow (Tag .EQUAL , line , line , line , line ));
202
204
}
203
205
204
206
// Inserted DiffRow
205
207
if (delta instanceof InsertDelta ) {
206
208
endPos = orig .last () + 1 ;
207
209
for (String line : rev .getLines ()) {
208
- diffRows .add (buildDiffRow (Tag .INSERT , "" , line ));
210
+ diffRows .add (buildDiffRow (Tag .INSERT , "" , line , "" , line ));
209
211
}
210
212
continue ;
211
213
}
@@ -214,7 +216,7 @@ public List<DiffRow> generateDiffRows(final List<String> original, Patch<String>
214
216
if (delta instanceof DeleteDelta ) {
215
217
endPos = orig .last () + 1 ;
216
218
for (String line : orig .getLines ()) {
217
- diffRows .add (buildDiffRow (Tag .DELETE , line , "" ));
219
+ diffRows .add (buildDiffRow (Tag .DELETE , line , "" , line , "" ));
218
220
}
219
221
continue ;
220
222
}
@@ -225,22 +227,26 @@ public List<DiffRow> generateDiffRows(final List<String> original, Patch<String>
225
227
for (int j = 0 ; j < Math .max (orig .size (), rev .size ()); j ++) {
226
228
diffRows .add (buildDiffRow (Tag .CHANGE ,
227
229
orig .getLines ().size () > j ? orig .getLines ().get (j ) : "" ,
228
- rev .getLines ().size () > j ? rev .getLines ().get (j ) : "" ));
230
+ rev .getLines ().size () > j ? rev .getLines ().get (j ) : "" ,
231
+ orig .getLines ().size () > j ? delta .getSource ().getLines ().get (j ) : "" ,
232
+ rev .getLines ().size () > j ? delta .getTarget ().getLines ().get (j ) : "" ));
229
233
}
230
234
}
231
235
endPos = orig .last () + 1 ;
232
236
}
233
237
234
238
// Copy the final matching chunk if any.
235
239
for (String line : original .subList (endPos , original .size ())) {
236
- diffRows .add (buildDiffRow (Tag .EQUAL , line , line ));
240
+ diffRows .add (buildDiffRow (Tag .EQUAL , line , line , line , line ));
237
241
}
238
242
return diffRows ;
239
243
}
240
244
241
- private DiffRow buildDiffRow (Tag type , String orgline , String newline ) {
245
+ private DiffRow buildDiffRow (Tag type , String orgline , String newline , String raworgline , String rawnewline ) {
242
246
if (reportLinesUnchanged ) {
243
- return new DiffRow (type , orgline , newline );
247
+ return rawValues
248
+ ? new DiffRow (type , orgline , newline , raworgline , rawnewline )
249
+ : new DiffRow (type , orgline , newline );
244
250
} else {
245
251
String wrapOrg = preprocessLine (orgline );
246
252
if (Tag .DELETE == type ) {
@@ -256,14 +262,22 @@ private DiffRow buildDiffRow(Tag type, String orgline, String newline) {
256
262
wrapNew = newTag .apply (true ) + wrapNew + newTag .apply (false );
257
263
}
258
264
}
259
- return new DiffRow (type , wrapOrg , wrapNew );
265
+ return rawValues
266
+ ? new DiffRow (type , wrapOrg , wrapNew , raworgline , rawnewline )
267
+ : new DiffRow (type , wrapOrg , wrapNew );
260
268
}
261
269
}
262
270
263
- private DiffRow buildDiffRowWithoutNormalizing (Tag type , String orgline , String newline ) {
264
- return new DiffRow (type ,
265
- StringUtils .wrapText (orgline , columnWidth ),
266
- StringUtils .wrapText (newline , columnWidth ));
271
+ private DiffRow buildDiffRowWithoutNormalizing (Tag type , String orgline , String newline , String raworgline , String rawnewline ) {
272
+ return rawValues
273
+ ? new DiffRow (type ,
274
+ StringUtils .wrapText (orgline , columnWidth ),
275
+ StringUtils .wrapText (newline , columnWidth ),
276
+ raworgline ,
277
+ rawnewline )
278
+ : new DiffRow (type ,
279
+ StringUtils .wrapText (orgline , columnWidth ),
280
+ StringUtils .wrapText (newline , columnWidth ));
267
281
}
268
282
269
283
List <String > normalizeLines (List <String > list ) {
@@ -343,7 +357,9 @@ private List<DiffRow> generateInlineDiffs(AbstractDelta<String> delta) throws Di
343
357
diffRows .
344
358
add (buildDiffRowWithoutNormalizing (Tag .CHANGE ,
345
359
original .size () > j ? original .get (j ) : "" ,
346
- revised .size () > j ? revised .get (j ) : "" ));
360
+ revised .size () > j ? revised .get (j ) : "" ,
361
+ original .size () > j ? delta .getSource ().getLines ().get (j ) : "" ,
362
+ revised .size () > j ? delta .getTarget ().getLines ().get (j ) : "" ));
347
363
}
348
364
return diffRows ;
349
365
}
@@ -375,6 +391,8 @@ public static class Builder {
375
391
private boolean reportLinesUnchanged = false ;
376
392
private Function <String , List <String >> inlineDiffSplitter = SPLITTER_BY_CHARACTER ;
377
393
private Function <String , String > lineNormalizer = LINE_NORMALIZER_FOR_HTML ;
394
+
395
+ private boolean rawValues = false ;
378
396
379
397
private Builder () {
380
398
}
@@ -505,5 +523,18 @@ public Builder lineNormalizer(Function<String, String> lineNormalizer) {
505
523
this .lineNormalizer = lineNormalizer ;
506
524
return this ;
507
525
}
526
+
527
+ /**
528
+ * In some cases the original values of the diffed text are needed as well as the formatted values.
529
+ * This option can be set to allow {@link DiffRow.getRawOldLine} and {@link DiffRow.getRawNewLine}
530
+ * to access the original text of the old and new lines.
531
+ *
532
+ * @param rawValues
533
+ * @return
534
+ */
535
+ public Builder rawValues (boolean rawValues ) {
536
+ this .rawValues = rawValues ;
537
+ return this ;
538
+ }
508
539
}
509
540
}
0 commit comments