Search in sources :

Example 1 with SelectionAnalyzer

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;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) Selection(org.eclipse.jdt.internal.corext.dom.Selection) TextSelection(org.eclipse.che.jface.text.TextSelection)

Example 2 with SelectionAnalyzer

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);
}
Also used : SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer)

Example 3 with SelectionAnalyzer

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;
}
Also used : SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 4 with SelectionAnalyzer

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)));
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) Selection(org.eclipse.jdt.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTParser(org.eclipse.jdt.core.dom.ASTParser) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)

Example 5 with SelectionAnalyzer

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;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) Selection(org.eclipse.jdt.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTParser(org.eclipse.jdt.core.dom.ASTParser) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)

Aggregations

SelectionAnalyzer (org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer)8 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 Selection (org.eclipse.jdt.internal.corext.dom.Selection)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 ASTParser (org.eclipse.jdt.core.dom.ASTParser)3 RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)3 Expression (org.eclipse.jdt.core.dom.Expression)2 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)2 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)2 Hashtable (java.util.Hashtable)1 TextSelection (org.eclipse.che.jface.text.TextSelection)1 IType (org.eclipse.jdt.core.IType)1 IProblem (org.eclipse.jdt.core.compiler.IProblem)1 ArrayType (org.eclipse.jdt.core.dom.ArrayType)1 Block (org.eclipse.jdt.core.dom.Block)1 ConstructorInvocation (org.eclipse.jdt.core.dom.ConstructorInvocation)1 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)1 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)1 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)1