diff --git a/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeAnalyzer.kt b/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeAnalyzer.kt index 3a44bb6..9d67aba 100644 --- a/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeAnalyzer.kt +++ b/src/commonMain/kotlin/dev/snipme/highlights/internal/CodeAnalyzer.kt @@ -1,9 +1,5 @@ package dev.snipme.highlights.internal -import dev.snipme.highlights.internal.locator.AnnotationLocator -import dev.snipme.highlights.model.CodeStructure -import dev.snipme.highlights.model.SyntaxLanguage -import dev.snipme.highlights.model.SyntaxLanguage.* import dev.snipme.highlights.internal.SyntaxTokens.ALL_KEYWORDS import dev.snipme.highlights.internal.SyntaxTokens.ALL_MIXED_KEYWORDS import dev.snipme.highlights.internal.SyntaxTokens.COFFEE_KEYWORDS @@ -19,13 +15,31 @@ import dev.snipme.highlights.internal.SyntaxTokens.RUBY_KEYWORDS import dev.snipme.highlights.internal.SyntaxTokens.RUST_KEYWORDS import dev.snipme.highlights.internal.SyntaxTokens.SH_KEYWORDS import dev.snipme.highlights.internal.SyntaxTokens.SWIFT_KEYWORDS +import dev.snipme.highlights.internal.locator.AnnotationLocator import dev.snipme.highlights.internal.locator.CommentLocator import dev.snipme.highlights.internal.locator.KeywordLocator -import dev.snipme.highlights.internal.locator.NumericLiteralLocator import dev.snipme.highlights.internal.locator.MarkLocator import dev.snipme.highlights.internal.locator.MultilineCommentLocator +import dev.snipme.highlights.internal.locator.NumericLiteralLocator import dev.snipme.highlights.internal.locator.PunctuationLocator import dev.snipme.highlights.internal.locator.StringLocator +import dev.snipme.highlights.model.CodeStructure +import dev.snipme.highlights.model.SyntaxLanguage +import dev.snipme.highlights.model.SyntaxLanguage.C +import dev.snipme.highlights.model.SyntaxLanguage.COFFEESCRIPT +import dev.snipme.highlights.model.SyntaxLanguage.CPP +import dev.snipme.highlights.model.SyntaxLanguage.CSHARP +import dev.snipme.highlights.model.SyntaxLanguage.DEFAULT +import dev.snipme.highlights.model.SyntaxLanguage.JAVA +import dev.snipme.highlights.model.SyntaxLanguage.JAVASCRIPT +import dev.snipme.highlights.model.SyntaxLanguage.KOTLIN +import dev.snipme.highlights.model.SyntaxLanguage.MIXED +import dev.snipme.highlights.model.SyntaxLanguage.PERL +import dev.snipme.highlights.model.SyntaxLanguage.PYTHON +import dev.snipme.highlights.model.SyntaxLanguage.RUBY +import dev.snipme.highlights.model.SyntaxLanguage.RUST +import dev.snipme.highlights.model.SyntaxLanguage.SHELL +import dev.snipme.highlights.model.SyntaxLanguage.SWIFT data class CodeSnapshot( val code: String, @@ -90,14 +104,19 @@ internal object CodeAnalyzer { } private fun analyzeCodeWithKeywords(code: String, keywords: List): CodeStructure { + val comments = CommentLocator.locate(code) + val multiLineComments = MultilineCommentLocator.locate(code) + + val allComments = comments + multiLineComments + return CodeStructure( marks = MarkLocator.locate(code), punctuations = PunctuationLocator.locate(code), - keywords = KeywordLocator.locate(code, keywords), + keywords = KeywordLocator.locate(code, keywords, allComments), strings = StringLocator.locate(code), literals = NumericLiteralLocator.locate(code), - comments = CommentLocator.locate(code), - multilineComments = MultilineCommentLocator.locate(code), + comments = comments, + multilineComments = multiLineComments, annotations = AnnotationLocator.locate(code), incremental = false, ) diff --git a/src/commonMain/kotlin/dev/snipme/highlights/internal/locator/KeywordLocator.kt b/src/commonMain/kotlin/dev/snipme/highlights/internal/locator/KeywordLocator.kt index bace199..bd65bc8 100644 --- a/src/commonMain/kotlin/dev/snipme/highlights/internal/locator/KeywordLocator.kt +++ b/src/commonMain/kotlin/dev/snipme/highlights/internal/locator/KeywordLocator.kt @@ -1,17 +1,27 @@ package dev.snipme.highlights.internal.locator -import dev.snipme.highlights.model.PhraseLocation import dev.snipme.highlights.internal.SyntaxTokens.TOKEN_DELIMITERS import dev.snipme.highlights.internal.indicesOf import dev.snipme.highlights.internal.isIndependentPhrase - +import dev.snipme.highlights.model.PhraseLocation internal object KeywordLocator { - fun locate(code: String, keywords: List): List { + fun locate( + code: String, + keywords: List, + ignoreRanges: List = emptyList(), + ): List { val locations = mutableListOf() val foundKeywords = findKeywords(code, keywords) - foundKeywords.forEach { keyword -> + + val interpretedKeywords = foundKeywords.filterNot { keyword -> + val index = code.indexOf(keyword) + val length = keyword.length + ignoreRanges.any { it.start <= index && it.end >= index + length } + } + + interpretedKeywords.forEach { keyword -> val indices = code .indicesOf(keyword) .filter { keyword.isIndependentPhrase(code, it) } diff --git a/src/commonTest/kotlin/dev/snipme/highlights/internal/locator/KeywordLocatorTest.kt b/src/commonTest/kotlin/dev/snipme/highlights/internal/locator/KeywordLocatorTest.kt index db93094..52abada 100644 --- a/src/commonTest/kotlin/dev/snipme/highlights/internal/locator/KeywordLocatorTest.kt +++ b/src/commonTest/kotlin/dev/snipme/highlights/internal/locator/KeywordLocatorTest.kt @@ -96,4 +96,30 @@ internal class KeywordLocatorTest { assertEquals(0, result.size) } + + @Test + fun `Not returns keywords from single comment`() { + val testCode = """ + // This class is static and should extend another class + """.trimIndent() + val keywords = listOf("static", "class", "extends") + + val result = KeywordLocator.locate(testCode, keywords, listOf(PhraseLocation(0, 55))) + + assertEquals(0, result.size) + } + + @Test + fun `Not returns keywords from multiline comment`() { + val testCode = """ + /* + This class is static and should extend another class + */ + """.trimIndent() + val keywords = listOf("static", "class", "extends") + + val result = KeywordLocator.locate(testCode, keywords, listOf(PhraseLocation(0, 56))) + + assertEquals(0, result.size) + } } \ No newline at end of file