Search in sources :

Example 1 with LookupItem

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

the class PriorityWeigher method weigh.

@Override
public Double weigh(@NotNull LookupElement element, @NotNull CompletionLocation location) {
    final PrioritizedLookupElement prioritized = element.as(PrioritizedLookupElement.CLASS_CONDITION_KEY);
    if (prioritized != null) {
        return prioritized.getPriority();
    }
    final LookupItem item = element.as(LookupItem.CLASS_CONDITION_KEY);
    if (item != null) {
        return item.getPriority();
    }
    return 0.0;
}
Also used : LookupItem(com.intellij.codeInsight.lookup.LookupItem)

Example 2 with LookupItem

use of com.intellij.codeInsight.lookup.LookupItem 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 3 with LookupItem

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

the class CompletionData method objectToLookupItem.

public static LookupElement objectToLookupItem(@NotNull final Object object) {
    if (object instanceof LookupElement)
        return (LookupElement) object;
    String s = null;
    TailType tailType = TailType.NONE;
    if (object instanceof PsiElement) {
        s = PsiUtilCore.getName((PsiElement) object);
    } else if (object instanceof PsiMetaData) {
        s = ((PsiMetaData) object).getName();
    } else if (object instanceof String) {
        s = (String) object;
    } else if (object instanceof Template) {
        s = ((Template) object).getKey();
    } else if (object instanceof PresentableLookupValue) {
        s = ((PresentableLookupValue) object).getPresentation();
    }
    if (s == null) {
        throw new AssertionError("Null string for object: " + object + " of class " + object.getClass());
    }
    LookupItem item = new LookupItem(object, s);
    if (object instanceof LookupValueWithUIHint && ((LookupValueWithUIHint) object).isBold()) {
        item.setBold();
    }
    item.setAttribute(LookupItem.TAIL_TYPE_ATTR, tailType);
    return item;
}
Also used : PsiMetaData(com.intellij.psi.meta.PsiMetaData) LookupValueWithUIHint(com.intellij.codeInsight.lookup.LookupValueWithUIHint) LookupItem(com.intellij.codeInsight.lookup.LookupItem) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PresentableLookupValue(com.intellij.codeInsight.lookup.PresentableLookupValue) TailType(com.intellij.codeInsight.TailType) Template(com.intellij.codeInsight.template.Template)

Example 4 with LookupItem

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

the class CompletionData method addLookupItem.

protected void addLookupItem(Set<LookupElement> set, TailType tailType, @NotNull Object completion, final CompletionVariant variant) {
    LookupElement ret = objectToLookupItem(completion);
    if (ret == null)
        return;
    if (!(ret instanceof LookupItem)) {
        set.add(ret);
        return;
    }
    LookupItem item = (LookupItem) ret;
    final InsertHandler insertHandler = variant.getInsertHandler();
    if (insertHandler != null && item.getInsertHandler() == null) {
        item.setInsertHandler(insertHandler);
        item.setTailType(TailType.UNKNOWN);
    } else if (tailType != TailType.NONE) {
        item.setTailType(tailType);
    }
    final Map<Object, Object> itemProperties = variant.getItemProperties();
    for (final Object key : itemProperties.keySet()) {
        item.setAttribute(key, itemProperties.get(key));
    }
    set.add(ret);
}
Also used : LookupItem(com.intellij.codeInsight.lookup.LookupItem) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Aggregations

LookupItem (com.intellij.codeInsight.lookup.LookupItem)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 TailType (com.intellij.codeInsight.TailType)2 ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)1 LookupValueWithUIHint (com.intellij.codeInsight.lookup.LookupValueWithUIHint)1 PresentableLookupValue (com.intellij.codeInsight.lookup.PresentableLookupValue)1 Template (com.intellij.codeInsight.template.Template)1 PsiMetaData (com.intellij.psi.meta.PsiMetaData)1 THashSet (gnu.trove.THashSet)1 HashSet (java.util.HashSet)1