Skip to content

Commit

Permalink
feat(api): Add possibility of using Component#join with a root style
Browse files Browse the repository at this point in the history
  • Loading branch information
KingOfSquares committed Dec 22, 2021
1 parent 1e075de commit 6296d98
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
19 changes: 18 additions & 1 deletion api/src/main/java/net/kyori/adventure/text/Component.java
Expand Up @@ -218,7 +218,24 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
*/
@Contract(pure = true)
static @NotNull Component join(final @NotNull JoinConfiguration config, final @NotNull Iterable<? extends ComponentLike> components) {
return JoinConfigurationImpl.join(config, components);
return join(config, components, Style.empty());
}

/**
* Joins {@code components} using the configuration in {@code config}.
*
* @param config the join configuration
* @param components the components
* @param rootStyle the style of the parent component containing all the joined components(e.g. if this is styled RED all joined components that does not have a color becomes RED)
* @return the resulting component
* @see JoinConfiguration#noSeparators()
* @see JoinConfiguration#separator(ComponentLike)
* @see JoinConfiguration#separators(ComponentLike, ComponentLike)
* @since 4.10.0
*/
@Contract(pure = true)
static @NotNull Component join(final @NotNull JoinConfiguration config, final @NotNull Iterable<? extends ComponentLike> components, final @NotNull Style rootStyle) {
return JoinConfigurationImpl.join(config, components, rootStyle);
}

/**
Expand Down
Expand Up @@ -28,6 +28,7 @@
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import net.kyori.adventure.text.format.Style;
import net.kyori.examination.ExaminableProperty;
import net.kyori.examination.string.StringExaminer;
import org.jetbrains.annotations.Contract;
Expand Down Expand Up @@ -135,15 +136,17 @@ public String toString() {
}

@Contract(pure = true)
static @NotNull Component join(final @NotNull JoinConfiguration config, final @NotNull Iterable<? extends ComponentLike> components) {
static @NotNull Component join(final @NotNull JoinConfiguration config, final @NotNull Iterable<? extends ComponentLike> components, final @NotNull Style rootStyle) {
Objects.requireNonNull(config, "config");
Objects.requireNonNull(components, "components");
Objects.requireNonNull(rootStyle, "rootStyle");

final Iterator<? extends ComponentLike> it = components.iterator();
final Component prefix = config.prefix();
final Component suffix = config.suffix();
final Function<ComponentLike, Component> convertor = config.convertor();
final Predicate<ComponentLike> predicate = config.predicate();
final boolean hasRootStyle = rootStyle != Style.empty();

if (!it.hasNext()) {
return singleElementJoin(config, null);
Expand All @@ -153,13 +156,21 @@ public String toString() {
int componentsSeen = 0;

if (!it.hasNext()) {
return singleElementJoin(config, component);
return singleElementJoin(
config,
hasRootStyle ?
Component.text().style(rootStyle).append(component).build() :
component
);
}

final Component separator = config.separator();
final boolean hasSeparator = separator != null;

final TextComponent.Builder builder = Component.text();
final TextComponent.Builder builder =
hasRootStyle ?
Component.text().style(rootStyle) :
Component.text();
if (prefix != null) builder.append(prefix);

while (component != null) {
Expand Down
41 changes: 41 additions & 0 deletions api/src/test/java/net/kyori/adventure/text/JoinTest.java
Expand Up @@ -23,9 +23,13 @@
*/
package net.kyori.adventure.text;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -319,6 +323,43 @@ final void testStandardJoinConfigurationsArrayLike() {
);
}

@Test
final void testJoinWithRootStyle() {
final Style style = Style.style(NamedTextColor.RED, TextDecoration.BOLD);
final Style epicStyle = Style.style(NamedTextColor.BLACK, TextDecoration.ITALIC);
final List<Component> componentsToJoin = new ArrayList<>();
componentsToJoin.add(Component.text("FIRST"));
componentsToJoin.add(Component.text("SECOND", style));
componentsToJoin.add(Component.text("THIRD"));
final Component result = Component.join(JoinConfiguration.commas(false), componentsToJoin, epicStyle);

assertEquals(
Component.text().style(epicStyle)
.append(Component.text("FIRST"))
.append(Component.text(","))
.append(Component.text("SECOND", style))
.append(Component.text(","))
.append(Component.text("THIRD"))
.build(),
result
);
}

@Test
final void testJoinWithRootStyleSingleComponent() {
final List<Component> componentsToJoin = new ArrayList<>();
componentsToJoin.add(Component.text("FIRST"));
final Style epicStyle = Style.style(NamedTextColor.BLACK, TextDecoration.ITALIC);
final Component result = Component.join(JoinConfiguration.commas(false), componentsToJoin, epicStyle);

assertEquals(
Component.text().style(epicStyle)
.append(Component.text("FIRST"))
.build(),
result
);
}

private static final class TestComponentLike implements ComponentLike {

@Override
Expand Down

0 comments on commit 6296d98

Please sign in to comment.