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 a new tag for line breaks (<br>) #687

Merged
merged 2 commits into from Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,49 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2022 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.minimessage.tag.standard;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.Context;
import net.kyori.adventure.text.minimessage.ParsingException;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue;
import org.jetbrains.annotations.ApiStatus;

/**
* Newline tag.
*
* @since 4.10.0
*/
@ApiStatus.Internal
public final class NewlineTag {
public static final String NEWLINE_2 = "newline";
Joo200 marked this conversation as resolved.
Show resolved Hide resolved
public static final String NEWLINE = "br";

private NewlineTag() {
}

static Tag create(final ArgumentQueue args, final Context ctx) throws ParsingException {
return Tag.inserting(Component.newline());
}
}
Expand Up @@ -64,6 +64,7 @@ private StandardTags() {
private static final TagResolver GRADIENT = TagResolver.resolver(GradientTag.GRADIENT, GradientTag::create);
private static final TagResolver RAINBOW = TagResolver.resolver(RainbowTag.RAINBOW, RainbowTag::create);
private static final TagResolver RESET = TagResolver.resolver(RESET_TAG, ParserDirective.RESET);
private static final TagResolver NEW_LINE = TagResolver.resolver(names(NewlineTag.NEWLINE, NewlineTag.NEWLINE_2), NewlineTag::create);
Joo200 marked this conversation as resolved.
Show resolved Hide resolved
private static final TagResolver ALL = TagResolver.builder()
.resolvers(
HOVER_EVENT,
Expand All @@ -76,7 +77,8 @@ private StandardTags() {
DECORATION,
GRADIENT,
RAINBOW,
RESET
RESET,
NEW_LINE
Joo200 marked this conversation as resolved.
Show resolved Hide resolved
)
.build();

Expand Down Expand Up @@ -196,6 +198,18 @@ public static TagResolver reset() {
return RESET;
}

/**
* Get a resolver for the {@value NewlineTag#NEWLINE} tag.
*
* <p>This tag also responds to {@value NewlineTag#NEWLINE_2}.</p>
*
* @return a resolver for the {@value NewlineTag#NEWLINE} tag.
* @since 4.10.0
*/
public static TagResolver newline() {
return NEW_LINE;
Joo200 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Get a resolver that handles all default standard tags.
*
Expand Down
Expand Up @@ -1711,4 +1711,18 @@ void testTreeOutput() {

assertEquals(expected, tree.toString());
}

@Test
void testNewLine() {
final String input = "<red>Line<br><gray>break!";
final Component expected = Component.text().color(RED)
.append(
text("Line"),
Component.newline(),
text("break!", color(GRAY))
)
.build();

this.assertParsedEquals(expected, input);
}
}