Search in sources :

Example 6 with TokenScanner

use of org.eclipse.jdt.internal.corext.dom.TokenScanner in project eclipse.jdt.ls 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.ls.core.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)6 TokenScanner (org.eclipse.jdt.internal.corext.dom.TokenScanner)6 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 Block (org.eclipse.jdt.core.dom.Block)3 IScanner (org.eclipse.jdt.core.compiler.IScanner)2 Expression (org.eclipse.jdt.core.dom.Expression)2 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)2 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)2 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)2 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)2 LinkedCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal)2 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 AST (org.eclipse.jdt.core.dom.AST)1 DoStatement (org.eclipse.jdt.core.dom.DoStatement)1 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 ForStatement (org.eclipse.jdt.core.dom.ForStatement)1 IfStatement (org.eclipse.jdt.core.dom.IfStatement)1 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)1