@Override
    void replaceWithSorted(PsiElement origin) {
      EnumContext context = getContext(origin);
      if (context == null) return;
      SortableList sortableList = readEntries(context);
      if (sortableList == null) return;
      PsiElement lastElement = sortableList.getLastElement();
      sortableList.sort();
      PsiClass aClass = PsiTreeUtil.getParentOfType(origin, PsiClass.class);
      if (aClass == null) return;
      String name = aClass.getName();
      if (name == null) return;
      PsiElement lBrace = aClass.getLBrace();
      PsiElement rBrace = aClass.getRBrace();
      if (lBrace == null || rBrace == null) return;

       //PsiEnumConstant holds comments inside, we need codegen to know about this comments to place \n correctly
      for (SortableEntry entry : sortableList.myEntries) {
        List<PsiComment> comments = StreamEx.ofTree(entry.myElement, el -> StreamEx.of(el.getChildren()))
          .select(PsiComment.class)
          .filter(comment -> !(comment instanceof PsiDocComment)).toList();
        for (PsiComment comment : comments) {
          entry.myBeforeSeparator.add((PsiComment)comment.copy());
          comment.delete();
        }
      }
      StringBuilder sb = new StringBuilder();
      if (sortableList.generate(sb)) {
        sb.append("\n");
      }
      PsiElement elementToPreserve = lastElement.getNextSibling();
      while (elementToPreserve != null && elementToPreserve != rBrace) {
        sb.append(elementToPreserve.getText());
        elementToPreserve = elementToPreserve.getNextSibling();
      }
      Project project = aClass.getProject();
      PsiClass newEnum = createEnum(project, sb.toString());
      if (newEnum == null) return;
      PsiElement newClassLBrace = newEnum.getLBrace();
      PsiElement newClassRBrace = newEnum.getRBrace();
      if (newClassLBrace == null || newClassRBrace == null) return;
      aClass.deleteChildRange(lBrace, rBrace);

      // Can't use addRangeAfter: when there are whitespaces with comments semicolon inserted
      StringBuilder finalText = new StringBuilder();
      for(PsiElement current = newClassLBrace.getNextSibling(); current != newClassRBrace; current = current.getNextSibling()) {
        finalText.append(current.getText());
      }
      String prefix = aClass.getText();

      // Workaround of deleting \n between comment after enum and lBrace when after class there are at least one \n
      PsiElement lastChild = aClass.getLastChild();
      if (lastChild instanceof PsiComment && ((PsiComment)lastChild).getTokenType() == JavaTokenType.END_OF_LINE_COMMENT)  {
        prefix += "\n";
      }
      //if (aClass.getLastChild() instanceof PsiComment)
      PsiClass anEnum = createEnum(project, finalText.toString(), prefix);
      if (anEnum != null) {
        aClass.replace(anEnum);
      }
    }
@Override
    void replaceWithSorted(PsiElement origin)