Search in sources :

Example 1 with Selection

use of org.eclipse.jdt.internal.corext.dom.Selection in project che by eclipse.

the class ExtractMethodAnalyzer method visit.

@Override
public boolean visit(LambdaExpression node) {
    Selection selection = getSelection();
    int selectionStart = selection.getOffset();
    int selectionExclusiveEnd = selection.getExclusiveEnd();
    int lambdaStart = node.getStartPosition();
    int lambdaExclusiveEnd = lambdaStart + node.getLength();
    ASTNode body = node.getBody();
    int bodyStart = body.getStartPosition();
    int bodyExclusiveEnd = bodyStart + body.getLength();
    boolean isValidSelection = false;
    if ((body instanceof Block) && (bodyStart < selectionStart && selectionExclusiveEnd <= bodyExclusiveEnd)) {
        // if selection is inside lambda body's block
        isValidSelection = true;
    } else if (body instanceof Expression) {
        try {
            TokenScanner scanner = new TokenScanner(fCUnit);
            int arrowExclusiveEnd = scanner.getTokenEndOffset(ITerminalSymbols.TokenNameARROW, lambdaStart);
            if (selectionStart >= arrowExclusiveEnd) {
                isValidSelection = true;
            }
        } catch (CoreException e) {
        // ignore
        }
    }
    if (selectionStart <= lambdaStart && selectionExclusiveEnd >= lambdaExclusiveEnd) {
        // if selection covers the lambda node
        isValidSelection = true;
    }
    if (!isValidSelection) {
        return false;
    }
    return super.visit(node);
}
Also used : TokenScanner(org.eclipse.jdt.internal.corext.dom.TokenScanner) CoreException(org.eclipse.core.runtime.CoreException) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) Selection(org.eclipse.jdt.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block)

Example 2 with Selection

use of org.eclipse.jdt.internal.corext.dom.Selection 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 3 with Selection

use of org.eclipse.jdt.internal.corext.dom.Selection 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 4 with Selection

use of org.eclipse.jdt.internal.corext.dom.Selection 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)

Example 5 with Selection

use of org.eclipse.jdt.internal.corext.dom.Selection in project che by eclipse.

the class CallInliner method flowAnalysis.

private void flowAnalysis() {
    fInvocationScope = fRootScope.findScope(fTargetNode.getStartPosition(), fTargetNode.getLength());
    fInvocationScope.setCursor(fTargetNode.getStartPosition());
    fFlowContext = new FlowContext(0, fNumberOfLocals + 1);
    fFlowContext.setConsiderAccessMode(true);
    fFlowContext.setComputeMode(FlowContext.ARGUMENTS);
    Selection selection = Selection.createFromStartLength(fInvocation.getStartPosition(), fInvocation.getLength());
    switch(fBodyDeclaration.getNodeType()) {
        case ASTNode.INITIALIZER:
        case ASTNode.FIELD_DECLARATION:
        case ASTNode.METHOD_DECLARATION:
        case ASTNode.ENUM_CONSTANT_DECLARATION:
            fFlowInfo = new InputFlowAnalyzer(fFlowContext, selection, true).perform(fBodyDeclaration);
            break;
        default:
            //$NON-NLS-1$
            Assert.isTrue(false, "Should not happen");
    }
}
Also used : Selection(org.eclipse.jdt.internal.corext.dom.Selection) InputFlowAnalyzer(org.eclipse.jdt.internal.corext.refactoring.code.flow.InputFlowAnalyzer) FlowContext(org.eclipse.jdt.internal.corext.refactoring.code.flow.FlowContext)

Aggregations

Selection (org.eclipse.jdt.internal.corext.dom.Selection)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)7 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 SelectionAnalyzer (org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer)4 ASTParser (org.eclipse.jdt.core.dom.ASTParser)3 Expression (org.eclipse.jdt.core.dom.Expression)3 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)3 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)3 RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)3 ArrayList (java.util.ArrayList)2 Block (org.eclipse.jdt.core.dom.Block)2 FlowContext (org.eclipse.jdt.internal.corext.refactoring.code.flow.FlowContext)2 InputFlowAnalyzer (org.eclipse.jdt.internal.corext.refactoring.code.flow.InputFlowAnalyzer)2 HashSet (java.util.HashSet)1 TextSelection (org.eclipse.che.jface.text.TextSelection)1 CoreException (org.eclipse.core.runtime.CoreException)1 IType (org.eclipse.jdt.core.IType)1 IProblem (org.eclipse.jdt.core.compiler.IProblem)1 ArrayType (org.eclipse.jdt.core.dom.ArrayType)1