Search in sources :

Example 36 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock 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)

Example 37 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock 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 38 with GrClosableBlock

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

the class GroovyDslDefaultMembers method enclosingCall.

/**
   * Returns enclosing method call of a given context's place
   */
@Nullable
public GrCall enclosingCall(String name, GdslMembersHolderConsumer consumer) {
    final PsiElement place = consumer.getPlace();
    if (place == null)
        return null;
    GrCall call = PsiTreeUtil.getParentOfType(place, GrCall.class, true);
    if (call == null)
        return null;
    while (call != null && !name.equals(getInvokedMethodName(call))) {
        call = PsiTreeUtil.getParentOfType(call, GrCall.class, true);
    }
    if (call == null)
        return null;
    final GrArgumentList argumentList = call.getArgumentList();
    if (argumentList != null) {
        for (GrExpression arg : argumentList.getExpressionArguments()) {
            if (arg instanceof GrClosableBlock && PsiTreeUtil.findCommonParent(place, arg) == arg) {
                return call;
            }
        }
    }
    if (call instanceof GrMethodCallExpression) {
        for (GrExpression arg : call.getClosureArguments()) {
            if (arg instanceof GrClosableBlock && PsiTreeUtil.findCommonParent(place, arg) == arg) {
                return call;
            }
        }
    }
    return null;
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 39 with GrClosableBlock

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

the class GebPageMemberContributor method processDynamicElements.

@Override
public void processDynamicElements(@NotNull PsiType qualifierType, PsiClass aClass, @NotNull PsiScopeProcessor processor, @NotNull PsiElement place, @NotNull ResolveState state) {
    if (!ResolveUtil.shouldProcessProperties(processor.getHint(ElementClassHint.KEY)))
        return;
    PsiElement grCall = place.getParent();
    if (grCall instanceof GrMethodCall) {
        PsiElement grClosure = grCall.getParent();
        if (grClosure instanceof GrClosableBlock) {
            PsiElement contentField = grClosure.getParent();
            if (contentField instanceof GrField) {
                GrField f = (GrField) contentField;
                if ("content".equals(f.getName()) && f.hasModifierProperty(PsiModifier.STATIC) && f.getContainingClass() == aClass) {
                    Map<String, PsiField> elements = GebUtil.getContentElements(aClass);
                    for (PsiField field : elements.values()) {
                        if (field.getNavigationElement() == place) {
                            // Don't resolve variable definition.
                            return;
                        }
                    }
                }
            }
        }
    }
    processPageFields(processor, aClass, state);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)

Example 40 with GrClosableBlock

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

the class GrReassignedLocalVarsChecker method getUsedVarsInsideBlock.

@NotNull
private static Set<String> getUsedVarsInsideBlock(@NotNull final GrCodeBlock block) {
    return CachedValuesManager.getCachedValue(block, () -> {
        final Set<String> result = ContainerUtil.newHashSet();
        block.acceptChildren(new GroovyRecursiveElementVisitor() {

            @Override
            public void visitOpenBlock(@NotNull GrOpenBlock openBlock) {
                result.addAll(getUsedVarsInsideBlock(openBlock));
            }

            @Override
            public void visitClosure(@NotNull GrClosableBlock closure) {
                result.addAll(getUsedVarsInsideBlock(closure));
            }

            @Override
            public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
                if (referenceExpression.getQualifier() == null && referenceExpression.getReferenceName() != null) {
                    result.add(referenceExpression.getReferenceName());
                }
            }
        });
        return CachedValueProvider.Result.create(result, block);
    });
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)116 PsiElement (com.intellij.psi.PsiElement)32 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)31 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)26 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)26 Nullable (org.jetbrains.annotations.Nullable)23 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)23 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)17 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)15 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)15 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)14 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)13 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 ArrayList (java.util.ArrayList)11 NotNull (org.jetbrains.annotations.NotNull)10 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)10 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)10 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)10 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)9