public final class SoyTreeUtils extends Object
Important: Do not use outside of Soy code (treat as superpackage-private).
| Modifier and Type | Class and Description |
|---|---|
static class |
SoyTreeUtils.VisitDirective
An enum that allows a {#visitAllNodes} visitor to control how the AST is traversed.
|
| Modifier and Type | Method and Description |
|---|---|
static StringBuilder |
buildAstString(SoyNode.ParentSoyNode<?> node,
int indent,
StringBuilder sb)
Given a Soy node, returns a
StringBuilder that can be used to pretty print the AST
structure. |
static StringBuilder |
buildAstStringWithPreview(SoyNode.ParentSoyNode<?> node,
int indent,
StringBuilder sb)
Similar to
buildAstString(com.google.template.soy.soytree.SoyNode.ParentSoyNode<?>, int, java.lang.StringBuilder), but also print the source string for debug usages. |
static <T extends SoyNode> |
cloneListWithNewIds(List<T> origNodes,
IdGenerator nodeIdGen)
Clones the given list of nodes and then generates and sets new ids on all the cloned nodes (by
default, SoyNode.copy(copyState) creates cloned nodes with the same ids as the original nodes).
|
static <T extends SoyNode> |
cloneWithNewIds(T origNode,
IdGenerator nodeIdGen)
Clones the given node and then generates and sets new ids on all the cloned nodes (by default,
SoyNode.copy(copyState) creates cloned nodes with the same ids as the original nodes).
|
static <R> void |
execOnAllV2Exprs(SoyNode node,
AbstractNodeVisitor<ExprNode,R> exprNodeVisitor)
Given a Soy node and a visitor for expression trees, traverses the subtree of the node and
executes the visitor on all expressions held by nodes in the subtree.
|
static <T extends Node> |
getAllNodesOfType(Node rootSoyNode,
Class<T> classObject)
Retrieves all nodes in a tree that are an instance of a particular class.
|
static boolean |
hasHtmlNodes(Node node)
Returns true if the given
node contains any children that are HTML nodes. |
static boolean |
hasNodesOfType(Node node,
Class<? extends Node>... types)
Returns true if the given
node contains any children of the given types. |
static boolean |
isDescendantOf(SoyNode node,
SoyNode ancestor)
Returns true if
node is a descendant of ancestor. |
static String |
toSourceString(List<? extends Node> nodes) |
static void |
visitAllNodes(Node node,
NodeVisitor<? super Node,SoyTreeUtils.VisitDirective> visitor)
Runs the visitor on all nodes (including
expr nodes) reachable from the given
node. |
@SafeVarargs public static boolean hasNodesOfType(Node node, Class<? extends Node>... types)
node contains any children of the given types.public static boolean hasHtmlNodes(Node node)
node contains any children that are HTML nodes.public static void visitAllNodes(Node node, NodeVisitor<? super Node,SoyTreeUtils.VisitDirective> visitor)
expr nodes) reachable from the given
node. The order of visiting is breadth first.
If the visitor return false from NodeVisitor.exec(Node) we will short
circuit visiting.
public static <T extends Node> com.google.common.collect.ImmutableList<T> getAllNodesOfType(Node rootSoyNode, Class<T> classObject)
T - The type of node to retrieve.rootSoyNode - The parse tree to search.classObject - The class whose instances to search for, including subclasses.public static StringBuilder buildAstString(SoyNode.ParentSoyNode<?> node, int indent, StringBuilder sb)
StringBuilder that can be used to pretty print the AST
structure.
For example, for the following soy source This method prints the AST string as follow:
{for i in range(5)}
{if $i % 2 == 0}
foo
{/if}
{/for}
FOR_NODE
IF_NODE
IF_COND_NODE
PRINT_NODE
node - The root of the AST.indent - The indentation for each level.sb - The StringBuilder instance used for recursion.public static StringBuilder buildAstStringWithPreview(SoyNode.ParentSoyNode<?> node, int indent, StringBuilder sb)
buildAstString(com.google.template.soy.soytree.SoyNode.ParentSoyNode<?>, int, java.lang.StringBuilder), but also print the source string for debug usages.public static <R> void execOnAllV2Exprs(SoyNode node, AbstractNodeVisitor<ExprNode,R> exprNodeVisitor)
Only processes expressions in V2 syntax. Ignores all expressions in V1 syntax.
R - The ExprNode visitor's return type.node - The root of the subtree to visit all expressions in.exprNodeVisitor - The visitor to execute on all expressions.public static <T extends SoyNode> T cloneWithNewIds(T origNode, IdGenerator nodeIdGen)
This function will use the original Soy tree's node id generator to generate the new node ids for the cloned nodes. Thus, the original node to be cloned must be part of a full Soy tree. However, this does not mean that the cloned node will become part of the original tree (unless it is manually attached later). The cloned node will be an independent subtree with parent set to null.
T - The type of the node being cloned.origNode - The original node to be cloned. This node must be part of a full Soy tree,
because the generator for the new node ids will be retrieved from the root (SoyFileSetNode)
of the tree.nodeIdGen - The ID generator used for the tree.public static <T extends SoyNode> List<T> cloneListWithNewIds(List<T> origNodes, IdGenerator nodeIdGen)
This function will use the original Soy tree's node id generator to generate the new node ids for the cloned nodes. Thus, the original nodes to be cloned must be part of a full Soy tree. However, this does not mean that the cloned nodes will become part of the original tree (unless they are manually attached later). The cloned nodes will be independent subtrees with parents set to null.
T - The type of the nodes being cloned.origNodes - The original nodes to be cloned. These nodes must be part of a full Soy tree,
because the generator for the new node ids will be retrieved from the root (SoyFileSetNode)
of the tree.nodeIdGen - The ID generator used for the tree.public static boolean isDescendantOf(SoyNode node, SoyNode ancestor)
node is a descendant of ancestor.