10000 Merge pull request #14 from SnipMeDev/feature/snapshot-per-instance · SnipMeDev/Highlights@f2aae5c · GitHub
[go: up one dir, main page]

Skip to content

Commit f2aae5c

Browse files
authored
Merge pull request #14 from SnipMeDev/feature/snapshot-per-instance
Moved snapshot to Highlights class
2 parents 2d7fe07 + d6b2f45 commit f2aae5c

File tree

7 files changed

+34
-28
lines changed

7 files changed

+34
-28
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [0.4.0]
2+
3+
### Changed
4+
5+
- Snapshot is moved to Highlights to keep it with each instance
6+
17
## [0.3.1]
28

39
### Added

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ Kotlin Multiplatform syntax highlighting engine
55

66
## Installation
77
```sh
8-
implementation("dev.snipme:highlights:0.3.1-SNAPSHOT")
8+
implementation("dev.snipme:highlights:0.4.0")
99
```
1010

1111
## Usage
1212

13+
> 💡 As each Highlights instance caches code analysis, it is recommended to re-use the same instance for small code changes.
14+
1315
To start, simply put any code snippet in the default builder
1416

1517
```kotlin

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.3.1-SNAPSHOT"
16+
version = "0.4.0-SNAPSHOT"
1717

1818
kotlin {
1919
// Android

sample/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ application {
3030
}
3131

3232
dependencies {
33-
implementation("dev.snipme:highlights:0.3.0-SNAPSHOT")
33+
implementation("dev.snipme:highlights:0.4.0-SNAPSHOT")
3434
}

src/commonMain/kotlin/dev/snipme/highlights/Highlights.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.snipme.highlights
22

33
import dev.snipme.highlights.internal.CodeAnalyzer
4+
import dev.snipme.highlights.internal.CodeSnapshot
45
import dev.snipme.highlights.model.BoldHighlight
56
import dev.snipme.highlights.model.CodeHighlight
67
import dev.snipme.highlights.model.CodeStructure
@@ -16,6 +17,8 @@ class Highlights private constructor(
1617
private val theme: SyntaxTheme,
1718
private var emphasisLocations: List<PhraseLocation>
1819
) {
20+
var snapshot: CodeSnapshot? = null
21+
private set
1922

2023
companion object {
2124
fun default() = fromBuilder(Builder())
@@ -50,7 +53,11 @@ class Highlights private constructor(
5053
this.emphasisLocations = locations.toList()
5154
}
5255

53-
fun getCodeStructure(): CodeStructure = CodeAnalyzer.analyze(code, language)
56+
fun getCodeStructure(): CodeStructure {
57+
val structure = CodeAnalyzer.analyze(code, language, snapshot)
58+
snapshot = CodeSnapshot(code, structure, language)
59+
return structure
60+
}
5461

5562
fun getHighlights(): List<CodeHighlight> {
5663
val highlights = mutableListOf<CodeHighlight>()

src/commonMain/kotlin/dev/snipme/highlights/internal/CodeAnalyzer.kt

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,20 @@ data class CodeSnapshot(
3434
)
3535

3636
internal object CodeAnalyzer {
37-
var snapshot: CodeSnapshot? = null
38-
private set
39-
40-
fun analyze(code: String, language: SyntaxLanguage = DEFAULT): CodeStructure =
37+
fun analyze(
38+
code: String,
39+
language: SyntaxLanguage = DEFAULT,
40+
snapshot: CodeSnapshot? = null,
41+
): CodeStructure =
4142
when {
4243
snapshot == null -> analyzeFull(code, language)
43-
language != snapshot!!.language -> analyzeFull(code, language)
44-
code != snapshot!!.code -> analyzePartial(snapshot!!, code)
45-
else -> snapshot!!.structure
44+
language != snapshot.language -> analyzeFull(code, language)
45+
code != snapshot.code -> analyzePartial(snapshot, code)
46+
else -> snapshot.structure
4647
}
4748

48-
fun clearSnapshot() {
49-
snapshot = null
50-
}
51-
5249
private fun analyzeFull(code: String, language: SyntaxLanguage): CodeStructure {
53-
val structure = analyzeForLanguage(code, language)
54-
snapshot = CodeSnapshot(code, structure, language)
55-
return structure
50+
return analyzeForLanguage(code, language)
5651
}
5752

5853
private fun analyzePartial(codeSnapshot: CodeSnapshot, code: String): CodeStructure {
@@ -72,8 +67,6 @@ internal object CodeAnalyzer {
7267
CodeDifference.None -> return codeSnapshot.structure
7368
}
7469

75-
snapshot = CodeSnapshot(code, structure, codeSnapshot.language)
76-
7770
return structure
7871
}
7972

src/commonTest/kotlin/dev/snipme/highlights/internal/CodeAnalyzerTest.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ internal class CodeAnalyzerTest {
2020
123.00f
2121
""".trimIndent()
2222

23-
CodeAnalyzer.clearSnapshot()
2423
val result = CodeAnalyzer.analyze(testCode)
2524

2625
assertEquals(
@@ -100,8 +99,6 @@ internal class CodeAnalyzerTest {
10099
123.00f
101100
""".trimIndent()
102101

103-
CodeAnalyzer.clearSnapshot()
104-
105102
val firstResult = CodeAnalyzer.analyze(testCode, SyntaxLanguage.KOTLIN)
106103
assertEquals(false, firstResult.incremental)
107104

@@ -117,8 +114,9 @@ internal class CodeAnalyzerTest {
117114
field.forEach { }
118115
""".trimIndent()
119116

120-
val result = CodeAnalyzer.analyze(secondTestCode, SyntaxLanguage.KOTLIN)
121-
assertNotNull(CodeAnalyzer.snapshot)
117+
val snapshot = CodeSnapshot(testCode, firstResult, SyntaxLanguage.KOTLIN)
118+
119+
val result = CodeAnalyzer.analyze(secondTestCode, SyntaxLanguage.KOTLIN, snapshot)
122120
assertEquals(true, result.incremental)
123121

124122
assertEquals(
@@ -200,7 +198,6 @@ internal class CodeAnalyzerTest {
200198
123.00f
201199
""".trimIndent()
202200

203-
CodeAnalyzer.clearSnapshot()
204201
val firstResult = CodeAnalyzer.analyze(testCode, SyntaxLanguage.KOTLIN)
205202
assertEquals(false, firstResult.incremental)
206203

@@ -210,8 +207,9 @@ internal class CodeAnalyzerTest {
210207
class
211208
""".trimIndent()
212209

213-
val result = CodeAnalyzer.analyze(secondTestCode, SyntaxLanguage.KOTLIN)
214-
assertNotNull(CodeAnalyzer.snapshot)
210+
val snapshot = CodeSnapshot(testCode, firstResult, SyntaxLanguage.KOTLIN)
211+
212+
val result = CodeAnalyzer.analyze(secondTestCode, SyntaxLanguage.KOTLIN, snapshot)
215213
assertEquals(true, result.incremental)
216214

217215
assertEquals(

0 commit comments

Comments
 (0)
0