Skip to content

Commit

Permalink
fix(api): Handle virtual components in compaction
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Mar 14, 2023
1 parent ff482d5 commit 8cdca8b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static Component compact(final @NotNull Component self, final @Nullable Style pa
}

// if there is only one child, check if self a useless empty component
if (childrenSize == 1 && optimized instanceof TextComponent) {
if (childrenSize == 1 && isText(optimized)) {
final TextComponent textComponent = (TextComponent) optimized;

if (textComponent.content().isEmpty()) {
Expand All @@ -87,7 +87,7 @@ static Component compact(final @NotNull Component self, final @Nullable Style pa
child = compact(child, childParentStyle);

// ignore useless empty children (regardless of its style)
if (child.children().isEmpty() && child instanceof TextComponent) {
if (child.children().isEmpty() && isText(child)) {
final TextComponent textComponent = (TextComponent) child;

if (textComponent.content().isEmpty()) {
Expand All @@ -99,12 +99,12 @@ static Component compact(final @NotNull Component self, final @Nullable Style pa
}

// try to merge children into this parent component
if (optimized instanceof TextComponent) {
if (isText(optimized)) {
while (!childrenToAppend.isEmpty()) {
final Component child = childrenToAppend.get(0);
final Style childStyle = child.style().merge(childParentStyle, Style.Merge.Strategy.IF_ABSENT_ON_TARGET);

if (child instanceof TextComponent && Objects.equals(childStyle, childParentStyle)) {
if (isText(child) && Objects.equals(childStyle, childParentStyle)) {
// merge child components into the parent if they are a text component with the same effective style
// in context of their parent style
optimized = joinText((TextComponent) optimized, (TextComponent) child);
Expand All @@ -125,7 +125,7 @@ static Component compact(final @NotNull Component self, final @Nullable Style pa
final Component child = childrenToAppend.get(i);
final Component neighbor = childrenToAppend.get(i + 1);

if (child.children().isEmpty() && child instanceof TextComponent && neighbor instanceof TextComponent) {
if (child.children().isEmpty() && isText(child) && isText(neighbor)) {
// calculate the children's styles in context of their parent style
final Style childStyle = child.style().merge(childParentStyle, Style.Merge.Strategy.IF_ABSENT_ON_TARGET);
final Style neighborStyle = neighbor.style().merge(childParentStyle, Style.Merge.Strategy.IF_ABSENT_ON_TARGET);
Expand Down Expand Up @@ -162,7 +162,7 @@ static Component compact(final @NotNull Component self, final @Nullable Style pa
* @return true if the provided component is blank, false otherwise
*/
private static boolean isBlank(final Component component) {
if (component instanceof TextComponent) {
if (isText(component)) {
final TextComponent textComponent = (TextComponent) component;

final String content = textComponent.content();
Expand Down Expand Up @@ -215,4 +215,8 @@ private static boolean isBlank(final Component component) {
private static TextComponent joinText(final TextComponent one, final TextComponent two) {
return TextComponentImpl.create(two.children(), one.style(), one.content() + two.content());
}

private static boolean isText(final Component component) {
return component instanceof TextComponent && !(component instanceof VirtualComponent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static net.kyori.adventure.text.Component.empty;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.Component.translatable;
import static net.kyori.adventure.text.Component.virtual;
import static net.kyori.adventure.text.JoinConfiguration.noSeparators;
import static net.kyori.adventure.text.format.Style.style;
import static net.kyori.adventure.text.format.TextColor.color;
Expand Down Expand Up @@ -417,4 +418,12 @@ void testColorPreservedWithDecorations() {
assertEquals(expectedComponent, expectedComponent.compact());
}

@Test
void testVirtualComponentsPreserved() {
final Component expectedComponent = virtual(() -> "meow :3")
.append(text("3"));

assertEquals(expectedComponent, expectedComponent.compact());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.NBTComponent;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.VirtualComponentHolder;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
Expand All @@ -43,7 +44,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

class ComponentFlattenerTest {

static class TrackingFlattener implements FlattenerListener {
int pushCount;
int popCount;
Expand Down Expand Up @@ -192,6 +192,14 @@ void testKeybind() {
.assertContents();
}

@Test
void testVirtualComponent() {
this.testFlatten(ComponentFlattener.basic(), Component.virtual((VirtualComponentHolder<String>) () -> "test123"))
.assertBalanced()
.assertPushesAndPops(1)
.assertContents("test123");
}

@Test
void testCustomHandler() {
final ComponentFlattener customized = ComponentFlattener.basic()
Expand Down

0 comments on commit 8cdca8b

Please sign in to comment.