Search in sources :

Example 11 with ExpectedTypeInfo

use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.

the class SmartCastProvider method addCastVariants.

static void addCastVariants(@NotNull CompletionParameters parameters, PrefixMatcher matcher, @NotNull Consumer<LookupElement> result) {
    if (!shouldSuggestCast(parameters))
        return;
    PsiElement position = parameters.getPosition();
    PsiElement parenthesisOwner = getParenthesisOwner(position);
    final boolean insideCast = parenthesisOwner instanceof PsiTypeCastExpression;
    if (insideCast) {
        PsiElement parent = parenthesisOwner.getParent();
        if (parent instanceof PsiParenthesizedExpression) {
            if (parent.getParent() instanceof PsiReferenceExpression) {
                for (ExpectedTypeInfo info : ExpectedTypesProvider.getExpectedTypes((PsiParenthesizedExpression) parent, false)) {
                    result.consume(PsiTypeLookupItem.createLookupItem(info.getType(), parent));
                }
            }
            ExpectedTypeInfo info = getParenthesizedCastExpectationByOperandType(position);
            if (info != null) {
                addHierarchyTypes(parameters, matcher, info, type -> result.consume(PsiTypeLookupItem.createLookupItem(type, parent)));
            }
            return;
        }
    }
    for (final ExpectedTypeInfo info : JavaSmartCompletionContributor.getExpectedTypes(parameters)) {
        PsiType type = info.getDefaultType();
        if (type instanceof PsiWildcardType) {
            type = ((PsiWildcardType) type).getBound();
        }
        if (type == null || PsiType.VOID.equals(type)) {
            continue;
        }
        if (type instanceof PsiPrimitiveType) {
            final PsiType castedType = getCastedExpressionType(parenthesisOwner);
            if (castedType != null && !(castedType instanceof PsiPrimitiveType)) {
                final PsiClassType boxedType = ((PsiPrimitiveType) type).getBoxedType(position);
                if (boxedType != null) {
                    type = boxedType;
                }
            }
        }
        result.consume(createSmartCastElement(parameters, insideCast, type));
    }
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo)

Example 12 with ExpectedTypeInfo

use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.

the class ExpectedTypeMacro method getExpectedTypes.

@Nullable
private static PsiType[] getExpectedTypes(Expression[] params, final ExpressionContext context) {
    if (params.length != 0)
        return null;
    final Project project = context.getProject();
    PsiType[] types = null;
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
    assert file != null;
    final PsiFile fileCopy = (PsiFile) file.copy();
    BlockSupport.getInstance(project).reparseRange(fileCopy, context.getTemplateStartOffset(), context.getTemplateEndOffset(), CompletionUtil.DUMMY_IDENTIFIER);
    PsiElement element = fileCopy.findElementAt(context.getTemplateStartOffset());
    if (element instanceof PsiIdentifier && element.getParent() instanceof PsiExpression) {
        ExpectedTypeInfo[] infos = ExpectedTypesProvider.getExpectedTypes((PsiExpression) element.getParent(), true);
        if (infos.length > 0) {
            types = new PsiType[infos.length];
            for (int i = 0; i < infos.length; i++) {
                ExpectedTypeInfo info = infos[i];
                types[i] = info.getType();
            }
        }
    }
    return types;
}
Also used : Project(com.intellij.openapi.project.Project) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with ExpectedTypeInfo

use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.

the class ExpectedTypesGetter method extractTypes.

@NotNull
public static PsiType[] extractTypes(ExpectedTypeInfo[] infos, boolean defaultTypes) {
    Set<PsiType> result = new THashSet<>(infos.length);
    for (ExpectedTypeInfo info : infos) {
        final PsiType type = info.getType();
        final PsiType defaultType = info.getDefaultType();
        if (!defaultTypes && !defaultType.equals(type)) {
            result.add(type);
        }
        result.add(defaultType);
    }
    return result.toArray(PsiType.createArray(result.size()));
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) THashSet(gnu.trove.THashSet) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with ExpectedTypeInfo

use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.

the class MagicCompletionContributor method addCompletionVariants.

