Class OffsetTreeCursor

  • All Implemented Interfaces:
    AutoCloseable

    public class OffsetTreeCursor
    extends TreeCursor
    Specialization of TreeCursor that applies a positional offset to visited nodes. Retrieving nodes by either getCurrentNode() or getCurrentTreeCursorNode() applies the offset to a copy of the original node, meaning that the tree is not mutated during traversal.

    So what exactly is the purpose of this cursor? Imagine a scenario in which we need to print out the node positions of a source code file that begins with n blank lines. Rather than modifying the original file, we can just use this cursor with a positional offset of new Position(-n, 0). Likewise, when printing node positions of an indented function, the n spaces of indentation can be negated with a positional offset of new Position(0, -n). Row and column offsets can be combined with various positive and negative arrangements.

    Since:
    1.2.0
    Author:
    Ozren Dabić
    • Constructor Detail

      • OffsetTreeCursor

        public OffsetTreeCursor​(@NotNull
                                @NotNull Node node,
                                @NotNull
                                @NotNull Point offset)
    • Method Detail

      • close

        public void close()
        Description copied from class: TreeCursor
        Delete the tree cursor, freeing all the memory that it used.
        Specified by:
        close in interface AutoCloseable
        Overrides:
        close in class TreeCursor
      • getCurrentFieldName

        public String getCurrentFieldName()
        Overrides:
        getCurrentFieldName in class TreeCursor
        Returns:
        The field name of the tree cursor's current node, null if the current node doesn't have a field
      • gotoFirstChild

        public boolean gotoFirstChild()
        Description copied from class: TreeCursor
        Move the cursor to the first child of its current node.
        Overrides:
        gotoFirstChild in class TreeCursor
        Returns:
        true if the cursor successfully moved, and false if there were no children
      • gotoNextSibling

        public boolean gotoNextSibling()
        Description copied from class: TreeCursor
        Move the cursor to the next sibling of its current node.
        Overrides:
        gotoNextSibling in class TreeCursor
        Returns:
        true if the cursor successfully moved, and false if there was no next sibling node
      • gotoParent

        public boolean gotoParent()
        Description copied from class: TreeCursor
        Move the cursor to the parent of its current node.
        Overrides:
        gotoParent in class TreeCursor
        Returns:
        true if the cursor successfully moved, and false if there was no parent node (the cursor was already on the root node)
      • preorderTraversal

        public void preorderTraversal​(@NotNull
                                      @NotNull Consumer<Node> callback)
        Description copied from class: TreeCursor
        Iteratively traverse over the parse tree, applying a callback to the nodes before they are visited.
        Overrides:
        preorderTraversal in class TreeCursor
        Parameters:
        callback - The callback consumer which will execute upon visiting a node
      • isNull

        public final boolean isNull()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object