use of org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer in project che by eclipse.
the class JavaTextSelection method resolveSelectedNodes.
public ASTNode[] resolveSelectedNodes() {
if (fNodesRequested)
return fSelectedNodes;
fNodesRequested = true;
CompilationUnit root = resolvePartialAstAtOffset();
if (root == null)
return null;
Selection ds = Selection.createFromStartLength(getOffset(), getLength());
SelectionAnalyzer analyzer = new SelectionAnalyzer(ds, false);
root.accept(analyzer);
fSelectedNodes = analyzer.getSelectedNodes();
fCoveringNode = analyzer.getLastCoveringNode();
return fSelectedNodes;
}
use of org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer in project che by eclipse.
the class ASTFragmentFactory method createFragmentForSourceRange.
/**
* If possible, this method creates a fragment whose source code
* range is <code>range</code> within compilation unit <code>cu</code>,
* and which resides somewhere within the subtree identified by
* <code>scope</code>.
*
* XXX: more doc (current assertions about input vs. output)
*
* @param range The source range which the create fragment must have.
* @param scope A node identifying the AST subtree in which the fragment must lie.
* @param cu The compilation unit to which the source range applies, and to which the AST corresponds.
* @return IASTFragment A fragment whose source range is <code>range</code> within
* compilation unit <code>cu</code>, residing somewhere within the
* AST subtree identified by <code>scope</code>.
* @throws JavaModelException
*/
public static IASTFragment createFragmentForSourceRange(ISourceRange range, ASTNode scope, ICompilationUnit cu) throws JavaModelException {
SelectionAnalyzer sa = new SelectionAnalyzer(Selection.createFromStartLength(range.getOffset(), range.getLength()), false);
scope.accept(sa);
if (isSingleNodeSelected(sa, range, cu))
return ASTFragmentFactory.createFragmentForFullSubtree(sa.getFirstSelectedNode());
if (isEmptySelectionCoveredByANode(range, sa))
return ASTFragmentFactory.createFragmentForFullSubtree(sa.getLastCoveringNode());
return ASTFragmentFactory.createFragmentForSubPartBySourceRange(sa.getLastCoveringNode(), range, cu);
}
use of org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer in project che by eclipse.
the class ASTNodeSearchUtil method getAstNode.
public static ASTNode getAstNode(CompilationUnit cuNode, int start, int length) {
SelectionAnalyzer analyzer = new SelectionAnalyzer(Selection.createFromStartLength(start, length), true);
cuNode.accept(analyzer);
//XXX workaround for jdt core feature 23527
ASTNode node = analyzer.getFirstSelectedNode();
if (node == null && analyzer.getLastCoveringNode() instanceof SuperConstructorInvocation)
node = analyzer.getLastCoveringNode().getParent();
else if (node == null && analyzer.getLastCoveringNode() instanceof ConstructorInvocation)
node = analyzer.getLastCoveringNode().getParent();
if (node == null)
return null;
ASTNode parentNode = node.getParent();
if (parentNode instanceof MethodDeclaration) {
MethodDeclaration md = (MethodDeclaration) parentNode;
if (!(node instanceof SimpleName) && md.isConstructor() && md.getBody() != null && md.getBody().statements().size() > 0 && (md.getBody().statements().get(0) instanceof ConstructorInvocation || md.getBody().statements().get(0) instanceof SuperConstructorInvocation) && ((ASTNode) md.getBody().statements().get(0)).getLength() == length + 1)
return (ASTNode) md.getBody().statements().get(0);
}
if (parentNode instanceof SuperConstructorInvocation) {
if (parentNode.getLength() == length + 1)
return parentNode;
}
if (parentNode instanceof ConstructorInvocation) {
if (parentNode.getLength() == length + 1)
return parentNode;
}
return node;
}
use of org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer in project che by eclipse.
the class ChangeSignatureProcessor method isValidExpression.
public static boolean isValidExpression(String string) {
String trimmed = string.trim();
if (//speed up for a common case //$NON-NLS-1$
"".equals(trimmed))
return false;
StringBuffer cuBuff = new StringBuffer();
cuBuff.append(CONST_CLASS_DECL).append(//$NON-NLS-1$
"Object").append(CONST_ASSIGN);
int offset = cuBuff.length();
cuBuff.append(trimmed).append(CONST_CLOSE);
ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
p.setSource(cuBuff.toString().toCharArray());
CompilationUnit cu = (CompilationUnit) p.createAST(null);
Selection selection = Selection.createFromStartLength(offset, trimmed.length());
SelectionAnalyzer analyzer = new SelectionAnalyzer(selection, false);
cu.accept(analyzer);
ASTNode selected = analyzer.getFirstSelectedNode();
return (selected instanceof Expression) && trimmed.equals(cuBuff.substring(cu.getExtendedStartPosition(selected), cu.getExtendedStartPosition(selected) + cu.getExtendedLength(selected)));
}
use of org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer in project che by eclipse.
the class ChangeSignatureProcessor method isValidVarargsExpression.
public static boolean isValidVarargsExpression(String string) {
String trimmed = string.trim();
if (//speed up for a common case //$NON-NLS-1$
"".equals(trimmed))
return true;
StringBuffer cuBuff = new StringBuffer();
//$NON-NLS-1$
cuBuff.append("class A{ {m(");
int offset = cuBuff.length();
cuBuff.append(trimmed).append(//$NON-NLS-1$
");}}");
ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
p.setSource(cuBuff.toString().toCharArray());
CompilationUnit cu = (CompilationUnit) p.createAST(null);
Selection selection = Selection.createFromStartLength(offset, trimmed.length());
SelectionAnalyzer analyzer = new SelectionAnalyzer(selection, false);
cu.accept(analyzer);
ASTNode[] selectedNodes = analyzer.getSelectedNodes();
if (selectedNodes.length == 0)
return false;
for (int i = 0; i < selectedNodes.length; i++) {
if (!(selectedNodes[i] instanceof Expression))
return false;
}
return true;
}
Aggregations