Search in sources :

Example 1 with InnovationContext

use of org.eclipse.jdt.ls.core.internal.corrections.InnovationContext in project eclipse.jdt.ls by eclipse.

the class LocalCorrectionsSubProcessor method getUnreachableCodeProposals.

public static void getUnreachableCodeProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
    CompilationUnit root = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(root);
    if (selectedNode == null) {
        return;
    }
    ASTNode parent = selectedNode.getParent();
    while (parent instanceof ExpressionStatement) {
        selectedNode = parent;
        parent = selectedNode.getParent();
    }
    if (parent instanceof WhileStatement) {
        addRemoveIncludingConditionProposal(context, parent, null, proposals);
    } else if (selectedNode.getLocationInParent() == IfStatement.THEN_STATEMENT_PROPERTY) {
        Statement elseStatement = ((IfStatement) parent).getElseStatement();
        addRemoveIncludingConditionProposal(context, parent, elseStatement, proposals);
    } else if (selectedNode.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
        Statement thenStatement = ((IfStatement) parent).getThenStatement();
        addRemoveIncludingConditionProposal(context, parent, thenStatement, proposals);
    } else if (selectedNode.getLocationInParent() == ForStatement.BODY_PROPERTY) {
        Statement body = ((ForStatement) parent).getBody();
        addRemoveIncludingConditionProposal(context, parent, body, proposals);
    } else if (selectedNode.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY) {
        Expression elseExpression = ((ConditionalExpression) parent).getElseExpression();
        addRemoveIncludingConditionProposal(context, parent, elseExpression, proposals);
    } else if (selectedNode.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
        Expression thenExpression = ((ConditionalExpression) parent).getThenExpression();
        addRemoveIncludingConditionProposal(context, parent, thenExpression, proposals);
    } else if (selectedNode.getLocationInParent() == InfixExpression.RIGHT_OPERAND_PROPERTY) {
        // also offer split && / || condition proposals:
        InfixExpression infixExpression = (InfixExpression) parent;
        Expression leftOperand = infixExpression.getLeftOperand();
        ASTRewrite rewrite = ASTRewrite.create(parent.getAST());
        Expression replacement = leftOperand;
        while (replacement instanceof ParenthesizedExpression) {
            replacement = ((ParenthesizedExpression) replacement).getExpression();
        }
        Expression toReplace = infixExpression;
        while (toReplace.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
            toReplace = (Expression) toReplace.getParent();
        }
        if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
            if (leftOperand instanceof ParenthesizedExpression) {
                replacement = (Expression) replacement.getParent();
            } else if (infixExpression.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
                toReplace = ((ParenthesizedExpression) toReplace).getExpression();
            }
        }
        rewrite.replace(toReplace, rewrite.createMoveTarget(replacement), null);
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
        addRemoveProposal(context, rewrite, label, proposals);
        InnovationContext assistContext = new InnovationContext(context.getCompilationUnit(), infixExpression.getRightOperand().getStartPosition() - 1, 0);
        assistContext.setASTRoot(root);
        AdvancedQuickAssistProcessor.getSplitAndConditionProposals(assistContext, infixExpression, proposals);
        AdvancedQuickAssistProcessor.getSplitOrConditionProposals(assistContext, infixExpression, proposals);
    } else if (selectedNode instanceof Statement && selectedNode.getLocationInParent().isChildListProperty()) {
        // remove all statements following the unreachable:
        List<Statement> statements = ASTNodes.<Statement>getChildListProperty(selectedNode.getParent(), (ChildListPropertyDescriptor) selectedNode.getLocationInParent());
        int idx = statements.indexOf(selectedNode);
        ASTRewrite rewrite = ASTRewrite.create(selectedNode.getAST());
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
        if (idx > 0) {
            Object prevStatement = statements.get(idx - 1);
            if (prevStatement instanceof IfStatement) {
                IfStatement ifStatement = (IfStatement) prevStatement;
                if (ifStatement.getElseStatement() == null) {
                    // remove if (true), see https://bugs.eclipse.org/bugs/show_bug.cgi?id=261519
                    rewrite.replace(ifStatement, rewrite.createMoveTarget(ifStatement.getThenStatement()), null);
                    label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
                }
            }
        }
        for (int i = idx; i < statements.size(); i++) {
            ASTNode statement = statements.get(i);
            if (statement instanceof SwitchCase) {
                // stop at case *: and default:
                break;
            }
            rewrite.remove(statement, null);
        }
        addRemoveProposal(context, rewrite, label, proposals);
    } else {
        // no special case, just remove the node:
        addRemoveProposal(context, selectedNode, proposals);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) IfStatement(org.eclipse.jdt.core.dom.IfStatement) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) 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) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with InnovationContext

