diff options
| author | Linnea Gräf <nea@nea.moe> | 2024-05-03 19:34:32 +0200 |
|---|---|---|
| committer | Linnea Gräf <nea@nea.moe> | 2024-05-03 19:34:32 +0200 |
| commit | 9ba9c9bf092b5873ab28162e105574d6c994300e (patch) | |
| tree | 4d9b3bbdf4da18819e26fa8e9a4d4d5260ccf4dd /src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt | |
| parent | e210a5849b2c31628489568470580ec0416285e7 (diff) | |
| download | SkyHanni-feat/styledreplace.tar.gz SkyHanni-feat/styledreplace.tar.bz2 SkyHanni-feat/styledreplace.zip | |
Basic styled replacefeat/styledreplace
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt')
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt | 13 |
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]. */ |
