Class NodeFinder


  • public final class NodeFinder
    extends Object
    For a given selection range, finds the covered node and the covering node.
    Since:
    3.5
    • Constructor Detail

      • NodeFinder

        public NodeFinder​(ASTNode root,
                          int start,
                          int length)
        Instantiate a new node finder using the given root node, the given start and the given length.
        Parameters:
        root - the given root node
        start - the given start
        length - the given length
    • Method Detail

      • perform

        public static ASTNode perform​(ASTNode root,
                                      int start,
                                      int length)
        Maps a selection to an ASTNode, where the selection is defined using a start and a length. The result node is determined as follows:
        • First, tries to find a node whose range is the exactly the given selection. If multiple matching nodes are found, the innermost is returned.
        • If no such node exists, then the last node in a preorder traversal of the AST is returned, where the node range fully contains the selection. If the length is zero, then ties between adjacent nodes are broken by choosing the right side.
        Parameters:
        root - the root node from which the search starts
        start - the start of the selection
        length - the length of the selection
        Returns:
        the innermost node that exactly matches the selection, or the first node that contains the selection
      • perform

        public static ASTNode perform​(ASTNode root,
                                      ISourceRange range)
        Maps a selection to an ASTNode, where the selection is defined using a source range. Calls perform(root, range.getOffset(), range.getLength()).
        Parameters:
        root - the root node from which the search starts
        range - the selection range
        Returns:
        the innermost node that exactly matches the selection, or the first node that contains the selection
        See Also:
        perform(ASTNode, int, int)
      • perform

        public static ASTNode perform​(ASTNode root,
                                      int start,
                                      int length,
                                      ITypeRoot source)
                               throws JavaModelException
        Maps a selection to an ASTNode, where the selection is given by a start and a length. The result node is determined as follows:
        • If getCoveredNode() doesn't find a node, returns null.
        • Otherwise, iff the selection only contains the covered node and optionally some whitespace or comments on either side of the node, returns the node.
        • Otherwise, returns the covering node.
        Parameters:
        root - the root node from which the search starts
        start - the start of the selection
        length - the length of the selection
        source - the source of the compilation unit
        Returns:
        the result node
        Throws:
        JavaModelException - if an error occurs in the Java model
      • getCoveredNode

        public ASTNode getCoveredNode()
        If the AST contains nodes whose range is equal to the selection, returns the innermost of those nodes. Otherwise, returns the first node in a preorder traversal of the AST, where the complete node range is covered by the selection.

        Example: For a SimpleType whose name is a SimpleName and a selection that equals both nodes' range, the covered node is the SimpleName. But if the selection is expanded to include a whitespace before or after the SimpleType, then the covered node is the SimpleType.

        Returns:
        the covered node, or null if the selection is empty or too short to cover an entire node
      • getCoveringNode

        public ASTNode getCoveringNode()
        Returns the innermost node that fully contains the selection. A node also contains the zero-length selection on either end.

        If more than one node covers the selection, the returned node is the last covering node found in a preorder traversal of the AST. This implies that for a zero-length selection between two adjacent sibling nodes, the node on the right is returned.

        Returns:
        the covering node