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

Commit

Permalink
two highlighting fixes (counts and surrogates)
Browse files Browse the repository at this point in the history
  • Loading branch information
homchom committed Dec 21, 2023
1 parent 20fcf40 commit 6a15a9b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class EditBoxExpressionFormatter(
min(startIndex, partialEnd) - partialStart
))

if (partialRange.first in startIndex..<endIndex || partialRange.last in startIndex..<endIndex) {
if (startIndex in partialRange || endIndex - 1 in partialRange) {
append(highlighted.text.subSequence(
max(startIndex, partialStart) - startIndex,
min(endIndex, partialEnd) - startIndex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import net.minecraft.util.FormattedCharSequence
* @see CharSequence.subSequence
*/
fun FormattedCharSequence.subSequence(startIndex: Int, endIndex: Int) = FormattedCharSequence { sink ->
var index = 0
var adjustedIndex = 0
accept { _, style, codePoint ->
if (index++ in startIndex..<endIndex) {
sink.accept(adjustedIndex++, style, codePoint)
acceptWithAbsoluteIndex { index, style, codePoint ->
if (index in startIndex..<endIndex) {
sink.accept(index - startIndex, style, codePoint)
} else true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ fun emptyText(style: StyleWrapper = style()) =
/**
* Builds a [Component] by adding [style] to [root] and applying [builder].
*
* Use [translatedText] and [literalText] when applicable, as their output is more optimized.
* Use [formattedCharSequence] if all text is literal and a [Component] representation is not necessary, as it
* is the most optimized.
* Use [translatedText] and [literalText] when applicable, as their output is more optimized. Also note
* that some functions take a [net.minecraft.util.FormattedCharSequence]; in those cases you should build
* a [formattedCharSequence] directly.
*
* @see TextBuilder
*/
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/io/github/homchom/recode/ui/text/TextFunctions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

package io.github.homchom.recode.ui.text

import io.github.homchom.recode.util.fromCodePoint
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.Style
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer
import net.minecraft.util.FormattedCharSequence
import net.minecraft.util.FormattedCharSink

/**
* @return A new [Component] created by merging this Component's style with [style], using [strategy] and [merges].
Expand Down Expand Up @@ -78,4 +80,19 @@ fun Regex.matchEntirePlain(text: Component) = matchEntire(text.plainText)
/**
* @return Whether [text]'s entire [plainText] matches this [Regex] pattern.
*/
fun Regex.matchesPlain(text: Component) = matches(text.plainText)
fun Regex.matchesPlain(text: Component) = matches(text.plainText)

/**
* [FormattedCharSequence.accept]s this [FormattedCharSequence], adjusting the `index` parameter
* passed to [sink] to be absolute instead of relative. Surrogate pairs are handled but not validated.
*/
fun FormattedCharSequence.acceptWithAbsoluteIndex(sink: FormattedCharSink): Boolean {
var absoluteIndex = 0
return accept { _, style, codePoint ->
val shouldContinue = sink.accept(absoluteIndex++, style, codePoint)
if (String.fromCodePoint(codePoint)[0].isHighSurrogate()) {
absoluteIndex++
}
shouldContinue
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ fun Boolean.unitOrNull() = if (this) Unit else null

// strings

/**
* @see Character.toString
*/
fun String.Companion.fromCodePoint(codePoint: Int): String = Character.toString(codePoint)

/**
* Appends multiple [substrings] to this [StringBuilder].
*/
Expand Down

0 comments on commit 6a15a9b

Please sign in to comment.