diff --git a/CHANGELOG.md b/CHANGELOG.md index 2687b29..e7de41c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [Unreleased] + +### Fixed +- invalid range in multiline comment locating + ## [1.1.0] ### Changed diff --git a/src/commonMain/kotlin/dev/snipme/highlights/internal/locator/MultilineCommentLocator.kt b/src/commonMain/kotlin/dev/snipme/highlights/internal/locator/MultilineCommentLocator.kt index 100001c..c357b2d 100644 --- a/src/commonMain/kotlin/dev/snipme/highlights/internal/locator/MultilineCommentLocator.kt +++ b/src/commonMain/kotlin/dev/snipme/highlights/internal/locator/MultilineCommentLocator.kt @@ -27,7 +27,8 @@ internal object MultilineCommentLocator { comments.forEach { val (start, end) = it - locations.add(PhraseLocation(start, end)) + // Only include valid ranges where start precedes end + if (start < end) locations.add(PhraseLocation(start, end)) } return locations.toSet() diff --git a/src/commonTest/kotlin/dev/snipme/highlights/internal/locator/MultilineCommentLocatorTest.kt b/src/commonTest/kotlin/dev/snipme/highlights/internal/locator/MultilineCommentLocatorTest.kt index cf03a09..0de3fc7 100644 --- a/src/commonTest/kotlin/dev/snipme/highlights/internal/locator/MultilineCommentLocatorTest.kt +++ b/src/commonTest/kotlin/dev/snipme/highlights/internal/locator/MultilineCommentLocatorTest.kt @@ -48,6 +48,28 @@ internal class MultilineCommentLocatorTest { assertEquals(1, result.size) assertEquals(PhraseLocation(0, 8747), result[0]) } + + @Test + fun `Returns empty when end delimiter precedes start`() { + val testCode = "*/resolvers/*" + + val result = MultilineCommentLocator.locate(testCode) + + assertEquals(0, result.size) + } + + @Test + fun `Returns only valid comment ignoring misordered delimiters`() { + val testCode = """ + /* valid comment */ + searchCode("query", pathFilter: "*/resolvers/*") + """.trimIndent() + + val result = MultilineCommentLocator.locate(testCode) + + assertEquals(1, result.size) + assertEquals(PhraseLocation(0, 19), result[0]) + } } private val complexComment =