Class Node

java.lang.Object
io.github.treesitter.jtreesitter.Node

@NullMarked public final class Node extends Object
A single node within a syntax tree.
Implementation Note:
Node lifetimes are tied to the Tree, TreeCursor, or Query that they belong to.
  • Method Details

    • getTree

      public Tree getTree()
      Get the tree that contains this node.
    • getId

      @Unsigned public long getId()
      Get the numerical ID of the node.
      API Note:
      Within any given syntax tree, no two nodes have the same ID. However, if a new tree is created based on an older tree, and a node from the old tree is reused in the process, then that node will have the same ID in both trees.
    • getSymbol

      @Unsigned public short getSymbol()
      Get the numerical ID of the node's type.
    • getGrammarSymbol

      @Unsigned public short getGrammarSymbol()
      Get the numerical ID of the node's type, as it appears in the grammar ignoring aliases.
    • getType

      public String getType()
      Get the type of the node.
    • getGrammarType

      public String getGrammarType()
      Get the type of the node, as it appears in the grammar ignoring aliases.
    • isNamed

      public boolean isNamed()
      Check if the node is named.

      Named nodes correspond to named rules in the grammar, whereas anonymous nodes correspond to string literals.

    • isExtra

      public boolean isExtra()
      Check if the node is extra.

      Extra nodes represent things which are not required by the grammar but can appear anywhere (e.g. whitespace).

    • isError

      public boolean isError()
      Check if the node is an ERROR node.
    • isMissing

      public boolean isMissing()
      Check if the node is MISSING.

      MISSING nodes are inserted by the parser in order to recover from certain kinds of syntax errors.

    • hasChanges

      public boolean hasChanges()
      Check if the node has been edited.
    • hasError

      public boolean hasError()
      Check if the node is an ERROR, or contains any ERROR nodes.
    • getParseState

      @Unsigned public short getParseState()
      Get the parse state of this node.
    • getNextParseState

      @Unsigned public short getNextParseState()
      Get the parse state after this node.
    • getStartByte

      @Unsigned public int getStartByte()
      Get the start byte of the node.
    • getEndByte

      @Unsigned public int getEndByte()
      Get the end byte of the node.
    • getRange

      public Range getRange()
      Get the range of the node.
    • getStartPoint

      public Point getStartPoint()
      Get the start point of the node.
    • getEndPoint

      public Point getEndPoint()
      Get the end point of the node.
    • getChildCount

      @Unsigned public int getChildCount()
      Get the number of this node's children.
    • getNamedChildCount

      @Unsigned public int getNamedChildCount()
      Get the number of this node's named children.
    • getDescendantCount

      @Unsigned public int getDescendantCount()
      Get the number of this node's descendants, including the node itself.
    • getParent

      public Optional<Node> getParent()
      The node's immediate parent, if any.
    • getNextSibling

      public Optional<Node> getNextSibling()
      The node's next sibling, if any.
    • getPrevSibling

      public Optional<Node> getPrevSibling()
      The node's previous sibling, if any.
    • getNextNamedSibling

      public Optional<Node> getNextNamedSibling()
      The node's next named sibling, if any.
    • getPrevNamedSibling

      public Optional<Node> getPrevNamedSibling()
      The node's previous named sibling, if any.
    • getChild

      public Optional<Node> getChild(@Unsigned int index) throws IndexOutOfBoundsException
      Get the node's child at the given index, if any.
      Throws:
      IndexOutOfBoundsException - If the index exceeds the child count.
      API Note:
      This method is fairly fast, but its cost is technically log(i), so if you might be iterating over a long list of children, you should use getChildren() or walk() instead.
    • getNamedChild

      public Optional<Node> getNamedChild(@Unsigned int index) throws IndexOutOfBoundsException
      Get the node's named child at the given index, if any.
      Throws:
      IndexOutOfBoundsException - If the index exceeds the child count.
      API Note:
      This method is fairly fast, but its cost is technically log(i), so if you might be iterating over a long list of children, you should use getNamedChildren() or walk() instead.
    • getFirstChildForByte

      public Optional<Node> getFirstChildForByte(@Unsigned int byte_offset)
      Get the node's first child that contains or starts after the given byte offset.
      Since:
      0.25.0
    • getFirstNamedChildForByte

      public Optional<Node> getFirstNamedChildForByte(@Unsigned int byte_offset)
      Get the node's first named child that contains or starts after the given byte offset.
      Since:
      0.25.0
    • getChildByFieldId

      public Optional<Node> getChildByFieldId(@Unsigned short id)
      Get the node's first child with the given field ID, if any.
      See Also:
    • getChildByFieldName

      public Optional<Node> getChildByFieldName(String name)
      Get the node's first child with the given field name, if any.
    • getChildren

      public List<Node> getChildren()
      Get this node's children.
      API Note:
      If you're walking the tree recursively, you may want to use walk() instead.
    • getNamedChildren

      public List<Node> getNamedChildren()
      Get this node's named children.
    • getChildrenByFieldId

      public List<Node> getChildrenByFieldId(@Unsigned short id)
      Get a list of the node's children with the given field ID.
      See Also:
    • getChildrenByFieldName

      public List<Node> getChildrenByFieldName(String name)
      Get a list of the node's child with the given field name.
    • getFieldNameForChild

      public @Nullable String getFieldNameForChild(@Unsigned int index) throws IndexOutOfBoundsException
      Get the field name of this node’s child at the given index, if available.
      Throws:
      IndexOutOfBoundsException - If the index exceeds the child count.
    • getFieldNameForNamedChild

      public @Nullable String getFieldNameForNamedChild(@Unsigned int index) throws IndexOutOfBoundsException
      Get the field name of this node's named child at the given index, if available.
      Throws:
      IndexOutOfBoundsException - If the index exceeds the child count.
      Since:
      0.24.0
    • getDescendant

      public Optional<Node> getDescendant(@Unsigned int start, @Unsigned int end) throws IllegalArgumentException
      Get the smallest node within this node that spans the given byte range, if any.
      Throws:
      IllegalArgumentException - If start > end.
    • getDescendant

      public Optional<Node> getDescendant(Point start, Point end) throws IllegalArgumentException
      Get the smallest node within this node that spans the given point range, if any.
      Throws:
      IllegalArgumentException - If start > end.
    • getNamedDescendant

      public Optional<Node> getNamedDescendant(@Unsigned int start, @Unsigned int end) throws IllegalArgumentException
      Get the smallest named node within this node that spans the given byte range, if any.
      Throws:
      IllegalArgumentException - If start > end.
    • getNamedDescendant

      public Optional<Node> getNamedDescendant(Point start, Point end)
      Get the smallest named node within this node that spans the given point range, if any.
      Throws:
      IllegalArgumentException - If start > end.
    • getChildWithDescendant

      public Optional<Node> getChildWithDescendant(Node descendant)
      Get the node that contains the given descendant, if any.
      Since:
      0.24.0
    • getText

      public @Nullable String getText()
      Get the source code of the node, if available.
    • edit

      public void edit(InputEdit edit)
      Edit this node to keep it in-sync with source code that has been edited.
      API Note:
      This method is only rarely needed. When you edit a syntax tree via Tree.edit(InputEdit), all of the nodes that you retrieve from the tree afterward will already reflect the edit. You only need to use this when you have a specific Node instance that you want to keep and continue to use after an edit.
    • walk

      public TreeCursor walk()
      Create a new tree cursor starting from this node.
    • toSexp

      public String toSexp()
      Get the S-expression representing the node.
    • equals

      public boolean equals(Object o)
      Check if two nodes are identical.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object