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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -90,14 +104,19 @@ internal object CodeAnalyzer {
}

private fun analyzeCodeWithKeywords(code: String, keywords: List<String>): 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,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>): List<PhraseLocation> {
fun locate(
code: String,
keywords: List<String>,
ignoreRanges: List<PhraseLocation> = emptyList(),
): List<PhraseLocation> {
val locations = mutableListOf<PhraseLocation>()
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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}