Search in sources :

Example 6 with GrCaseSection

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

the class ControlFlowUtils method blockCompletesWithStatement.

public static boolean blockCompletesWithStatement(@NotNull GrBlockStatement body, @NotNull GrStatement statement) {
    GrStatement statementToCheck = statement;
    while (true) {
        if (statementToCheck == null) {
            return false;
        }
        final PsiElement container = statementToCheck.getParent();
        if (container == null) {
            return false;
        }
        if (container instanceof GrLoopStatement) {
            return false;
        } else if (container instanceof GrCaseSection) {
            final GrCaseSection caseSection = (GrCaseSection) container;
            if (!statementIsLastInBlock(caseSection, statementToCheck))
                return false;
            final PsiElement parent = container.getParent();
            assert parent instanceof GrSwitchStatement;
            final GrSwitchStatement switchStatement = (GrSwitchStatement) parent;
            final GrCaseSection[] sections = switchStatement.getCaseSections();
            if (ArrayUtil.getLastElement(sections) != caseSection) {
                return false;
            }
        } else if (container instanceof GrOpenBlock) {
            final GrOpenBlock block = (GrOpenBlock) container;
            if (!statementIsLastInBlock(block, statementToCheck))
                return false;
            final PsiElement parent = block.getParent();
            if (parent instanceof GrBlockStatement) {
                final GrBlockStatement blockStatement = (GrBlockStatement) parent;
                if (blockStatement == body)
                    return true;
            }
        } else if (container instanceof GrClosableBlock) {
            return false;
        }
        statementToCheck = getContainingStatement(statementToCheck);
    }
}
Also used : 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) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 7 with GrCaseSection

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

the class ControlFlowBuilderUtil method isLastStatementInCaseSection.

private static boolean isLastStatementInCaseSection(GrCaseSection caseSection, GrSwitchStatement switchStatement) {
    final GrCaseSection[] sections = switchStatement.getCaseSections();
    final int i = ArrayUtilRt.find(sections, caseSection);
    if (i == sections.length - 1) {
        return true;
    }
    for (int j = i + 1; j < sections.length; j++) {
        GrCaseSection section = sections[j];
        for (GrStatement statement : section.getStatements()) {
            if (!(statement instanceof GrBreakStatement)) {
                return false;
            }
        }
    }
    return true;
}
Also used : GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)

Example 8 with GrCaseSection

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

the class GroovyRefactoringUtil method findStatementsInRange.

@NotNull
public static PsiElement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset, boolean strict) {
    if (!(file instanceof GroovyFileBase))
        return PsiElement.EMPTY_ARRAY;
    Language language = GroovyLanguage.INSTANCE;
    PsiElement element1 = file.getViewProvider().findElementAt(startOffset, language);
    PsiElement element2 = file.getViewProvider().findElementAt(endOffset - 1, language);
    if (element1 instanceof PsiWhiteSpace || org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(element1)) {
        startOffset = element1.getTextRange().getEndOffset();
        element1 = file.findElementAt(startOffset);
    }
    if (element2 instanceof PsiWhiteSpace || org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(element2)) {
        endOffset = element2.getTextRange().getStartOffset();
        element2 = file.findElementAt(endOffset - 1);
    }
    if (element1 == null || element2 == null)
        return PsiElement.EMPTY_ARRAY;
    PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2);
    if (parent == null)
        return PsiElement.EMPTY_ARRAY;
    while (true) {
        if (parent instanceof GrCodeBlock)
            break;
        if (parent instanceof GroovyFileBase)
            break;
        if (parent instanceof GrCaseSection)
            break;
        if (parent instanceof GrStatement) {
            parent = parent.getParent();
            break;
        }
        if (parent == null)
            return PsiElement.EMPTY_ARRAY;
        final PsiElement prev = parent;
        parent = parent.getParent();
        if (parent instanceof GrCodeBlock && prev instanceof LeafPsiElement) {
            //braces
            parent = parent.getParent();
        }
    }
    if (!parent.equals(element1)) {
        while (!parent.equals(element1.getParent())) {
            element1 = element1.getParent();
        }
    }
    if (startOffset != element1.getTextRange().getStartOffset() && strict)
        return PsiElement.EMPTY_ARRAY;
    if (!parent.equals(element2)) {
        while (!parent.equals(element2.getParent())) {
            element2 = element2.getParent();
        }
    }
    if (endOffset != element2.getTextRange().getEndOffset() && strict)
        return PsiElement.EMPTY_ARRAY;
    if (parent instanceof GrCodeBlock && parent.getParent() instanceof GrBlockStatement && element1 == ((GrCodeBlock) parent).getLBrace() && element2 == ((GrCodeBlock) parent).getRBrace()) {
        return new PsiElement[] { parent.getParent() };
    }
    // calculate children
    PsiElement[] children = PsiElement.EMPTY_ARRAY;
    PsiElement psiChild = parent.getFirstChild();
    if (psiChild != null) {
        List<PsiElement> result = new ArrayList<>();
        while (psiChild != null) {
            result.add(psiChild);
            psiChild = psiChild.getNextSibling();
        }
        children = PsiUtilCore.toPsiElementArray(result);
    }
    ArrayList<PsiElement> possibleStatements = new ArrayList<>();
    boolean flag = false;
    for (PsiElement child : children) {
        if (child == element1) {
            flag = true;
        }
        if (flag) {
            possibleStatements.add(child);
        }
        if (child == element2) {
            break;
        }
    }
    for (PsiElement element : possibleStatements) {
        if (!(element instanceof GrStatement || element instanceof PsiWhiteSpace || element instanceof PsiComment || TokenSets.SEPARATORS.contains(element.getNode().getElementType()))) {
            return PsiElement.EMPTY_ARRAY;
        }
    }
    return PsiUtilCore.toPsiElementArray(possibleStatements);
}
Also used : Language(com.intellij.lang.Language) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with GrCaseSection

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

