Search in sources :

Example 1 with ExpectedTypeInfo

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

the class JavaCompletionStatistician method serialize.

@Override
public StatisticsInfo serialize(final LookupElement element, final CompletionLocation location) {
    Object o = element.getObject();
    if (o instanceof PsiLocalVariable || o instanceof PsiParameter || o instanceof PsiThisExpression || o instanceof PsiKeyword) {
        return StatisticsInfo.EMPTY;
    }
    if (!(o instanceof PsiMember)) {
        return null;
    }
    PsiElement position = location.getCompletionParameters().getPosition();
    if (SUPER_CALL.accepts(position) || ReferenceExpressionCompletionContributor.IN_SWITCH_LABEL.accepts(position)) {
        return StatisticsInfo.EMPTY;
    }
    ExpectedTypeInfo firstInfo = getExpectedTypeInfo(location);
    if (o instanceof PsiClass) {
        return getClassInfo((PsiClass) o, position, firstInfo);
    }
    return getFieldOrMethodInfo((PsiMember) o, element, firstInfo);
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo)

Example 2 with ExpectedTypeInfo

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

the class JavaClassNameCompletionContributor method addAllClasses.

public static void addAllClasses(@NotNull CompletionParameters parameters, final boolean filterByScope, @NotNull final PrefixMatcher matcher, @NotNull final Consumer<LookupElement> consumer) {
    final PsiElement insertedElement = parameters.getPosition();
    if (JavaCompletionContributor.ANNOTATION_NAME.accepts(insertedElement)) {
        MultiMap<String, PsiClass> annoMap = getAllAnnotationClasses(insertedElement, matcher);
        Processor<PsiClass> processor = new LimitedAccessibleClassPreprocessor(parameters, filterByScope, anno -> {
            JavaPsiClassReferenceElement item = AllClassesGetter.createLookupItem(anno, JAVA_CLASS_INSERT_HANDLER);
            item.addLookupStrings(getClassNameWithContainers(anno));
            consumer.consume(item);
        });
        for (String name : CompletionUtil.sortMatching(matcher, annoMap.keySet())) {
            if (!ContainerUtil.process(annoMap.get(name), processor))
                break;
        }
        return;
    }
    final ElementFilter filter = IN_EXTENDS_IMPLEMENTS.accepts(insertedElement) ? new ExcludeDeclaredFilter(new ClassFilter(PsiClass.class)) : IN_TYPE_PARAMETER.accepts(insertedElement) ? new ExcludeDeclaredFilter(new ClassFilter(PsiTypeParameter.class)) : TrueFilter.INSTANCE;
    final boolean inJavaContext = parameters.getPosition() instanceof PsiIdentifier;
    final boolean afterNew = AFTER_NEW.accepts(insertedElement);
    if (afterNew) {
        final PsiExpression expr = PsiTreeUtil.getContextOfType(insertedElement, PsiExpression.class, true);
        for (final ExpectedTypeInfo info : ExpectedTypesProvider.getExpectedTypes(expr, true)) {
            final PsiType type = info.getType();
            final PsiClass psiClass = PsiUtil.resolveClassInType(type);
            if (psiClass != null && psiClass.getName() != null) {
                consumer.consume(createClassLookupItem(psiClass, inJavaContext));
            }
            final PsiType defaultType = info.getDefaultType();
            if (!defaultType.equals(type)) {
                final PsiClass defClass = PsiUtil.resolveClassInType(defaultType);
                if (defClass != null && defClass.getName() != null) {
                    consumer.consume(createClassLookupItem(defClass, true));
                }
            }
        }
    }
    final boolean pkgContext = JavaCompletionUtil.inSomePackage(insertedElement);
    AllClassesGetter.processJavaClasses(parameters, matcher, filterByScope, new Consumer<PsiClass>() {

        @Override
        public void consume(PsiClass psiClass) {
            processClass(psiClass, null, "");
        }

        private void processClass(PsiClass psiClass, @Nullable Set<PsiClass> visited, String prefix) {
            boolean isInnerClass = StringUtil.isNotEmpty(prefix);
            if (isInnerClass && isProcessedIndependently(psiClass)) {
                return;
            }
            if (filter.isAcceptable(psiClass, insertedElement)) {
                if (!inJavaContext) {
                    JavaPsiClassReferenceElement element = AllClassesGetter.createLookupItem(psiClass, AllClassesGetter.TRY_SHORTENING);
                    element.setLookupString(prefix + element.getLookupString());
                    consumer.consume(element);
                } else {
                    Condition<PsiClass> condition = eachClass -> filter.isAcceptable(eachClass, insertedElement) && AllClassesGetter.isAcceptableInContext(insertedElement, eachClass, filterByScope, pkgContext);
                    for (JavaPsiClassReferenceElement element : createClassLookupItems(psiClass, afterNew, JAVA_CLASS_INSERT_HANDLER, condition)) {
                        element.setLookupString(prefix + element.getLookupString());
                        JavaConstructorCallElement.wrap(element, insertedElement).forEach(consumer::consume);
                    }
                }
            } else {
                String name = psiClass.getName();
                if (name != null) {
                    PsiClass[] innerClasses = psiClass.getInnerClasses();
                    if (innerClasses.length > 0) {
                        if (visited == null)
                            visited = ContainerUtil.newHashSet();
                        for (PsiClass innerClass : innerClasses) {
                            if (visited.add(innerClass)) {
                                processClass(innerClass, visited, prefix + name + ".");
                            }
                        }
                    }
                }
            }
        }

        private boolean isProcessedIndependently(PsiClass psiClass) {
            String innerName = psiClass.getName();
            return innerName != null && matcher.prefixMatches(innerName);
        }
    });
}
Also used : Condition(com.intellij.openapi.util.Condition) ExcludeDeclaredFilter(com.intellij.psi.filters.element.ExcludeDeclaredFilter) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) ClassFilter(com.intellij.psi.filters.ClassFilter) ElementFilter(com.intellij.psi.filters.ElementFilter)

