Interface ASTNode

All Superinterfaces:
UserDataHolder

public interface ASTNode extends UserDataHolder
A node in the AST tree. The AST is an intermediate parsing tree created by PsiBuilder, out of which a PSI tree is then created.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final ASTNode[]
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addChild(@NotNull ASTNode child)
    Adds the specified child node as the last child of this node.
    void
    addChild(@NotNull ASTNode child, @Nullable ASTNode anchorBefore)
    Adds the specified child node at the specified position in the child list.
    void
    addChildren(@NotNull ASTNode firstChild, ASTNode firstChildToNotAdd, ASTNode anchorBefore)
    Adds a range of nodes belonging to the same parent to the list of children of this node, starting with firstChild, up to and not including firstChildToNotAdd.
    void
    addLeaf(@NotNull IElementType leafType, @NotNull CharSequence leafText, @Nullable ASTNode anchorBefore)
    Add leaf element with specified type and text in the child list.
    @NotNull Object
    Creates and returns a deep copy of the AST tree part starting at this node.
    Creates a copy of the entire AST tree containing this node and returns a counterpart of this node in the resulting tree.
    @Nullable ASTNode
    Returns the first child of the specified node which has the specified type.
    @Nullable ASTNode
    findChildByType(@NotNull IElementType type, @Nullable ASTNode anchor)
    Returns the first child after anchor of the specified node which has the specified type.
    @Nullable ASTNode
    findChildByType(@NotNull TokenSet typesSet)
    Returns the first child of the specified node which has type from specified set.
    @Nullable ASTNode
    findChildByType(@NotNull TokenSet typesSet, @Nullable ASTNode anchor)
    Returns the first child after anchor of the specified node which has type from specified set.
    @Nullable ASTNode
    findLeafElementAt(int offset)
    Finds a leaf child node at the specified offset from the start of the text range of this node.
    @NotNull CharSequence
    Returns same text getText() returns but might be more effective eliminating toString() transformation from internal CharSequence representation
    ASTNode @NotNull []
    getChildren(@Nullable TokenSet filter)
    Returns the list of children of the specified node, optionally filtered by the specified token type filter.
    <T> T
    getCopyableUserData(@NotNull Key<T> key)
    Returns a copyable user data object attached to this node.
    @NotNull IElementType
    Returns the type of this node.
    Returns the first child of this node in the tree.
    Returns the last child of this node in the tree.
    Returns the PSI element for this node.
    <T extends PsiElement>
    T
    getPsi(@NotNull Class<T> clazz)
    Checks and returns the PSI element for this node.
    int
    Returns the starting offset of the node text in the document.
    default int
    Returns the starting offset of the node text relative to getTreeParent().
    @NotNull String
    Returns the text of this node.
    int
    Returns the length of the node text.
    Returns the text range (a combination of starting offset in the document and length) for this node.
    Returns the next sibling of this node in the tree.
    Returns the parent of this node in the tree.
    Returns the previous sibling of this node in the tree.
    <T> void
    putCopyableUserData(@NotNull Key<T> key, T value)
    Attaches a copyable user data object to this node.
    void
    removeChild(@NotNull ASTNode child)
    Removes the specified node from the list of children of this node.
    void
    removeRange(@NotNull ASTNode firstNodeToRemove, ASTNode firstNodeToKeep)
    Removes a range of nodes from the list of children, starting with firstNodeToRemove, up to and not including firstNodeToKeep.
    void
    replaceAllChildrenToChildrenOf(@NotNull ASTNode anotherParent)
    Replaces all child nodes with the children of the specified node.
    void
    replaceChild(@NotNull ASTNode oldChild, @NotNull ASTNode newChild)
    Replaces the specified child node with another node.
    boolean
    textContains(char c)
    Checks if the specified character is present in the text of this node.

    Methods inherited from interface com.intellij.openapi.util.UserDataHolder

    getUserData, putUserData
  • Field Details

    • EMPTY_ARRAY

      static final ASTNode[] EMPTY_ARRAY
  • Method Details

    • getElementType

      @NotNull @NotNull IElementType getElementType()
      Returns the type of this node.
      Returns:
      the element type.
    • getText

      @NotNull @NotNull String getText()
      Returns the text of this node.

      Note: This call requires traversing whole subtree, so it can be expensive for composite nodes, and should be avoided if possible.
      Returns:
      the node text.
    • getChars

      @NotNull @NotNull CharSequence getChars()
      Returns same text getText() returns but might be more effective eliminating toString() transformation from internal CharSequence representation

      Note: This call requires traversing whole subtree, so it can be expensive for composite nodes, and should be avoided if possible.
      Returns:
      the node text.
      See Also:
    • textContains

      boolean textContains(char c)
      Checks if the specified character is present in the text of this node.
      Parameters:
      c - the character to search for.
      Returns:
      true if the character is found, false otherwise.
    • getStartOffset

      int getStartOffset()
      Returns the starting offset of the node text in the document.

      Note: it works in O(tree_depth) time, which can be slow in deep trees, so invoking this method should be avoided if possible.
      Returns:
      the start offset.
    • getStartOffsetInParent

      default int getStartOffsetInParent()
      Returns the starting offset of the node text relative to getTreeParent().
      Returns:
      the start offset relative to node parent
    • getTextLength

      int getTextLength()
      Returns the length of the node text.
      Returns:
      the text length.
    • getTextRange

      TextRange getTextRange()
      Returns the text range (a combination of starting offset in the document and length) for this node.

      Note: it works in O(tree_depth) time, which can be slow in deep trees, so invoking this method should be avoided if possible.
      Returns:
      the text range.
    • getTreeParent

      ASTNode getTreeParent()
      Returns the parent of this node in the tree.
      Returns:
      the parent node.
    • getFirstChildNode

      ASTNode getFirstChildNode()
      Returns the first child of this node in the tree.
      Returns:
      the first child node.
    • getLastChildNode

      ASTNode getLastChildNode()
      Returns the last child of this node in the tree.
      Returns:
      the last child node.
    • getTreeNext

      ASTNode getTreeNext()
      Returns the next sibling of this node in the tree.
      Returns:
      the next sibling node.
    • getTreePrev

      ASTNode getTreePrev()
      Returns the previous sibling of this node in the tree.
      Returns:
      the previous sibling node.
    • getChildren

      ASTNode @NotNull [] getChildren(@Nullable @Nullable TokenSet filter)
      Returns the list of children of the specified node, optionally filtered by the specified token type filter.
      Parameters:
      filter - the token set used to filter the returned children, or null if all children should be returned.
      Returns:
      the children array.
    • addChild

      void addChild(@NotNull @NotNull ASTNode child)
      Adds the specified child node as the last child of this node.
      Parameters:
      child - the child node to add.
    • addChild

      void addChild(@NotNull @NotNull ASTNode child, @Nullable @Nullable ASTNode anchorBefore)
      Adds the specified child node at the specified position in the child list.
      Parameters:
      child - the child node to add.
      anchorBefore - the node before which the child node is inserted (null to add a child as a last node).
    • addLeaf

      void addLeaf(@NotNull @NotNull IElementType leafType, @NotNull @NotNull CharSequence leafText, @Nullable @Nullable ASTNode anchorBefore)
      Add leaf element with specified type and text in the child list.
      Parameters:
      leafType - type of leaf element to add.
      leafText - text of added leaf.
      anchorBefore - the node before which the child node is inserted.
    • removeChild

      void removeChild(@NotNull @NotNull ASTNode child)
      Removes the specified node from the list of children of this node.
      Parameters:
      child - the child node to remove.
    • removeRange

      void removeRange(@NotNull @NotNull ASTNode firstNodeToRemove, ASTNode firstNodeToKeep)
      Removes a range of nodes from the list of children, starting with firstNodeToRemove, up to and not including firstNodeToKeep.
      Parameters:
      firstNodeToRemove - the first child node to remove from the tree.
      firstNodeToKeep - the first child node to keep in the tree.
    • replaceChild

      void replaceChild(@NotNull @NotNull ASTNode oldChild, @NotNull @NotNull ASTNode newChild)
      Replaces the specified child node with another node.
      Parameters:
      oldChild - the child node to replace.
      newChild - the node to replace with.
    • replaceAllChildrenToChildrenOf

      void replaceAllChildrenToChildrenOf(@NotNull @NotNull ASTNode anotherParent)
      Replaces all child nodes with the children of the specified node.
      Parameters:
      anotherParent - the parent node whose children are used for replacement.
    • addChildren

      void addChildren(@NotNull @NotNull ASTNode firstChild, ASTNode firstChildToNotAdd, ASTNode anchorBefore)
      Adds a range of nodes belonging to the same parent to the list of children of this node, starting with firstChild, up to and not including firstChildToNotAdd.
      Parameters:
      firstChild - the first node to add.
      firstChildToNotAdd - the first child node following firstChild which will not be added to the tree.
      anchorBefore - the node before which the child nodes are inserted.
    • clone

      @NotNull @NotNull Object clone()
      Creates and returns a deep copy of the AST tree part starting at this node.
      Returns:
      the top node of the copied tree (as an ASTNode object)
    • copyElement

      ASTNode copyElement()
      Creates a copy of the entire AST tree containing this node and returns a counterpart of this node in the resulting tree.
      Returns:
      the counterpart of this node in the copied tree.
    • findLeafElementAt

      @Nullable @Nullable ASTNode findLeafElementAt(int offset)
      Finds a leaf child node at the specified offset from the start of the text range of this node.
      Parameters:
      offset - the relative offset for which the child node is requested.
      Returns:
      the child node, or null if none is found.
    • getCopyableUserData

      @Nullable <T> T getCopyableUserData(@NotNull @NotNull Key<T> key)
      Returns a copyable user data object attached to this node.
      Parameters:
      key - the key for accessing the user data object.
      Returns:
      the user data object, or null if no such object is found in the current node.
      See Also:
    • putCopyableUserData

      <T> void putCopyableUserData(@NotNull @NotNull Key<T> key, @Nullable T value)
      Attaches a copyable user data object to this node. Copyable user data objects are copied when the AST tree nodes are copied.
      Parameters:
      key - the key for accessing the user data object.
      value - the user data object to attach.
      See Also:
    • findChildByType

      @Nullable @Nullable ASTNode findChildByType(@NotNull @NotNull IElementType type)
      Returns the first child of the specified node which has the specified type.
      Parameters:
      type - the type of the node to return.
      Returns:
      the found node, or null if none was found.
    • findChildByType

      @Nullable @Nullable ASTNode findChildByType(@NotNull @NotNull IElementType type, @Nullable @Nullable ASTNode anchor)
      Returns the first child after anchor of the specified node which has the specified type.
      Parameters:
      type - the type of the node to return.
      anchor - to start search from
      Returns:
      the found node, or null if none was found.
    • findChildByType

      @Nullable @Nullable ASTNode findChildByType(@NotNull @NotNull TokenSet typesSet)
      Returns the first child of the specified node which has type from specified set.
      Parameters:
      typesSet - the token set used to filter the returned children.
      Returns:
      the found node, or null if none was found.
    • findChildByType

      @Nullable @Nullable ASTNode findChildByType(@NotNull @NotNull TokenSet typesSet, @Nullable @Nullable ASTNode anchor)
      Returns the first child after anchor of the specified node which has type from specified set.
      Parameters:
      typesSet - the token set used to filter the returned children.
      anchor - to start search from
      Returns:
      the found node, or null if none was found.
    • getPsi

      PsiElement getPsi()
      Returns the PSI element for this node.
      Returns:
      the PSI element.
    • getPsi

      <T extends PsiElement> T getPsi(@NotNull @NotNull Class<T> clazz)
      Checks and returns the PSI element for this node.
      Parameters:
      clazz - expected psi class
      Returns:
      the PSI element.