Search in sources :

Example 16 with InfixExpression

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

the class AdvancedQuickAssistProcessor method getExchangeOperandsProposals.

private static boolean getExchangeOperandsProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    // check that user invokes quick assist on infix expression
    if (!(node instanceof InfixExpression)) {
        return false;
    }
    InfixExpression infixExpression = (InfixExpression) node;
    Operator operator = infixExpression.getOperator();
    if (operator != InfixExpression.Operator.CONDITIONAL_AND && operator != InfixExpression.Operator.AND && operator != InfixExpression.Operator.CONDITIONAL_OR && operator != InfixExpression.Operator.OR && operator != InfixExpression.Operator.EQUALS && operator != InfixExpression.Operator.NOT_EQUALS && operator != InfixExpression.Operator.LESS && operator != InfixExpression.Operator.LESS_EQUALS && operator != InfixExpression.Operator.GREATER && operator != InfixExpression.Operator.GREATER_EQUALS && operator != InfixExpression.Operator.PLUS && operator != InfixExpression.Operator.TIMES && operator != InfixExpression.Operator.XOR) {
        return false;
    }
    int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
    if (offset == -1) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    AST ast = infixExpression.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare left and right expressions
    Expression leftExpression = null;
    Expression rightExpression = null;
    InfixExpression currentExpression = infixExpression;
    leftExpression = combineOperands(rewrite, leftExpression, infixExpression.getLeftOperand(), false, operator);
    if (infixExpression.getRightOperand().getStartPosition() <= context.getSelectionOffset()) {
        leftExpression = combineOperands(rewrite, leftExpression, infixExpression.getRightOperand(), false, operator);
    } else {
        rightExpression = combineOperands(rewrite, rightExpression, infixExpression.getRightOperand(), false, operator);
    }
    for (Iterator<Expression> iter = currentExpression.extendedOperands().iterator(); iter.hasNext(); ) {
        Expression extendedOperand = iter.next();
        if (extendedOperand.getStartPosition() <= context.getSelectionOffset()) {
            leftExpression = combineOperands(rewrite, leftExpression, extendedOperand, false, operator);
        } else {
            rightExpression = combineOperands(rewrite, rightExpression, extendedOperand, false, operator);
        }
    }
    if (NecessaryParenthesesChecker.needsParentheses(leftExpression, infixExpression, InfixExpression.RIGHT_OPERAND_PROPERTY)) {
        leftExpression = getParenthesizedExpression(ast, leftExpression);
    }
    if (NecessaryParenthesesChecker.needsParentheses(rightExpression, infixExpression, InfixExpression.LEFT_OPERAND_PROPERTY)) {
        rightExpression = getParenthesizedExpression(ast, rightExpression);
    }
    if (operator == InfixExpression.Operator.LESS) {
        operator = InfixExpression.Operator.GREATER;
    } else if (operator == InfixExpression.Operator.LESS_EQUALS) {
        operator = InfixExpression.Operator.GREATER_EQUALS;
    } else if (operator == InfixExpression.Operator.GREATER) {
        operator = InfixExpression.Operator.LESS;
    } else if (operator == InfixExpression.Operator.GREATER_EQUALS) {
        operator = InfixExpression.Operator.LESS_EQUALS;
    }
    // create new infix expression
    InfixExpression newInfix = ast.newInfixExpression();
    newInfix.setOperator(operator);
    newInfix.setLeftOperand(rightExpression);
    newInfix.setRightOperand(leftExpression);
    rewrite.replace(infixExpression, newInfix, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_exchangeOperands_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_OPERANDS);
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) 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) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 17 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 18 with InfixExpression

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

the class AdvancedQuickAssistProcessor method combineOperands.

private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) {
    if (existing == null && removeParentheses) {
        while (originalNode instanceof ParenthesizedExpression) {
            originalNode = ((ParenthesizedExpression) originalNode).getExpression();
        }
    }
    Expression newRight = (Expression) rewrite.createMoveTarget(originalNode);
    if (originalNode instanceof InfixExpression) {
        ((InfixExpression) newRight).setOperator(((InfixExpression) originalNode).getOperator());
    }
    if (existing == null) {
        return newRight;
    }
    AST ast = rewrite.getAST();
    InfixExpression infix = ast.newInfixExpression();
    infix.setOperator(operator);
    infix.setLeftOperand(existing);
    infix.setRightOperand(newRight);
    return infix;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) 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) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 19 with InfixExpression

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

the class AdvancedQuickAssistProcessor method getInversedAndOrExpression.

private static Expression getInversedAndOrExpression(ASTRewrite rewrite, InfixExpression infixExpression, Operator newOperator, SimpleNameRenameProvider provider) {
    InfixExpression newExpression = rewrite.getAST().newInfixExpression();
    newExpression.setOperator(newOperator);
    int newOperatorPrecedence = OperatorPrecedence.getOperatorPrecedence(newOperator);
    //
    Expression leftOperand = getInversedExpression(rewrite, infixExpression.getLeftOperand(), provider);
    newExpression.setLeftOperand(parenthesizeIfRequired(leftOperand, newOperatorPrecedence));
    Expression rightOperand = getInversedExpression(rewrite, infixExpression.getRightOperand(), provider);
    newExpression.setRightOperand(parenthesizeIfRequired(rightOperand, newOperatorPrecedence));
    List<Expression> extraOperands = infixExpression.extendedOperands();
    List<Expression> newExtraOperands = newExpression.extendedOperands();
    for (int i = 0; i < extraOperands.size(); i++) {
        Expression extraOperand = getInversedExpression(rewrite, extraOperands.get(i), provider);
        newExtraOperands.add(parenthesizeIfRequired(extraOperand, newOperatorPrecedence));
    }
    return newExpression;
}
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 20 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)73 Expression (org.eclipse.jdt.core.dom.Expression)50 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)40 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)30 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)23 CastExpression (org.eclipse.jdt.core.dom.CastExpression)22 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)19 AST (org.eclipse.jdt.core.dom.AST)18 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 Operator (org.eclipse.jdt.core.dom.InfixExpression.Operator)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)10 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)10 Assignment (org.eclipse.jdt.core.dom.Assignment)8 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)8 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)8 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7