diff --git a/simpleast-core/src/main/java/com/discord/simpleast/code/CodeRules.kt b/simpleast-core/src/main/java/com/discord/simpleast/code/CodeRules.kt index d1e5ce6..a09cd4d 100644 --- a/simpleast-core/src/main/java/com/discord/simpleast/code/CodeRules.kt +++ b/simpleast-core/src/main/java/com/discord/simpleast/code/CodeRules.kt @@ -59,14 +59,15 @@ object CodeRules { internal fun createWordPattern(vararg words: String) = Pattern.compile("""^\b(?:${words.joinToString("|")})\b""") - fun Pattern.toFullMatchRule( + fun Pattern.toMatchGroupRule( + group: Int = 0, stylesProvider: StyleNode.SpanProvider? = null ) = object : Rule, S>(this) { override fun parse( matcher: Matcher, parser: Parser, S>, state: S ): ParseSpec, S> { - val content = matcher.group() + val content = matcher.group(group).orEmpty() val node = stylesProvider ?.let { StyleNode.Text(content, it) } ?: TextNode(content) @@ -101,13 +102,13 @@ object CodeRules { codeStyleProviders, additionalRules = listOf( createSingleLineCommentPattern("#") - .toFullMatchRule(codeStyleProviders.commentStyleProvider), + .toMatchGroupRule(stylesProvider = codeStyleProviders.commentStyleProvider), Pattern.compile("""^"[\s\S]*?(?, S>>( Xml.PATTERN_XML_COMMENT - .toFullMatchRule(codeStyleProviders.commentStyleProvider), + .toMatchGroupRule(stylesProvider = codeStyleProviders.commentStyleProvider), Xml.createTagRule(codeStyleProviders), - PATTERN_LEADING_WS_CONSUMER.toFullMatchRule(), - PATTERN_TEXT.toFullMatchRule(), + PATTERN_LEADING_WS_CONSUMER.toMatchGroupRule(), + PATTERN_TEXT.toMatchGroupRule(), ) return mapOf( @@ -175,11 +176,11 @@ object CodeRules { additionalRules + listOf( createDefinitionRule(codeStyleProviders, *definitions), - createWordPattern(*builtIns).toFullMatchRule(codeStyleProviders.genericsStyleProvider), - createWordPattern(*keywords).toFullMatchRule(codeStyleProviders.keywordStyleProvider), - PATTERN_NUMBERS.toFullMatchRule(codeStyleProviders.literalStyleProvider), - PATTERN_LEADING_WS_CONSUMER.toFullMatchRule(), - PATTERN_TEXT.toFullMatchRule(), + createWordPattern(*builtIns).toMatchGroupRule(stylesProvider = codeStyleProviders.genericsStyleProvider), + createWordPattern(*keywords).toMatchGroupRule(stylesProvider = codeStyleProviders.keywordStyleProvider), + PATTERN_NUMBERS.toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider), + PATTERN_LEADING_WS_CONSUMER.toMatchGroupRule(), + PATTERN_TEXT.toMatchGroupRule(), ) /** diff --git a/simpleast-core/src/main/java/com/discord/simpleast/code/Kotlin.kt b/simpleast-core/src/main/java/com/discord/simpleast/code/Kotlin.kt index 68bb711..ed54565 100644 --- a/simpleast-core/src/main/java/com/discord/simpleast/code/Kotlin.kt +++ b/simpleast-core/src/main/java/com/discord/simpleast/code/Kotlin.kt @@ -1,6 +1,6 @@ package com.discord.simpleast.code -import com.discord.simpleast.code.CodeRules.toFullMatchRule +import com.discord.simpleast.code.CodeRules.toMatchGroupRule import com.discord.simpleast.core.node.Node import com.discord.simpleast.core.node.StyleNode import com.discord.simpleast.core.parser.ParseSpec @@ -134,9 +134,9 @@ object Kotlin { codeStyleProviders: CodeStyleProviders ): List, S>> = listOf( - PATTERN_KOTLIN_COMMENTS.toFullMatchRule(codeStyleProviders.commentStyleProvider), - PATTERN_KOTLIN_STRINGS.toFullMatchRule(codeStyleProviders.literalStyleProvider), - PATTERN_KOTLIN_ANNOTATION.toFullMatchRule(codeStyleProviders.genericsStyleProvider), + PATTERN_KOTLIN_COMMENTS.toMatchGroupRule(stylesProvider = codeStyleProviders.commentStyleProvider), + PATTERN_KOTLIN_STRINGS.toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider), + PATTERN_KOTLIN_ANNOTATION.toMatchGroupRule(stylesProvider = codeStyleProviders.genericsStyleProvider), FieldNode.createFieldRule(codeStyleProviders), FunctionNode.createFunctionRule(codeStyleProviders), )