Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
add <space> support
Browse files Browse the repository at this point in the history
  • Loading branch information
homchom committed Dec 16, 2023
1 parent 27c51f8 commit 1987908
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ For Minecraft 1.20.2, Fabric API 0.90.7 or newer
- More obscure fixes to location overlay and state-related features (thanks ShadowEmpress_)

### Removals
- Removed `/schem`
- Removed `/schem` (#65)
- Removed `/gradient` in favor of `<gradient>`
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.github.homchom.recode.feature.visual

import io.github.homchom.recode.hypercube.CommandAliasGroup
import io.github.homchom.recode.hypercube.DFValueMeta
import io.github.homchom.recode.hypercube.dfMiniMessage
import io.github.homchom.recode.hypercube.dfValueMeta
import io.github.homchom.recode.hypercube.*
import io.github.homchom.recode.ui.text.*
import io.github.homchom.recode.util.regex.regex
import net.kyori.adventure.text.BuildableComponent
Expand Down Expand Up @@ -77,6 +74,9 @@ class ExpressionHighlighter {
0xff4242
)

private val miniMessage = dfMiniMessage
private val miniMessageHighlighter = MiniMessageHighlighter(DFMiniMessageTags.all)

private val codeRegex = regex {
group {
str("%")
Expand Down Expand Up @@ -171,15 +171,15 @@ class ExpressionHighlighter {

if (parseMiniMessage) builder.raw.mapChildren { text ->
if (text is TextComponent && text.style().isEmpty) {
MiniMessageHighlighter.highlight(text.content()) as BuildableComponent<*, *>
miniMessageHighlighter.highlight(text.content()) as BuildableComponent<*, *>
} else {
text
}
}

val text = builder.build().toFormattedCharSequence(false)
val preview = if (parseMiniMessage) {
dfMiniMessage.deserialize(string).toFormattedCharSequence()
miniMessage.deserialize(string).toFormattedCharSequence()
} else null
return HighlightedExpression(text, preview)
}
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/io/github/homchom/recode/hypercube/DFMiniMessage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.github.homchom.recode.hypercube

import io.github.homchom.recode.ui.text.literalText
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.minimessage.tag.Tag
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags

/**
* A [MiniMessage] instance to match DiamondFire's MiniMessage behavior.
*/
val dfMiniMessage = MiniMessage.builder().run {
tags(DFMiniMessageTags.all)
build()
}

/**
* An object providing access to DiamondFire's MiniMessage [TagResolver]s.
*
* @see dfMiniMessage
* @see StandardTags
*/
@Suppress("MemberVisibilityCanBePrivate")
object DFMiniMessageTags {
inline val standard get() = StandardTags.defaults()

val space = TagResolver.resolver("space") { args, context ->
val count = if (args.hasNext()) {
args.pop().asInt().orElseThrow {
context.newException("Count must be a number")
}
} else 1
if (count < 0) throw context.newException("Count must be non-negative")
Tag.inserting(literalText(" ".repeat(count)))
}

val all = TagResolver.resolver(standard, space)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

package io.github.homchom.recode.hypercube

import net.kyori.adventure.text.minimessage.MiniMessage

const val SERVER_ADDRESS = "mcdiamondfire.com"

const val DIAMOND = ''
Expand All @@ -14,9 +12,4 @@ const val BOOSTER_ARROW = '⏵'

const val TOKEN_NOTCH_CHAR = ''

const val LAGSLAYER_PREFIX = """[LagSlayer]"""

/**
* A [MiniMessage] instance to match DiamondFire's MiniMessage behavior.
*/
val dfMiniMessage = MiniMessage.miniMessage()
const val LAGSLAYER_PREFIX = """[LagSlayer]"""
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.homchom.recode.ui.text

import io.github.homchom.recode.ui.text.MiniMessageHighlighter.highlight
import io.github.homchom.recode.util.interpolate
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.TextDecoration
Expand All @@ -20,7 +19,7 @@ import net.kyori.adventure.text.minimessage.tree.Node
*
* @see highlight
*/
object MiniMessageHighlighter {
class MiniMessageHighlighter(private val standardTags: TagResolver) {
// a resolver for tags that determine their own style
private val nonLiteralTags = TagResolver.resolver(
StandardTags.color(),
Expand All @@ -33,8 +32,6 @@ object MiniMessageHighlighter {
StandardTags.rainbow()
)

private val standardTags = TagResolver.standard()

private val instance = MiniMessage.builder().run {
tags(object : TagResolver {
override fun resolve(name: String, arguments: ArgumentQueue, ctx: Context): Tag? {
Expand Down

0 comments on commit 1987908

Please sign in to comment.