diff --git a/build.gradle.kts b/build.gradle.kts index 836a946..0a08812 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "net.sergeych" -version = "0.0.6-SNAPSHOT" +version = "0.0.8-SNAPSHOT" repositories { mavenCentral() diff --git a/src/commonMain/kotlin/net.sergeych.merge3/merge3.kt b/src/commonMain/kotlin/net.sergeych.merge3/merge3.kt index a45cfa8..27c85f7 100644 --- a/src/commonMain/kotlin/net.sergeych.merge3/merge3.kt +++ b/src/commonMain/kotlin/net.sergeych.merge3/merge3.kt @@ -20,15 +20,22 @@ class MergeResult( val changedAreas: List, val sourceIndices: MutableList, ) { + fun oldIndexOf(i: Int) = sourceIndices.indexOf(i) + val blocks: List> by lazy { if (changedAreas.isEmpty()) - listOf(MergedBlock.Unchanged(merged, sourceIndices[0])) + listOf(MergedBlock.Unchanged(merged, oldIndexOf(0))) else { val result = mutableListOf>() var start = 0 for (r in changedAreas) { if (start != r.start) { - result.add(MergedBlock.Unchanged(merged.slice(start until r.start), sourceIndices[start])) + result.add( + MergedBlock.Unchanged( + merged.slice(start until r.start), + oldIndexOf(start) + ) + ) } if (!r.isEmpty()) { result.add(MergedBlock.Resolved(merged.slice(r))) diff --git a/src/commonTest/kotlin/testMerge.kt b/src/commonTest/kotlin/testMerge.kt index 4d95bc0..d0d6957 100644 --- a/src/commonTest/kotlin/testMerge.kt +++ b/src/commonTest/kotlin/testMerge.kt @@ -114,8 +114,22 @@ class BasicTest { val our = "lssdfasdwerwev".toList() val their = "lxcvasdfasdfs".toList() val result = merge3(source, their, our) - println("got result") - val unchanged = result.blocks.filter { it is MergedBlock.Unchanged } + println("got result: ${result.toTrace(source)}") + println("got result: ${result.blocks}") + result.blocks.filter { it is MergedBlock.Unchanged } + assertTrue(true) + } + + @Test + fun testMergeEmpty() { + Log.connectConsole() + val source = "".toList() + val our = "".toList() + val their = "".toList() + val result = merge3(source, their, our) + println("got result: ${result.toTrace(source)}") + println("got result: ${result.blocks}") + result.blocks.filter { it is MergedBlock.Unchanged } assertTrue(true) } } \ No newline at end of file