the class ControlFlowBuilderUtil method isCertainlyReturnStatement.

/**
   * check whether statement is return (the statement which provides return value) statement of method or closure.
   *
   * @param st
   * @return
   */
public static boolean isCertainlyReturnStatement(GrStatement st) {
    final PsiElement parent = st.getParent();
    if (parent instanceof GrOpenBlock) {
        if (st != ArrayUtil.getLastElement(((GrOpenBlock) parent).getStatements()))
            return false;
        PsiElement pparent = parent.getParent();
        if (pparent instanceof GrMethod) {
            return true;
        }
        if (pparent instanceof GrBlockStatement || pparent instanceof GrCatchClause || pparent instanceof GrLabeledStatement) {
            pparent = pparent.getParent();
        }
        if (pparent instanceof GrIfStatement || pparent instanceof GrControlStatement || pparent instanceof GrTryCatchStatement) {
            return isCertainlyReturnStatement((GrStatement) pparent);
        }
    } else if (parent instanceof GrClosableBlock) {
        return st == ArrayUtil.getLastElement(((GrClosableBlock) parent).getStatements());
    } else if (parent instanceof GroovyFileBase) {
        return st == ArrayUtil.getLastElement(((GroovyFileBase) parent).getStatements());
    } else if (parent instanceof GrForStatement || parent instanceof GrIfStatement && st != ((GrIfStatement) parent).getCondition() || parent instanceof GrSynchronizedStatement && st != ((GrSynchronizedStatement) parent).getMonitor() || parent instanceof GrWhileStatement && st != ((GrWhileStatement) parent).getCondition() || parent instanceof GrConditionalExpression && st != ((GrConditionalExpression) parent).getCondition() || parent instanceof GrElvisExpression) {
        return isCertainlyReturnStatement((GrStatement) parent);
    } else if (parent instanceof GrCaseSection) {
        final GrStatement[] statements = ((GrCaseSection) parent).getStatements();
        final GrStatement last = ArrayUtil.getLastElement(statements);
        final GrSwitchStatement switchStatement = (GrSwitchStatement) parent.getParent();
        if (last instanceof GrBreakStatement && statements.length > 1 && statements[statements.length - 2] == st) {
            return isCertainlyReturnStatement(switchStatement);
        } else if (st == last) {
            if (st instanceof GrBreakStatement || isLastStatementInCaseSection((GrCaseSection) parent, switchStatement)) {
                return isCertainlyReturnStatement(switchStatement);
            }
        }
    }
    return false;
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) PsiElement(com.intellij.psi.PsiElement)

Example 10 with GrCaseSection

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

the class ControlFlowUtils method switchStatementMayReturnNormally.

private static boolean switchStatementMayReturnNormally(@NotNull GrSwitchStatement switchStatement) {
    if (statementIsBreakTarget(switchStatement)) {
        return true;
    }
    final GrCaseSection[] caseClauses = switchStatement.getCaseSections();
    if (ContainerUtil.find(caseClauses, section -> section.isDefault()) == null) {
        return true;
    }
    final GrCaseSection lastClause = caseClauses[caseClauses.length - 1];
    final GrStatement[] statements = lastClause.getStatements();
    if (statements.length == 0) {
        return true;
    }
    return statementMayCompleteNormally(statements[statements.length - 1]);
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) IElementType(com.intellij.psi.tree.IElementType) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) MaybeReturnInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction) HashSet(com.intellij.util.containers.HashSet) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) org.jetbrains.plugins.groovy.lang.psi.api.statements.branch(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) CachedValueProvider(com.intellij.psi.util.CachedValueProvider) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) AfterCallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) ThrowingInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Logger(com.intellij.openapi.diagnostic.Logger) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GroovyTokenTypes(org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes) ControlFlowBuilder(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ControlFlowBuilder) GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) TextRange(com.intellij.openapi.util.TextRange) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) CachedValuesManager(com.intellij.psi.util.CachedValuesManager) Semilattice(org.jetbrains.plugins.groovy.lang.psi.dataFlow.Semilattice) Nullable(org.jetbrains.annotations.Nullable) org.jetbrains.plugins.groovy.lang.psi.api.statements(org.jetbrains.plugins.groovy.lang.psi.api.statements) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) DFAEngine(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GrCondition(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) NotNull(org.jetbrains.annotations.NotNull) DfaInstance(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DfaInstance) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)

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