Search in sources :

Example 16 with LinkedCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal in project che by eclipse.

the class QuickAssistProcessor method getConvertStringConcatenationProposals.

private static boolean getConvertStringConcatenationProposals(IInvocationContext context, Collection<ICommandAccess> resultingCollections) {
    ASTNode node = context.getCoveringNode();
    BodyDeclaration parentDecl = ASTResolving.findParentBodyDeclaration(node);
    if (!(parentDecl instanceof MethodDeclaration || parentDecl instanceof Initializer))
        return false;
    AST ast = node.getAST();
    //$NON-NLS-1$
    ITypeBinding stringBinding = ast.resolveWellKnownType("java.lang.String");
    if (node instanceof Expression && !(node instanceof InfixExpression)) {
        node = node.getParent();
    }
    if (node instanceof VariableDeclarationFragment) {
        node = ((VariableDeclarationFragment) node).getInitializer();
    } else if (node instanceof Assignment) {
        node = ((Assignment) node).getRightHandSide();
    }
    InfixExpression oldInfixExpression = null;
    while (node instanceof InfixExpression) {
        InfixExpression curr = (InfixExpression) node;
        if (curr.resolveTypeBinding() == stringBinding && curr.getOperator() == InfixExpression.Operator.PLUS) {
            // is a infix expression we can use
            oldInfixExpression = curr;
        } else {
            break;
        }
        node = node.getParent();
    }
    if (oldInfixExpression == null)
        return false;
    if (resultingCollections == null) {
        return true;
    }
    LinkedCorrectionProposal stringBufferProposal = getConvertToStringBufferProposal(context, ast, oldInfixExpression);
    resultingCollections.add(stringBufferProposal);
    ASTRewriteCorrectionProposal messageFormatProposal = getConvertToMessageFormatProposal(context, ast, oldInfixExpression);
    if (messageFormatProposal != null)
        resultingCollections.add(messageFormatProposal);
    return true;
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal)

Example 17 with LinkedCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal in project che by eclipse.

the class QuickAssistProcessor method getJoinVariableProposals.

private static boolean getJoinVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    ASTNode parent = node.getParent();
    VariableDeclarationFragment fragment = null;
    boolean onFirstAccess = false;
    if (node instanceof SimpleName && node.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
        onFirstAccess = true;
        SimpleName name = (SimpleName) node;
        IBinding binding = name.resolveBinding();
        if (!(binding instanceof IVariableBinding)) {
            return false;
        }
        ASTNode declaring = context.getASTRoot().findDeclaringNode(binding);
        if (declaring instanceof VariableDeclarationFragment) {
            fragment = (VariableDeclarationFragment) declaring;
        } else {
            return false;
        }
    } else if (parent instanceof VariableDeclarationFragment) {
        fragment = (VariableDeclarationFragment) parent;
    } else {
        return false;
    }
    IVariableBinding binding = fragment.resolveBinding();
    Expression initializer = fragment.getInitializer();
    if ((initializer != null && initializer.getNodeType() != ASTNode.NULL_LITERAL) || binding == null || binding.isField()) {
        return false;
    }
    if (!(fragment.getParent() instanceof VariableDeclarationStatement)) {
        return false;
    }
    VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
    SimpleName[] names = LinkedNodeFinder.findByBinding(statement.getParent(), binding);
    if (names.length <= 1 || names[0] != fragment.getName()) {
        return false;
    }
    SimpleName firstAccess = names[1];
    if (onFirstAccess) {
        if (firstAccess != node) {
            return false;
        }
    } else {
        if (firstAccess.getLocationInParent() != Assignment.LEFT_HAND_SIDE_PROPERTY) {
            return false;
        }
    }
    Assignment assignment = (Assignment) firstAccess.getParent();
    if (assignment.getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
        return false;
    }
    ExpressionStatement assignParent = (ExpressionStatement) assignment.getParent();
    if (resultingCollections == null) {
        return true;
    }
    AST ast = statement.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
    sourceRangeComputer.addTightSourceNode(assignParent);
    rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    String label = CorrectionMessages.QuickAssistProcessor_joindeclaration_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_VARIABLE_DECLARATION, image);
    proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
    Expression placeholder = (Expression) rewrite.createMoveTarget(assignment.getRightHandSide());
    rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, placeholder, null);
    if (onFirstAccess) {
        // replace assignment with variable declaration
        rewrite.replace(assignParent, rewrite.createMoveTarget(statement), null);
    } else {
        // different scopes -> remove assignments, set variable initializer
        if (ASTNodes.isControlStatementBody(assignParent.getLocationInParent())) {
            Block block = ast.newBlock();
            rewrite.replace(assignParent, block, null);
        } else {
            rewrite.remove(assignParent, null);
        }
    }
    proposal.setEndPosition(rewrite.track(fragment.getName()));
    resultingCollections.add(proposal);
    return true;
}
Also used : Image(org.eclipse.swt.graphics.Image) TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 18 with LinkedCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal in project che by eclipse.

