Exemplo n.º 1
0
  private static Icon getIcon(PsiElement element, DeclarationDescriptor declarationDescriptor) {
    if (element.isValid()) {
      boolean isClass = element instanceof PsiClass || element instanceof JetClass;
      int flags = isClass ? 0 : Iconable.ICON_FLAG_VISIBILITY;
      if (element instanceof JetDeclaration) { // kotlin declaration
        // visibility and abstraction better detect by a descriptor
        return JetDescriptorIconProvider.getIcon(declarationDescriptor, element, flags);
      } else {
        // it is better to show java icons for java code
        return element.getIcon(flags);
      }
    }

    return JetDescriptorIconProvider.getIcon(declarationDescriptor, element, 0);
  }
Exemplo n.º 2
0
  @NotNull
  public static LookupElement createLookupElement(
      @NotNull KotlinCodeAnalyzer analyzer,
      @NotNull DeclarationDescriptor descriptor,
      @Nullable PsiElement declaration) {
    if (declaration != null) {
      MutableLookupElement javaLookupElement = createJavaLookupElementIfPossible(declaration);
      if (javaLookupElement != null) {
        InsertHandler<LookupElement> customHandler = getInsertHandler(descriptor);
        if (customHandler != null) {
          return javaLookupElement.setInsertHandler(getInsertHandler(descriptor));
        } else {
          return javaLookupElement;
        }
      }
    }

    LookupElementBuilder element =
        LookupElementBuilder.create(
            new JetLookupObject(descriptor, analyzer, declaration), descriptor.getName().getName());

    String presentableText = descriptor.getName().getName();
    String typeText = "";
    String tailText = "";
    boolean tailTextGrayed = true;

    if (descriptor instanceof FunctionDescriptor) {
      FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
      JetType returnType = functionDescriptor.getReturnType();
      typeText = DescriptorRenderer.TEXT.renderType(returnType);
      presentableText += DescriptorRenderer.TEXT.renderFunctionParameters(functionDescriptor);

      boolean extensionFunction = functionDescriptor.getReceiverParameter() != null;
      DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
      if (containingDeclaration != null && extensionFunction) {
        tailText +=
            " for "
                + DescriptorRenderer.TEXT.renderType(
                    functionDescriptor.getReceiverParameter().getType());
        tailText += " in " + DescriptorUtils.getFQName(containingDeclaration);
      }
    } else if (descriptor instanceof VariableDescriptor) {
      JetType outType = ((VariableDescriptor) descriptor).getType();
      typeText = DescriptorRenderer.TEXT.renderType(outType);
    } else if (descriptor instanceof ClassDescriptor) {
      DeclarationDescriptor declaredIn = descriptor.getContainingDeclaration();
      assert declaredIn != null;
      tailText = " (" + DescriptorUtils.getFQName(declaredIn) + ")";
      tailTextGrayed = true;
    } else {
      typeText = DescriptorRenderer.TEXT.render(descriptor);
    }

    element = element.withInsertHandler(getInsertHandler(descriptor));
    element =
        element
            .withTailText(tailText, tailTextGrayed)
            .withTypeText(typeText)
            .withPresentableText(presentableText);
    element =
        element.withIcon(
            JetDescriptorIconProvider.getIcon(descriptor, Iconable.ICON_FLAG_VISIBILITY));
    element = element.withStrikeoutness(KotlinBuiltIns.getInstance().isDeprecated(descriptor));
    return element;
  }