Class TreeCursor

java.lang.Object
io.github.treesitter.jtreesitter.TreeCursor
All Implemented Interfaces:
AutoCloseable, Cloneable

@NullMarked public final class TreeCursor extends Object implements AutoCloseable, Cloneable
A class that can be used to efficiently walk a syntax tree.
See Also:
API Note:
The node the cursor was constructed with is considered the root of the cursor, and the cursor cannot walk outside this node.
  • Method Details

    • getCurrentNode

      public Node getCurrentNode()
      Get the current node of the cursor.
      Implementation Note:
      The node will become invalid once the cursor is closed.
    • getCurrentNode

      public Node getCurrentNode(SegmentAllocator allocator)
      Get the current node of the cursor using the given allocator.
      Since:
      0.25.0
    • getCurrentDepth

      @Unsigned public int getCurrentDepth()
      Get the depth of the cursor's current node relative to the original node that the cursor was constructed with.
    • getCurrentFieldId

      @Unsigned public short getCurrentFieldId()
      Get the field ID of the tree cursor's current node, or 0.
      See Also:
    • getCurrentFieldName

      public @Nullable String getCurrentFieldName()
      Get the field name of the tree cursor's current node, or null.
      See Also:
    • getCurrentDescendantIndex

      @Unsigned public int getCurrentDescendantIndex()
      Get the index of the cursor's current node out of the descendants of the original node that the cursor was constructed with.
    • gotoFirstChild

      public boolean gotoFirstChild()
      Move the cursor to the first child of its current node.
      Returns:
      true if the cursor successfully moved, or false if there were no children.
    • gotoLastChild

      public boolean gotoLastChild()
      Move the cursor to the last child of its current node.
      Returns:
      true if the cursor successfully moved, or false if there were no children.
    • gotoParent

      public boolean gotoParent()
      Move the cursor to the parent of its current node.
      Returns:
      true if the cursor successfully moved, or false if there was no parent node.
    • gotoNextSibling

      public boolean gotoNextSibling()
      Move the cursor to the next sibling of its current node.
      Returns:
      true if the cursor successfully moved, or false if there was no next sibling node.
    • gotoPreviousSibling

      public boolean gotoPreviousSibling()
      Move the cursor to the previous sibling of its current node.
      Returns:
      true if the cursor successfully moved, or false if there was no previous sibling node.
    • gotoDescendant

      public void gotoDescendant(@Unsigned int index)
      Move the cursor to the node that is the nth descendant of the original node that the cursor was constructed with.
      API Note:
      The index 0 represents the original node itself.
    • gotoFirstChildForByte

      @Unsigned public OptionalInt gotoFirstChildForByte(@Unsigned int offset)
      Move the cursor to the first child of its current node that contains or starts after the given byte offset.
      Returns:
      The index of the child node, if found.
    • gotoFirstChildForPoint

      @Unsigned public OptionalInt gotoFirstChildForPoint(Point point)
      Move the cursor to the first child of its current node that contains or starts after the given point.
      Returns:
      The index of the child node, if found.
    • reset

      public void reset(Node node)
      Reset the cursor to start at a different node.
    • reset

      public void reset(TreeCursor cursor)
      Reset the cursor to start at the same position as another cursor.
    • clone

      public TreeCursor clone()
      Create a shallow copy of the tree cursor.
    • close

      public void close() throws RuntimeException
      Specified by:
      close in interface AutoCloseable
      Throws:
      RuntimeException
    • toString

      public String toString()
      Overrides:
      toString in class Object