Class OffsetTreeCursor

  • All Implemented Interfaces:
    AutoCloseable, Cloneable

    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 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()
        Delete the external resource, freeing all the memory that it used.
        Specified by:
        close in interface AutoCloseable
      • getCurrentDepth

        public int getCurrentDepth()
        Description copied from class: TreeCursor
        Get the depth of the cursor's current Node relative to the original Node that the cursor was constructed from.
        Returns:
        the relative depth of the tree cursor's current node
      • getCurrentFieldName

        public String getCurrentFieldName()
        Description copied from class: TreeCursor
        Get the field name of the Node that the cursor is currently pointing to.
        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.
        Returns:
        true if the cursor successfully moved, and false if there were no children
      • gotoFirstChild

        public boolean gotoFirstChild​(int offset)
        Description copied from class: TreeCursor
        Move the cursor to the first child of its current node that extends beyond the given byte offset.
        Parameters:
        offset - the starting byte of the child
        Returns:
        true if the cursor successfully moved, and false if no such child was found
      • gotoFirstChild

        public boolean gotoFirstChild​(@NotNull
                                      @NotNull Point point)
        Description copied from class: TreeCursor
        Move the cursor to the first child of its current node that extends beyond the given row-column offset.
        Parameters:
        point - the starting row-column position of the child
        Returns:
        true if the cursor successfully moved, and false if no such child was found
      • gotoLastChild

        public boolean gotoLastChild()
        Description copied from class: TreeCursor
        Move the cursor to the last child of its current node.

        Note that this method may be slower than TreeCursor.gotoFirstChild() because it needs to iterate through all the children to compute the child's position.

        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.
        Returns:
        true if the cursor successfully moved, and false if there was no next sibling node
      • gotoPrevSibling

        public boolean gotoPrevSibling()
        Description copied from class: TreeCursor
        Move the cursor to the previous sibling of its current node.

        Note that this method may be slower than TreeCursor.gotoNextSibling() due to how node positions are stored. In the worst case, this method may need to iterate through all the previous nodes up to the destination.

        Returns:
        true if the cursor successfully moved, and false if there was no previous sibling node
      • gotoParent

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

        public boolean gotoNode​(@NotNull
                                @NotNull Node node)
        Description copied from class: TreeCursor
        Move the cursor to an arbitrary node.
        Parameters:
        node - target node to move the cursor to.
        Returns:
        true if the cursor successfully moved, and false if it was already at the specified 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.
        Parameters:
        callback - The callback consumer which will execute upon visiting a node
      • reset

        public boolean reset​(@NotNull
                             @NotNull TreeCursor other)
        Description copied from class: TreeCursor
        Reset the cursor to the same state as another cursor.
        Parameters:
        other - the cursor to copy state from
        Returns:
        true if the cursor successfully moved, and false if it was already located at the same node
      • getCurrentNode

        public Node getCurrentNode()
        Description copied from class: TreeCursor
        Get the Node that the cursor is currently pointing to.
        Returns:
        the tree cursor's current node
      • clone

        public TreeCursor clone()
        Description copied from class: TreeCursor
        Clone this cursor, creating a separate, independent instance.
        Returns:
        a clone of this instance
      • getCurrentTreeCursorNode

        public TreeCursorNode getCurrentTreeCursorNode()
        Description copied from class: TreeCursor
        Get the TreeCursorNode representation of the Node that the cursor is currently pointing to.
        Returns:
        the tree cursor's current node
      • isNull

        public final boolean isNull()
        Checks whether the memory address associated with this external resource is nullptr.
        Returns:
        true if the memory address is 0, otherwise false
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object