Skip to content

Commit

Permalink
Merge pull request #786 from KingOfSquares/append-convenience
Browse files Browse the repository at this point in the history
Closes #235
  • Loading branch information
zml2008 committed Nov 7, 2022
2 parents 8f736f9 + d0402e7 commit c624efc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
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());
}
}

0 comments on commit c624efc

Please sign in to comment.