Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience methods for appending a newline/space to a component #786

Merged
merged 1 commit into from Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Expand Up @@ -1733,6 +1733,28 @@ default void detectCycle(final @NotNull Component that) {
return this.append(builder.build());
}

/**
* Appends a newline to this component.
*
* @return a component with the newline added
* @since 4.12.0
*/
@Contract(pure = true)
default @NotNull Component appendNewline() {
return this.append(newline());
}

/**
* Appends a space to this component.
*
* @return a component with the space added
* @since 4.12.0
*/
@Contract(pure = true)
default @NotNull Component appendSpace() {
return this.append(space());
}

/**
* Apply a fallback style for this component and its children.
*
Expand Down
20 changes: 20 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java
Expand Up @@ -115,6 +115,26 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
@Contract("_ -> this")
@NotNull B append(final @NotNull Iterable<? extends ComponentLike> components);

/**
* Appends a newline to this component.
*
* @return this builder
* @since 4.12.0
*/
default @NotNull B appendNewline() {
return this.append(Component.newline());
}

/**
* Appends a space to this component.
*
* @return this builder
* @since 4.12.0
*/
default @NotNull B appendSpace() {
return this.append(Component.space());
}

/**
* Applies an action to this builder.
*
Expand Down
20 changes: 20 additions & 0 deletions api/src/test/java/net/kyori/adventure/text/TextComponentTest.java
Expand Up @@ -166,4 +166,24 @@ void testWrapping() {
assertEquals(wrappedItalic.compact(), Component.text("italic").decoration(TextDecoration.ITALIC, true));
assertEquals(wrappedNotItalic.compact(), Component.text("non-italic").decoration(TextDecoration.ITALIC, false));
}

@Test
void testAppendNewline() {
final Component c0 = Component.text("tuba").appendNewline().append(Component.text("time"));
final Component c1 = Component.text().content("tuba").appendNewline().append(Component.text("time")).build();

final Component c0Compact = c0.compact();
assertEquals(c0Compact, Component.text("tuba\ntime"));
assertEquals(c0Compact, c1.compact());
}

@Test
void testAppendSpace() {
final Component c0 = Component.text("tuba").appendSpace().append(Component.text("time"));
final Component c1 = Component.text().content("tuba").appendSpace().append(Component.text("time")).build();

final Component c0Compact = c0.compact();
assertEquals(c0Compact, Component.text("tuba time"));
assertEquals(c0Compact, c1.compact());
}
}