Package software.amazon.smithy.syntax
Class TreeCursor
- java.lang.Object
-
- software.amazon.smithy.syntax.TreeCursor
-
- All Implemented Interfaces:
FromSourceLocation
public final class TreeCursor extends java.lang.Object implements FromSourceLocation
Externally traverses aTokenTree
to provide access to parents and siblings.- See Also:
TokenTree.zipper()
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object o)
TreeCursor
findAt(int line, int column)
Find the innermost tree that contains the given coordinates.java.util.List<TreeCursor>
findChildrenByType(TreeType... types)
Recursively find every node in the tree that has the givenTreeType
.java.util.List<TreeCursor>
getChildren()
Get all children of the tree as a list of cursors.java.util.List<TreeCursor>
getChildrenByType(TreeType... types)
Get direct children from the current tree of a specific type.TreeCursor
getFirstChild()
Get the first child of the wrapped tree.TreeCursor
getFirstChild(TreeType type)
Get the first child of the wrapped tree with the given type.TreeCursor
getLastChild()
Get the last child of the wrapped tree.TreeCursor
getLastChild(TreeType type)
Get the last child of the wrapped tree with the given type.TreeCursor
getNextSibling()
Get the next sibling of this tree, if present.TreeCursor
getParent()
Get the parent cursor, or null if not present.java.util.List<TreeCursor>
getPathToCursor()
Get a list of tree cursors that lead up to the current tree, starting from the root as the first element, and including the current tree as the last element.TreeCursor
getPreviousSibling()
Get the previous sibling of this tree, if present.TreeCursor
getRoot()
Get the root of the tree, returning itself if the tree has no parent.SourceLocation
getSourceLocation()
Gets the source location of a value.TokenTree
getTree()
Get the wrappedTokenTree
.int
hashCode()
static TreeCursor
of(TokenTree tree)
Create a TreeCursor from the given TokenTree, treating it as the root of the tree.
-
-
-
Method Detail
-
of
public static TreeCursor of(TokenTree tree)
Create a TreeCursor from the given TokenTree, treating it as the root of the tree.- Parameters:
tree
- Tree to create a cursor from.- Returns:
- Returns the created cursor.
-
getSourceLocation
public SourceLocation getSourceLocation()
Description copied from interface:FromSourceLocation
Gets the source location of a value.- Specified by:
getSourceLocation
in interfaceFromSourceLocation
- Returns:
- Returns the source location of the value.
-
getParent
public TreeCursor getParent()
Get the parent cursor, or null if not present.- Returns:
- Nullable parent cursor.
-
getRoot
public TreeCursor getRoot()
Get the root of the tree, returning itself if the tree has no parent.- Returns:
- Non-nullable root tree.
-
getPathToCursor
public java.util.List<TreeCursor> getPathToCursor()
Get a list of tree cursors that lead up to the current tree, starting from the root as the first element, and including the current tree as the last element.- Returns:
- Returns the path to the current tree from the root.
-
getPreviousSibling
public TreeCursor getPreviousSibling()
Get the previous sibling of this tree, if present.- Returns:
- Return the nullable previous sibling.
-
getNextSibling
public TreeCursor getNextSibling()
Get the next sibling of this tree, if present.- Returns:
- Return the nullable next sibling.
-
getChildren
public java.util.List<TreeCursor> getChildren()
Get all children of the tree as a list of cursors.- Returns:
- Return the cursors to each child.
-
getChildrenByType
public java.util.List<TreeCursor> getChildrenByType(TreeType... types)
Get direct children from the current tree of a specific type.- Parameters:
types
- Types of children to get.- Returns:
- Returns the collected children, or an empty list.
-
getFirstChild
public TreeCursor getFirstChild()
Get the first child of the wrapped tree.- Returns:
- Return the first child, or null if the tree has no children.
-
getFirstChild
public TreeCursor getFirstChild(TreeType type)
Get the first child of the wrapped tree with the given type.- Parameters:
type
- Child type to get.- Returns:
- Return the first child, or null if a matching child is not found.
-
getLastChild
public TreeCursor getLastChild()
Get the last child of the wrapped tree.- Returns:
- Return the last child, or null if the tree has no children.
-
getLastChild
public TreeCursor getLastChild(TreeType type)
Get the last child of the wrapped tree with the given type.- Parameters:
type
- Child type to get.- Returns:
- Return the last child, or null if a matching child is not found.
-
findChildrenByType
public java.util.List<TreeCursor> findChildrenByType(TreeType... types)
Recursively find every node in the tree that has the givenTreeType
.- Parameters:
types
- Types of children to return.- Returns:
- Returns the matching tree cursors.
-
findAt
public TreeCursor findAt(int line, int column)
Find the innermost tree that contains the given coordinates.- Parameters:
line
- Line to find.column
- Column to find.- Returns:
- Returns the innermost tree that contains the coordinates.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-