@@ -152,6 +152,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
152152 private final Function <String , String > lineNormalizer ;
153153
154154 private final boolean showInlineDiffs ;
155+ private final boolean rawValues ;
155156
156157 private DiffRowGenerator (Builder builder ) {
157158 showInlineDiffs = builder .showInlineDiffs ;
@@ -164,6 +165,7 @@ private DiffRowGenerator(Builder builder) {
164165 equalizer = ignoreWhiteSpaces ? IGNORE_WHITESPACE_EQUALIZER : DEFAULT_EQUALIZER ;
165166 reportLinesUnchanged = builder .reportLinesUnchanged ;
166167 lineNormalizer = builder .lineNormalizer ;
168+ rawValues = builder .rawValues ;
167169
168170 Objects .requireNonNull (inlineDiffSplitter );
169171 Objects .requireNonNull (lineNormalizer );
@@ -198,14 +200,14 @@ public List<DiffRow> generateDiffRows(final List<String> original, Patch<String>
198200 Chunk <String > rev = delta .getTarget ();
199201
200202 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 ));
202204 }
203205
204206 // Inserted DiffRow
205207 if (delta instanceof InsertDelta ) {
206208 endPos = orig .last () + 1 ;
207209 for (String line : rev .getLines ()) {
208- diffRows .add (buildDiffRow (Tag .INSERT , "" , line ));
210+ diffRows .add (buildDiffRow (Tag .INSERT , "" , line , "" , line ));
209211 }
210212 continue ;
211213 }
@@ -214,7 +216,7 @@ public List<DiffRow> generateDiffRows(final List<String> original, Patch<String>
214216 if (delta instanceof DeleteDelta ) {
215217 endPos = orig .last () + 1 ;
216218 for (String line : orig .getLines ()) {
217- diffRows .add (buildDiffRow (Tag .DELETE , line , "" ));
219+ diffRows .add (buildDiffRow (Tag .DELETE , line , "" , line , "" ));
218220 }
219221 continue ;
220222 }
@@ -225,22 +227,26 @@ public List<DiffRow> generateDiffRows(final List<String> original, Patch<String>
225227 for (int j = 0 ; j < Math .max (orig .size (), rev .size ()); j ++) {
226228 diffRows .add (buildDiffRow (Tag .CHANGE ,
227229 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 ) : "" ));
229233 }
230234 }
231235 endPos = orig .last () + 1 ;
232236 }
233237
234238 // Copy the final matching chunk if any.
235239 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 ));
237241 }
238242 return diffRows ;
239243 }
240244
241- private DiffRow buildDiffRow (Tag type , String orgline , String newline ) {
245+ private DiffRow buildDiffRow (Tag type , String orgline , String newline , String raworgline , String rawnewline ) {
242246 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 );
244250 } else {
245251 String wrapOrg = preprocessLine (orgline );
246252 if (Tag .DELETE == type ) {
@@ -256,14 +262,22 @@ private DiffRow buildDiffRow(Tag type, String orgline, String newline) {
256262 wrapNew = newTag .apply (true ) + wrapNew + newTag .apply (false );
257263 }
258264 }
259- return new DiffRow (type , wrapOrg , wrapNew );
265+ return rawValues
266+ ? new DiffRow (type , wrapOrg , wrapNew , raworgline , rawnewline )
267+ : new DiffRow (type , wrapOrg , wrapNew );
260268 }
261269 }
262270
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 ));
267281 }
268282
269283 List <String > normalizeLines (List <String > list ) {
@@ -343,7 +357,9 @@ private List<DiffRow> generateInlineDiffs(AbstractDelta<String> delta) throws Di
343357 diffRows .
344358 add (buildDiffRowWithoutNormalizing (Tag .CHANGE ,
345359 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 ) : "" ));
347363 }
348364 return diffRows ;
349365 }
@@ -375,6 +391,8 @@ public static class Builder {
375391 private boolean reportLinesUnchanged = false ;
376392 private Function <String , List <String >> inlineDiffSplitter = SPLITTER_BY_CHARACTER ;
377393 private Function <String , String > lineNormalizer = LINE_NORMALIZER_FOR_HTML ;
394+
395+ private boolean rawValues = false ;
378396
379397 private Builder () {
380398 }
@@ -505,5 +523,18 @@ public Builder lineNormalizer(Function<String, String> lineNormalizer) {
505523 this .lineNormalizer = lineNormalizer ;
506524 return this ;
507525 }
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+ }
508539 }
509540}
0 commit comments