Skip to content

Commit 00544ec

Browse files
committed
feat: allow skipping delta decompression
1 parent db20b12 commit 00544ec

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

src/commonMain/kotlin/io/github/petertrr/diffutils/text/DiffRowGenerator.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ public class DiffRowGenerator(
124124
* readable the linefeeds could be replaced by spaces.
125125
*/
126126
public val replaceOriginalLinefeedInChangesWithSpaces: Boolean = false,
127+
128+
/**
129+
* Deltas could be in a state, that would produce some unreasonable
130+
* results within an inline diff. So the deltas are decompressed into
131+
* smaller parts and rebuild. But this could result in more differences.
132+
*/
133+
public val decompressDeltas: Boolean = true,
127134
) {
128135
/**
129136
* Get the DiffRows describing the difference between original and revised
@@ -147,10 +154,17 @@ public class DiffRowGenerator(
147154
* @return the DiffRows between original and revised texts
148155
*/
149156
public fun generateDiffRows(original: List<String>, patch: Patch<String>): List<DiffRow> {
150-
val diffRows: MutableList<DiffRow> = ArrayList()
157+
val diffRows = ArrayList<DiffRow>()
151158
var endPos = 0
152-
for (originalDelta in patch.deltas) {
153-
for (delta in decompressDeltas(originalDelta)) {
159+
160+
if (decompressDeltas) {
161+
for (originalDelta in patch.deltas) {
162+
for (delta in decompressDeltas(originalDelta)) {
163+
endPos = transformDeltaIntoDiffRow(original, endPos, diffRows, delta)
164+
}
165+
}
166+
} else {
167+
for (delta in patch.deltas) {
154168
endPos = transformDeltaIntoDiffRow(original, endPos, diffRows, delta)
155169
}
156170
}
@@ -159,6 +173,7 @@ public class DiffRowGenerator(
159173
for (line in original.subList(endPos, original.size)) {
160174
diffRows.add(buildDiffRow(DiffRow.Tag.EQUAL, line, line))
161175
}
176+
162177
return diffRows
163178
}
164179

src/commonTest/kotlin/io/github/petertrr/diffutils/text/DiffRowGeneratorTest.kt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,4 +714,75 @@ Bengal tiger panther but singapura but bombay munchkin for cougar. And more.""".
714714
rows.filter { it.tag != DiffRow.Tag.EQUAL }
715715
.forEach { println(it) }
716716
}
717+
718+
@Test
719+
fun testIssue129WithDeltaDecompression() {
720+
val lines1 = listOf(
721+
"apple1",
722+
"apple2",
723+
"apple3",
724+
"A man named Frankenstein abc to Switzerland for cookies!",
725+
"banana1",
726+
"banana2",
727+
"banana3",
728+
)
729+
730+
val lines2 = listOf(
731+
"apple1",
732+
"apple2",
733+
"apple3",
734+
"A man named Frankenstein",
735+
"xyz",
736+
"to Switzerland for cookies!",
737+
"banana1",
738+
"banana2",
739+
"banana3",
740+
)
741+
742+
val generator = DiffRowGenerator(
743+
showInlineDiffs = true,
744+
oldTag = { tag, isOpening -> if (isOpening) "==old$tag==>" else "<==old==" },
745+
newTag = { tag, isOpening -> if (isOpening) "==new$tag==>" else "<==new==" },
746+
)
747+
748+
val diffRows = generator.generateDiffRows(lines1, lines2)
749+
val txt = diffRows.joinToString(separator = " ") { row -> row.tag.toString() }
750+
assertEquals(txt, "EQUAL EQUAL EQUAL CHANGE INSERT INSERT EQUAL EQUAL EQUAL")
751+
}
752+
753+
@Test
754+
fun testIssue129SkipDeltaDecompression() {
755+
val lines1 = listOf(
756+
"apple1",
757+
"apple2",
758+
"apple3",
759+
"A man named Frankenstein abc to Switzerland for cookies!",
760+
"banana1",
761+
"banana2",
762+
"banana3",
763+
)
764+
765+
val lines2 = listOf(
766+
"apple1",
767+
"apple2",
768+
"apple3",
769+
"A man named Frankenstein",
770+
"xyz",
771+
"to Switzerland for cookies!",
772+
"banana1",
773+
"banana2",
774+
"banana3",
775+
)
776+
777+
val generator = DiffRowGenerator(
778+
showInlineDiffs = true,
779+
decompressDeltas = false,
780+
oldTag = { tag, isOpening -> if (isOpening) "==old$tag==>" else "<==old==" },
781+
newTag = { tag, isOpening -> if (isOpening) "==new$tag==>" else "<==new==" },
782+
)
783+
784+
val diffRows = generator.generateDiffRows(lines1, lines2)
785+
val txt = diffRows.joinToString(separator = " ") { row -> row.tag.toString() }
786+
assertEquals(txt, "EQUAL EQUAL EQUAL CHANGE CHANGE CHANGE EQUAL EQUAL EQUAL")
787+
}
717788
}

0 commit comments

Comments
 (0)