use of org.eclipse.jdt.ls.core.internal.corrections.InnovationContext in project eclipse.jdt.ls by eclipse.

the class LocalCorrectionsSubProcessor method getUnreachableCodeProposals.

public static void getUnreachableCodeProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
    CompilationUnit root = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(root);
    if (selectedNode == null) {
        return;
    }
    ASTNode parent = selectedNode.getParent();
    while (parent instanceof ExpressionStatement) {
        selectedNode = parent;
        parent = selectedNode.getParent();
    }
    if (parent instanceof WhileStatement) {
        addRemoveIncludingConditionProposal(context, parent, null, proposals);
    } else if (selectedNode.getLocationInParent() == IfStatement.THEN_STATEMENT_PROPERTY) {
        Statement elseStatement = ((IfStatement) parent).getElseStatement();
        addRemoveIncludingConditionProposal(context, parent, elseStatement, proposals);
    } else if (selectedNode.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
        Statement thenStatement = ((IfStatement) parent).getThenStatement();
        addRemoveIncludingConditionProposal(context, parent, thenStatement, proposals);
    } else if (selectedNode.getLocationInParent() == ForStatement.BODY_PROPERTY) {
        Statement body = ((ForStatement) parent).getBody();
        addRemoveIncludingConditionProposal(context, parent, body, proposals);
    } else if (selectedNode.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY) {
        Expression elseExpression = ((ConditionalExpression) parent).getElseExpression();
        addRemoveIncludingConditionProposal(context, parent, elseExpression, proposals);
    } else if (selectedNode.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
        Expression thenExpression = ((ConditionalExpression) parent).getThenExpression();
        addRemoveIncludingConditionProposal(context, parent, thenExpression, proposals);
    } else if (selectedNode.getLocationInParent() == InfixExpression.RIGHT_OPERAND_PROPERTY) {
        // also offer split && / || condition proposals:
        InfixExpression infixExpression = (InfixExpression) parent;
        Expression leftOperand = infixExpression.getLeftOperand();
        ASTRewrite rewrite = ASTRewrite.create(parent.getAST());
        Expression replacement = leftOperand;
        while (replacement instanceof ParenthesizedExpression) {
            replacement = ((ParenthesizedExpression) replacement).getExpression();
        }
        Expression toReplace = infixExpression;
        while (toReplace.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
            toReplace = (Expression) toReplace.getParent();
        }
        if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
            if (leftOperand instanceof ParenthesizedExpression) {
                replacement = (Expression) replacement.getParent();
            } else if (infixExpression.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
                toReplace = ((ParenthesizedExpression) toReplace).getExpression();
            }
        }
        rewrite.replace(toReplace, rewrite.createMoveTarget(replacement), null);
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
        addRemoveProposal(context, rewrite, label, proposals);
        InnovationContext assistContext = new InnovationContext(context.getCompilationUnit(), infixExpression.getRightOperand().getStartPosition() - 1, 0);
        assistContext.setASTRoot(root);
        InvertBooleanUtility.getSplitAndConditionProposals(assistContext, infixExpression, proposals);
        InvertBooleanUtility.getSplitOrConditionProposals(assistContext, infixExpression, proposals);
    } else if (selectedNode instanceof Statement && selectedNode.getLocationInParent().isChildListProperty()) {
        // remove all statements following the unreachable:
        List<Statement> statements = ASTNodes.<Statement>getChildListProperty(selectedNode.getParent(), (ChildListPropertyDescriptor) selectedNode.getLocationInParent());
        int idx = statements.indexOf(selectedNode);
        ASTRewrite rewrite = ASTRewrite.create(selectedNode.getAST());
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
        if (idx > 0) {
            Object prevStatement = statements.get(idx - 1);
            if (prevStatement instanceof IfStatement) {
                IfStatement ifStatement = (IfStatement) prevStatement;
                if (ifStatement.getElseStatement() == null) {
                    // remove if (true), see https://bugs.eclipse.org/bugs/show_bug.cgi?id=261519
                    rewrite.replace(ifStatement, rewrite.createMoveTarget(ifStatement.getThenStatement()), null);
                    label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
                }
            }
        }
        for (int i = idx; i < statements.size(); i++) {
            ASTNode statement = statements.get(i);
            if (statement instanceof SwitchCase) {
                // stop at case *: and default:
                break;
            }
            rewrite.remove(statement, null);
        }
        addRemoveProposal(context, rewrite, label, proposals);
    } else {
        // no special case, just remove the node:
        addRemoveProposal(context, selectedNode, proposals);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) IfStatement(org.eclipse.jdt.core.dom.IfStatement) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) 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) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with InnovationContext

