Search in sources :

Example 11 with GrCaseSection

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection in project intellij-community by JetBrains.

the class EquivalenceChecker method switchStatementsAreEquivalent.

private static boolean switchStatementsAreEquivalent(@NotNull GrSwitchStatement statement1, @NotNull GrSwitchStatement statement2) {
    final GrExpression switchExpression1 = statement1.getCondition();
    final GrExpression switchExpression2 = statement2.getCondition();
    if (!expressionsAreEquivalent(switchExpression1, switchExpression2)) {
        return false;
    }
    final GrCaseSection[] clauses1 = statement1.getCaseSections();
    final GrCaseSection[] clauses2 = statement2.getCaseSections();
    if (clauses1.length != clauses2.length) {
        return false;
    }
    for (int i = 0; i < clauses1.length; i++) {
        final GrCaseSection clause1 = clauses1[i];
        final GrCaseSection clause2 = clauses2[i];
        if (!caseClausesAreEquivalent(clause1, clause2)) {
            return false;
        }
    }
    return true;
}
Also used : GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)

Example 12 with GrCaseSection

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection in project intellij-community by JetBrains.

the class GroovyPsiElementFactoryImpl method createSwitchSection.

@NotNull
@Override
public GrCaseSection createSwitchSection(@NotNull String text) {
    final GrStatement statement = createStatementFromText("switch (a) {\n" + text + "\n}");
    if (!(statement instanceof GrSwitchStatement)) {
        throw new IncorrectOperationException("Cannot create switch section from text: " + text);
    }
    final GrCaseSection[] sections = ((GrSwitchStatement) statement).getCaseSections();
    if (sections.length != 1)
        throw new IncorrectOperationException("Cannot create switch section from text: " + text);
    return sections[0];
}
Also used : GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with GrCaseSection

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection in project intellij-community by JetBrains.

the class GrCreateMissingSwitchBranchesIntention method findUnusedConstants.

private static List<PsiEnumConstant> findUnusedConstants(GrSwitchStatement switchStatement) {
    final GrExpression condition = switchStatement.getCondition();
    if (condition == null)
        return Collections.emptyList();
    final PsiType type = condition.getType();
    if (!(type instanceof PsiClassType))
        return Collections.emptyList();
    final PsiClass resolved = ((PsiClassType) type).resolve();
    if (resolved == null || !resolved.isEnum())
        return Collections.emptyList();
    final PsiField[] fields = resolved.getFields();
    final List<PsiEnumConstant> constants = ContainerUtil.findAll(fields, PsiEnumConstant.class);
    final GrCaseSection[] sections = switchStatement.getCaseSections();
    for (GrCaseSection section : sections) {
        for (GrCaseLabel label : section.getCaseLabels()) {
            final GrExpression value = label.getValue();
            if (value instanceof GrReferenceExpression) {
                final PsiElement r = ((GrReferenceExpression) value).resolve();
                constants.remove(r);
            }
        }
    }
    return constants;
}
Also used : GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)

Example 14 with GrCaseSection

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection in project intellij-community by JetBrains.

the class GroovyStatementMover method allRanges.

private List<LineRange> allRanges(final GroovyPsiElement scope, final boolean stmtLevel, final boolean topLevel) {
    final ArrayList<LineRange> result = new ArrayList<>();
    scope.accept(new PsiRecursiveElementVisitor() {

        int lastStart = -1;

        private void addRange(int endLine) {
            if (lastStart >= 0) {
                result.add(new LineRange(lastStart, endLine));
            }
            lastStart = endLine;
        }

        @Override
        public void visitElement(PsiElement element) {
            if (stmtLevel && element instanceof GrCodeBlock) {
                final PsiElement lBrace = ((GrCodeBlock) element).getLBrace();
                if (nlsAfter(lBrace)) {
                    assert lBrace != null;
                    addRange(new LineRange(lBrace).endLine);
                }
                addChildRanges(((GrCodeBlock) element).getStatements());
                final PsiElement rBrace = ((GrCodeBlock) element).getRBrace();
                if (nlsAfter(rBrace)) {
                    assert rBrace != null;
                    final int endLine = new LineRange(rBrace).endLine;
                    if (lastStart >= 0) {
                        for (int i = lastStart + 1; i < endLine; i++) {
                            addRange(i);
                        }
                    }
                }
            } else if (stmtLevel && element instanceof GrCaseSection) {
                final GrCaseLabel[] allLabels = ((GrCaseSection) element).getCaseLabels();
                final GrCaseLabel label = allLabels[0];
                if (nlsAfter(label)) {
                    addRange(new LineRange(label).endLine);
                }
                addChildRanges(((GrCaseSection) element).getStatements());
            } else if (element instanceof GroovyFileBase) {
                addChildRanges(((GroovyFileBase) element).getTopStatements());
            } else if (!stmtLevel && !topLevel && element instanceof GrTypeDefinitionBody) {
                addChildRanges(((GrTypeDefinitionBody) element).getMemberDeclarations());
            } else {
                super.visitElement(element);
            }
        }

        private boolean shouldDigInside(GroovyPsiElement statement) {
            if (stmtLevel && (statement instanceof GrMethod || statement instanceof GrTypeDefinition)) {
                return false;
            }
            if (statement instanceof GrVariableDeclaration && !stmtLevel) {
                return false;
            }
            return true;
        }

        private void addChildRanges(GroovyPsiElement[] statements) {
            for (int i = 0; i < statements.length; i++) {
                GroovyPsiElement statement = statements[i];
                if (nlsAfter(statement)) {
                    final LineRange range = getLineRange(statement);
                    if ((i == 0 || isStatement(statements[i - 1])) && isStatement(statement)) {
                        for (int j = lastStart; j < range.startLine; j++) {
                            addRange(j + 1);
                        }
                    }
                    lastStart = range.startLine;
                    if (shouldDigInside(statement)) {
                        statement.accept(this);
                    }
                    addRange(range.endLine);
                }
            }
        }
    });
    return result;
}
Also used : PsiRecursiveElementVisitor(com.intellij.psi.PsiRecursiveElementVisitor) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 15 with GrCaseSection

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection in project intellij-community by JetBrains.

the class SwitchStatementGenerator method generate.

public static void generate(@NotNull StringBuilder builder, @NotNull ExpressionContext context, @NotNull GrSwitchStatement switchStatement) {
    final GrExpression condition = switchStatement.getCondition();
    final GrCaseSection[] caseSections = switchStatement.getCaseSections();
    final PsiType type = condition == null ? null : TypesUtil.unboxPrimitiveTypeWrapper(condition.getType());
    if (type == null || isValidTypeForSwitchSelector(type)) {
        generateSwitch(builder, context, condition, caseSections);
    } else {
        generateIfs(builder, context, condition, caseSections);
    }
}
Also used : GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiType(com.intellij.psi.PsiType)

Aggregations

GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)15 PsiElement (com.intellij.psi.PsiElement)4 NotNull (org.jetbrains.annotations.NotNull)4 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)4 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)4 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)4 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)4 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)3 GrCaseLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 GrBreakStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 ASTNode (com.intellij.lang.ASTNode)1 Language (com.intellij.lang.Language)1 Logger (com.intellij.openapi.diagnostic.Logger)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiRecursiveElementVisitor (com.intellij.psi.PsiRecursiveElementVisitor)1 PsiType (com.intellij.psi.PsiType)1