Search in sources :

Example 1 with SwitchExpressionsFixCore

use of org.eclipse.jdt.internal.corext.fix.SwitchExpressionsFixCore in project eclipse.jdt.ls by eclipse.

the class QuickAssistProcessor method getConvertToSwitchExpressionProposals.

private static boolean getConvertToSwitchExpressionProposals(IInvocationContext context, ASTNode covering, Collection<ChangeCorrectionProposal> resultingCollections) {
    if (covering instanceof Block) {
        List<Statement> statements = ((Block) covering).statements();
        int startIndex = QuickAssistProcessorUtil.getIndex(context.getSelectionOffset(), statements);
        if (startIndex == -1 || startIndex >= statements.size()) {
            return false;
        }
        covering = statements.get(startIndex);
    } else {
        while (covering instanceof SwitchCase || covering instanceof SwitchExpression) {
            covering = covering.getParent();
        }
    }
    SwitchStatement switchStatement;
    if (covering instanceof SwitchStatement) {
        switchStatement = (SwitchStatement) covering;
    } else {
        return false;
    }
    SwitchExpressionsFixCore fix = SwitchExpressionsFixCore.createConvertToSwitchExpressionFix(switchStatement);
    if (fix == null) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    // add correction proposal
    try {
        CompilationUnitChange change = fix.createChange(null);
        ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(fix.getDisplayString(), JavaCodeActionKind.QUICK_ASSIST, change, IProposalRelevance.CONVERT_TO_SWITCH_EXPRESSION);
        resultingCollections.add(proposal);
    } catch (CoreException e) {
    // continue
    }
    return true;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) CoreException(org.eclipse.core.runtime.CoreException) SwitchExpressionsFixCore(org.eclipse.jdt.internal.corext.fix.SwitchExpressionsFixCore) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)1 Block (org.eclipse.jdt.core.dom.Block)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)1 Statement (org.eclipse.jdt.core.dom.Statement)1 SwitchCase (org.eclipse.jdt.core.dom.SwitchCase)1 SwitchExpression (org.eclipse.jdt.core.dom.SwitchExpression)1 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)1 ThrowStatement (org.eclipse.jdt.core.dom.ThrowStatement)1 TryStatement (org.eclipse.jdt.core.dom.TryStatement)1 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)1 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)1 SwitchExpressionsFixCore (org.eclipse.jdt.internal.corext.fix.SwitchExpressionsFixCore)1 ChangeCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal)1