Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ gradle-app.setting
# JDT-specific (Eclipse Java Development Tools)
.classpath
# MacOS files
*.DS_Store
*.DS_Store
/.kotlin/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.9.3]

### Fixed
- strings in comment locating

## [0.9.2]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repositories {
```

```shell
implementation("dev.snipme:highlights:0.9.2")
implementation("dev.snipme:highlights:0.9.3")
```

## Features ✨
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "dev.snipme"
version = "0.9.2"
version = "0.9.3"

kotlin {
// Android
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ internal object StringLocator {

// Find index of each string delimiter like " or ' or """
STRING_DELIMITERS.forEach {
val textIndices = mutableListOf<Int>()
var textIndices = mutableListOf<Int>()
textIndices += code.indicesOf(it)

// Exclude positions basing on ignoreRanges
textIndices = textIndices.filter { index ->
val textRange = IntRange(index, index + QUOTE_ENDING_POSITION)
ignoreRanges.none { ignored -> textRange in ignored }
}.toMutableList()

// For given indices find words between
for (i in START_INDEX..textIndices.lastIndex step TWO_ELEMENTS) {
if (textIndices.getOrNull(i + 1) == null) continue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package dev.snipme.highlights.internal.language

import dev.snipme.highlights.Highlights
import dev.snipme.highlights.model.SyntaxLanguage
import kotlin.test.Test
import kotlin.test.assertEquals

class CommentTest {

@Test
fun test() {
val code = """
/**
* Class's start
*/
class PagesComponent(
context: ComponentContext,
container: () -> PagesContainer, // inject using DI or create
) : ComponentContext by context,
PagesStore by context.retainedStore(factory = container) {

'a'

private val navigator = PagesNavigation<PageConfig>()

val pages = childPages(
source = navigator,
serializer = PageConfig.serializer(),
initialPages = { Pages(items = List(5) { PageConfig(it) }, selectedIndex = 0) },
childFactory = ::PageComponent,
)

"b"

init {
// subscribe to the store following the component 's lifecycle
subscribe {
actions.collect { action ->
when (action) {
is SelectPage -> navigator.select(action.index)
}
}
}
}
}

class PageComponent(
page: PageConfig,
context: ComponentContext,
) : ComponentContext by context,
Container<PageState, PageIntent, Nothing> {

override val store = store(PageState(page.page), coroutineScope()) {

`c`

// state keeper will preserve the store's state
keepState(context.stateKeeper, PageState.serializer())
reduce { intent ->
when (intent) {
is ClickedIncrementCounter -> updateState {
copy(counter = counter + 1)
}
}
}
}
}
""".trimIndent()

val result = Highlights.Builder(
language = SyntaxLanguage.KOTLIN,
code = code
).build().getCodeStructure()

assertEquals(2, result.strings.size)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class KotlinTest {
code = code
).build().getCodeStructure()

result.printStructure(code)

assertEquals(11, result.keywords.size)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,24 @@ internal class CommentLocatorTest {
assertEquals(1, result.size)
assertEquals(PhraseLocation(6, 21), result.first())
}

@Test
fun `Returns location of whole comment with single apostrophe`() {
val testCode = "// the component's lifecycle"

val result = CommentLocator.locate(testCode)

assertEquals(1, result.size)
assertEquals(PhraseLocation(0, 28), result.first())
}

@Test
fun `Returns location of whole comment with multiple quotations`() {
val testCode = """// "This" 'is a' comment`s quote"""

val result = CommentLocator.locate(testCode)

assertEquals(1, result.size)
assertEquals(PhraseLocation(0, 32), result.first())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,18 @@ internal class StringLocatorTest {
assertEquals(1, result.size)
assertEquals(PhraseLocation(19, 22), result[0])
}

@Test
fun `NOT returns location of escaped string phrase`() {
val testCode = """
val b = "a\""
val a = 'a\''
""".trimIndent()

val result = StringLocator.locate(testCode)

assertEquals(2, result.size)
assertEquals(PhraseLocation(8, 12), result[1])
assertEquals(PhraseLocation(22, 26), result[0])
}
}