Search in sources :

Example 21 with GrTypeElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.

the class GrTraitType method createTraitType.

// todo move this method to org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.types.GrSafeCastExpressionImpl
@Nullable
public static PsiType createTraitType(@NotNull GrSafeCastExpression safeCastExpression) {
    GrExpression operand = safeCastExpression.getOperand();
    PsiType exprType = operand.getType();
    if (!(exprType instanceof PsiClassType) && !(exprType instanceof GrTraitType))
        return null;
    GrTypeElement typeElement = safeCastExpression.getCastTypeElement();
    if (typeElement == null)
        return null;
    PsiType type = typeElement.getType();
    if (!GrTraitUtil.isTrait(PsiTypesUtil.getPsiClass(type)))
        return null;
    return createTraitType(exprType, ContainerUtil.newSmartList(type));
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with GrTypeElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.

the class GroovyPsiElementFactoryImpl method createReferenceElementFromText.

@NotNull
@Override
public GrCodeReferenceElement createReferenceElementFromText(@NotNull String refName, final PsiElement context) {
    GroovyFile file = createGroovyFileChecked("(" + refName + ")foo", false, context);
    GrTypeElement typeElement = ((GrTypeCastExpression) file.getTopStatements()[0]).getCastTypeElement();
    return ((GrClassTypeElement) typeElement).getReferenceElement();
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrClassTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClassTypeElement) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with GrTypeElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.

the class GrVariableBaseImpl method getTypeGroovy.

@Override
@Nullable
public PsiType getTypeGroovy() {
    final GrExpression initializer = getInitializerGroovy();
    GrTypeElement typeElement = getTypeElementGroovy();
    PsiType declaredType = null;
    if (typeElement != null) {
        declaredType = typeElement.getType();
        if (!(declaredType instanceof PsiClassType)) {
            return declaredType;
        }
    }
    if (initializer != null) {
        PsiType initializerType = ourGuard.doPreventingRecursion(this, true, initializer::getType);
        if (declaredType == null)
            return initializerType;
        if (initializerType instanceof PsiClassType && TypesUtil.isAssignable(declaredType, initializerType, this)) {
            final PsiClassType.ClassResolveResult initializerResult = ((PsiClassType) initializerType).resolveGenerics();
            final PsiClass initializerClass = initializerResult.getElement();
            if (initializerClass != null && !com.intellij.psi.util.PsiUtil.isRawSubstitutor(initializerClass, initializerResult.getSubstitutor())) {
                return initializerType;
            }
        }
    }
    return declaredType;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with GrTypeElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.

the class GrVariableBaseImpl method setType.

@Override
public void setType(@Nullable PsiType type) {
    final GrVariableDeclaration variableDeclaration = getDeclaration();
    if (variableDeclaration == null)
        return;
    final GrTypeElement typeElement = variableDeclaration.getTypeElementGroovyForVariable(this);
    if (type == null) {
        if (typeElement != null) {
            if (!variableDeclaration.isTuple() && variableDeclaration.getModifierList().getModifiers().length == 0) {
                variableDeclaration.getModifierList().setModifierProperty(GrModifier.DEF, true);
            }
            typeElement.delete();
        }
        return;
    }
    type = TypesUtil.unboxPrimitiveTypeWrapper(type);
    GrTypeElement newTypeElement;
    try {
        newTypeElement = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(type);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
        return;
    }
    if (typeElement == null) {
        newTypeElement = (GrTypeElement) getParent().addBefore(newTypeElement, this);
    } else {
        newTypeElement = (GrTypeElement) typeElement.replace(newTypeElement);
    }
    JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(newTypeElement);
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 25 with GrTypeElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.

the class GrVariableDeclarationImpl method setType.

@Override
public void setType(@Nullable PsiType type) {
    final GrTypeElement typeElement = getTypeElementGroovy();
    if (type == null) {
        if (typeElement == null)
            return;
        if (getModifierList().getModifiers().length == 0) {
            getModifierList().setModifierProperty(GrModifier.DEF, true);
        }
        typeElement.delete();
        return;
    }
    type = TypesUtil.unboxPrimitiveTypeWrapper(type);
    GrTypeElement newTypeElement;
    try {
        newTypeElement = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(type);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
        return;
    }
    if (typeElement == null) {
        getModifierList().setModifierProperty(GrModifier.DEF, false);
        final GrVariable[] variables = getVariables();
        if (variables.length == 0)
            return;
        newTypeElement = (GrTypeElement) addBefore(newTypeElement, variables[0]);
    } else {
        newTypeElement = (GrTypeElement) typeElement.replace(newTypeElement);
    }
    JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(newTypeElement);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)52 Nullable (org.jetbrains.annotations.Nullable)12 PsiType (com.intellij.psi.PsiType)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 PsiElement (com.intellij.psi.PsiElement)7 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)7 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 NotNull (org.jetbrains.annotations.NotNull)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 TextRange (com.intellij.openapi.util.TextRange)5 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)5 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)5 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)4 GrTypeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression)4 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)4 Document (com.intellij.openapi.editor.Document)3 GrSafeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression)3 Template (com.intellij.codeInsight.template.Template)2 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)2