Package org.eclipse.jdt.core.dom
Class NodeFinder
java.lang.Object
org.eclipse.jdt.core.dom.NodeFinder
For a given selection range, finds the covered node and the covering node.
- Since:
- 3.5
-
Constructor Summary
ConstructorDescriptionNodeFinder
(ASTNode root, int start, int length) Instantiate a new node finder using the given root node, the given start and the given length. -
Method Summary
Modifier and TypeMethodDescriptionIf the AST contains nodes whose range is equal to the selection, returns the innermost of those nodes.Returns the innermost node that fully contains the selection.static ASTNode
Maps a selection to an ASTNode, where the selection is defined using a start and a length.static ASTNode
Maps a selection to an ASTNode, where the selection is given by a start and a length.static ASTNode
perform
(ASTNode root, ISourceRange range) Maps a selection to an ASTNode, where the selection is defined using a source range.
-
Constructor Details
-
NodeFinder
Instantiate a new node finder using the given root node, the given start and the given length.- Parameters:
root
- the given root nodestart
- the given startlength
- the given length
-
-
Method Details
-
perform
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 startsstart
- the start of the selectionlength
- the length of the selection- Returns:
- the innermost node that exactly matches the selection, or the first node that contains the selection
-
perform
Maps a selection to an ASTNode, where the selection is defined using a source range. Callsperform(root, range.getOffset(), range.getLength())
.- Parameters:
root
- the root node from which the search startsrange
- the selection range- Returns:
- the innermost node that exactly matches the selection, or the first node that contains the selection
- See Also:
-
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, returnsnull
. - 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 startsstart
- the start of the selectionlength
- the length of the selectionsource
- the source of the compilation unit- Returns:
- the result node
- Throws:
JavaModelException
- if an error occurs in the Java model
- If
-
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 aSimpleName
and a selection that equals both nodes' range, the covered node is theSimpleName
. But if the selection is expanded to include a whitespace before or after theSimpleType
, then the covered node is theSimpleType
.- Returns:
- the covered node, or
null
if the selection is empty or too short to cover an entire node
-
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
-