Example 3 with ExpectedTypeInfo

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

the class JavaCompletionSorting method calcMatch.

private static int calcMatch(final List<String> words, int max, ExpectedTypeInfo[] myExpectedInfos) {
    for (ExpectedTypeInfo myExpectedInfo : myExpectedInfos) {
        String expectedName = ((ExpectedTypeInfoImpl) myExpectedInfo).getExpectedName();
        if (expectedName == null)
            continue;
        max = calcMatch(expectedName, words, max);
        max = calcMatch(truncDigits(expectedName), words, max);
    }
    return max;
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) ExpectedTypeInfoImpl(com.intellij.codeInsight.ExpectedTypeInfoImpl)

Example 4 with ExpectedTypeInfo

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

the class SmartCompletionDecorator method computeTailType.

@Override
protected TailType computeTailType(InsertionContext context) {
    if (context.getCompletionChar() == Lookup.COMPLETE_STATEMENT_SELECT_CHAR) {
        return TailType.NONE;
    }
    if (LookupItem.getDefaultTailType(context.getCompletionChar()) != null) {
        return null;
    }
    LookupElement delegate = getDelegate();
    LookupItem item = as(LookupItem.CLASS_CONDITION_KEY);
    Object object = delegate.getObject();
    if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && (object instanceof PsiMethod || object instanceof PsiClass)) {
        return TailType.NONE;
    }
    final PsiExpression enclosing = PsiTreeUtil.getContextOfType(myPosition, PsiExpression.class, true);
    if (enclosing != null) {
        final PsiType type = JavaCompletionUtil.getLookupElementType(delegate);
        final TailType itemType = item != null ? item.getTailType() : TailType.NONE;
        if (type != null && type.isValid()) {
            Set<TailType> voidTyped = new HashSet<>();
            Set<TailType> sameTyped = new HashSet<>();
            Set<TailType> assignableTyped = new HashSet<>();
            for (ExpectedTypeInfo info : myExpectedTypeInfos) {
                final PsiType infoType = info.getType();
                final PsiType originalInfoType = JavaCompletionUtil.originalize(infoType);
                if (PsiType.VOID.equals(infoType)) {
                    voidTyped.add(info.getTailType());
                } else if (infoType.equals(type) || originalInfoType.equals(type)) {
                    sameTyped.add(info.getTailType());
                } else if ((info.getKind() == ExpectedTypeInfo.TYPE_OR_SUBTYPE && (infoType.isAssignableFrom(type) || originalInfoType.isAssignableFrom(type))) || (info.getKind() == ExpectedTypeInfo.TYPE_OR_SUPERTYPE && (type.isAssignableFrom(infoType) || type.isAssignableFrom(originalInfoType)))) {
                    assignableTyped.add(info.getTailType());
                }
            }
            if (!sameTyped.isEmpty()) {
                return sameTyped.size() == 1 ? sameTyped.iterator().next() : itemType;
            }
            if (!assignableTyped.isEmpty()) {
                return assignableTyped.size() == 1 ? assignableTyped.iterator().next() : itemType;
            }
            if (!voidTyped.isEmpty()) {
                return voidTyped.size() == 1 ? voidTyped.iterator().next() : itemType;
            }
        } else {
            if (myExpectedTypeInfos.size() == 1) {
                return myExpectedTypeInfos.iterator().next().getTailType();
            }
        }
        return itemType;
    }
    return null;
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) LookupItem(com.intellij.codeInsight.lookup.LookupItem) LookupElement(com.intellij.codeInsight.lookup.LookupElement) TailType(com.intellij.codeInsight.TailType) THashSet(gnu.trove.THashSet) HashSet(java.util.HashSet)

Example 5 with ExpectedTypeInfo

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

the class JavaSmartCompletionContributor method getExpectedTypes.

@NotNull
public static ExpectedTypeInfo[] getExpectedTypes(PsiElement position, boolean voidable) {
    if (psiElement().withParent(psiElement(PsiReferenceExpression.class).withParent(PsiThrowStatement.class)).accepts(position)) {
        final PsiElementFactory factory = JavaPsiFacade.getInstance(position.getProject()).getElementFactory();
        final PsiClassType classType = factory.createTypeByFQClassName(CommonClassNames.JAVA_LANG_RUNTIME_EXCEPTION, position.getResolveScope());
        final List<ExpectedTypeInfo> result = new SmartList<>();
        result.add(new ExpectedTypeInfoImpl(classType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, classType, TailType.SEMICOLON, null, ExpectedTypeInfoImpl.NULL));
        final PsiMethod method = PsiTreeUtil.getContextOfType(position, PsiMethod.class, true);
        if (method != null) {
            for (final PsiClassType type : method.getThrowsList().getReferencedTypes()) {
                result.add(new ExpectedTypeInfoImpl(type, ExpectedTypeInfo.TYPE_OR_SUBTYPE, type, TailType.SEMICOLON, null, ExpectedTypeInfoImpl.NULL));
            }
        }
        return result.toArray(new ExpectedTypeInfo[result.size()]);
    }
    PsiExpression expression = PsiTreeUtil.getContextOfType(position, PsiExpression.class, true);
    if (expression == null)
        return ExpectedTypeInfo.EMPTY_ARRAY;
    return ExpectedTypesProvider.getExpectedTypes(expression, true, voidable, false);
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) ExpectedTypeInfoImpl(com.intellij.codeInsight.ExpectedTypeInfoImpl) NotNull(org.jetbrains.annotations.NotNull)

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