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

text-minimessage: Use a Consumer rather than Appendable for debug output #671

Merged
merged 2 commits into from Feb 5, 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
Expand Up @@ -23,6 +23,7 @@
*/
package net.kyori.adventure.text.minimessage;

import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.placeholder.PlaceholderResolver;
Expand All @@ -38,15 +39,15 @@
*/
class ContextImpl implements Context {
private final boolean strict;
private final Appendable debugOutput;
private final Consumer<String> debugOutput;
private final String originalMessage;
private final MiniMessage miniMessage;
private final PlaceholderResolver placeholderResolver;
private final UnaryOperator<Component> postProcessor;

ContextImpl(
final boolean strict,
final Appendable debugOutput,
final Consumer<String> debugOutput,
final String originalMessage,
final MiniMessage miniMessage,
final @NotNull PlaceholderResolver placeholderResolver,
Expand All @@ -62,7 +63,7 @@ class ContextImpl implements Context {

static ContextImpl of(
final boolean strict,
final Appendable debugOutput,
final Consumer<String> debugOutput,
final String input,
final MiniMessageImpl miniMessage,
final PlaceholderResolver placeholderResolver,
Expand All @@ -75,7 +76,7 @@ public boolean strict() {
return this.strict;
}

public Appendable debugOutput() {
public Consumer<String> debugOutput() {
return this.debugOutput;
}

Expand Down
Expand Up @@ -185,7 +185,7 @@ interface Builder extends Buildable.Builder<MiniMessage> {
* @return this builder
* @since 4.10.0
*/
@NotNull Builder debug(final @Nullable Appendable debugOutput);
@NotNull Builder debug(final @Nullable Consumer<String> debugOutput);
zml2008 marked this conversation as resolved.
Show resolved Hide resolved

/**
* If in lenient mode, MiniMessage will output helpful messages.
Expand Down
Expand Up @@ -47,12 +47,12 @@ final class MiniMessageImpl implements MiniMessage {
static final MiniMessage INSTANCE = new MiniMessageImpl(TransformationRegistry.standard(), PlaceholderResolver.empty(), false, null, DEFAULT_ERROR_CONSUMER, DEFAULT_COMPACTING_METHOD);

private final boolean strict;
private final Appendable debugOutput;
private final @Nullable Consumer<String> debugOutput;
private final Consumer<List<String>> parsingErrorMessageConsumer;
private final UnaryOperator<Component> postProcessor;
final MiniMessageParser parser;

MiniMessageImpl(final @NotNull TransformationRegistry registry, final @NotNull PlaceholderResolver placeholderResolver, final boolean strict, final Appendable debugOutput, final @NotNull Consumer<List<String>> parsingErrorMessageConsumer, final @NotNull UnaryOperator<Component> postProcessor) {
MiniMessageImpl(final @NotNull TransformationRegistry registry, final @NotNull PlaceholderResolver placeholderResolver, final boolean strict, final @Nullable Consumer<String> debugOutput, final @NotNull Consumer<List<String>> parsingErrorMessageConsumer, final @NotNull UnaryOperator<Component> postProcessor) {
this.parser = new MiniMessageParser(registry, placeholderResolver);
this.strict = strict;
this.debugOutput = debugOutput;
Expand Down Expand Up @@ -122,7 +122,7 @@ static final class BuilderImpl implements Builder {
private TransformationRegistry registry = TransformationRegistry.standard();
private PlaceholderResolver placeholderResolver = null;
private boolean strict = false;
private Appendable debug = null;
private Consumer<String> debug = null;
private Consumer<List<String>> parsingErrorMessageConsumer = DEFAULT_ERROR_CONSUMER;
private UnaryOperator<Component> postProcessor = DEFAULT_COMPACTING_METHOD;

Expand Down Expand Up @@ -164,7 +164,7 @@ static final class BuilderImpl implements Builder {
}

@Override
public @NotNull Builder debug(final @Nullable Appendable debugOutput) {
public @NotNull Builder debug(final @Nullable Consumer<String> debugOutput) {
this.debug = debugOutput;
return this;
}
Expand Down
Expand Up @@ -23,13 +23,13 @@
*/
package net.kyori.adventure.text.minimessage;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -120,45 +120,47 @@ private void processTokens(final @NotNull StringBuilder sb, final @NotNull Strin

@NotNull Component parseFormat(final @NotNull String richMessage, final @NotNull ContextImpl context) {
final PlaceholderResolver combinedResolver = PlaceholderResolver.combining(context.placeholderResolver(), this.placeholderResolver);
final Appendable debug = context.debugOutput();
final Consumer<String> debug = context.debugOutput();
if (debug != null) {
try {
debug.append("Beginning parsing message ").append(richMessage).append('\n');
} catch (final IOException ignored) {
}
debug.accept("Beginning parsing message ");
debug.accept(richMessage);
debug.accept("\n");
zml2008 marked this conversation as resolved.
Show resolved Hide resolved
}

final Function<TagNode, Transformation> transformationFactory;
if (debug != null) {
transformationFactory = node -> {
try {
try {
debug.append("Attempting to match node '").append(node.name()).append("' at column ")
.append(String.valueOf(node.token().startIndex())).append('\n');
} catch (final IOException ignored) {
}
debug.accept("Attempting to match node '");
debug.accept(node.name());
debug.accept("' at column ");
debug.accept(String.valueOf(node.token().startIndex()));
debug.accept("\n");

final Transformation transformation = this.registry.get(this.sanitizePlaceholderName(node.name()), node.parts(), combinedResolver, context);

try {
if (transformation == null) {
debug.append("Could not match node '").append(node.name()).append("'\n");
} else {
debug.append("Successfully matched node '").append(node.name()).append("' to transformation ")
.append(transformation.examinableName()).append('\n');
}
} catch (final IOException ignored) {
if (transformation == null) {
debug.accept("Could not match node '");
debug.accept(node.name());
debug.accept("'\n");
} else {
debug.accept("Successfully matched node '");
debug.accept(node.name());
debug.accept("' to transformation ");
debug.accept(transformation.examinableName());
debug.accept("\n");
}

return transformation;
} catch (final ParsingException e) {
try {
if (e.tokens().length == 0) {
e.tokens(new Token[]{node.token()});
}
debug.append("Could not match node '").append(node.name()).append("' - ").append(e.getMessage()).append('\n');
} catch (final IOException ignored) {
if (e.tokens().length == 0) {
e.tokens(new Token[]{node.token()});
}
debug.accept("Could not match node '");
debug.accept(node.name());
debug.accept("' - ");
debug.accept(e.getMessage());
debug.accept("\n");
return null;
}
};
Expand All @@ -179,11 +181,8 @@ private void processTokens(final @NotNull StringBuilder sb, final @NotNull Strin
final ElementNode root = TokenParser.parse(transformationFactory, tagNameChecker, combinedResolver, richMessage, context.strict());

if (debug != null) {
try {
debug.append("Text parsed into element tree:\n");
debug.append(root.toString());
} catch (final IOException ignored) {
}
debug.accept("Text parsed into element tree:\n");
debug.accept(root.toString());
}

return Objects.requireNonNull(context.postProcessor().apply(this.treeToComponent(root, context)), "Post-processor must not return null");
Expand Down Expand Up @@ -225,12 +224,13 @@ private void processTokens(final @NotNull StringBuilder sb, final @NotNull Strin
comp = this.handleModifying((Modifying) transformation, comp, 0);
}

final Appendable debug = context.debugOutput();
final Consumer<String> debug = context.debugOutput();
if (debug != null) {
try {
debug.append("==========\ntreeToComponent \n").append(node.toString()).append("\n").append(comp.examine(MultiLineStringExaminer.simpleEscaping()).collect(Collectors.joining("\n"))).append("\n==========\n");
} catch (final IOException ignored) {
}
debug.accept("==========\ntreeToComponent \n");
debug.accept(node.toString());
debug.accept("\n");
debug.accept(comp.examine(MultiLineStringExaminer.simpleEscaping()).collect(Collectors.joining("\n")));
debug.accept("\n==========\n");
}

return comp;
Expand Down
Expand Up @@ -366,7 +366,7 @@ void debugModeSimple() {
final String input = "<red> RED </red>";

final StringBuilder sb = new StringBuilder();
MiniMessage.builder().debug(sb).build().deserialize(input);
MiniMessage.builder().debug(sb::append).build().deserialize(input);
final List<String> messages = Arrays.asList(sb.toString().split("\n"));

assertTrue(messages.contains("Beginning parsing message <red> RED </red>"));
Expand All @@ -385,7 +385,7 @@ void debugModeMoreComplex() {
final String input = "<red> RED <blue> BLUE <click> bad click </click>";

final StringBuilder sb = new StringBuilder();
MiniMessage.builder().debug(sb).build().deserialize(input);
MiniMessage.builder().debug(sb::append).build().deserialize(input);
final List<String> messages = Arrays.asList(sb.toString().split("\n"));

assertTrue(messages.contains("Beginning parsing message <red> RED <blue> BLUE <click> bad click </click>"));
Expand Down Expand Up @@ -413,7 +413,7 @@ void debugModeMoreComplexNoError() {
final String input = "<red> RED <blue> BLUE <click:open_url:https://github.com> good click </click>";

final StringBuilder sb = new StringBuilder();
MiniMessage.builder().debug(sb).build().deserialize(input);
MiniMessage.builder().debug(sb::append).build().deserialize(input);
final List<String> messages = Arrays.asList(sb.toString().split("\n"));

assertTrue(messages.contains("Beginning parsing message <red> RED <blue> BLUE <click:open_url:https://github.com> good click </click>"));
Expand Down
Expand Up @@ -34,7 +34,7 @@

public class TestBase {

final MiniMessage PARSER = MiniMessage.builder().debug(System.out).build();
final MiniMessage PARSER = MiniMessage.builder().debug(System.out::print).build();

void assertParsedEquals(final @NotNull Component expected, final @NotNull String input) {
this.assertParsedEquals(this.PARSER, expected, input);
Expand Down