private static void addCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull final CompletionResultSet result, PsiElement pos, MagicConstantInspection.AllowedValues allowedValues) {
    final Set<PsiElement> allowed = new THashSet<>(new TObjectHashingStrategy<PsiElement>() {

        @Override
        public int computeHashCode(PsiElement object) {
            return 0;
        }

        @Override
        public boolean equals(PsiElement o1, PsiElement o2) {
            return parameters.getOriginalFile().getManager().areElementsEquivalent(o1, o2);
        }
    });
    if (allowedValues.canBeOred) {
        PsiElementFactory factory = JavaPsiFacade.getElementFactory(pos.getProject());
        PsiExpression zero = factory.createExpressionFromText("0", pos);
        result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(zero, "0"), PRIORITY - 1));
        PsiExpression minusOne = factory.createExpressionFromText("-1", pos);
        result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(minusOne, "-1"), PRIORITY - 1));
        allowed.add(zero);
        allowed.add(minusOne);
    }
    List<ExpectedTypeInfo> types = Arrays.asList(JavaSmartCompletionContributor.getExpectedTypes(parameters));
    for (PsiAnnotationMemberValue value : allowedValues.values) {
        if (value instanceof PsiReference) {
            PsiElement resolved = ((PsiReference) value).resolve();
            if (resolved instanceof PsiNamedElement) {
                LookupElement lookupElement = LookupItemUtil.objectToLookupItem(resolved);
                if (lookupElement instanceof VariableLookupItem) {
                    ((VariableLookupItem) lookupElement).setSubstitutor(PsiSubstitutor.EMPTY);
                }
                LookupElement element = PrioritizedLookupElement.withPriority(lookupElement, PRIORITY);
                element = decorate(parameters, types, element);
                result.addElement(element);
                allowed.add(resolved);
                continue;
            }
        }
        LookupElement element = LookupElementBuilder.create(value, value.getText());
        element = decorate(parameters, types, element);
        result.addElement(element);
        allowed.add(value);
    }
    result.runRemainingContributors(parameters, completionResult -> {
        LookupElement element = completionResult.getLookupElement();
        Object object = element.getObject();
        if (object instanceof PsiElement && allowed.contains(object)) {
            return;
        }
        result.passResult(completionResult);
    });
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement) THashSet(gnu.trove.THashSet) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) VariableLookupItem(com.intellij.codeInsight.lookup.VariableLookupItem)

Example 15 with ExpectedTypeInfo

use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.

the class CreateClassFromNewFix method setupInheritance.

private static void setupInheritance(PsiNewExpression element, PsiClass targetClass) throws IncorrectOperationException {
    if (element.getParent() instanceof PsiReferenceExpression)
        return;
    ExpectedTypeInfo[] expectedTypes = ExpectedTypesProvider.getExpectedTypes(element, false);
    for (ExpectedTypeInfo expectedType : expectedTypes) {
        PsiType type = expectedType.getType();
        if (!(type instanceof PsiClassType))
            continue;
        final PsiClassType classType = (PsiClassType) type;
        PsiClass aClass = classType.resolve();
        if (aClass == null)
            continue;
        if (aClass.equals(targetClass) || aClass.hasModifierProperty(PsiModifier.FINAL))
            continue;
        PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory();
        if (aClass.isInterface()) {
            PsiReferenceList implementsList = targetClass.getImplementsList();
            assert implementsList != null : targetClass;
            implementsList.add(factory.createReferenceElementByType(classType));
        } else {
            PsiReferenceList extendsList = targetClass.getExtendsList();
            assert extendsList != null : targetClass;
            if (extendsList.getReferencedTypes().length == 0 && !CommonClassNames.JAVA_LANG_OBJECT.equals(classType.getCanonicalText())) {
                extendsList.add(factory.createReferenceElementByType(classType));
            }
        }
    }
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo)

Aggregations

ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)24 Project (com.intellij.openapi.project.Project)6 NotNull (org.jetbrains.annotations.NotNull)5 ExpectedTypeInfoImpl (com.intellij.codeInsight.ExpectedTypeInfoImpl)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)4 THashSet (gnu.trove.THashSet)3 Nullable (org.jetbrains.annotations.Nullable)3 TailType (com.intellij.codeInsight.TailType)2 Template (com.intellij.codeInsight.template.Template)2 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)2 com.intellij.psi (com.intellij.psi)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)2 PsiUtil (com.intellij.psi.util.PsiUtil)2 Consumer (com.intellij.util.Consumer)2 ProcessingContext (com.intellij.util.ProcessingContext)2 QuickFixBundle (com.intellij.codeInsight.daemon.QuickFixBundle)1 EmptyExpression (com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression)1 GuessTypeParameters (com.intellij.codeInsight.daemon.impl.quickfix.GuessTypeParameters)1 GenerateMembersUtil (com.intellij.codeInsight.generation.GenerateMembersUtil)1