Search in sources :

Example 26 with GrTypeDefinition

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

the class GroovyFoldingBuilder method isSingleHighLevelClassBody.

private static boolean isSingleHighLevelClassBody(PsiElement element) {
    if (!(element instanceof GrTypeDefinitionBody))
        return false;
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrTypeDefinition))
        return false;
    final GrTypeDefinition clazz = (GrTypeDefinition) parent;
    if (clazz.isAnonymous() || clazz.getContainingClass() != null)
        return false;
    final PsiFile file = element.getContainingFile();
    return file instanceof GroovyFile && ((GroovyFile) file).getClasses().length == 1;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 27 with GrTypeDefinition

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

the class GroovyFileImpl method checkIsScript.

private boolean checkIsScript() {
    final GrTopStatement[] topStatements = findChildrenByClass(GrTopStatement.class);
    boolean hasClassDefinitions = false;
    boolean hasTopStatements = false;
    for (GrTopStatement st : topStatements) {
        if (st instanceof GrTypeDefinition) {
            hasClassDefinitions = true;
        } else if (!(st instanceof GrImportStatement || st instanceof GrPackageDefinition)) {
            hasTopStatements = true;
            break;
        }
    }
    return hasTopStatements || !hasClassDefinitions;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)

Example 28 with GrTypeDefinition

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

the class GroovyFileImpl method processDeclarationsNoGuess.

private boolean processDeclarationsNoGuess(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable PsiElement lastParent, @NotNull PsiElement place) {
    ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);
    if (myContext != null) {
        if (ResolveUtil.shouldProcessProperties(classHint)) {
            if (!processChildrenScopes(processor, state, lastParent, place))
                return false;
        }
        return true;
    }
    boolean processClasses = ResolveUtil.shouldProcessClasses(classHint);
    GrImportStatement[] importStatements = getImportStatements();
    if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.ALIAS, false))
        return false;
    GroovyScriptClass scriptClass = getScriptClass();
    if (scriptClass != null && StringUtil.isJavaIdentifier(scriptClass.getName())) {
        if (!(lastParent instanceof GrTypeDefinition)) {
            if (!ResolveUtil.processClassDeclarations(scriptClass, processor, state, lastParent, place))
                return false;
        }
        if (processClasses) {
            if (!ResolveUtil.processElement(processor, scriptClass, state))
                return false;
        }
    }
    if (processClasses) {
        for (GrTypeDefinition definition : getTypeDefinitions()) {
            if (!ResolveUtil.processElement(processor, definition, state))
                return false;
        }
    }
    if (ResolveUtil.shouldProcessProperties(classHint)) {
        if (!processChildrenScopes(processor, state, lastParent, place))
            return false;
    }
    if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.ALIAS, true))
        return false;
    if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.SIMPLE, null))
        return false;
    if (!processDeclarationsInPackage(processor, state, lastParent, place))
        return false;
    if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.ON_DEMAND, null))
        return false;
    if (!ImplicitImportsKt.processImplicitImports(processor, state, lastParent, place, this))
        return false;
    if (ResolveUtil.shouldProcessPackages(classHint)) {
        NameHint nameHint = processor.getHint(NameHint.KEY);
        String expectedName = nameHint != null ? nameHint.getName(state) : null;
        final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
        if (expectedName != null) {
            final PsiPackage pkg = facade.findPackage(expectedName);
            if (pkg != null && !processor.execute(pkg, state)) {
                return false;
            }
        } else {
            PsiPackage defaultPackage = facade.findPackage("");
            if (defaultPackage != null) {
                for (PsiPackage subPackage : defaultPackage.getSubPackages(getResolveScope())) {
                    if (!ResolveUtil.processElement(processor, subPackage, state))
                        return false;
                }
            }
        }
    }
    if (ResolveUtil.shouldProcessProperties(classHint)) {
        if (lastParent != null && !(lastParent instanceof GrTypeDefinition) && scriptClass != null) {
            if (!ResolveUtil.processElement(processor, getSyntheticArgsParameter(), state))
                return false;
        }
    }
    return true;
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ElementClassHint(com.intellij.psi.scope.ElementClassHint) NameHint(com.intellij.psi.scope.NameHint) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)

Example 29 with GrTypeDefinition

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

the class GrSuperReferenceResolver method resolveSuperExpression.

@Nullable("null if ref is not 'super' reference")
public static GroovyResolveResult[] resolveSuperExpression(@NotNull GrReferenceExpression ref) {
    GrExpression qualifier = ref.getQualifier();
    if (qualifier == null) {
        final PsiElement parent = ref.getParent();
        if (parent instanceof GrConstructorInvocation) {
            return ((GrConstructorInvocation) parent).multiResolve(false);
        }
        PsiClass aClass = PsiUtil.getContextClass(ref);
        if (aClass != null) {
            return getSuperClass(aClass);
        }
    } else if (qualifier instanceof GrReferenceExpression) {
        GroovyResolveResult result = ((GrReferenceExpression) qualifier).advancedResolve();
        PsiElement resolved = result.getElement();
        if (resolved instanceof PsiClass) {
            PsiClass superClass = (PsiClass) resolved;
            GrTypeDefinition scopeClass = PsiTreeUtil.getParentOfType(ref, GrTypeDefinition.class, true);
            if (scopeClass != null && GrTraitUtil.isTrait(superClass) && scopeClass.isInheritor(superClass, false)) {
                PsiSubstitutor superClassSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, scopeClass, PsiSubstitutor.EMPTY);
                return new GroovyResolveResultImpl[] { new GroovyResolveResultImpl(superClass, null, null, superClassSubstitutor, true, true) };
            }
            if (PsiUtil.hasEnclosingInstanceInScope(superClass, ref, false)) {
                return getSuperClass(superClass);
            }
        }
    }
    return null;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiClass(com.intellij.psi.PsiClass) PsiSubstitutor(com.intellij.psi.PsiSubstitutor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GroovyResolveResultImpl(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with GrTypeDefinition

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

the class GrClosableBlockImpl method getOwner.

private PsiVariable getOwner() {
    return CachedValuesManager.getCachedValue(this, () -> {
        final GroovyPsiElement context = PsiTreeUtil.getParentOfType(this, GrTypeDefinition.class, GrClosableBlock.class, GroovyFile.class);
        final PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
        PsiType type = null;
        if (context instanceof GrTypeDefinition) {
            type = factory.createType((PsiClass) context);
        } else if (context instanceof GrClosableBlock) {
            type = GrClosureType.create((GrClosableBlock) context, true);
        } else if (context instanceof GroovyFile) {
            final PsiClass scriptClass = ((GroovyFile) context).getScriptClass();
            if (scriptClass != null && GroovyNamesUtil.isIdentifier(scriptClass.getName()))
                type = factory.createType(scriptClass);
        }
        if (type == null) {
            type = TypesUtil.getJavaLangObject(this);
        }
        PsiVariable owner = new GrLightVariable(getManager(), OWNER_NAME, type, this);
        return CachedValueProvider.Result.create(owner, PsiModificationTracker.MODIFICATION_COUNT);
    });
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrLightVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Aggregations

GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)78 PsiElement (com.intellij.psi.PsiElement)17 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)17 PsiClass (com.intellij.psi.PsiClass)16 NotNull (org.jetbrains.annotations.NotNull)15 Nullable (org.jetbrains.annotations.Nullable)14 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)9 PsiFile (com.intellij.psi.PsiFile)8 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)8 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)8 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)7 Project (com.intellij.openapi.project.Project)6 ArrayList (java.util.ArrayList)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 Editor (com.intellij.openapi.editor.Editor)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4