@Override
  public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (!super.isAvailable(project, editor, file) || !(file instanceof JetFile)) {
      return false;
    }

    // When running single test 'isAvailable()' is invoked multiple times, so we need to clear
    // lists.
    overriddenNonOverridableMembers.clear();
    containingDeclarationsNames.clear();

    DeclarationDescriptor descriptor = ResolvePackage.resolveToDescriptor(element);
    if (!(descriptor instanceof CallableMemberDescriptor)) return false;

    for (CallableMemberDescriptor overriddenDescriptor :
        getAllDeclaredNonOverridableOverriddenDescriptors((CallableMemberDescriptor) descriptor)) {
      assert overriddenDescriptor.getKind() == DECLARATION : "Can only be applied to declarations.";
      PsiElement overriddenMember = descriptorToDeclaration(overriddenDescriptor);
      if (overriddenMember == null
          || !QuickFixUtil.canModifyElement(overriddenMember)
          || !(overriddenMember instanceof JetCallableDeclaration)) {
        return false;
      }
      String containingDeclarationName =
          overriddenDescriptor.getContainingDeclaration().getName().asString();
      overriddenNonOverridableMembers.add((JetCallableDeclaration) overriddenMember);
      containingDeclarationsNames.add(containingDeclarationName);
    }
    return overriddenNonOverridableMembers.size() > 0;
  }
Beispiel #2
0
 @NotNull
 public static List<ClassDescriptor> getSuperclassDescriptors(
     @NotNull ClassDescriptor classDescriptor) {
   Collection<KotlinType> superclassTypes = classDescriptor.getTypeConstructor().getSupertypes();
   List<ClassDescriptor> superClassDescriptors = new ArrayList<ClassDescriptor>();
   for (KotlinType type : superclassTypes) {
     ClassDescriptor result = getClassDescriptorForType(type);
     if (!isAny(result)) {
       superClassDescriptors.add(result);
     }
   }
   return superClassDescriptors;
 }
  @NotNull
  @Override
  public String getText() {
    if (overriddenNonOverridableMembers.size() == 1) {
      return JetBundle.message(
          "make.element.modifier",
          containingDeclarationsNames.get(0) + "." + element.getName(),
          OPEN_KEYWORD);
    }

    StringBuilder declarations = new StringBuilder();
    Collections.sort(containingDeclarationsNames);
    for (int i = 0; i < containingDeclarationsNames.size() - 2; i++) {
      declarations.append(containingDeclarationsNames.get(i));
      declarations.append(", ");
    }
    declarations.append(containingDeclarationsNames.get(containingDeclarationsNames.size() - 2));
    declarations.append(" and ");
    declarations.append(containingDeclarationsNames.get(containingDeclarationsNames.size() - 1));
    return JetBundle.message(
        "make.element.in.classifiers.open", element.getName(), declarations.toString());
  }