Search in sources :

Example 16 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che 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, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Example 17 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.

the class AdvancedQuickAssistProcessor method getExchangeInnerAndOuterIfConditionsProposals.

private static boolean getExchangeInnerAndOuterIfConditionsProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    boolean result = false;
    //
    if (!(node instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) node;
    if (ifStatement.getElseStatement() != null) {
        return false;
    }
    // case when current IfStatement is sole child of another IfStatement
    {
        IfStatement outerIf = null;
        if (ifStatement.getParent() instanceof IfStatement) {
            outerIf = (IfStatement) ifStatement.getParent();
        } else if (ifStatement.getParent() instanceof Block) {
            Block block = (Block) ifStatement.getParent();
            if (block.getParent() instanceof IfStatement && block.statements().size() == 1) {
                outerIf = (IfStatement) block.getParent();
            }
        }
        if (outerIf != null && outerIf.getElseStatement() == null) {
            if (resultingCollections == null) {
                return true;
            }
            //
            AST ast = node.getAST();
            ASTRewrite rewrite = ASTRewrite.create(ast);
            // prepare conditions
            Expression outerCondition = (Expression) rewrite.createCopyTarget(outerIf.getExpression());
            Expression innerCondition = (Expression) rewrite.createCopyTarget(ifStatement.getExpression());
            // exchange conditions
            rewrite.replace(outerIf.getExpression(), innerCondition, null);
            rewrite.replace(ifStatement.getExpression(), outerCondition, null);
            // add correction proposal
            String label = CorrectionMessages.AdvancedQuickAssistProcessor_exchangeInnerAndOuterIfConditions_description;
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_INNER_AND_OUTER_IF_CONDITIONS, image);
            resultingCollections.add(proposal);
            result = true;
        }
    }
    // case when current IfStatement has another IfStatement as sole child
    {
        IfStatement innerIf = null;
        if (ifStatement.getThenStatement() instanceof IfStatement) {
            innerIf = (IfStatement) ifStatement.getThenStatement();
        } else if (ifStatement.getThenStatement() instanceof Block) {
            Block block = (Block) ifStatement.getThenStatement();
            if (block.statements().size() == 1 && block.statements().get(0) instanceof IfStatement) {
                innerIf = (IfStatement) block.statements().get(0);
            }
        }
        if (innerIf != null && innerIf.getElseStatement() == null) {
            if (resultingCollections == null) {
                return true;
            }
            //
            AST ast = node.getAST();
            ASTRewrite rewrite = ASTRewrite.create(ast);
            // prepare conditions
            Expression innerCondition = (Expression) rewrite.createCopyTarget(innerIf.getExpression());
            Expression outerCondition = (Expression) rewrite.createCopyTarget(ifStatement.getExpression());
            // exchange conditions
            rewrite.replace(innerIf.getExpression(), outerCondition, null);
            rewrite.replace(ifStatement.getExpression(), innerCondition, null);
            // add correction proposal
            String label = CorrectionMessages.AdvancedQuickAssistProcessor_exchangeInnerAndOuterIfConditions_description;
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_INNER_AND_OUTER_IF_CONDITIONS, image);
            resultingCollections.add(proposal);
            result = true;
        }
    }
    return result;
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Example 18 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.

the class AdvancedQuickAssistProcessor method getInverseConditionalExpressionProposals.

private static boolean getInverseConditionalExpressionProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    // try to find conditional expression as parent
    while (covering instanceof Expression) {
        if (covering instanceof ConditionalExpression)
            break;
        covering = covering.getParent();
    }
    if (!(covering instanceof ConditionalExpression)) {
        return false;
    }
    ConditionalExpression expression = (ConditionalExpression) covering;
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    AST ast = covering.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare new conditional expression
    ConditionalExpression newExpression = ast.newConditionalExpression();
    newExpression.setExpression(getInversedExpression(rewrite, expression.getExpression()));
    newExpression.setThenExpression((Expression) rewrite.createCopyTarget(expression.getElseExpression()));
    newExpression.setElseExpression((Expression) rewrite.createCopyTarget(expression.getThenExpression()));
    // replace old expression with new
    rewrite.replace(expression, newExpression, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseConditionalExpression_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_CONDITIONAL_EXPRESSION, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Example 19 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.

the class AdvancedQuickAssistProcessor method getReplaceConditionalWithIfElseProposals.

private static boolean getReplaceConditionalWithIfElseProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    ASTNode node = covering;
    while (!(node instanceof ConditionalExpression) && node instanceof Expression) {
        node = node.getParent();
    }
    if (!(node instanceof ConditionalExpression)) {
        node = covering;
        while (node != null && !(node instanceof Statement)) {
            node = node.getParent();
        }
        if (node instanceof VariableDeclarationStatement) {
            node = (ASTNode) (((VariableDeclarationStatement) node).fragments().get(0));
            node = ((VariableDeclarationFragment) node).getInitializer();
        }
        if (node instanceof ExpressionStatement) {
            node = ((ExpressionStatement) node).getExpression();
            if (node instanceof Assignment) {
                node = ((Assignment) node).getRightHandSide();
            }
        }
        if (node instanceof ReturnStatement) {
            node = ((ReturnStatement) node).getExpression();
        }
    }
    if (!(node instanceof ConditionalExpression)) {
        return false;
    }
    covering = node;
    StructuralPropertyDescriptor locationInParent = covering.getLocationInParent();
    if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
        if (covering.getParent().getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
            return false;
        }
    } else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
        ASTNode statement = covering.getParent().getParent();
        if (!(statement instanceof VariableDeclarationStatement) || statement.getLocationInParent() != Block.STATEMENTS_PROPERTY) {
            return false;
        }
    } else if (locationInParent != ReturnStatement.EXPRESSION_PROPERTY) {
        return false;
    }
    ConditionalExpression conditional = (ConditionalExpression) covering;
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    AST ast = covering.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare new 'if' statement
    Expression expression = conditional.getExpression();
    while (expression instanceof ParenthesizedExpression) {
        expression = ((ParenthesizedExpression) expression).getExpression();
    }
    IfStatement ifStatement = ast.newIfStatement();
    ifStatement.setExpression((Expression) rewrite.createCopyTarget(expression));
    if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
        Assignment assignment = (Assignment) covering.getParent();
        Expression assignee = assignment.getLeftHandSide();
        Assignment.Operator op = assignment.getOperator();
        ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression()));
        ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
        // replace return conditional expression with if/then/else/return
        rewrite.replace(covering.getParent().getParent(), ifStatement, null);
    } else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
        ifStatement.setThenStatement(createReturnExpression(rewrite, conditional.getThenExpression()));
        ifStatement.setElseStatement(createReturnExpression(rewrite, conditional.getElseExpression()));
        //
        // replace return conditional expression with if/then/else/return
        rewrite.replace(conditional.getParent(), ifStatement, null);
    } else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
        VariableDeclarationFragment frag = (VariableDeclarationFragment) covering.getParent();
        Assignment.Operator op = Assignment.Operator.ASSIGN;
        Expression assignee = frag.getName();
        ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression()));
        ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
        // clear initializer
        rewrite.set(frag, VariableDeclarationFragment.INITIALIZER_PROPERTY, null, null);
        ASTNode statement = frag.getParent();
        rewrite.getListRewrite(statement.getParent(), Block.STATEMENTS_PROPERTY).insertAfter(ifStatement, statement, null);
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_replaceConditionalWithIf;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_CONDITIONAL_WITH_IF_ELSE, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 20 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.

the class AdvancedQuickAssistProcessor method getReplaceIfElseWithConditionalProposals.

private static boolean getReplaceIfElseWithConditionalProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    if (!(node instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) node;
    Statement thenStatement = getSingleStatement(ifStatement.getThenStatement());
    Statement elseStatement = getSingleStatement(ifStatement.getElseStatement());
    if (thenStatement == null || elseStatement == null) {
        return false;
    }
    Expression assigned = null;
    Expression thenExpression = null;
    Expression elseExpression = null;
    ITypeBinding exprBinding = null;
    if (thenStatement instanceof ReturnStatement && elseStatement instanceof ReturnStatement) {
        thenExpression = ((ReturnStatement) thenStatement).getExpression();
        elseExpression = ((ReturnStatement) elseStatement).getExpression();
        MethodDeclaration declaration = ASTResolving.findParentMethodDeclaration(node);
        if (declaration == null || declaration.isConstructor()) {
            return false;
        }
        exprBinding = declaration.getReturnType2().resolveBinding();
    } else if (thenStatement instanceof ExpressionStatement && elseStatement instanceof ExpressionStatement) {
        Expression inner1 = ((ExpressionStatement) thenStatement).getExpression();
        Expression inner2 = ((ExpressionStatement) elseStatement).getExpression();
        if (inner1 instanceof Assignment && inner2 instanceof Assignment) {
            Assignment assign1 = (Assignment) inner1;
            Assignment assign2 = (Assignment) inner2;
            Expression left1 = assign1.getLeftHandSide();
            Expression left2 = assign2.getLeftHandSide();
            if (left1 instanceof Name && left2 instanceof Name && assign1.getOperator() == assign2.getOperator()) {
                IBinding bind1 = ((Name) left1).resolveBinding();
                IBinding bind2 = ((Name) left2).resolveBinding();
                if (bind1 == bind2 && bind1 instanceof IVariableBinding) {
                    assigned = left1;
                    exprBinding = ((IVariableBinding) bind1).getType();
                    thenExpression = assign1.getRightHandSide();
                    elseExpression = assign2.getRightHandSide();
                }
            }
        }
    }
    if (thenExpression == null || elseExpression == null) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    AST ast = node.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
    sourceRangeComputer.addTightSourceNode(ifStatement);
    rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_replaceIfWithConditional;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_IF_ELSE_WITH_CONDITIONAL, image);
    // prepare conditional expression
    ConditionalExpression conditionalExpression = ast.newConditionalExpression();
    Expression conditionCopy = (Expression) rewrite.createCopyTarget(ifStatement.getExpression());
    conditionalExpression.setExpression(conditionCopy);
    Expression thenCopy = (Expression) rewrite.createCopyTarget(thenExpression);
    Expression elseCopy = (Expression) rewrite.createCopyTarget(elseExpression);
    IJavaProject project = context.getCompilationUnit().getJavaProject();
    if (!JavaModelUtil.is50OrHigher(project)) {
        ITypeBinding thenBinding = thenExpression.resolveTypeBinding();
        ITypeBinding elseBinding = elseExpression.resolveTypeBinding();
        if (thenBinding != null && elseBinding != null && exprBinding != null && !elseBinding.isAssignmentCompatible(thenBinding)) {
            CastExpression castException = ast.newCastExpression();
            ImportRewrite importRewrite = proposal.createImportRewrite(context.getASTRoot());
            ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, importRewrite);
            castException.setType(importRewrite.addImport(exprBinding, ast, importRewriteContext));
            castException.setExpression(elseCopy);
            elseCopy = castException;
        }
    } else if (JavaModelUtil.is17OrHigher(project)) {
        addExplicitTypeArgumentsIfNecessary(rewrite, proposal, thenExpression);
        addExplicitTypeArgumentsIfNecessary(rewrite, proposal, elseExpression);
    }
    conditionalExpression.setThenExpression(thenCopy);
    conditionalExpression.setElseExpression(elseCopy);
    // replace 'if' statement with conditional expression
    if (assigned == null) {
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(conditionalExpression);
        rewrite.replace(ifStatement, returnStatement, null);
    } else {
        Assignment assignment = ast.newAssignment();
        assignment.setLeftHandSide((Expression) rewrite.createCopyTarget(assigned));
        assignment.setRightHandSide(conditionalExpression);
        assignment.setOperator(((Assignment) assigned.getParent()).getOperator());
        ExpressionStatement expressionStatement = ast.newExpressionStatement(assignment);
        rewrite.replace(ifStatement, expressionStatement, null);
    }
    // add correction proposal
    resultingCollections.add(proposal);
    return true;
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Image(org.eclipse.swt.graphics.Image) TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) IJavaProject(org.eclipse.jdt.core.IJavaProject) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Aggregations

ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)125 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)93 Image (org.eclipse.swt.graphics.Image)70 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 AST (org.eclipse.jdt.core.dom.AST)38 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)35 ASTNode (org.eclipse.jdt.core.dom.ASTNode)33 Expression (org.eclipse.jdt.core.dom.Expression)32 ArrayList (java.util.ArrayList)29 CastExpression (org.eclipse.jdt.core.dom.CastExpression)24 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)23 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)23 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)23 Test (org.junit.Test)23 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)21 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)21 Block (org.eclipse.jdt.core.dom.Block)19 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)19 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)19 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)19