From 64df8ed9f2422c15fe451ea0eb72e3fc94d3c8b1 Mon Sep 17 00:00:00 2001 From: sergeych Date: Fri, 21 Apr 2023 15:09:20 +0100 Subject: [PATCH] =?UTF-8?q?0.0.8-SNAPSHOT=20fixed=20block=20indexes=20in?= =?UTF-8?q?=20merge3=20result=D0=B1=20yet=20again?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- .../kotlin/net.sergeych.merge3/merge3.kt | 11 +++++++++-- src/commonTest/kotlin/testMerge.kt | 18 ++++++++++++++++-- 3 files changed, 26 insertions(+), 5 deletions(-) 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