Search in sources :

Example 1 with GrEnumTypeDefinition

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

the class MoveGroovyMemberHandler method addEnumConstant.

private static PsiElement addEnumConstant(PsiClass targetClass, GrEnumConstant constant, @Nullable PsiElement anchor) {
    if (targetClass instanceof GrEnumTypeDefinition) {
        final GrEnumTypeDefinition enumeration = (GrEnumTypeDefinition) targetClass;
        final GrEnumConstantList constantList = enumeration.getEnumConstantList();
        if (constantList != null) {
            ASTNode node = constantList.getNode();
            node.addLeaf(GroovyTokenTypes.mCOMMA, ",", node.getFirstChildNode());
            return constantList.addBefore(constant, constantList.getFirstChild());
        } else {
            final PsiElement parent = constant.getParent();
            assert parent instanceof GrEnumConstantList;
            final GrEnumConstantList constListCopy = ((GrEnumConstantList) targetClass.add(parent));
            return constListCopy.getEnumConstants()[0];
        }
    }
    return (anchor != null ? targetClass.addAfter(constant, anchor) : targetClass.add(constant));
}
Also used : GrEnumConstantList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstantList) GrEnumTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumTypeDefinition) ASTNode(com.intellij.lang.ASTNode)

Example 2 with GrEnumTypeDefinition

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

the class GrIntroduceFieldProcessor method insertField.

@NotNull
protected GrVariableDeclaration insertField(@NotNull PsiClass targetClass) {
    GrVariableDeclaration declaration = createField(targetClass);
    if (targetClass instanceof GrEnumTypeDefinition) {
        final GrEnumConstantList enumConstants = ((GrEnumTypeDefinition) targetClass).getEnumConstantList();
        return (GrVariableDeclaration) targetClass.addAfter(declaration, enumConstants);
    }
    if (targetClass instanceof GrTypeDefinition) {
        PsiElement anchor = getAnchorForDeclaration((GrTypeDefinition) targetClass);
        return (GrVariableDeclaration) targetClass.addAfter(declaration, anchor);
    } else {
        assert targetClass instanceof GroovyScriptClass;
        final GroovyFile file = ((GroovyScriptClass) targetClass).getContainingFile();
        PsiElement[] elements = file.getMethods();
        if (elements.length == 0)
            elements = file.getStatements();
        final PsiElement anchor = ArrayUtil.getFirstElement(elements);
        return (GrVariableDeclaration) file.addBefore(declaration, anchor);
    }
}
Also used : GrEnumConstantList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstantList) GrEnumTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumTypeDefinition) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GrEnumTypeDefinition

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

the class GrIntroduceConstantProcessor method addDeclaration.

protected GrVariableDeclaration addDeclaration(PsiClass targetClass) {
    GrVariableDeclaration declaration = createField(targetClass);
    final GrVariableDeclaration added;
    if (targetClass instanceof GrEnumTypeDefinition) {
        final GrEnumConstantList enumConstants = ((GrEnumTypeDefinition) targetClass).getEnumConstantList();
        added = (GrVariableDeclaration) targetClass.addAfter(declaration, enumConstants);
    } else {
        added = ((GrVariableDeclaration) targetClass.add(declaration));
    }
    JavaCodeStyleManager.getInstance(added.getProject()).shortenClassReferences(added);
    return added;
}
Also used : GrEnumConstantList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstantList) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrEnumTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumTypeDefinition)

Example 4 with GrEnumTypeDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumTypeDefinition 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

GrEnumTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumTypeDefinition)4 GrEnumConstantList (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstantList)3 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)2 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)2 ASTNode (com.intellij.lang.ASTNode)1 PsiClass (com.intellij.psi.PsiClass)1 NotNull (org.jetbrains.annotations.NotNull)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1 GrEnumConstant (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant)1 GrMembersDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMembersDeclaration)1 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)1