Skip to content

Customize text/DiffRowGenerator with algorithm and equalizer #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions src/main/java/com/github/difflib/text/DiffRowGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.github.difflib.patch.Patch;
import com.github.difflib.text.DiffRow.Tag;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -106,7 +107,7 @@ protected final static List<String> splitStringPreserveDelimiter(String str, Pat
* @param tagGenerator the tag generator
*/
static void wrapInTag(List<String> sequence, int startPosition,
int endPosition, Function<Boolean, String> tagGenerator) {
int endPosition, BiFunction<Tag, Boolean, String> tagGenerator) {
int endPos = endPosition;

while (endPos >= startPosition) {
Expand All @@ -123,7 +124,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
break;
}

sequence.add(endPos, tagGenerator.apply(false));
sequence.add(endPos, tagGenerator.apply(null, false));
endPos--;

//search position for end tag
Expand All @@ -134,7 +135,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
endPos--;
}

sequence.add(endPos, tagGenerator.apply(true));
sequence.add(endPos, tagGenerator.apply(null, true));
endPos--;
}

Expand All @@ -146,8 +147,8 @@ static void wrapInTag(List<String> sequence, int startPosition,
private final boolean ignoreWhiteSpaces;
private final Function<String, List<String>> inlineDiffSplitter;
private final boolean mergeOriginalRevised;
private final Function<Boolean, String> newTag;
private final Function<Boolean, String> oldTag;
private final BiFunction<Tag, Boolean, String> newTag;
private final BiFunction<Tag, Boolean, String> oldTag;
private final boolean reportLinesUnchanged;
private final Function<String, String> lineNormalizer;

Expand All @@ -161,7 +162,11 @@ private DiffRowGenerator(Builder builder) {
columnWidth = builder.columnWidth;
mergeOriginalRevised = builder.mergeOriginalRevised;
inlineDiffSplitter = builder.inlineDiffSplitter;
equalizer = ignoreWhiteSpaces ? IGNORE_WHITESPACE_EQUALIZER : DEFAULT_EQUALIZER;
if (Objects.nonNull(builder.equalizer)) {
equalizer = builder.equalizer;
} else {
equalizer = ignoreWhiteSpaces ? IGNORE_WHITESPACE_EQUALIZER : DEFAULT_EQUALIZER;
}
reportLinesUnchanged = builder.reportLinesUnchanged;
lineNormalizer = builder.lineNormalizer;

Expand Down Expand Up @@ -245,15 +250,15 @@ private DiffRow buildDiffRow(Tag type, String orgline, String newline) {
String wrapOrg = preprocessLine(orgline);
if (Tag.DELETE == type) {
if (mergeOriginalRevised || showInlineDiffs) {
wrapOrg = oldTag.apply(true) + wrapOrg + oldTag.apply(false);
wrapOrg = oldTag.apply(type, true) + wrapOrg + oldTag.apply(type, false);
}
}
String wrapNew = preprocessLine(newline);
if (Tag.INSERT == type) {
if (mergeOriginalRevised) {
wrapOrg = newTag.apply(true) + wrapNew + newTag.apply(false);
wrapOrg = newTag.apply(type, true) + wrapNew + newTag.apply(type, false);
} else if (showInlineDiffs) {
wrapNew = newTag.apply(true) + wrapNew + newTag.apply(false);
wrapNew = newTag.apply(type, true) + wrapNew + newTag.apply(type, false);
}
}
return new DiffRow(type, wrapOrg, wrapNew);
Expand Down Expand Up @@ -367,15 +372,17 @@ public static class Builder {
private boolean showInlineDiffs = false;
private boolean ignoreWhiteSpaces = false;

private Function<Boolean, String> oldTag = f -> f ? "<span class=\"editOldInline\">" : "</span>";
private Function<Boolean, String> newTag = f -> f ? "<span class=\"editNewInline\">" : "</span>";
private BiFunction<Tag, Boolean, String> oldTag = (tag, isStart) -> isStart ? "<span class=\"editOldInline\">" : "</span>";
private BiFunction<Tag, Boolean, String> newTag = (tag, isStart) -> isStart ? "<span class=\"editNewInline\">" : "</span>";

private int columnWidth = 0;
private boolean mergeOriginalRevised = false;
private boolean reportLinesUnchanged = false;
private Function<String, List<String>> inlineDiffSplitter = SPLITTER_BY_CHARACTER;
private Function<String, String> lineNormalizer = LINE_NORMALIZER_FOR_HTML;

private BiPredicate<String, String> equalizer = null;

private Builder() {
}

Expand Down Expand Up @@ -419,7 +426,7 @@ public Builder reportLinesUnchanged(final boolean val) {
* @param generator the tag generator
* @return builder with configured ignoreBlankLines parameter
*/
public Builder oldTag(Function<Boolean, String> generator) {
public Builder oldTag(BiFunction<Tag, Boolean, String> generator) {
this.oldTag = generator;
return this;
}
Expand All @@ -430,7 +437,7 @@ public Builder oldTag(Function<Boolean, String> generator) {
* @param generator
* @return
*/
public Builder newTag(Function<Boolean, String> generator) {
public Builder newTag(BiFunction<Tag, Boolean, String> generator) {
this.newTag = generator;
return this;
}
Expand Down Expand Up @@ -505,5 +512,16 @@ public Builder lineNormalizer(Function<String, String> lineNormalizer) {
this.lineNormalizer = lineNormalizer;
return this;
}

/**
* Provide an equalizer for diff processing.
*
* @param val equalizer for diff processing.
* @return builder with configured equalizer parameter
*/
public Builder equalizer(BiPredicate<String, String> val) {
equalizer = val;
return this;
}
}
}
32 changes: 16 additions & 16 deletions src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ public void testGeneratorExample1() throws DiffException {
.showInlineDiffs(true)
.mergeOriginalRevised(true)
.inlineDiffByWord(true)
.oldTag(f -> "~")
.newTag(f -> "**")
.oldTag((tag, isStart) -> "~")
.newTag((tag, isStart) -> "**")
.build();
List<DiffRow> rows = generator.generateDiffRows(
Arrays.asList("This is a test senctence."),
Expand All @@ -243,8 +243,8 @@ public void testGeneratorExample2() throws DiffException {
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.oldTag(f -> "~")
.newTag(f -> "**")
.oldTag((tag, isStart) -> "~")
.newTag((tag, isStart) -> "**")
.build();
List<DiffRow> rows = generator.generateDiffRows(
Arrays.asList("This is a test senctence.", "This is the second line.", "And here is the finish."),
Expand Down Expand Up @@ -285,8 +285,8 @@ public void testGeneratorIssue14() throws DiffException {
.showInlineDiffs(true)
.mergeOriginalRevised(true)
.inlineDiffBySplitter(line -> DiffRowGenerator.splitStringPreserveDelimiter(line, Pattern.compile(",")))
.oldTag(f -> "~")
.newTag(f -> "**")
.oldTag((tag, isStart) -> "~")
.newTag((tag, isStart) -> "**")
.build();
List<DiffRow> rows = generator.generateDiffRows(
Arrays.asList("J. G. Feldstein, Chair"),
Expand All @@ -304,8 +304,8 @@ public void testGeneratorIssue15() throws DiffException, IOException {
.showInlineDiffs(true) //show the ~ ~ and ** ** symbols on each difference
.inlineDiffByWord(true) //show the ~ ~ and ** ** around each different word instead of each letter
//.reportLinesUnchanged(true) //experiment
.oldTag(f -> "~")
.newTag(f -> "**")
.oldTag((tag, isStart) -> "~")
.newTag((tag, isStart) -> "**")
.build();

List<String> listOne = Files.lines(new File("target/test-classes/mocks/issue15_1.txt").toPath())
Expand All @@ -332,8 +332,8 @@ public void testGeneratorIssue22() throws DiffException {
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.oldTag(f -> "~")
.newTag(f -> "**")
.oldTag((tag, isStart) -> "~")
.newTag((tag, isStart) -> "**")
.build();
String aa = "This is a test senctence.";
String bb = "This is a test for diffutils.\nThis is the second line.";
Expand All @@ -356,8 +356,8 @@ public void testGeneratorIssue22_2() throws DiffException {
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.oldTag(f -> "~")
.newTag(f -> "**")
.oldTag((tag, isStart) -> "~")
.newTag((tag, isStart) -> "**")
.build();
String aa = "This is a test for diffutils.\nThis is the second line.";
String bb = "This is a test senctence.";
Expand All @@ -374,8 +374,8 @@ public void testGeneratorIssue22_3() throws DiffException {
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.oldTag(f -> "~")
.newTag(f -> "**")
.oldTag((tag, isStart) -> "~")
.newTag((tag, isStart) -> "**")
.build();
String aa = "This is a test senctence.";
String bb = "This is a test for diffutils.\nThis is the second line.\nAnd one more.";
Expand Down Expand Up @@ -411,8 +411,8 @@ public void testGenerationIssue44reportLinesUnchangedProblem() throws DiffExcept
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.reportLinesUnchanged(true)
.oldTag(f -> "~~")
.newTag(f -> "**")
.oldTag((tag, isStart) -> "~~")
.newTag((tag, isStart) -> "**")
.build();
List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("<dt>To do</dt>"), Arrays.asList("<dt>Done</dt>"));
assertEquals("[[CHANGE,<dt>~~T~~o~~ do~~</dt>,<dt>**D**o**ne**</dt>]]", rows.toString());
Expand Down