-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
simpleast-core/src/main/java/com/discord/simpleast/code/Xml.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.discord.simpleast.code | ||
|
||
import android.text.SpannableStringBuilder | ||
import android.text.Spanned | ||
import com.discord.simpleast.core.node.Node | ||
import com.discord.simpleast.core.parser.ParseSpec | ||
import com.discord.simpleast.core.parser.Parser | ||
import com.discord.simpleast.core.parser.Rule | ||
import java.util.regex.Matcher | ||
import java.util.regex.Pattern | ||
|
||
|
||
object Xml { | ||
val PATTERN_XML_COMMENT = Pattern.compile("""^<!--[\s\S]*?-->""", Pattern.DOTALL) | ||
|
||
val PATTERN_XML_TAG = Pattern.compile("""^<([\s\S]+?)(?:>(.*?)<\/([\s\S]+?))?>""", Pattern.DOTALL) | ||
val PATTERN_XML_TAG_OPENING_GROUP = 1 | ||
val PATTERN_XML_TAG_CONTENT_GROUP = 2 | ||
val PATTERN_XML_TAG_CLOSING_GROUP = 3 | ||
|
||
class TagNode<RC>( | ||
val opening: String, val closing: String?, | ||
val codeStyleProviders: CodeStyleProviders<RC> | ||
) : Node.Parent<RC>() { | ||
override fun render(builder: SpannableStringBuilder, renderContext: RC) { | ||
val (name, remainder) = when (val index = opening.indexOfFirst { it.isWhitespace() || it.equals("/") }) { | ||
-1 -> opening to "" | ||
else -> opening.substring(0, index) to opening.substring(index) | ||
} | ||
val typeStylesProvider = codeStyleProviders.genericsStyleProvider::get | ||
|
||
var startIndex = builder.length | ||
builder.append("<$name") | ||
typeStylesProvider(renderContext).forEach { | ||
builder.setSpan(it, startIndex, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) | ||
} | ||
|
||
startIndex = builder.length | ||
builder.append("$remainder>") | ||
codeStyleProviders.paramsStyleProvider.get(renderContext).forEach { | ||
builder.setSpan(it, startIndex, builder.length - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) | ||
} | ||
typeStylesProvider(renderContext).forEach { | ||
builder.setSpan(it, builder.length - 1, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) | ||
} | ||
|
||
super.render(builder, renderContext) | ||
|
||
if (!closing.isNullOrEmpty()) { | ||
startIndex = builder.length | ||
builder.append("</$closing>") | ||
typeStylesProvider(renderContext).forEach { | ||
builder.setSpan(it, startIndex + 1, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun <RC, S> createTagRule( | ||
codeStyleProviders: CodeStyleProviders<RC> | ||
) = | ||
object : Rule<RC, Node<RC>, S>(PATTERN_XML_TAG) { | ||
override fun parse(matcher: Matcher, parser: Parser<RC, in Node<RC>, S>, state: S): | ||
ParseSpec<RC, Node<RC>, S> { | ||
val opening = matcher.group(PATTERN_XML_TAG_OPENING_GROUP)!! | ||
val closing = matcher.group(PATTERN_XML_TAG_CLOSING_GROUP) | ||
|
||
return if (matcher.group(PATTERN_XML_TAG_CONTENT_GROUP) != null) { | ||
ParseSpec.createNonterminal( | ||
TagNode(opening, closing, codeStyleProviders), | ||
state, | ||
matcher.start(PATTERN_XML_TAG_CONTENT_GROUP), | ||
matcher.end(PATTERN_XML_TAG_CONTENT_GROUP)) | ||
} else { | ||
ParseSpec.createTerminal( | ||
TagNode(opening, closing, codeStyleProviders), | ||
state) | ||
} | ||
} | ||
} | ||
} |