private static @NotNull <T extends GenerationInfo> List<T> insertMembersAtOffset(@NotNull PsiFile file,
                                                                                   int offset,
                                                                                   @NotNull List<T> memberPrototypes,
                                                                                   @NotNull Function<? super PsiElement, ? extends PsiClass> aClassFunction) throws IncorrectOperationException {
    if (memberPrototypes.isEmpty()) return memberPrototypes;
    final PsiElement leaf = file.findElementAt(offset);
    PsiElement anchor;
    PsiClass aClass;
    if (leaf != null){
      aClass = aClassFunction.fun(leaf);
      if (aClass == null) return Collections.emptyList();
      anchor = memberPrototypes.get(0).findInsertionAnchor(aClass, leaf);
    }
    else if (file instanceof PsiJavaFile javaFile &&
             javaFile.getClasses().length == 1 &&
             javaFile.getClasses()[0] instanceof PsiImplicitClass implicitClass) {
      anchor = null;
      aClass = implicitClass;
    }
    else {
      return Collections.emptyList();
    }

    if (anchor instanceof PsiWhiteSpace) {
      final ASTNode spaceNode = anchor.getNode();
      anchor = anchor.getNextSibling();

      assert spaceNode != null;
      if (spaceNode.getStartOffset() <= offset && spaceNode.getStartOffset() + spaceNode.getTextLength() >= offset) {
        String whiteSpace = spaceNode.getText().substring(0, offset - spaceNode.getStartOffset());
        if (!StringUtil.containsLineBreak(whiteSpace)) {
          // There is a possible case that the caret is located at the end of the line that already contains expression, say, we
          // want to override particular method while caret is located after the field.
          // Example - consider that we want to override toString() method at the class below:
          //     class Test {
          //         int i;<caret>
          //     }
          // We want to add line feed then in order to avoid situation like below:
          //     class Test {
          //         int i;@Override String toString() {
          //             super.toString();
          //         }
          //     }
          whiteSpace += "\n";
        }
        final PsiParserFacade parserFacade = PsiParserFacade.getInstance(file.getProject());
        final ASTNode singleNewLineWhitespace = parserFacade.createWhiteSpaceFromText(whiteSpace).getNode();
        if (singleNewLineWhitespace != null) {
          spaceNode.getTreeParent().replaceChild(spaceNode, singleNewLineWhitespace); // See http://jetbrains.net/jira/browse/IDEADEV-12837
        }
      }
    }

    // Q: shouldn't it be somewhere in PSI?
    PsiElement element = anchor;
    while (true) {
      if (element == null) break;
      if (element instanceof PsiField || element instanceof PsiMethod || element instanceof PsiClassInitializer) break;
      element = element.getNextSibling();
    }
    if (element instanceof PsiField field) {
      PsiTypeElement typeElement = field.getTypeElement();
      if (typeElement != null && !field.equals(typeElement.getParent())) {
        field.normalizeDeclaration();
        anchor = field;
      }
    }

    return insertMembersBeforeAnchor(aClass, anchor, memberPrototypes);
  }
private static @NotNull <T extends GenerationInfo> List<T> insertMembersAtOffset(@NotNull PsiFile file,
                                                                                   int offset,
                                                                                   @NotNull List<T> memberPrototypes,
                                                                                   @NotNull Function<? super PsiElement, ? extends PsiClass> aClassFunction)