diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dc196d..8736058 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [0.9.2] + +### Fixed +- partial analysis for same length strings + ## [0.9.1] ### Fixed diff --git a/README.md b/README.md index 39d78c1..300ff75 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ repositories { ``` ```shell -implementation("dev.snipme:highlights:0.9.1") +implementation("dev.snipme:highlights:0.9.2") ``` ## Features ✨ diff --git a/build.gradle.kts b/build.gradle.kts index bcb70c1..c00f902 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { } group = "dev.snipme" -version = "0.9.1" +version = "0.9.2" kotlin { // Android diff --git a/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeAnalyzer.kt b/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeAnalyzer.kt index 42e4ef3..61019fb 100644 --- a/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeAnalyzer.kt +++ b/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeAnalyzer.kt @@ -84,6 +84,8 @@ internal object CodeAnalyzer { codeSnapshot.structure - newStructure.move(lengthDifference) } + is CodeDifference.Full -> analyzeForLanguage(code, codeSnapshot.language) + CodeDifference.None -> return codeSnapshot.structure } diff --git a/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeComparator.kt b/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeComparator.kt index 1298152..89333eb 100644 --- a/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeComparator.kt +++ b/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeComparator.kt @@ -5,16 +5,19 @@ private const val WORDS_DELIMITER = " " internal sealed class CodeDifference { data class Increase(val change: String) : CodeDifference() data class Decrease(val change: String) : CodeDifference() - object None : CodeDifference() + data object Full : CodeDifference() + data object None : CodeDifference() } internal object CodeComparator { fun difference(current: String, updated: String): CodeDifference { - val currentWords = current.tokenize() - val updatedWords = updated.tokenize() + val currentWords = current.tokenize().map { it.trim() } + val updatedWords = updated.tokenize().map { it.trim() } return when { - currentWords.size == updatedWords.size -> CodeDifference.None + currentWords.size == updatedWords.size -> + if (currentWords == updatedWords) CodeDifference.None + else CodeDifference.Full currentWords.size < updatedWords.size -> CodeDifference.Increase( findDifference( diff --git a/src/commonTest/kotlin/dev/snipme/highlights/internal/CodeComparatorTest.kt b/src/commonTest/kotlin/dev/snipme/highlights/internal/CodeComparatorTest.kt index 197b2ea..82eee50 100644 --- a/src/commonTest/kotlin/dev/snipme/highlights/internal/CodeComparatorTest.kt +++ b/src/commonTest/kotlin/dev/snipme/highlights/internal/CodeComparatorTest.kt @@ -66,13 +66,46 @@ internal class CodeComparatorTest { } @Test - fun `Returns none difference for the mixed new phrase`() { + fun `Returns full difference for the mixed new phrase`() { val currentCode = "@ABCD abcd dd ee" val newCode = "@ABCD abcd ee dd" val result = CodeComparator.difference(currentCode, newCode) - assertEquals(CodeDifference.None, result) + assertEquals(CodeDifference.Full, result) + } + + @Test + fun `Returns full difference for the char change in token`() { + val currentCode = "const foo = 'bar';" + + val newCode = "const foo = 'baz';" + + val result = CodeComparator.difference(currentCode, newCode) + + assertEquals(CodeDifference.Full, result) + } + + @Test + fun `Returns difference for the char addition in single token`() { + val currentCode = "const foo = 'bar';" + + val newCode = "const foo = 'barrr';" + + val result = CodeComparator.difference(currentCode, newCode) + + assertEquals(CodeDifference.Full, result) + } + + @Test + fun `Returns difference for the char subtraction in single token`() { + val currentCode = "const foo = 'barrr';" + + val newCode = "const foo = 'bar';" + + val result = CodeComparator.difference(currentCode, newCode) + + assertEquals(CodeDifference.Full, result) } @Test