Search in sources :

Example 71 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class ComponentTypeOfMacro method calculateResult.

@Override
public Result calculateResult(@NotNull Expression[] params, final ExpressionContext context) {
    if (params.length != 1)
        return null;
    final Result result = params[0].calculateResult(context);
    if (result == null)
        return null;
    if (result instanceof PsiTypeResult) {
        PsiType type = ((PsiTypeResult) result).getType();
        if (type instanceof PsiArrayType) {
            return new PsiTypeResult(((PsiArrayType) type).getComponentType(), context.getProject());
        }
    }
    PsiExpression expr = MacroUtil.resultToPsiExpression(result, context);
    PsiType type = expr == null ? MacroUtil.resultToPsiType(result, context) : expr.getType();
    if (type instanceof PsiArrayType) {
        return new PsiTypeResult(((PsiArrayType) type).getComponentType(), context.getProject());
    }
    LookupElement[] elements = params[0].calculateLookupItems(context);
    if (elements != null) {
        for (LookupElement element : elements) {
            PsiTypeLookupItem typeLookupItem = element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
            if (typeLookupItem != null) {
                PsiType psiType = typeLookupItem.getType();
                if (psiType instanceof PsiArrayType) {
                    return new PsiTypeResult(((PsiArrayType) psiType).getComponentType(), context.getProject());
                }
            }
        }
    }
    return new PsiElementResult(null);
}
Also used : PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) PsiExpression(com.intellij.psi.PsiExpression) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiArrayType(com.intellij.psi.PsiArrayType) PsiType(com.intellij.psi.PsiType)

Example 72 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class SubtypesMacro method calculateLookupItems.

@Override
public LookupElement[] calculateLookupItems(@NotNull Expression[] params, ExpressionContext context) {
    if (params.length == 0)
        return LookupElement.EMPTY_ARRAY;
    Result paramResult = params[0].calculateQuickResult(context);
    if (paramResult instanceof PsiTypeResult) {
        final PsiType type = ((PsiTypeResult) paramResult).getType();
        final PsiFile file = PsiDocumentManager.getInstance(context.getProject()).getPsiFile(context.getEditor().getDocument());
        final PsiElement element = file.findElementAt(context.getStartOffset());
        final Set<LookupElement> set = new LinkedHashSet<>();
        JavaTemplateUtil.addTypeLookupItem(set, type);
        CodeInsightUtil.processSubTypes(type, element, false, PrefixMatcher.ALWAYS_TRUE, psiType -> JavaTemplateUtil.addTypeLookupItem(set, psiType));
        return set.toArray(new LookupElement[set.size()]);
    }
    return LookupElement.EMPTY_ARRAY;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PsiFile(com.intellij.psi.PsiFile) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 73 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class OptionalPostfixTemplate method getTemplateString.

@Nullable
@Override
public String getTemplateString(@NotNull PsiElement element) {
    String className = "Optional";
    String methodName = "ofNullable";
    if (element instanceof PsiExpression) {
        PsiType type = ((PsiExpression) element).getType();
        if (type instanceof PsiPrimitiveType) {
            if (PsiType.INT.equals(type)) {
                className = "OptionalInt";
            } else if (PsiType.DOUBLE.equals(type)) {
                className = "OptionalDouble";
            } else if (PsiType.LONG.equals(type)) {
                className = "OptionalLong";
            }
            methodName = "of";
        }
    }
    return "java.util." + className + "." + methodName + "($expr$)";
}
Also used : PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) PsiExpression(com.intellij.psi.PsiExpression) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 74 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class TypeSelector method setTypes.

public void setTypes(PsiType[] types) {
    if (myComboBoxModel == null)
        return;
    PsiType oldType;
    if (myComboBoxModel.getSize() > 0) {
        oldType = getSelectedType();
    } else {
        oldType = null;
    }
    myComboBoxModel.setSuggestions(wrapToItems(types, myProject));
    if (oldType != null) {
        for (int i = 0; i < types.length; i++) {
            PsiType type = types[i];
            if (type.equals(oldType)) {
                ((JComboBox) myComponent).setSelectedIndex(i);
                return;
            }
        }
    }
    if (types.length > 0) {
        ((JComboBox) myComponent).setSelectedIndex(0);
    }
}
Also used : PsiType(com.intellij.psi.PsiType)

Example 75 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class NullityInference method inferNullity.

public static Nullness inferNullity(PsiMethodImpl method) {
    if (!InferenceFromSourceUtil.shouldInferFromSource(method)) {
        return Nullness.UNKNOWN;
    }
    PsiType type = method.getReturnType();
    if (type == null || type instanceof PsiPrimitiveType) {
        return Nullness.UNKNOWN;
    }
    return CachedValuesManager.getCachedValue(method, () -> {
        MethodData data = ContractInferenceIndexKt.getIndexedData(method);
        NullityInferenceResult result = data == null ? null : data.getNullity();
        Nullness nullness = result == null ? null : RecursionManager.doPreventingRecursion(method, true, () -> result.getNullness(method, data.methodBody(method)));
        if (nullness == null)
            nullness = Nullness.UNKNOWN;
        return CachedValueProvider.Result.create(nullness, method, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT);
    });
}
Also used : PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) PsiType(com.intellij.psi.PsiType)

Aggregations

PsiType (com.intellij.psi.PsiType)157 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)24 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)24 NotNull (org.jetbrains.annotations.NotNull)21 PsiClassType (com.intellij.psi.PsiClassType)20 PsiClass (com.intellij.psi.PsiClass)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 PsiPrimitiveType (com.intellij.psi.PsiPrimitiveType)12 ArrayList (java.util.ArrayList)9 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)9 PsiArrayType (com.intellij.psi.PsiArrayType)7 PsiExpression (com.intellij.psi.PsiExpression)7 PsiField (com.intellij.psi.PsiField)7 NonNls (org.jetbrains.annotations.NonNls)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 PsiLiteralExpression (com.intellij.psi.PsiLiteralExpression)6 PsiMethod (com.intellij.psi.PsiMethod)6 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)6 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)6