aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt
index f5c25ab2b..aed388ef3 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt
@@ -31,6 +31,19 @@ object ComponentMatcherUtils {
)
}
+ fun ComponentSpan.replace(pattern: Pattern, replacement: ComponentMatcher.() -> IChatComponent): ChatComponentText {
+ val matcher = pattern.matcher(getText())
+ val elements = ChatComponentText("")
+ var lastIndex = 0
+ while (matcher.find()) {
+ elements.appendSibling(slice(lastIndex, matcher.start()).intoComponent())
+ elements.appendSibling(replacement(ComponentMatcher(matcher, this)))
+ lastIndex = matcher.end()
+ }
+ elements.appendSibling(slice(lastIndex, length).intoComponent())
+ return elements
+ }
+
/**
* Create a styled matcher, analogous to [Pattern.matcher], but while preserving [ChatStyle].
*/