Skip to content

Commit d9c302c

Browse files
committed
Implement rawValues option
1 parent b32e3aa commit d9c302c

File tree

3 files changed

+127
-13
lines changed

3 files changed

+127
-13
lines changed

src/main/java/com/github/difflib/text/DiffRow.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.Serializable;
1919
import java.util.Objects;
20+
import java.util.Optional;
2021

2122
/**
2223
* Describes the diff row in form [tag, oldLine, newLine) for showing the difference between two texts
@@ -28,11 +29,19 @@ public final class DiffRow implements Serializable {
2829
private Tag tag;
2930
private final String oldLine;
3031
private final String newLine;
32+
private final String rawOldLine;
33+
private final String rawNewLine;
3134

3235
public DiffRow(Tag tag, String oldLine, String newLine) {
36+
this(tag, oldLine, newLine, null, null);
37+
}
38+
39+
public DiffRow(Tag tag, String oldLine, String newLine, String rawOldLine, String rawNewLine) {
3340
this.tag = tag;
3441
this.oldLine = oldLine;
3542
this.newLine = newLine;
43+
this.rawOldLine = rawOldLine;
44+
this.rawNewLine = rawNewLine;
3645
}
3746

3847
public enum Tag {
@@ -67,6 +76,24 @@ public String getNewLine() {
6776
return newLine;
6877
}
6978

79+
/**
80+
* This accessor will only return a non -null value of {@link DiffRowGenerator.Builder.rawValues}
81+
* is <ode>true</code>.
82+
* @return the raw value of the old line
83+
*/
84+
public Optional<String> getRawOldLine() {
85+
return Optional.ofNullable(rawOldLine);
86+
}
87+
88+
/**
89+
* This accessor will only return a non -null value of {@link DiffRowGenerator.Builder.rawValues}
90+
* is <ode>true</code>.
91+
* @return the raw value of the old line
92+
*/
93+
public Optional<String> getRawNewLine() {
94+
return Optional.ofNullable(rawNewLine);
95+
}
96+
7097
@Override
7198
public int hashCode() {
7299
return Objects.hash(newLine, oldLine, tag);

src/main/java/com/github/difflib/text/DiffRowGenerator.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static java.util.stream.Collectors.toList;
1212
import static org.junit.Assert.assertEquals;
1313
import static org.junit.Assert.assertTrue;
14+
import static org.junit.Assert.assertFalse;
1415
import org.junit.Test;
1516

1617
public class DiffRowGeneratorTest {
@@ -417,4 +418,59 @@ public void testGenerationIssue44reportLinesUnchangedProblem() throws DiffExcept
417418
List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("<dt>To do</dt>"), Arrays.asList("<dt>Done</dt>"));
418419
assertEquals("[[CHANGE,<dt>~~T~~o~~ do~~</dt>,<dt>**D**o**ne**</dt>]]", rows.toString());
419420
}
421+
422+
@Test
423+
public void testGenerationIssue44noRawValues() throws DiffException {
424+
DiffRowGenerator generator = DiffRowGenerator.create()
425+
.showInlineDiffs(true)
426+
.reportLinesUnchanged(false)
427+
.rawValues(false)
428+
.oldTag(f -> "~~")
429+
.newTag(f -> "**")
430+
.build();
431+
List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("<dt>To do</dt>"), Arrays.asList("<dt>Done</dt>"));
432+
DiffRow row = rows.get(0);
433+
assertEquals("&lt;dt&gt;~~T~~o~~ do~~&lt;/dt&gt;", row.getOldLine());
434+
assertEquals("&lt;dt&gt;**D**o**ne**&lt;/dt&gt;", row.getNewLine());
435+
assertFalse(row.getRawOldLine().isPresent());
436+
assertFalse(row.getRawNewLine().isPresent());
437+
}
438+
439+
@Test
440+
public void testGenerationIssue44rawValuesInlineDiffs() throws DiffException {
441+
DiffRowGenerator generator = DiffRowGenerator.create()
442+
.showInlineDiffs(true)
443+
.reportLinesUnchanged(false)
444+
.rawValues(true)
445+
.oldTag(f -> "~~")
446+
.newTag(f -> "**")
447+
.build();
448+
List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("<dt>To do</dt>"), Arrays.asList("<dt>Done</dt>"));
449+
DiffRow row = rows.get(0);
450+
assertEquals("&lt;dt&gt;~~T~~o~~ do~~&lt;/dt&gt;", row.getOldLine());
451+
assertEquals("&lt;dt&gt;**D**o**ne**&lt;/dt&gt;", row.getNewLine());
452+
assertTrue(row.getRawOldLine().isPresent());
453+
assertTrue(row.getRawNewLine().isPresent());
454+
assertEquals("<dt>To do</dt>", row.getRawOldLine().get());
455+
assertEquals("<dt>Done</dt>", row.getRawNewLine().get());
456+
}
457+
458+
@Test
459+
public void testGenerationIssue44rawValuesNoInlineDiffs() throws DiffException {
460+
DiffRowGenerator generator = DiffRowGenerator.create()
461+
.showInlineDiffs(false)
462+
.reportLinesUnchanged(false)
463+
.rawValues(true)
464+
.oldTag(f -> "~~")
465+
.newTag(f -> "**")
466+
.build();
467+
List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("<dt>To do</dt>"), Arrays.asList("<dt>Done</dt>"));
468+
DiffRow row = rows.get(0);
469+
assertEquals("&lt;dt&gt;To do&lt;/dt&gt;", row.getOldLine());
470+
assertEquals("&lt;dt&gt;Done&lt;/dt&gt;", row.getNewLine());
471+
assertTrue(row.getRawOldLine().isPresent());
472+
assertTrue(row.getRawNewLine().isPresent());
473+
assertEquals("<dt>To do</dt>", row.getRawOldLine().get());
474+
assertEquals("<dt>Done</dt>", row.getRawNewLine().get());
475+
}
420476
}

0 commit comments

Comments
 (0)