Beispiel #1
0
  protected void addLookupItem(
      Set<LookupElement> set,
      TailType tailType,
      @NotNull Object completion,
      final PsiFile file,
      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);
  }
  public static LookupItem createLookupItem(@NotNull PsiType type, @Nullable PsiElement context) {
    final PsiType original = type;
    int dim = 0;
    while (type instanceof PsiArrayType) {
      type = ((PsiArrayType) type).getComponentType();
      dim++;
    }

    LookupItem item = doCreateItem(type, context);

    if (dim > 0) {
      final StringBuilder tail = new StringBuilder();
      for (int i = 0; i < dim; i++) {
        tail.append("[]");
      }
      item.setAttribute(TAIL_TEXT_ATTR, " " + tail.toString());
      item.setAttribute(TAIL_TEXT_SMALL_ATTR, "");
      item.putUserData(BRACKETS_COUNT_ATTR, dim);
    }
    item.setAttribute(TYPE, original);
    return item;
  }
Beispiel #3
0
  public static LookupElement objectToLookupItem(Object object) {
    if (object instanceof LookupElement) return (LookupElement) object;

    String s = null;
    TailType tailType = TailType.NONE;
    if (object instanceof PsiElement) {
      s = PsiUtilBase.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();
    } else {
      LOG.assertTrue(
          false,
          "Null string for object: "
              + object
              + " of class "
              + (object != null ? object.getClass() : null));
    }

    LookupItem item = new LookupItem(object, s);

    if (object instanceof LookupValueWithUIHint && ((LookupValueWithUIHint) object).isBold()) {
      item.setBold();
    }
    if (object instanceof LookupValueWithTail) {
      item.setAttribute(
          LookupItem.TAIL_TEXT_ATTR, " " + ((LookupValueWithTail) object).getTailText());
    }
    item.setAttribute(CompletionUtil.TAIL_TYPE_ATTR, tailType);
    return item;
  }
  private static LookupItem doCreateItem(PsiType type, PsiElement context) {
    if (type instanceof PsiClassType) {
      PsiClassType.ClassResolveResult classResolveResult = ((PsiClassType) type).resolveGenerics();
      final PsiClass psiClass = classResolveResult.getElement();
      final PsiSubstitutor substitutor = classResolveResult.getSubstitutor();
      final String text = type.getCanonicalText();
      String typeString = text;
      String typeParams = "";
      if (text.indexOf('<') > 0 && text.endsWith(">")) {
        typeString = text.substring(0, text.indexOf('<'));
        typeParams = text.substring(text.indexOf('<'));
      }

      String lookupString = text.substring(typeString.lastIndexOf('.') + 1);
      if (psiClass != null) {
        PsiClass resolved =
            JavaPsiFacade.getInstance(psiClass.getProject())
                .getResolveHelper()
                .resolveReferencedClass(psiClass.getName(), context);
        if (!psiClass.getManager().areElementsEquivalent(resolved, psiClass)) {
          // inner class name should be shown qualified if its not accessible by single name
          PsiClass aClass = psiClass;
          lookupString = "";
          while (aClass != null) {
            lookupString = aClass.getName() + (lookupString == "" ? "" : ".") + lookupString;
            aClass = aClass.getContainingClass();
          }
          lookupString += typeParams;
        }
        LookupItem item = new PsiTypeLookupItem(psiClass, lookupString);
        item.setAttribute(SUBSTITUTOR, substitutor);
        return item;
      }
    }
    return new LookupItem(type, type.getPresentableText());
  }
 public static LookupItem setShowFQN(final LookupItem ret) {
   ret.setAttribute(
       JavaPsiClassReferenceElement.PACKAGE_NAME,
       PsiFormatUtil.getPackageDisplayName((PsiClass) ret.getObject()));
   return ret;
 }