the class QuickAssistProcessor method getInvertEqualsProposal.

private static boolean getInvertEqualsProposal(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    if (!(node instanceof MethodInvocation)) {
        node = node.getParent();
        if (!(node instanceof MethodInvocation)) {
            return false;
        }
    }
    MethodInvocation method = (MethodInvocation) node;
    String identifier = method.getName().getIdentifier();
    if (!"equals".equals(identifier) && !"equalsIgnoreCase".equals(identifier)) {
        //$NON-NLS-1$ //$NON-NLS-2$
        return false;
    }
    List<Expression> arguments = method.arguments();
    if (arguments.size() != 1) {
        //overloaded equals w/ more than 1 argument
        return false;
    }
    Expression right = arguments.get(0);
    ITypeBinding binding = right.resolveTypeBinding();
    if (binding != null && !(binding.isClass() || binding.isInterface() || binding.isEnum())) {
        //overloaded equals w/ non-class/interface argument or null
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    Expression left = method.getExpression();
    AST ast = method.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    if (left == null) {
        // equals(x) -> x.equals(this)
        MethodInvocation replacement = ast.newMethodInvocation();
        replacement.setName((SimpleName) rewrite.createCopyTarget(method.getName()));
        replacement.arguments().add(ast.newThisExpression());
        replacement.setExpression((Expression) rewrite.createCopyTarget(right));
        rewrite.replace(method, replacement, null);
    } else if (right instanceof ThisExpression) {
        // x.equals(this) -> equals(x)
        MethodInvocation replacement = ast.newMethodInvocation();
        replacement.setName((SimpleName) rewrite.createCopyTarget(method.getName()));
        replacement.arguments().add(rewrite.createCopyTarget(left));
        rewrite.replace(method, replacement, null);
    } else {
        ASTNode leftExpression = left;
        while (leftExpression instanceof ParenthesizedExpression) {
            leftExpression = ((ParenthesizedExpression) left).getExpression();
        }
        rewrite.replace(right, rewrite.createCopyTarget(leftExpression), null);
        if (right instanceof CastExpression || right instanceof Assignment || right instanceof ConditionalExpression || right instanceof InfixExpression) {
            ParenthesizedExpression paren = ast.newParenthesizedExpression();
            paren.setExpression((Expression) rewrite.createCopyTarget(right));
            rewrite.replace(left, paren, null);
        } else {
            rewrite.replace(left, rewrite.createCopyTarget(right), null);
        }
    }
    String label = CorrectionMessages.QuickAssistProcessor_invertequals_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERT_EQUALS, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : Image(org.eclipse.swt.graphics.Image) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 19 with LinkedCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal in project che by eclipse.

the class AdvancedQuickAssistProcessor method getInverseLocalVariableProposals.

private static boolean getInverseLocalVariableProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    final AST ast = covering.getAST();
    // cursor should be placed on variable name
    if (!(covering instanceof SimpleName)) {
        return false;
    }
    SimpleName coveringName = (SimpleName) covering;
    if (!coveringName.isDeclaration()) {
        return false;
    }
    // prepare bindings
    final IBinding variableBinding = coveringName.resolveBinding();
    if (!(variableBinding instanceof IVariableBinding)) {
        return false;
    }
    IVariableBinding binding = (IVariableBinding) variableBinding;
    if (binding.isField()) {
        return false;
    }
    // we operate only on boolean variable
    if (!isBoolean(coveringName)) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    // find linked nodes
    final MethodDeclaration method = ASTResolving.findParentMethodDeclaration(covering);
    SimpleName[] linkedNodes = LinkedNodeFinder.findByBinding(method, variableBinding);
    //
    final ASTRewrite rewrite = ASTRewrite.create(ast);
    // create proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseBooleanVariable;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    //$NON-NLS-1$
    final String KEY_NAME = "name";
    final LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_BOOLEAN_VARIABLE, image);
    // prepare new variable identifier
    final String oldIdentifier = coveringName.getIdentifier();
    //$NON-NLS-1$
    final String notString = Messages.format(CorrectionMessages.AdvancedQuickAssistProcessor_negatedVariableName, "");
    final String newIdentifier;
    if (oldIdentifier.startsWith(notString)) {
        int notLength = notString.length();
        if (oldIdentifier.length() > notLength) {
            newIdentifier = Character.toLowerCase(oldIdentifier.charAt(notLength)) + oldIdentifier.substring(notLength + 1);
        } else {
            newIdentifier = oldIdentifier;
        }
    } else {
        newIdentifier = Messages.format(CorrectionMessages.AdvancedQuickAssistProcessor_negatedVariableName, Character.toUpperCase(oldIdentifier.charAt(0)) + oldIdentifier.substring(1));
    }
    //
    proposal.addLinkedPositionProposal(KEY_NAME, newIdentifier, null);
    proposal.addLinkedPositionProposal(KEY_NAME, oldIdentifier, null);
    // iterate over linked nodes and replace variable references with negated reference
    final HashSet<SimpleName> renamedNames = new HashSet<SimpleName>();
    for (int i = 0; i < linkedNodes.length; i++) {
        SimpleName name = linkedNodes[i];
        if (renamedNames.contains(name)) {
            continue;
        }
        // prepare new name with new identifier
        SimpleName newName = ast.newSimpleName(newIdentifier);
        proposal.addLinkedPosition(rewrite.track(newName), name == coveringName, KEY_NAME);
        //
        StructuralPropertyDescriptor location = name.getLocationInParent();
        if (location == SingleVariableDeclaration.NAME_PROPERTY) {
            // set new name
            rewrite.replace(name, newName, null);
        } else if (location == Assignment.LEFT_HAND_SIDE_PROPERTY) {
            Assignment assignment = (Assignment) name.getParent();
            Expression expression = assignment.getRightHandSide();
            int exStart = expression.getStartPosition();
            int exEnd = exStart + expression.getLength();
            // collect all names that are used in assignments
            HashSet<SimpleName> overlapNames = new HashSet<SimpleName>();
            for (int j = 0; j < linkedNodes.length; j++) {
                SimpleName name2 = linkedNodes[j];
                if (name2 == null) {
                    continue;
                }
                int name2Start = name2.getStartPosition();
                if (exStart <= name2Start && name2Start < exEnd) {
                    overlapNames.add(name2);
                }
            }
            // prepare inverted expression
            SimpleNameRenameProvider provider = new SimpleNameRenameProvider() {

                public SimpleName getRenamed(SimpleName simpleName) {
                    if (simpleName.resolveBinding() == variableBinding) {
                        renamedNames.add(simpleName);
                        return ast.newSimpleName(newIdentifier);
                    }
                    return null;
                }
            };
            Expression inversedExpression = getInversedExpression(rewrite, expression, provider);
            // if any name was not renamed during expression inverting, we can not already rename it, so fail to create assist
            for (Iterator<SimpleName> iter = overlapNames.iterator(); iter.hasNext(); ) {
                Object o = iter.next();
                if (!renamedNames.contains(o)) {
                    return false;
                }
            }
            // check operator and replace if needed
            Assignment.Operator operator = assignment.getOperator();
            if (operator == Assignment.Operator.BIT_AND_ASSIGN) {
                Assignment newAssignment = ast.newAssignment();
                newAssignment.setLeftHandSide(newName);
                newAssignment.setRightHandSide(inversedExpression);
                newAssignment.setOperator(Assignment.Operator.BIT_OR_ASSIGN);
                rewrite.replace(assignment, newAssignment, null);
            } else if (operator == Assignment.Operator.BIT_OR_ASSIGN) {
                Assignment newAssignment = ast.newAssignment();
                newAssignment.setLeftHandSide(newName);
                newAssignment.setRightHandSide(inversedExpression);
                newAssignment.setOperator(Assignment.Operator.BIT_AND_ASSIGN);
                rewrite.replace(assignment, newAssignment, null);
            } else {
                rewrite.replace(expression, inversedExpression, null);
                // set new name
                rewrite.replace(name, newName, null);
            }
        } else if (location == VariableDeclarationFragment.NAME_PROPERTY) {
            // replace initializer for variable
            VariableDeclarationFragment vdf = (VariableDeclarationFragment) name.getParent();
            Expression expression = vdf.getInitializer();
            if (expression != null) {
                rewrite.replace(expression, getInversedExpression(rewrite, expression), null);
            }
            // set new name
            rewrite.replace(name, newName, null);
        } else if (name.getParent() instanceof PrefixExpression && ((PrefixExpression) name.getParent()).getOperator() == PrefixExpression.Operator.NOT) {
            rewrite.replace(name.getParent(), newName, null);
        } else {
            PrefixExpression expression = ast.newPrefixExpression();
            expression.setOperator(PrefixExpression.Operator.NOT);
            expression.setOperand(newName);
            rewrite.replace(name, expression, null);
        }
    }
    // add correction proposal
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) Image(org.eclipse.swt.graphics.Image) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) Iterator(java.util.Iterator) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) HashSet(java.util.HashSet)

