Skip to content

Commit

Permalink
fix explain printing
Browse files Browse the repository at this point in the history
  • Loading branch information
gnembon committed Feb 19, 2025
1 parent d40d9b7 commit f6ec86c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/main/java/carpet/script/CarpetExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;

import javax.annotation.Nullable;
import java.util.List;

public class CarpetExpression
Expand All @@ -45,7 +46,7 @@ public BlockPos getOrigin()
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;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/carpet/script/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ String getCodeString()
private boolean allowNewlineSubstitutions = true;
private boolean allowComments = false;

@Nullable
public Module module = null;

public String getModuleName()
Expand All @@ -83,14 +84,15 @@ public void asATextSource()
allowComments = true;
}

public void asAModule(Module mi)
public void asAModule(@Nullable Module mi)
{
module = mi;
}

/**
* Cached AST (Abstract Syntax Tree) (root) of the expression
*/
@Nullable
private LazyValue ast = null;

/**
Expand Down
52 changes: 32 additions & 20 deletions src/main/java/carpet/script/ScriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
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;
Expand Down Expand Up @@ -561,34 +563,43 @@ private static int explain(CommandContext<CommandSourceStack> context, @Nullable
});
}

private static void prettyPrintTokens(CommandSourceStack source, List<Token> tokens) {
//Collections.sort(tokens);
// indentations per line
private static void prettyPrintTokens(CommandSourceStack source, List<Token> tokens)
{
Map<Integer, Integer> indents = new HashMap<>();
for (Token token : tokens)
{
indents.put(token.lineno, Math.min(indents.getOrDefault(token.lineno, token.linepos), token.linepos));
}
List<Integer> lines = new ArrayList<>(indents.keySet());
Collections.sort(lines);
Deque<Integer> indentsStack = new ArrayDeque<>(lines);


int lastLine = 0;
int lastPos = 0;
List<Component> 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);
}
Expand All @@ -600,7 +611,8 @@ private static Component tokenToComponent(Token token)
{
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);
Expand All @@ -610,21 +622,21 @@ private static String styleForToken(Token token)
{
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<Component> elements)
{
if (elements.size() > 0)
if (!elements.isEmpty())
{
Carpet.Messenger_message(source, elements.toArray(new Component[0]));
elements.clear();
Expand Down

0 comments on commit f6ec86c

Please sign in to comment.