Skip to content

Commit 98caa5c

Browse files
authored
Merge pull request #28 from SnipMeDev/release/0.8.0
Release 0.8.0
2 parents 0be7017 + 6e923db commit 98caa5c

File tree

10 files changed

+160
-32
lines changed

10 files changed

+160
-32
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## [0.8.0]
2+
3+
### Changed
4+
- Kotlin version to 1.9.22
5+
6+
### Fixed
7+
- scientific notation numbers highlight length
8+
- redundant keyword highlights in strings and comments
9+
- ambiguous nested forEach returns
10+
111
## [0.7.1]
212

313
### Fixed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
![highlights_banner_opaque](https://github.com/SnipMeDev/Highlights/assets/8405055/e123ce0f-6f58-451a-9e0a-893c0809b909)
22

33
[![Maven Central](https://img.shields.io/maven-central/v/dev.snipme/highlights)](https://mvnrepository.com/artifact/dev.snipme)
4-
[![Kotlin](https://img.shields.io/badge/kotlin-1.9.0-blue.svg?logo=kotlin)](http://kotlinlang.org)
4+
[![Kotlin](https://img.shields.io/badge/kotlin-1.9.22-blue.svg?logo=kotlin)](http://kotlinlang.org)
55
[![GitHub License](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0)
66

77
# Highlights
@@ -15,7 +15,7 @@ repositories {
1515
```
1616

1717
```shell
18-
implementation("dev.snipme:highlights:0.7.1")
18+
implementation("dev.snipme:highlights:0.8.0")
1919
```
2020

2121
## Features ✨

build.gradle.kts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2-
import org.gradle.api.publish.maven.MavenPublication
3-
import org.gradle.configurationcache.extensions.capitalized
4-
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.archivesName
5-
61
apply(from = "publish-root.gradle")
72

83
plugins {
9-
kotlin("multiplatform") version "1.9.0"
4+
kotlin("multiplatform") version "1.9.22"
105
id("maven-publish")
116
id("io.github.gradle-nexus.publish-plugin") version "1.3.0"
127
id("signing")
138
}
149

1510
group = "dev.snipme"
16-
version = "0.7.1"
11+
version = "0.8.0"
1712

1813
kotlin {
1914
// Android

sample/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
22

33
plugins {
4-
kotlin("jvm") version "1.9.0"
4+
kotlin("jvm") version "1.9.22"
55
application
66
}
77

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

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

3-
import dev.snipme.highlights.internal.locator.AnnotationLocator
4-
import dev.snipme.highlights.model.CodeStructure
5-
import dev.snipme.highlights.model.SyntaxLanguage
6-
import dev.snipme.highlights.model.SyntaxLanguage.*
73
import dev.snipme.highlights.internal.SyntaxTokens.ALL_KEYWORDS
84
import dev.snipme.highlights.internal.SyntaxTokens.ALL_MIXED_KEYWORDS
95
import dev.snipme.highlights.internal.SyntaxTokens.COFFEE_KEYWORDS
@@ -19,13 +15,31 @@ import dev.snipme.highlights.internal.SyntaxTokens.RUBY_KEYWORDS
1915
import dev.snipme.highlights.internal.SyntaxTokens.RUST_KEYWORDS
2016
import dev.snipme.highlights.internal.SyntaxTokens.SH_KEYWORDS
2117
import dev.snipme.highlights.internal.SyntaxTokens.SWIFT_KEYWORDS
18+
import dev.snipme.highlights.internal.locator.AnnotationLocator
2219
import dev.snipme.highlights.internal.locator.CommentLocator
2320
import dev.snipme.highlights.internal.locator.KeywordLocator
24-
import dev.snipme.highlights.internal.locator.NumericLiteralLocator
2521
import dev.snipme.highlights.internal.locator.MarkLocator
2622
import dev.snipme.highlights.internal.locator.MultilineCommentLocator
23+
import dev.snipme.highlights.internal.locator.NumericLiteralLocator
2724
import dev.snipme.highlights.internal.locator.PunctuationLocator
2825
import dev.snipme.highlights.internal.locator.StringLocator
26+
import dev.snipme.highlights.model.CodeStructure
27+
import dev.snipme.highlights.model.SyntaxLanguage
28+
import dev.snipme.highlights.model.SyntaxLanguage.C
29+
import dev.snipme.highlights.model.SyntaxLanguage.COFFEESCRIPT
30+
import dev.snipme.highlights.model.SyntaxLanguage.CPP
31+
import dev.snipme.highlights.model.SyntaxLanguage.CSHARP
32+
import dev.snipme.highlights.model.SyntaxLanguage.DEFAULT
33+
import dev.snipme.highlights.model.SyntaxLanguage.JAVA
34+
import dev.snipme.highlights.model.SyntaxLanguage.JAVASCRIPT
35+
import dev.snipme.highlights.model.SyntaxLanguage.KOTLIN
36+
import dev.snipme.highlights.model.SyntaxLanguage.MIXED
37+
import dev.snipme.highlights.model.SyntaxLanguage.PERL
38+
import dev.snipme.highlights.model.SyntaxLanguage.PYTHON
39+
import dev.snipme.highlights.model.SyntaxLanguage.RUBY
40+
import dev.snipme.highlights.model.SyntaxLanguage.RUST
41+
import dev.snipme.highlights.model.SyntaxLanguage.SHELL
42+
import dev.snipme.highlights.model.SyntaxLanguage.SWIFT
2943

3044
data class CodeSnapshot(
3145
val code: String,
@@ -90,14 +104,20 @@ internal object CodeAnalyzer {
90104
}
91105

92106
private fun analyzeCodeWithKeywords(code: String, keywords: List<String>): CodeStructure {
107+
val comments = CommentLocator.locate(code)
108+
val multiLineComments = MultilineCommentLocator.locate(code)
109+
val strings = StringLocator.locate(code)
110+
111+
val plainTextRanges = comments + multiLineComments + strings
112+
93113
return CodeStructure(
94114
marks = MarkLocator.locate(code),
95115
punctuations = PunctuationLocator.locate(code),
96-
keywords = KeywordLocator.locate(code, keywords),
97-
strings = StringLocator.locate(code),
116+
keywords = KeywordLocator.locate(code, keywords, plainTextRanges),
117+
strings = strings,
98118
literals = NumericLiteralLocator.locate(code),
99-
comments = CommentLocator.locate(code),
100-
multilineComments = MultilineCommentLocator.locate(code),
119+
comments = comments,
120+
multilineComments = multiLineComments,
101121
annotations = AnnotationLocator.locate(code),
102122
incremental = false,
103123
)

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
package dev.snipme.highlights.internal.locator
22

3-
import dev.snipme.highlights.model.PhraseLocation
43
import dev.snipme.highlights.internal.SyntaxTokens.TOKEN_DELIMITERS
54
import dev.snipme.highlights.internal.indicesOf
65
import dev.snipme.highlights.internal.isIndependentPhrase
7-
6+
import dev.snipme.highlights.model.PhraseLocation
87

98
internal object KeywordLocator {
109

11-
fun locate(code: String, keywords: List<String>): List<PhraseLocation> {
10+
fun locate(
11+
code: String,
12+
keywords: List<String>,
13+
ignoreRanges: List<PhraseLocation> = emptyList(),
14+
): List<PhraseLocation> {
1215
val locations = mutableListOf<PhraseLocation>()
1316
val foundKeywords = findKeywords(code, keywords)
14-
foundKeywords.forEach { keyword ->
17+
18+
val interpretedKeywords = foundKeywords.filterNot { keyword ->
19+
val index = code.indexOf(keyword)
20+
val length = keyword.length
21+
ignoreRanges.any { it.start <= index && it.end >= index + length }
22+
}
23+
24+
interpretedKeywords.forEach { keyword ->
1525
val indices = code
1626
.indicesOf(keyword)
1727
.filter { keyword.isIndependentPhrase(code, it) }

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package dev.snipme.highlights.internal.locator
22

3-
import dev.snipme.highlights.model.PhraseLocation
43
import dev.snipme.highlights.internal.SyntaxTokens.TOKEN_DELIMITERS
54
import dev.snipme.highlights.internal.indicesOf
5+
import dev.snipme.highlights.model.PhraseLocation
66

7-
private const val NUMBER_ENDING_LETTER_COUNT = 1
87
private val NUMBER_START_CHARACTERS = listOf('-', '.')
98
private val NUMBER_TYPE_CHARACTERS = listOf('e', 'u', 'f', 'l')
109
private val HEX_NUMBER_CHARACTERS = listOf('a', 'b', 'c', 'd', 'e', 'f')
10+
private val NUMBER_SPECIAL_CHARACTERS = listOf('_')
1111

1212
internal object NumericLiteralLocator {
1313

@@ -24,7 +24,7 @@ internal object NumericLiteralLocator {
2424
code.split(*delimiters) // Separate words
2525
.asSequence() // Manipulate on given word separately
2626
.filterNot { foundPhrases.contains(it) }
27-
.filter { it.isNotEmpty() } // Filter spaces and others
27+
.filter { it.isNotBlank() } // Filter spaces and others
2828
.filter {
2929
it.first().isDigit() || (NUMBER_START_CHARACTERS.contains(it.first())
3030
&& it.getOrNull(1)?.isDigit() == true)
@@ -36,7 +36,7 @@ internal object NumericLiteralLocator {
3636
// Omit in the middle of text, probably variable name (this100)
3737
if (code.isNumberFirstIndex(startIndex).not()) return@forEach
3838
// Add matching occurrence to the output locations
39-
val length = calculateNumberLength(number)
39+
val length = calculateNumberLength(number.lowercase())
4040
locations.add(PhraseLocation(startIndex, startIndex + length))
4141
}
4242

@@ -79,13 +79,23 @@ internal object NumericLiteralLocator {
7979
}
8080
}
8181

82+
// Highlight only 4f when e.g. number is like 4fff
8283
if (NUMBER_TYPE_CHARACTERS.any { letters.contains(it) }) {
83-
return number.count { it.isDigit() } +
84-
number.count { NUMBER_START_CHARACTERS.contains(it) } +
85-
NUMBER_ENDING_LETTER_COUNT
84+
var length = 1 // Single letter
85+
length += number.count { it.isDigit() }
86+
length += number.count { NUMBER_START_CHARACTERS.contains(it) }
87+
length += number.count { NUMBER_SPECIAL_CHARACTERS.contains(it) }
88+
if ("e+" in number) length++
89+
return length
8690
}
8791

88-
return number.length
92+
return number.filter {
93+
it.isDigit() ||
94+
NUMBER_START_CHARACTERS.contains(it) ||
95+
NUMBER_TYPE_CHARACTERS.contains(it) ||
96+
NUMBER_SPECIAL_CHARACTERS.contains(it)
97+
98+
}.length
8999
}
90100

91101
private fun getLengthOfSubstringFor(number: String, condition: (Char) -> Boolean): Int {

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,42 @@ internal class KeywordLocatorTest {
9696

9797
assertEquals(0, result.size)
9898
}
99+
100+
@Test
101+
fun `Not returns keywords from single comment`() {
102+
val testCode = """
103+
// This class is static and should extend another class
104+
""".trimIndent()
105+
val keywords = listOf("static", "class", "extends")
106+
107+
val result = KeywordLocator.locate(testCode, keywords, listOf(PhraseLocation(0, 55)))
108+
109+
assertEquals(0, result.size)
110+
}
111+
112+
@Test
113+
fun `Not returns keywords from multiline comment`() {
114+
val testCode = """
115+
/*
116+
This class is static and should extend another class
117+
*/
118+
""".trimIndent()
119+
val keywords = listOf("static", "class", "extends")
120+
121+
val result = KeywordLocator.locate(testCode, keywords, listOf(PhraseLocation(0, 56)))
122+
123+
assertEquals(0, result.size)
124+
}
125+
126+
@Test
127+
fun `Not returns keywords from string`() {
128+
val testCode = """
129+
val text = "This class is static and should extend another class"
130+
""".trimIndent()
131+
val keywords = listOf("static", "class", "extends")
132+
133+
val result = KeywordLocator.locate(testCode, keywords, listOf(PhraseLocation(0, 54)))
134+
135+
assertEquals(0, result.size)
136+
}
99137
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,50 @@ internal class NumericLiteralLocatorTest {
142142

143143
assertEquals(0, result.size)
144144
}
145+
146+
@Test
147+
fun `Returns whole location of the all scientific notations`() {
148+
val testCode = """
149+
1e+10
150+
100e100
151+
0.11E-10
152+
123.456E+10
153+
100_00E10
154+
12e+1000
155+
""".trimIndent()
156+
157+
val result = NumericLiteralLocator.locate(testCode)
158+
159+
assertEquals(6, result.size)
160+
assertEquals(PhraseLocation(0, 5), result[0])
161+
assertEquals(PhraseLocation(6, 13), result[1])
162+
assertEquals(PhraseLocation(14, 22), result[2])
163+
assertEquals(PhraseLocation(23, 34), result[3])
164+
assertEquals(PhraseLocation(35, 44), result[4])
165+
assertEquals(PhraseLocation(45, 53), result[5])
166+
}
167+
168+
@Test
169+
fun `Returns only proper length number with letter`() {
170+
val testCode = """
171+
12e+1000
172+
12.dp
173+
12f.d
174+
-2b
175+
12sss
176+
0b10000
177+
13.22f
178+
""".trimIndent()
179+
180+
val result = NumericLiteralLocator.locate(testCode)
181+
182+
assertEquals(7, result.size)
183+
assertEquals(PhraseLocation(0, 8), result[0])
184+
assertEquals(PhraseLocation(9, 12), result[1])
185+
assertEquals(PhraseLocation(15, 19), result[2])
186+
assertEquals(PhraseLocation(21, 23), result[3])
187+
assertEquals(PhraseLocation(25, 27), result[4])
188+
assertEquals(PhraseLocation(31, 38), result[5])
189+
assertEquals(PhraseLocation(39, 45), result[6])
190+
}
145191
}

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

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

3-
import dev.snipme.highlights.internal.printResults
43
import dev.snipme.highlights.model.PhraseLocation
54
import kotlin.test.Test
65
import kotlin.test.assertEquals

0 commit comments

Comments
 (0)