Search in sources :

Example 1 with GrCaseSection

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

the class SwitchStatementGenerator method generateSwitch.

private static void generateSwitch(@NotNull StringBuilder builder, @NotNull ExpressionContext context, @Nullable GrExpression condition, @NotNull GrCaseSection[] caseSections) {
    builder.append("switch (");
    if (condition != null) {
        condition.accept(new ExpressionGenerator(builder, context));
    }
    builder.append(") {\n");
    final ExpressionContext innerContext = context.extend();
    for (GrCaseSection section : caseSections) {
        generateCaseSection(builder, context, innerContext, section);
    }
    builder.append('}');
}
Also used : GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)

Example 2 with GrCaseSection

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

the class GroovyBlock method getChildAttributes.

@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
    ASTNode astNode = getNode();
    final PsiElement psiParent = astNode.getPsi();
    if (psiParent instanceof GroovyFileBase) {
        return new ChildAttributes(Indent.getNoneIndent(), null);
    }
    if (psiParent instanceof GrSwitchStatement) {
        List<Block> subBlocks = getSubBlocks();
        if (newChildIndex > 0) {
            Block block = subBlocks.get(newChildIndex - 1);
            if (block instanceof GroovyBlock) {
                PsiElement anchorPsi = ((GroovyBlock) block).getNode().getPsi();
                if (anchorPsi instanceof GrCaseSection) {
                    for (GrStatement statement : ((GrCaseSection) anchorPsi).getStatements()) {
                        if (statement instanceof GrBreakStatement || statement instanceof GrContinueStatement || statement instanceof GrReturnStatement || statement instanceof GrThrowStatement) {
                            final Indent indent = GroovyIndentProcessor.getSwitchCaseIndent(myContext.getSettings());
                            return new ChildAttributes(indent, null);
                        }
                    }
                    int indentSize = myContext.getSettings().getIndentOptions().INDENT_SIZE;
                    final int spaces = myContext.getSettings().INDENT_CASE_FROM_SWITCH ? 2 * indentSize : indentSize;
                    return new ChildAttributes(Indent.getSpaceIndent(spaces), null);
                }
            }
        }
    }
    if (psiParent instanceof GrCaseLabel) {
        return new ChildAttributes(GroovyIndentProcessor.getSwitchCaseIndent(getContext().getSettings()), null);
    }
    if (psiParent instanceof GrCaseSection) {
        return getSwitchIndent((GrCaseSection) psiParent, newChildIndex);
    }
    if (TokenSets.BLOCK_SET.contains(astNode.getElementType()) || GroovyElementTypes.SWITCH_STATEMENT.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (GroovyElementTypes.CASE_SECTION.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrBinaryExpression || psiParent instanceof GrConditionalExpression || psiParent instanceof GrCommandArgumentList || psiParent instanceof GrArgumentList || psiParent instanceof GrParameterList || psiParent instanceof GrListOrMap || psiParent instanceof GrAnnotationArgumentList || psiParent instanceof GrVariable || psiParent instanceof GrAssignmentExpression) {
        return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null);
    }
    if (psiParent instanceof GrDocComment || psiParent instanceof GrDocTag) {
        return new ChildAttributes(Indent.getSpaceIndent(GroovyIndentProcessor.GDOC_COMMENT_INDENT), null);
    }
    if (psiParent instanceof GrIfStatement || psiParent instanceof GrLoopStatement) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrLabeledStatement && newChildIndex == 2) {
        final Indent indent = getContext().getGroovySettings().INDENT_LABEL_BLOCKS ? Indent.getLabelIndent() : Indent.getNoneIndent();
        return new ChildAttributes(indent, null);
    }
    return new ChildAttributes(Indent.getNoneIndent(), null);
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrContinueStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrContinueStatement) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) ASTNode(com.intellij.lang.ASTNode) GrDocTag(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag) GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GrThrowStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrAnnotationArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GrCaseSection

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

the class GrCreateMissingSwitchBranchesIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    if (!(element instanceof GrSwitchStatement))
        return;
    final List<PsiEnumConstant> constants = findUnusedConstants((GrSwitchStatement) element);
    if (constants.isEmpty())
        return;
    final PsiEnumConstant first = constants.get(0);
    final PsiClass aClass = first.getContainingClass();
    if (aClass == null)
        return;
    String qName = aClass.getQualifiedName();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    PsiElement anchor = findAnchor(element);
    for (PsiEnumConstant constant : constants) {
        final GrCaseSection section = factory.createSwitchSection("case " + qName + "." + constant.getName() + ":\n break");
        final PsiElement added = element.addBefore(section, anchor);
        element.addBefore(factory.createLineTerminator(1), anchor);
        JavaCodeStyleManager.getInstance(project).shortenClassReferences(added);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrSwitchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement)

Example 4 with GrCaseSection

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

the class CyclomaticComplexityVisitor method visitSwitchStatement.

@Override
public void visitSwitchStatement(@NotNull GrSwitchStatement statement) {
    super.visitSwitchStatement(statement);
    final GrCaseSection[] caseClauses = statement.getCaseSections();
    for (GrCaseSection clause : caseClauses) {
        final GrStatement[] statements = clause.getStatements();
        if (statements != null && statements.length != 0) {
            complexity++;
        }
    }
}
Also used : GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)

Example 5 with GrCaseSection

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

the class ControlFlowUtils method openBlockCompletesWithStatement.

public static boolean openBlockCompletesWithStatement(@NotNull GrCodeBlock body, @NotNull GrStatement statement) {
    GroovyPsiElement elementToCheck = statement;
    while (true) {
        if (elementToCheck == null)
            return false;
        final GroovyPsiElement container = PsiTreeUtil.getParentOfType(elementToCheck, GrStatement.class, GrCodeBlock.class, GrCaseSection.class);
        if (container == null)
            return false;
        if (isLoop(container))
            return false;
        if (container instanceof GrCaseSection) {
            final GrSwitchStatement switchStatement = (GrSwitchStatement) container.getParent();
            final GrCaseSection[] sections = switchStatement.getCaseSections();
            if (container == sections[sections.length - 1])
                return false;
        }
        if (container instanceof GrCodeBlock) {
            if (elementToCheck instanceof GrStatement) {
                final GrCodeBlock codeBlock = (GrCodeBlock) container;
                if (!statementIsLastInBlock(codeBlock, (GrStatement) elementToCheck)) {
                    return false;
                }
            }
            if (container instanceof GrOpenBlock || container instanceof GrClosableBlock) {
                if (container.equals(body)) {
                    return true;
                }
                elementToCheck = PsiTreeUtil.getParentOfType(container, GrStatement.class);
            } else {
                elementToCheck = container;
            }
        } else {
            elementToCheck = container;
        }
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

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