Search in sources :

Example 41 with GrTypeDefinition

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

the class CreateClassFix method createClassFromNewAction.

public static IntentionAction createClassFromNewAction(final GrNewExpression expression) {
    return new CreateClassActionBase(GrCreateClassKind.CLASS, expression.getReferenceElement()) {

        @Override
        protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
            final PsiFile file = element.getContainingFile();
            if (!(file instanceof GroovyFileBase))
                return;
            GroovyFileBase groovyFile = (GroovyFileBase) file;
            final PsiManager manager = myRefElement.getManager();
            final String qualifier;
            final String name;
            final Module module;
            final AccessToken accessToken = ReadAction.start();
            try {
                qualifier = groovyFile instanceof GroovyFile ? groovyFile.getPackageName() : "";
                name = myRefElement.getReferenceName();
                assert name != null;
                module = ModuleUtilCore.findModuleForPsiElement(file);
            } finally {
                accessToken.finish();
            }
            PsiDirectory targetDirectory = getTargetDirectory(project, qualifier, name, module, getText());
            if (targetDirectory == null)
                return;
            final GrTypeDefinition targetClass = createClassByType(targetDirectory, name, manager, myRefElement, GroovyTemplates.GROOVY_CLASS, true);
            if (targetClass == null)
                return;
            PsiType[] argTypes = getArgTypes(myRefElement);
            if (argTypes != null && argTypes.length > 0) {
                generateConstructor(myRefElement, name, argTypes, targetClass, project);
                bindRef(targetClass, myRefElement);
            } else {
                bindRef(targetClass, myRefElement);
                IntentionUtils.positionCursor(project, targetClass.getContainingFile(), targetClass);
            }
        }
    };
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) AccessToken(com.intellij.openapi.application.AccessToken) Editor(com.intellij.openapi.editor.Editor) Module(com.intellij.openapi.module.Module) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 42 with GrTypeDefinition

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

the class GroovyLineMarkerProvider method getGroovyCategory.

private static int getGroovyCategory(@NotNull PsiElement element, @NotNull CharSequence documentChars) {
    if (element instanceof GrVariableDeclarationImpl) {
        GrVariable[] variables = ((GrVariableDeclarationImpl) element).getVariables();
        if (variables.length == 1 && variables[0] instanceof GrField && variables[0].getInitializerGroovy() instanceof GrClosableBlock) {
            return 2;
        }
    }
    if (element instanceof GrField || element instanceof GrTypeParameter)
        return 1;
    if (element instanceof GrTypeDefinition || element instanceof GrClassInitializer)
        return 2;
    if (element instanceof GrMethod) {
        if (((GrMethod) element).hasModifierProperty(PsiModifier.ABSTRACT) && !(((GrMethod) element).getBlock() != null && GrTraitUtil.isTrait(((GrMethod) element).getContainingClass()))) {
            return 1;
        }
        TextRange textRange = element.getTextRange();
        int start = textRange.getStartOffset();
        int end = Math.min(documentChars.length(), textRange.getEndOffset());
        int crlf = StringUtil.getLineBreakCount(documentChars.subSequence(start, end));
        return crlf == 0 ? 1 : 2;
    }
    return 0;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrClassInitializer(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrClassInitializer) GrTypeParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameter) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrVariableDeclarationImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableDeclarationImpl) TextRange(com.intellij.openapi.util.TextRange)

Example 43 with GrTypeDefinition

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

the class GrMemberInfo method extractClassMembers.

public static void extractClassMembers(PsiClass subclass, List<GrMemberInfo> result, Filter<GrMember> filter, final boolean extractInterfacesDeep) {
    if (!(subclass instanceof GrTypeDefinition))
        return;
    if (extractInterfacesDeep) {
        extractSuperInterfaces(subclass, filter, result, ContainerUtil.<PsiClass>newHashSet());
    } else {
        PsiClass[] interfaces = subclass.getInterfaces();
        GrReferenceList sourceRefList = subclass.isInterface() ? ((GrTypeDefinition) subclass).getExtendsClause() : ((GrTypeDefinition) subclass).getImplementsClause();
        for (PsiClass anInterface : interfaces) {
            if (anInterface instanceof GrTypeDefinition && filter.includeMember((GrMember) anInterface)) {
                result.add(new GrMemberInfo((GrMember) anInterface, true, sourceRefList));
            }
        }
    }
    PsiClass[] innerClasses = subclass.getInnerClasses();
    for (PsiClass innerClass : innerClasses) {
        if (innerClass instanceof GrTypeDefinition && filter.includeMember((GrMember) innerClass)) {
            result.add(new GrMemberInfo((GrMember) innerClass));
        }
    }
    GrMethod[] methods = ((GrTypeDefinition) subclass).getCodeMethods();
    for (GrMethod method : methods) {
        if (!method.isConstructor() && filter.includeMember(method)) {
            result.add(new GrMemberInfo(method));
        }
    }
    GrField[] fields = ((GrTypeDefinition) subclass).getCodeFields();
    for (final GrField field : fields) {
        if (filter.includeMember(field)) {
            result.add(new GrMemberInfo(field));
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 44 with GrTypeDefinition

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

the class GrMemberInfoStorage method buildSubClassesMapForList.

private void buildSubClassesMapForList(final PsiClassType[] classesList, GrTypeDefinition aClass) {
    for (int i = 0; i < classesList.length; i++) {
        PsiClassType element = classesList[i];
        PsiClass resolved = element.resolve();
        if (resolved instanceof GrTypeDefinition) {
            GrTypeDefinition superClass = (GrTypeDefinition) resolved;
            getSubclasses(superClass).add(aClass);
            buildSubClassesMap(superClass);
        }
    }
}
Also used : PsiClassType(com.intellij.psi.PsiClassType) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiClass(com.intellij.psi.PsiClass)

Example 45 with GrTypeDefinition

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

the class ClassGenerator method writeMembers.

public void writeMembers(StringBuilder text, PsiClass typeDefinition) {
    if (typeDefinition instanceof GrEnumTypeDefinition) {
        final GrEnumConstant[] enumConstants = ((GrEnumTypeDefinition) typeDefinition).getEnumConstants();
        for (GrEnumConstant constant : enumConstants) {
            classItemGenerator.writeEnumConstant(text, constant);
            text.append(',');
        }
        if (enumConstants.length > 0) {
            //text.removeFromTheEnd(1).append(";\n");
            text.delete(text.length() - 1, text.length());
        }
        text.append(";\n");
    }
    writeAllMethods(text, classItemGenerator.collectMethods(typeDefinition), typeDefinition);
    if (typeDefinition instanceof GrTypeDefinition) {
        for (GrMembersDeclaration declaration : ((GrTypeDefinition) typeDefinition).getMemberDeclarations()) {
            if (declaration instanceof GrVariableDeclaration) {
                classItemGenerator.writeVariableDeclarations(text, (GrVariableDeclaration) declaration);
            }
        }
        for (PsiClass inner : typeDefinition.getInnerClasses()) {
            writeTypeDefinition(text, inner, false, false);
            text.append('\n');
        }
    }
    classItemGenerator.writePostponed(text, typeDefinition);
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrEnumTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumTypeDefinition) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMembersDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMembersDeclaration) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) PsiClass(com.intellij.psi.PsiClass)

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