use of org.eclipse.jdt.ls.core.internal.corrections.InnovationContext in project eclipse.jdt.ls by eclipse.

the class OrganizeImportsCommand method organizeImportsInCompilationUnit.

public void organizeImportsInCompilationUnit(ICompilationUnit unit, WorkspaceEdit rootEdit) {
    try {
        InnovationContext context = new InnovationContext(unit, 0, unit.getBuffer().getLength() - 1);
        CUCorrectionProposal proposal = new CUCorrectionProposal("OrganizeImports", CodeActionKind.SourceOrganizeImports, unit, null, IProposalRelevance.ORGANIZE_IMPORTS) {

            @Override
            protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
                CompilationUnit astRoot = context.getASTRoot();
                OrganizeImportsOperation op = new OrganizeImportsOperation(unit, astRoot, true, false, true, null);
                TextEdit edit = op.createTextEdit(null);
                TextEdit staticEdit = OrganizeImportsHandler.wrapStaticImports(edit, astRoot, unit);
                if (staticEdit.getChildrenSize() > 0) {
                    editRoot.addChild(staticEdit);
                }
            }
        };
        addWorkspaceEdit(unit, proposal, rootEdit);
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Problem organize imports ", e);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) CUCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.CUCorrectionProposal) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) IDocument(org.eclipse.jface.text.IDocument)

Example 4 with InnovationContext

use of org.eclipse.jdt.ls.core.internal.corrections.InnovationContext in project eclipse.jdt.ls by eclipse.

the class MoveHandler method getSelectedMethodDeclaration.

private static MethodDeclaration getSelectedMethodDeclaration(ICompilationUnit unit, CodeActionParams params) {
    int start = DiagnosticsHelper.getStartOffset(unit, params.getRange());
    int end = DiagnosticsHelper.getEndOffset(unit, params.getRange());
    InnovationContext context = new InnovationContext(unit, start, end - start);
    context.setASTRoot(CodeActionHandler.getASTRoot(unit));
    ASTNode node = context.getCoveredNode();
    if (node == null) {
        node = context.getCoveringNode();
    }
    while (node != null && !(node instanceof BodyDeclaration)) {
        node = node.getParent();
    }
    if (node != null && node instanceof MethodDeclaration) {
        return (MethodDeclaration) node;
    }
    return null;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext)

Example 5 with InnovationContext

use of org.eclipse.jdt.ls.core.internal.corrections.InnovationContext in project eclipse.jdt.ls by eclipse.

the class MoveHandler method getSelectedTypeDeclaration.

private static AbstractTypeDeclaration getSelectedTypeDeclaration(ICompilationUnit unit, CodeActionParams params) {
    int start = DiagnosticsHelper.getStartOffset(unit, params.getRange());
    int end = DiagnosticsHelper.getEndOffset(unit, params.getRange());
    InnovationContext context = new InnovationContext(unit, start, end - start);
    context.setASTRoot(CodeActionHandler.getASTRoot(unit));
    ASTNode node = context.getCoveredNode();
    if (node == null) {
        node = context.getCoveringNode();
    }
    while (node != null && !(node instanceof AbstractTypeDeclaration)) {
        node = node.getParent();
    }
    return (AbstractTypeDeclaration) node;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

InnovationContext (org.eclipse.jdt.ls.core.internal.corrections.InnovationContext)11 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 ASTNode (org.eclipse.jdt.core.dom.ASTNode)7 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 CoreException (org.eclipse.core.runtime.CoreException)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Expression (org.eclipse.jdt.core.dom.Expression)3 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)3 Map (java.util.Map)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)2 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)2 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 ChildListPropertyDescriptor (org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)2 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)2 EmptyStatement (org.eclipse.jdt.core.dom.EmptyStatement)2 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)2 ForStatement (org.eclipse.jdt.core.dom.ForStatement)2 IfStatement (org.eclipse.jdt.core.dom.IfStatement)2