Package io.github.treesitter.jtreesitter
Class Node
java.lang.Object
io.github.treesitter.jtreesitter.Node
A single node within a syntax tree.
- Implementation Note:
- Node lifetimes are tied to the
Tree
,TreeCursor
, orQuery
that they belong to.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Edit this node to keep it in-sync with source code that has been edited.boolean
Check if two nodes are identical.getChild
(int index) Get the node's child at the given index, if any.getChildByFieldId
(short id) Get the node's first child with the given field ID, if any.getChildByFieldName
(String name) Get the node's first child with the given field name, if any.getChildContainingDescendant
(Node descendant) Deprecated, for removal: This API element is subject to removal in a future version.int
Get the number of this node's children.Get this node's children.getChildrenByFieldId
(short id) Get a list of the node's children with the given field ID.getChildrenByFieldName
(String name) Get a list of the node's child with the given field name.getChildWithDescendant
(Node descendant) Get the node that contains the given descendant, if any.getDescendant
(int start, int end) Get the smallest node within this node that spans the given byte range, if any.getDescendant
(Point start, Point end) Get the smallest node within this node that spans the given point range, if any.int
Get the number of this node's descendants, including the node itself.int
Get the end byte of the node.Get the end point of the node.getFieldNameForChild
(int index) Get the field name of this node’s child at the given index, if available.getFieldNameForNamedChild
(int index) Get the field name of this node's named child at the given index, if available.short
Get the numerical ID of the node's type, as it appears in the grammar ignoring aliases.Get the type of the node, as it appears in the grammar ignoring aliases.long
getId()
Get the numerical ID of the node.getNamedChild
(int index) Get the node's named child at the given index, if any.int
Get the number of this node's named children.Get this node's named children.getNamedDescendant
(int start, int end) Get the smallest named node within this node that spans the given byte range, if any.getNamedDescendant
(Point start, Point end) Get the smallest named node within this node that spans the given point range, if any.The node's next named sibling, if any.short
Get the parse state after this node.The node's next sibling, if any.The node's immediate parent, if any.short
Get the parse state of this node.The node's previous named sibling, if any.The node's previous sibling, if any.getRange()
Get the range of the node.int
Get the start byte of the node.Get the start point of the node.short
Get the numerical ID of the node's type.getText()
Get the source code of the node, if available.getTree()
Get the tree that contains this node.getType()
Get the type of the node.boolean
Check if the node has been edited.boolean
hasError()
Check if the node is an ERROR, or contains any ERROR nodes.int
hashCode()
boolean
isError()
Check if the node is an ERROR node.boolean
isExtra()
Check if the node is extra.boolean
Check if the node is MISSING.boolean
isNamed()
Check if the node is named.toSexp()
Get the S-expression representing the node.toString()
walk()
Create a new tree cursor starting from this node.
-
Method Details
-
getTree
Get the tree that contains this node. -
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
Get the numerical ID of the node's type. -
getGrammarSymbol
Get the numerical ID of the node's type, as it appears in the grammar ignoring aliases. -
getType
Get the type of the node. -
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
Get the parse state of this node. -
getNextParseState
Get the parse state after this node. -
getStartByte
Get the start byte of the node. -
getEndByte
Get the end byte of the node. -
getRange
Get the range of the node. -
getStartPoint
Get the start point of the node. -
getEndPoint
Get the end point of the node. -
getChildCount
Get the number of this node's children. -
getNamedChildCount
Get the number of this node's named children. -
getDescendantCount
Get the number of this node's descendants, including the node itself. -
getParent
-
getNextSibling
-
getPrevSibling
-
getNextNamedSibling
-
getPrevNamedSibling
-
getChild
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 usegetChildren()
orwalk()
instead.
-
getNamedChild
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 usegetNamedChildren()
orwalk()
instead.
-
getChildByFieldId
-
getChildByFieldName
-
getChildren
-
getNamedChildren
-
getChildrenByFieldId
-
getChildrenByFieldName
-
getFieldNameForChild
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
- Ifstart > end
.
-
getDescendant
Get the smallest node within this node that spans the given point range, if any.- Throws:
IllegalArgumentException
- Ifstart > 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
- Ifstart > end
.
-
getNamedDescendant
Get the smallest named node within this node that spans the given point range, if any.- Throws:
IllegalArgumentException
- Ifstart > end
.
-
getChildContainingDescendant
Deprecated, for removal: This API element is subject to removal in a future version.Get the child of the node that contains the given descendant, if any.- API Note:
- This method will not return the descendant if it is a direct child of this node.
-
getChildWithDescendant
-
getText
-
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(io.github.treesitter.jtreesitter.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
Create a new tree cursor starting from this node. -
toSexp
Get the S-expression representing the node. -
equals
-
hashCode
-
toString
-
getChildWithDescendant(io.github.treesitter.jtreesitter.Node)
instead.