Skip to content

Commit 85d4cde

Browse files
committed
Fixed unclosed string exception
1 parent 0e4a2df commit 85d4cde

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [0.7.1]
2+
3+
### Fixed
4+
- unclosed string notation during input
5+
16
## [0.7.0]
27

38
### Added

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ plugins {
1313
}
1414

1515
group = "dev.snipme"
16-
version = "0.7.0"
16+
version = "0.7.1"
1717

1818
kotlin {
1919
// Android

src/commonMain/kotlin/dev/snipme/highlights/internal/locator/StringLocator.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ internal object StringLocator {
1414

1515
private fun findStrings(code: String): List<PhraseLocation> {
1616
val locations = mutableListOf<PhraseLocation>()
17-
val textIndices = mutableListOf<Int>()
1817

1918
// Find index of each string delimiter like " or ' or """
2019
STRING_DELIMITERS.forEach {
20+
val textIndices = mutableListOf<Int>()
2121
textIndices += code.indicesOf(it)
22-
}
2322

24-
// For given indices find words between
25-
for (i in START_INDEX..textIndices.lastIndex step TWO_ELEMENTS) {
26-
locations.add(PhraseLocation(textIndices[i], textIndices[i + 1] + QUOTE_ENDING_POSITION))
23+
// For given indices find words between
24+
for (i in START_INDEX..textIndices.lastIndex step TWO_ELEMENTS) {
25+
if (textIndices.getOrNull(i + 1) != null) {
26+
locations.add(
27+
PhraseLocation(
28+
textIndices[i],
29+
textIndices[i + 1] + QUOTE_ENDING_POSITION
30+
)
31+
)
32+
}
33+
}
2734
}
2835

2936
return locations

src/commonTest/kotlin/dev/snipme/highlights/internal/locator/StringLocatorTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.snipme.highlights.internal.locator
22

3+
import dev.snipme.highlights.internal.printResults
34
import dev.snipme.highlights.model.PhraseLocation
45
import kotlin.test.assertEquals
56
import kotlin.test.Test
@@ -68,4 +69,29 @@ internal class StringLocatorTest {
6869
assertEquals(PhraseLocation(20, 23), result[0])
6970
assertEquals(PhraseLocation(8, 11), result[1])
7071
}
72+
73+
@Test
74+
fun `No returns location of unclosed string phrase`() {
75+
val testCode = """
76+
val b = "a
77+
val a = 'a
78+
""".trimIndent()
79+
80+
val result = StringLocator.locate(testCode)
81+
82+
assertEquals(0, result.size)
83+
}
84+
85+
@Test
86+
fun `Returns location only of closed string phrase`() {
87+
val testCode = """
88+
val b = 'a
89+
val a = "a"
90+
""".trimIndent()
91+
92+
val result = StringLocator.locate(testCode)
93+
94+
assertEquals(1, result.size)
95+
assertEquals(PhraseLocation(19, 22), result[0])
96+
}
7197
}

0 commit comments

Comments
 (0)