Search in sources :

Example 51 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getSplitAndConditionProposals.

public static boolean getSplitAndConditionProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    Operator andOperator = InfixExpression.Operator.CONDITIONAL_AND;
    // check that user invokes quick assist on infix expression
    if (!(node instanceof InfixExpression)) {
        return false;
    }
    InfixExpression infixExpression = (InfixExpression) node;
    if (infixExpression.getOperator() != andOperator) {
        return false;
    }
    int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
    if (offset == -1) {
        return false;
    }
    // check that infix expression belongs to IfStatement
    Statement statement = ASTResolving.findParentStatement(node);
    if (!(statement instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) statement;
    // check that infix expression is part of first level && condition of IfStatement
    InfixExpression topInfixExpression = infixExpression;
    while (topInfixExpression.getParent() instanceof InfixExpression && ((InfixExpression) topInfixExpression.getParent()).getOperator() == andOperator) {
        topInfixExpression = (InfixExpression) topInfixExpression.getParent();
    }
    if (ifStatement.getExpression() != topInfixExpression) {
        return false;
    }
    // 
    if (resultingCollections == null) {
        return true;
    }
    AST ast = ifStatement.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare left and right conditions
    Expression[] newOperands = { null, null };
    breakInfixOperationAtOperation(rewrite, topInfixExpression, andOperator, offset, true, newOperands);
    Expression leftCondition = newOperands[0];
    Expression rightCondition = newOperands[1];
    // replace conditions in outer IfStatement
    rewrite.set(ifStatement, IfStatement.EXPRESSION_PROPERTY, leftCondition, null);
    // prepare inner IfStatement
    IfStatement innerIf = ast.newIfStatement();
    innerIf.setExpression(rightCondition);
    innerIf.setThenStatement((Statement) rewrite.createMoveTarget(ifStatement.getThenStatement()));
    Block innerBlock = ast.newBlock();
    innerBlock.statements().add(innerIf);
    Statement elseStatement = ifStatement.getElseStatement();
    if (elseStatement != null) {
        innerIf.setElseStatement((Statement) rewrite.createCopyTarget(elseStatement));
    }
    // replace outer thenStatement
    rewrite.replace(ifStatement.getThenStatement(), innerBlock, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_splitAndCondition_description;
    // Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_AND_CONDITION);
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) AST(org.eclipse.jdt.core.dom.AST) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Example 52 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getJoinOrIfStatementsProposals.

private static boolean getJoinOrIfStatementsProposals(IInvocationContext context, ASTNode covering, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
    Operator orOperator = InfixExpression.Operator.CONDITIONAL_OR;
    if (coveredNodes.size() < 2)
        return false;
    // check that all covered nodes are IfStatement's with same 'then' statement and without 'else'
    String commonThenSource = null;
    for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
        ASTNode node = iter.next();
        if (!(node instanceof IfStatement))
            return false;
        // 
        IfStatement ifStatement = (IfStatement) node;
        if (ifStatement.getElseStatement() != null)
            return false;
        // 
        Statement thenStatement = ifStatement.getThenStatement();
        try {
            String thenSource = context.getCompilationUnit().getBuffer().getText(thenStatement.getStartPosition(), thenStatement.getLength());
            if (commonThenSource == null) {
                commonThenSource = thenSource;
            } else {
                if (!commonThenSource.equals(thenSource))
                    return false;
            }
        } catch (Throwable e) {
            return false;
        }
    }
    if (resultingCollections == null) {
        return true;
    }
    // 
    final AST ast = covering.getAST();
    final ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare OR'ed condition
    InfixExpression condition = null;
    boolean hasRightOperand = false;
    Statement thenStatement = null;
    for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
        IfStatement ifStatement = (IfStatement) iter.next();
        if (thenStatement == null)
            thenStatement = (Statement) rewrite.createCopyTarget(ifStatement.getThenStatement());
        if (condition == null) {
            condition = ast.newInfixExpression();
            condition.setOperator(orOperator);
            condition.setLeftOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.LEFT_OPERAND_PROPERTY));
        } else if (!hasRightOperand) {
            condition.setRightOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY));
            hasRightOperand = true;
        } else {
            InfixExpression newCondition = ast.newInfixExpression();
            newCondition.setOperator(orOperator);
            newCondition.setLeftOperand(condition);
            newCondition.setRightOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY));
            condition = newCondition;
        }
    }
    // prepare new IfStatement with OR'ed condition
    IfStatement newIf = ast.newIfStatement();
    newIf.setExpression(condition);
    newIf.setThenStatement(thenStatement);
    // 
    ListRewrite listRewriter = null;
    for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
        IfStatement ifStatement = (IfStatement) iter.next();
        if (listRewriter == null) {
            Block sourceBlock = (Block) ifStatement.getParent();
            // int insertIndex = sourceBlock.statements().indexOf(ifStatement);
            listRewriter = rewrite.getListRewrite(sourceBlock, (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
        }
        if (newIf != null) {
            listRewriter.replace(ifStatement, newIf, null);
            newIf = null;
        } else {
            listRewriter.remove(ifStatement, null);
        }
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_joinWithOr_description;
    // Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_IF_STATEMENTS_WITH_OR);
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) AST(org.eclipse.jdt.core.dom.AST) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Example 53 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.

the class AdvancedQuickAssistProcessor method breakInfixOperationAtOperation.

/*
	 * Breaks an infix operation with possible extended operators at the given operator and returns the new left and right operands.
	 * a & b & c   ->  [[a' & b' ] & c' ]   (c' == copy of c)
	 */
private static void breakInfixOperationAtOperation(ASTRewrite rewrite, Expression expression, Operator operator, int operatorOffset, boolean removeParentheses, Expression[] res) {
    if (expression.getStartPosition() + expression.getLength() <= operatorOffset) {
        // add to the left
        res[0] = combineOperands(rewrite, res[0], expression, removeParentheses, operator);
        return;
    }
    if (operatorOffset <= expression.getStartPosition()) {
        // add to the right
        res[1] = combineOperands(rewrite, res[1], expression, removeParentheses, operator);
        return;
    }
    if (!(expression instanceof InfixExpression)) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("Cannot break up non-infix expression");
    }
    InfixExpression infixExpression = (InfixExpression) expression;
    if (infixExpression.getOperator() != operator) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("Incompatible operator");
    }
    breakInfixOperationAtOperation(rewrite, infixExpression.getLeftOperand(), operator, operatorOffset, removeParentheses, res);
    breakInfixOperationAtOperation(rewrite, infixExpression.getRightOperand(), operator, operatorOffset, removeParentheses, res);
    List<Expression> extended = infixExpression.extendedOperands();
    for (int i = 0; i < extended.size(); i++) {
        breakInfixOperationAtOperation(rewrite, extended.get(i), operator, operatorOffset, removeParentheses, res);
    }
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 54 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getPushNegationDownProposals.

