Skip to content

Commit

Permalink
text-minimessage: Add depth to the Modifying interface
Browse files Browse the repository at this point in the history
Needed for #509
  • Loading branch information
zml2008 committed Feb 28, 2022
1 parent c66a4eb commit 7f4ddc2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package net.kyori.adventure.text.minimessage;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -217,12 +216,7 @@ private void processTokens(final @NotNull StringBuilder sb, final @NotNull Strin
final Modifying modTransformation = (Modifying) tag;

// first walk the tree
final LinkedList<ElementNode> toVisit = new LinkedList<>(node.unsafeChildren());
while (!toVisit.isEmpty()) {
final ElementNode curr = toVisit.removeFirst();
modTransformation.visit(curr);
toVisit.addAll(0, curr.unsafeChildren());
}
this.visitModifying(modTransformation, tagNode, 0);
modTransformation.postVisit();
}

Expand Down Expand Up @@ -257,6 +251,13 @@ private void processTokens(final @NotNull StringBuilder sb, final @NotNull Strin
return comp;
}

private void visitModifying(final Modifying modTransformation, final ElementNode node, final int depth) {
modTransformation.visit(node, depth);
for (final ElementNode child : node.unsafeChildren()) {
this.visitModifying(modTransformation, child, depth + 1);
}
}

private Component handleModifying(final Modifying modTransformation, final Component current, final int depth) {
Component newComp = modTransformation.apply(current, depth);
for (final Component child : current.children()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,28 @@

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tree.Node;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
* A tag that can transform a whole subtree of nodes.
*
* @since 4.10.0
*/
@ApiStatus.OverrideOnly
public /* non-sealed */ interface Modifying extends Tag {
/**
* Method called once for every element in the subtree, allowing calculations to be made before {@link #apply(Component, int) application}.
*
* @param current the current element in the subtree
* @param depth depth in the tree this node is at
* @since 4.10.0
*/
default void visit(final @NotNull Node current) {
default void visit(final @NotNull Node current, final int depth) {
}

/**
* Called after the entire tree has been {@link #visit(Node) visited}.
* Called after the entire tree has been {@link #visit(Node, int) visited}.
*
* <p>This allows for finalizing calculations made during the tree visit, but before actual application to the child components of this tag.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected final int size() {
}

@Override
public final void visit(final @NotNull Node current) {
public final void visit(final @NotNull Node current, final int depth) {
if (this.visited) {
throw new IllegalStateException("Color changing tag instances cannot be re-used, return a new one for each resolve");
}
Expand Down

0 comments on commit 7f4ddc2

Please sign in to comment.