Skip to content

Commit cb48073

Browse files
committed
corrected inlinde diff viewing without mergeing
1 parent fe364c5 commit cb48073

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public class DiffRowGenerator {
5353

5454
private final boolean showInlineDiffs;
5555
private final boolean ignoreWhiteSpaces;
56-
private final Function<Boolean,String> oldTag;
57-
private final Function<Boolean,String> newTag;
56+
private final Function<Boolean, String> oldTag;
57+
private final Function<Boolean, String> newTag;
5858
private final boolean inlineDiffByWord;
5959
private final int columnWidth;
6060
private final Equalizer<String> equalizer;
@@ -70,10 +70,10 @@ public static class Builder {
7070

7171
private boolean showInlineDiffs = false;
7272
private boolean ignoreWhiteSpaces = false;
73-
74-
private Function<Boolean,String> oldTag = f -> f?"<span class=\"editOldInline\">":"</span>";
75-
private Function<Boolean,String> newTag = f -> f?"<span class=\"editNewInline\">":"</span>";
76-
73+
74+
private Function<Boolean, String> oldTag = f -> f ? "<span class=\"editOldInline\">" : "</span>";
75+
private Function<Boolean, String> newTag = f -> f ? "<span class=\"editNewInline\">" : "</span>";
76+
7777
private int columnWidth = 80;
7878
private boolean mergeOriginalRevised = false;
7979
private boolean inlineDiffByWord = false;
@@ -109,17 +109,18 @@ public Builder ignoreWhiteSpaces(boolean val) {
109109
* @param tag the tag to set. Without angle brackets. Default: span.
110110
* @return builder with configured ignoreBlankLines parameter
111111
*/
112-
public Builder oldTag(Function<Boolean,String> generator) {
112+
public Builder oldTag(Function<Boolean, String> generator) {
113113
this.oldTag = generator;
114114
return this;
115115
}
116116

117117
/**
118118
* Generator for New-Text-Tags.
119+
*
119120
* @param generator
120-
* @return
121+
* @return
121122
*/
122-
public Builder newTag(Function<Boolean,String> generator) {
123+
public Builder newTag(Function<Boolean, String> generator) {
123124
this.newTag = generator;
124125
return this;
125126
}
@@ -206,12 +207,18 @@ public List<DiffRow> generateDiffRows(List<String> original, List<String> revise
206207

207208
private DiffRow buildDiffRow(Tag type, String orgline, String newline) {
208209
String wrapOrg = StringUtils.wrapText(StringUtils.normalize(orgline), columnWidth);
209-
if (mergeOriginalRevised && Tag.DELETE == type) {
210-
wrapOrg = oldTag.apply(true) + wrapOrg + oldTag.apply(false);
210+
if (Tag.DELETE == type) {
211+
if (mergeOriginalRevised || showInlineDiffs) {
212+
wrapOrg = oldTag.apply(true) + wrapOrg + oldTag.apply(false);
213+
}
211214
}
212215
String wrapNew = StringUtils.wrapText(StringUtils.normalize(newline), columnWidth);
213-
if (mergeOriginalRevised && Tag.INSERT == type) {
214-
wrapOrg = newTag.apply(true) + wrapNew + newTag.apply(false);
216+
if (Tag.INSERT == type) {
217+
if (mergeOriginalRevised) {
218+
wrapOrg = newTag.apply(true) + wrapNew + newTag.apply(false);
219+
} else if (showInlineDiffs) {
220+
wrapNew = newTag.apply(true) + wrapNew + newTag.apply(false);
221+
}
215222
}
216223
return new DiffRow(type, wrapOrg, wrapNew);
217224
}
@@ -374,7 +381,7 @@ private List<DiffRow> generateInlineDiffs(Delta<String> delta) throws DiffExcept
374381
* @param cssClass the optional css class
375382
*/
376383
public static void wrapInTag(List<String> sequence, int startPosition,
377-
int endPosition, Function<Boolean,String> generator) {
384+
int endPosition, Function<Boolean, String> generator) {
378385
sequence.add(startPosition, generator.apply(true));
379386
sequence.add(endPosition, generator.apply(false));
380387
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,27 @@ public void testGeneratorExample1() throws DiffException {
207207
assertEquals(1, rows.size());
208208
assertEquals("This is a test ~senctence~**for diffutils**.", rows.get(0).getOldLine());
209209
}
210+
211+
@Test
212+
public void testGeneratorExample2() throws DiffException {
213+
DiffRowGenerator generator = DiffRowGenerator.create()
214+
.showInlineDiffs(true)
215+
.inlineDiffByWord(true)
216+
.oldTag(f -> "~")
217+
.newTag(f -> "**")
218+
.build();
219+
List<DiffRow> rows = generator.generateDiffRows(
220+
Arrays.asList("This is a test senctence.", "This is the second line.", "And here is the finish."),
221+
Arrays.asList("This is a test for diffutils.", "This is the second line."));
222+
223+
System.out.println("|original|new|");
224+
System.out.println("|--------|---|");
225+
for (DiffRow row : rows) {
226+
System.out.println("|" + row.getOldLine() + "|" + row.getNewLine() + "|");
227+
}
228+
229+
assertEquals(3, rows.size());
230+
assertEquals("This is a test ~senctence~.", rows.get(0).getOldLine());
231+
assertEquals("This is a test **for diffutils**.", rows.get(0).getNewLine());
232+
}
210233
}

0 commit comments

Comments
 (0)