Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix: Ensure equalities processed via new processEqualities hook (Fixes #219)#224

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
wumpz merged 1 commit intojava-diff-utils:masterfromtusharsoni52:fix-219-option3
Dec 10, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -189,6 +189,8 @@ static void wrapInTag(
private final Function<String, String> lineNormalizer;
private final Function<String, String> processDiffs;
private final Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> inlineDeltaMerger;
// processor for equal (unchanged) lines
private final Function<String, String> equalityProcessor;

private final boolean showInlineDiffs;
private final boolean replaceOriginalLinefeedInChangesWithSpaces;
Expand All@@ -214,6 +216,7 @@ private DiffRowGenerator(Builder builder) {
lineNormalizer = builder.lineNormalizer;
processDiffs = builder.processDiffs;
inlineDeltaMerger = builder.inlineDeltaMerger;
equalityProcessor = builder.equalityProcessor;

replaceOriginalLinefeedInChangesWithSpaces = builder.replaceOriginalLinefeedInChangesWithSpaces;

Expand DownExpand Up@@ -262,7 +265,8 @@ public List<DiffRow> generateDiffRows(final List<String> original, Patch<String>

// Copy the final matching chunk if any.
for (String line : original.subList(endPos, original.size())) {
diffRows.add(buildDiffRow(Tag.EQUAL, line, line));
String processed = processEqualities(line);
diffRows.add(buildDiffRow(Tag.EQUAL, processed, processed));
}
return diffRows;
}
Expand All@@ -276,7 +280,8 @@ private int transformDeltaIntoDiffRow(
Chunk<String> rev = delta.getTarget();

for (String line : original.subList(endPos, orig.getPosition())) {
diffRows.add(buildDiffRow(Tag.EQUAL, line, line));
String processed = processEqualities(line);
diffRows.add(buildDiffRow(Tag.EQUAL, processed, processed));
}

switch (delta.getType()) {
Expand DownExpand Up@@ -496,6 +501,19 @@ private String preprocessLine(String line) {
}
}

/**
* Hook for processing equal (unchanged) text segments.
* Delegates to the builder-configured equalityProcessor if present.
*
* @author tusharsoni52
* @param text
* @return
*
*/
protected String processEqualities(final String text) {
return equalityProcessor != null ? equalityProcessor.apply(text) : text;
}

/**
* This class used for building the DiffRowGenerator.
*
Expand All@@ -521,6 +539,8 @@ public static class Builder {
private boolean replaceOriginalLinefeedInChangesWithSpaces = false;
private Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> inlineDeltaMerger =
DEFAULT_INLINE_DELTA_MERGER;
// Processor for equalities
private Function<String, String> equalityProcessor = null;

private Builder() {}

Expand DownExpand Up@@ -613,6 +633,20 @@ public Builder processDiffs(Function<String, String> processDiffs) {
return this;
}

/**
* Processor for equal (unchanged) text parts.
* Allows applying the same escaping/transformation as for diffs.
*
* @author tusharsoni52
* @param equalityProcessor
* @return
*
*/
public Builder processEqualities(Function<String, String> equalityProcessor) {
this.equalityProcessor = equalityProcessor;
return this;
}

/**
* Set the column width of generated lines of original and revised
* texts.
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
package com.github.difflib.text;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

public class DiffRowGeneratorEqualitiesTest {

@Test
public void testDefaultEqualityProcessingLeavesTextUnchanged() {
DiffRowGenerator generator =
DiffRowGenerator.create().showInlineDiffs(false).build();

List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("hello world"), Arrays.asList("hello world"));

assertEquals(1, rows.size());
assertEquals("hello world", rows.get(0).getOldLine());
assertEquals("hello world", rows.get(0).getNewLine());
assertEquals(DiffRow.Tag.EQUAL, rows.get(0).getTag());
}

@Test
public void testCustomEqualityProcessingIsApplied() {
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(false)
.processEqualities(text -> "[" + text + "]")
.build();

List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("A", "B"), Arrays.asList("A", "B"));

assertEquals(2, rows.size());
assertEquals("[A]", rows.get(0).getOldLine());
assertEquals("[B]", rows.get(1).getOldLine());
}

/**
* Verifies that processEqualities can be used to HTML-escape unchanged
* lines while still working together with the default HTML-oriented
* lineNormalizer.
*/
@Test
public void testHtmlEscapingEqualitiesWorksWithDefaultNormalizer() {
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.processEqualities(s -> s.replace("<", "&lt;").replace(">", "&gt;"))
.build();

// both lines are equal -> Tag.EQUAL, processEqualities is invoked
List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("hello <world>"), Arrays.asList("hello <world>"));

DiffRow row = rows.get(0);

assertTrue(row.getOldLine().contains("&lt;world&gt;"));
assertTrue(row.getNewLine().contains("&lt;world&gt;"));
}

/**
* Ensures equalities are processed while inline diff markup is still
* present somewhere in the line.
*/
@Test
public void testEqualitiesProcessedButInlineDiffStillPresent() {
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.processEqualities(s -> "(" + s + ")")
.build();

List<DiffRow> rows = generator.generateDiffRows(Arrays.asList("hello world"), Arrays.asList("hello there"));

DiffRow row = rows.get(0);

System.out.println("OLD = " + row.getOldLine());
System.out.println("NEW = " + row.getNewLine());

// Row must be CHANGE
assertEquals(DiffRow.Tag.CHANGE, row.getTag());

// Inline diff markup must appear
assertTrue(
row.getOldLine().contains("span") || row.getNewLine().contains("span"),
"Expected inline <span> diff markup in old or new line");

// Equalities inside CHANGE row must NOT be wrapped by processEqualities
// Option 3 does NOT modify inline equalities
assertTrue(row.getOldLine().startsWith("hello "), "Equal (unchanged) inline segment should remain unchanged");
}
}

[8]ページ先頭

©2009-2026 Movatter.jp