Skip to content

Commit

Permalink
text-minimessage: Use a Consumer rather than Appendable for debug output
Browse files Browse the repository at this point in the history
Fixes GH-667
  • Loading branch information
zml2008 committed Feb 4, 2022
1 parent 41d2a9d commit 2734f2a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 48 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);

/**
* If in lenient mode, MiniMessage will output helpful messages.
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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");
}

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 2734f2a

Please sign in to comment.