private static boolean getPushNegationDownProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    PrefixExpression negationExpression = null;
    ParenthesizedExpression parenthesizedExpression = null;
    // check for case when cursor is on '!' before parentheses
    if (covering instanceof PrefixExpression) {
        PrefixExpression prefixExpression = (PrefixExpression) covering;
        if (prefixExpression.getOperator() == PrefixExpression.Operator.NOT && prefixExpression.getOperand() instanceof ParenthesizedExpression) {
            negationExpression = prefixExpression;
            parenthesizedExpression = (ParenthesizedExpression) prefixExpression.getOperand();
        }
    }
    // check for case when cursor is on parenthesized expression that is negated
    if (covering instanceof ParenthesizedExpression && covering.getParent() instanceof PrefixExpression && ((PrefixExpression) covering.getParent()).getOperator() == PrefixExpression.Operator.NOT) {
        negationExpression = (PrefixExpression) covering.getParent();
        parenthesizedExpression = (ParenthesizedExpression) covering;
    }
    if (negationExpression == null || (!(parenthesizedExpression.getExpression() instanceof InfixExpression) && !(parenthesizedExpression.getExpression() instanceof ConditionalExpression))) {
        return false;
    }
    // we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    // 
    final AST ast = covering.getAST();
    final ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepared inverted expression
    Expression inversedExpression = getInversedExpression(rewrite, parenthesizedExpression.getExpression());
    // check, may be we should keep parentheses
    boolean keepParentheses = false;
    if (negationExpression.getParent() instanceof Expression) {
        int parentPrecedence = OperatorPrecedence.getExpressionPrecedence(((Expression) negationExpression.getParent()));
        int inversedExpressionPrecedence = OperatorPrecedence.getExpressionPrecedence(inversedExpression);
        keepParentheses = parentPrecedence > inversedExpressionPrecedence;
    }
    // replace negated expression with inverted one
    if (keepParentheses) {
        ParenthesizedExpression pe = ast.newParenthesizedExpression();
        pe.setExpression(inversedExpression);
        rewrite.replace(negationExpression, pe, null);
    } else {
        rewrite.replace(negationExpression, inversedExpression, null);
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_pushNegationDown;
    // Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PULL_NEGATION_DOWN);
    resultingCollections.add(proposal);
    return true;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AST(org.eclipse.jdt.core.dom.AST) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 55 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project flux by eclipse.

the class AdvancedQuickAssistProcessor method isOperatorSelected.

private static int isOperatorSelected(InfixExpression infixExpression, int offset, int length) {
    ASTNode left = infixExpression.getLeftOperand();
    ASTNode right = infixExpression.getRightOperand();
    if (isSelectingOperator(left, right, offset, length)) {
        return ASTNodes.getExclusiveEnd(left);
    }
    List<Expression> extended = infixExpression.extendedOperands();
    for (int i = 0; i < extended.size(); i++) {
        left = right;
        right = extended.get(i);
        if (isSelectingOperator(left, right, offset, length)) {
            return ASTNodes.getExclusiveEnd(left);
        }
    }
    return -1;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)196 Expression (org.eclipse.jdt.core.dom.Expression)137 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)85 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)67 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)55 CastExpression (org.eclipse.jdt.core.dom.CastExpression)47 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)37 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)34 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)31 AST (org.eclipse.jdt.core.dom.AST)29 ASTNode (org.eclipse.jdt.core.dom.ASTNode)29 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)25 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)24 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)24 TextEditGroup (org.eclipse.text.edits.TextEditGroup)22 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)20 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)20 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)18 Assignment (org.eclipse.jdt.core.dom.Assignment)17 IfStatement (org.eclipse.jdt.core.dom.IfStatement)17