Search in sources :

Example 26 with GrTypeElement

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

the class GrMethodBaseImpl method setReturnType.

@Override
@Nullable
public GrTypeElement setReturnType(@Nullable PsiType newReturnType) {
    GrTypeElement typeElement = getReturnTypeElementGroovy();
    if (newReturnType == null || newReturnType == PsiType.NULL) {
        if (typeElement != null)
            typeElement.delete();
        insertPlaceHolderToModifierList();
        return null;
    }
    final GrTypeElement stub = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(newReturnType);
    GrTypeElement newTypeElement;
    if (typeElement == null) {
        final GrTypeParameterList typeParemeterList = getTypeParameterList();
        PsiElement anchor = typeParemeterList != null ? typeParemeterList : getModifierList();
        newTypeElement = (GrTypeElement) addAfter(stub, anchor);
    } else {
        newTypeElement = (GrTypeElement) typeElement.replace(stub);
    }
    return newTypeElement;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrTypeParameterList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with GrTypeElement

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

the class GrParameterImpl method setType.

@Override
public void setType(@Nullable PsiType type) {
    final GrTypeElement typeElement = getTypeElementGroovy();
    if (type == null) {
        if (typeElement != null)
            typeElement.delete();
        return;
    }
    GrTypeElement newTypeElement;
    try {
        newTypeElement = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(type);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
        return;
    }
    if (typeElement == null) {
        final GrModifierList modifierList = getModifierList();
        newTypeElement = (GrTypeElement) addAfter(newTypeElement, modifierList);
    } else {
        newTypeElement = (GrTypeElement) typeElement.replace(newTypeElement);
    }
    JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(newTypeElement);
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 28 with GrTypeElement

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

the class GrClassReferenceTypePointer method calcType.

@Nullable
@Override
protected GrClassReferenceType calcType() {
    final GrReferenceElement reference = mySmartPsiElementPointer.getElement();
    if (reference != null) {
        return new GrClassReferenceType(reference);
    }
    try {
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
        GrTypeElement typeElement = factory.createTypeElement(myReferenceText, null);
        return (GrClassReferenceType) typeElement.getType();
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrClassReferenceType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClassReferenceType) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with GrTypeElement

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

the class ClosureAsAnonymousParameterEnhancer method getClosureParameterType.

@Nullable
@Override
protected PsiType getClosureParameterType(GrClosableBlock closure, int index) {
    List<PsiType> expectedTypes;
    if (closure.getParent() instanceof GrSafeCastExpression) {
        GrSafeCastExpression safeCastExpression = (GrSafeCastExpression) closure.getParent();
        GrTypeElement typeElement = safeCastExpression.getCastTypeElement();
        if (typeElement != null) {
            PsiType castType = typeElement.getType();
            expectedTypes = ContainerUtil.newArrayList(GroovyExpectedTypesProvider.getDefaultExpectedTypes(safeCastExpression));
            for (Iterator<PsiType> iterator = expectedTypes.iterator(); iterator.hasNext(); ) {
                if (!TypesUtil.isAssignable(iterator.next(), castType, closure)) {
                    iterator.remove();
                }
            }
            if (expectedTypes.isEmpty())
                expectedTypes.add(castType);
        } else {
            expectedTypes = GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure);
        }
    } else {
        expectedTypes = GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure);
    }
    for (PsiType constraint : expectedTypes) {
        final PsiType suggestion = GppClosureParameterTypeProvider.getSingleMethodParameterType(constraint, index, closure);
        if (suggestion != null) {
            if (GroovyConfigUtils.getInstance().isVersionAtLeast(closure, GroovyConfigUtils.GROOVY2_3)) {
                if (suggestion instanceof PsiWildcardType && ((PsiWildcardType) suggestion).isSuper()) {
                    return ((PsiWildcardType) suggestion).getBound();
                }
            }
            return TypesUtil.substituteAndNormalizeType(suggestion, PsiSubstitutor.EMPTY, null, closure);
        }
    }
    return null;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) PsiWildcardType(com.intellij.psi.PsiWildcardType) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with GrTypeElement

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

the class GrDisjunctionTypeElementImpl method getType.

@NotNull
@Override
public PsiType getType() {
    PsiType cachedType = myCachedType;
    if (cachedType != null)
        return myCachedType;
    final GrTypeElement[] typeElements = getTypeElements();
    final ArrayList<PsiType> types = new ArrayList<>();
    for (GrTypeElement typeElement : typeElements) {
        types.add(typeElement.getType());
    }
    cachedType = new PsiDisjunctionType(types, getManager());
    myCachedType = cachedType;
    return cachedType;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) PsiDisjunctionType(com.intellij.psi.PsiDisjunctionType) ArrayList(java.util.ArrayList) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

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