From f6ec86c20df9bc0c5da9c9c1c03509f546feae18 Mon Sep 17 00:00:00 2001 From: gnembon <41132274+gnembon@users.noreply.github.com> Date: Wed, 19 Feb 2025 19:23:36 +0100 Subject: [PATCH] fix explain printing --- .../java/carpet/script/CarpetExpression.java | 3 +- src/main/java/carpet/script/Expression.java | 4 +- .../java/carpet/script/ScriptCommand.java | 52 ++++++++++++------- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 23971947..31a8111c 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -21,6 +21,7 @@ import net.minecraft.commands.CommandSourceStack; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.Entity; +import javax.annotation.Nullable; import java.util.List; public class CarpetExpression @@ -45,7 +46,7 @@ public class CarpetExpression return origin; } - public CarpetExpression(Module module, String expression, CommandSourceStack source, BlockPos origin) + public CarpetExpression(@Nullable Module module, String expression, CommandSourceStack source, BlockPos origin) { this.origin = origin; this.source = source; diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index dcf4f419..2854b87e 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -70,6 +70,7 @@ public class Expression private boolean allowNewlineSubstitutions = true; private boolean allowComments = false; + @Nullable public Module module = null; public String getModuleName() @@ -83,7 +84,7 @@ public class Expression allowComments = true; } - public void asAModule(Module mi) + public void asAModule(@Nullable Module mi) { module = mi; } @@ -91,6 +92,7 @@ public class Expression /** * Cached AST (Abstract Syntax Tree) (root) of the expression */ + @Nullable private LazyValue ast = null; /** diff --git a/src/main/java/carpet/script/ScriptCommand.java b/src/main/java/carpet/script/ScriptCommand.java index 109e9d68..3161f99c 100644 --- a/src/main/java/carpet/script/ScriptCommand.java +++ b/src/main/java/carpet/script/ScriptCommand.java @@ -19,9 +19,11 @@ import net.minecraft.network.chat.Component; import org.apache.commons.lang3.StringUtils; import java.io.IOException; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Deque; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -561,34 +563,43 @@ public class ScriptCommand }); } - private static void prettyPrintTokens(CommandSourceStack source, List tokens) { - //Collections.sort(tokens); - // indentations per line + private static void prettyPrintTokens(CommandSourceStack source, List tokens) + { Map indents = new HashMap<>(); for (Token token : tokens) { indents.put(token.lineno, Math.min(indents.getOrDefault(token.lineno, token.linepos), token.linepos)); } + List lines = new ArrayList<>(indents.keySet()); + Collections.sort(lines); + Deque indentsStack = new ArrayDeque<>(lines); - - int lastLine = 0; - int lastPos = 0; List elements = new ArrayList<>(); - for (Token token : tokens) + for (int i = 0; i < tokens.size(); i++) { - if (token.lineno != lastLine) + Token token = tokens.get(i); + if (!indentsStack.isEmpty() && token.lineno == indentsStack.peekFirst()) { flushElements(source, elements); - lastLine = token.lineno; - lastPos = indents.getOrDefault(token.lineno, 0); + indentsStack.pollFirst(); + // remove all elements of intendStack that don't have any more tokens + outer: while (!indentsStack.isEmpty()) + { + int nextLineNo = indentsStack.peekFirst(); + for (int j = i + 1; j < tokens.size(); j++) + { + if (tokens.get(j).lineno == nextLineNo) + { + break outer; + } + } + indentsStack.pollFirst(); + } + + int lastPos = indents.getOrDefault(token.lineno, 0); elements.add(Carpet.Messenger_compose("w " + " ".repeat(lastPos))); } - //else if (token.linepos > lastPos) - //{ - // lastPos = token.linepos; - //} elements.add(tokenToComponent(token)); - lastPos += token.surface.length() + 1; } flushElements(source, elements); } @@ -600,7 +611,8 @@ public class ScriptCommand { surface = token.display; } - if (token.comment.isEmpty()) { + if (token.comment.isEmpty()) + { return Carpet.Messenger_compose(styleForToken(token) + " " + surface); } return Carpet.Messenger_compose(styleForToken(token)+"br " + surface, "^gi " + token.comment); @@ -610,21 +622,21 @@ public class ScriptCommand { return switch (token.type) { case Token.TokenType.LITERAL -> "l"; - case Token.TokenType.HEX_LITERAL -> "e"; + case Token.TokenType.HEX_LITERAL -> "m"; case Token.TokenType.CONSTANT -> "q"; case Token.TokenType.OPEN_PAREN, Token.TokenType.CLOSE_PAREN, Token.TokenType.COMMA, Token.TokenType.OPERATOR, Token.TokenType.UNARY_OPERATOR -> "w"; - case Token.TokenType.FUNCTION -> "y"; + case Token.TokenType.FUNCTION -> "d"; case Token.TokenType.VARIABLE -> "y"; case Token.TokenType.STRINGPARAM -> "c"; default -> // marker - "w"; + "f"; }; } private static void flushElements(CommandSourceStack source, List elements) { - if (elements.size() > 0) + if (!elements.isEmpty()) { Carpet.Messenger_message(source, elements.toArray(new Component[0])); elements.clear();