Example 20 with LinkedCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal in project flux by eclipse.

the class QuickAssistProcessor method getJoinVariableProposals.

private static boolean getJoinVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    ASTNode parent = node.getParent();
    VariableDeclarationFragment fragment = null;
    boolean onFirstAccess = false;
    if (node instanceof SimpleName && node.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
        onFirstAccess = true;
        SimpleName name = (SimpleName) node;
        IBinding binding = name.resolveBinding();
        if (!(binding instanceof IVariableBinding)) {
            return false;
        }
        ASTNode declaring = context.getASTRoot().findDeclaringNode(binding);
        if (declaring instanceof VariableDeclarationFragment) {
            fragment = (VariableDeclarationFragment) declaring;
        } else {
            return false;
        }
    } else if (parent instanceof VariableDeclarationFragment) {
        fragment = (VariableDeclarationFragment) parent;
    } else {
        return false;
    }
    IVariableBinding binding = fragment.resolveBinding();
    Expression initializer = fragment.getInitializer();
    if ((initializer != null && initializer.getNodeType() != ASTNode.NULL_LITERAL) || binding == null || binding.isField()) {
        return false;
    }
    if (!(fragment.getParent() instanceof VariableDeclarationStatement)) {
        return false;
    }
    VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
    SimpleName[] names = LinkedNodeFinder.findByBinding(statement.getParent(), binding);
    if (names.length <= 1 || names[0] != fragment.getName()) {
        return false;
    }
    SimpleName firstAccess = names[1];
    if (onFirstAccess) {
        if (firstAccess != node) {
            return false;
        }
    } else {
        if (firstAccess.getLocationInParent() != Assignment.LEFT_HAND_SIDE_PROPERTY) {
            return false;
        }
    }
    Assignment assignment = (Assignment) firstAccess.getParent();
    if (assignment.getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
        return false;
    }
    ExpressionStatement assignParent = (ExpressionStatement) assignment.getParent();
    if (resultingCollections == null) {
        return true;
    }
    AST ast = statement.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    //		TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer();
    //		sourceRangeComputer.addTightSourceNode(assignParent);
    //		rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    String label = CorrectionMessages.QuickAssistProcessor_joindeclaration_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_VARIABLE_DECLARATION);
    proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
    Expression placeholder = (Expression) rewrite.createMoveTarget(assignment.getRightHandSide());
    rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, placeholder, null);
    if (onFirstAccess) {
        // replace assignment with variable declaration
        rewrite.replace(assignParent, rewrite.createMoveTarget(statement), null);
    } else {
        // different scopes -> remove assignments, set variable initializer
        if (ASTNodes.isControlStatementBody(assignParent.getLocationInParent())) {
            Block block = ast.newBlock();
            rewrite.replace(assignParent, block, null);
        } else {
            rewrite.remove(assignParent, null);
        }
    }
    proposal.setEndPosition(rewrite.track(fragment.getName()));
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Assignment(org.eclipse.jdt.core.dom.Assignment) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Aggregations

LinkedCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal)29 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)27 Image (org.eclipse.swt.graphics.Image)18 AST (org.eclipse.jdt.core.dom.AST)13 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 Expression (org.eclipse.jdt.core.dom.Expression)9 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)8 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)8 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)8 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)8 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)8 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)7 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)7 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)7 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)7 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)7 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)7 ArrayList (java.util.ArrayList)6