Search in sources :

Example 1 with GrEnumConstantList

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

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

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

Aggregations

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