public static int insertClassReference(PsiClass psiClass, PsiFile file, int startOffset, int endOffset) {
    Project project = file.getProject();
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    documentManager.commitAllDocuments();

    PsiManager manager = file.getManager();

    Document document = FileDocumentManager.getInstance().getDocument(file.getViewProvider().getVirtualFile());

    PsiReference reference = file.findReferenceAt(startOffset);
    if (reference != null && manager.areElementsEquivalent(psiClass, resolve(project, reference))) {
      return endOffset;
    }

    String name = psiClass.getName();
    if (name == null) {
      return endOffset;
    }

    if (reference != null && !psiClass.hasModifierProperty(PsiModifier.STATIC)) {
      PsiClass containingClass = psiClass.getContainingClass();
      if (containingClass != null && containingClass.hasTypeParameters()) {
        PsiModifierListOwner enclosingStaticElement = PsiUtil.getEnclosingStaticElement(reference.getElement(), null);
        if (enclosingStaticElement != null && !PsiTreeUtil.isAncestor(enclosingStaticElement, psiClass, false)) {
          return endOffset;
        }
      }
    }

    assert document != null;
    document.replaceString(startOffset, endOffset, name);

    int newEndOffset = startOffset + name.length();
    RangeMarker toDelete = insertTemporary(newEndOffset, document, " ");

    documentManager.commitAllDocuments();

    PsiElement element = file.findElementAt(startOffset);
    if (element instanceof PsiIdentifier) {
      PsiElement parent = element.getParent();
      if (parent instanceof PsiJavaCodeReferenceElement ref && !ref.isQualified() &&
          !(parent.getParent() instanceof PsiPackageStatement) && psiClass.isValid() &&
          !psiClass.getManager().areElementsEquivalent(psiClass, resolveReference(ref))) {
        boolean staticImport = ref instanceof PsiImportStaticReferenceElement;
        PsiElement newElement;
        try {
          newElement = staticImport
                       ? ((PsiImportStaticReferenceElement)ref).bindToTargetClass(psiClass)
                       : ref.bindToElement(psiClass);
        }
        catch (IncorrectOperationException e) {
          return endOffset; // can happen if fqn contains reserved words, for example
        }
        SmartPsiElementPointer<PsiClass> classPointer = SmartPointerManager.createPointer(psiClass);

        RangeMarker rangeMarker = document.createRangeMarker(newElement.getTextRange());
        documentManager.doPostponedOperationsAndUnblockDocument(document);
        documentManager.commitDocument(document);

        newElement = CodeInsightUtilCore.findElementInRange(file, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(),
                                                            PsiJavaCodeReferenceElement.class,
                                                            JavaLanguage.INSTANCE);
        rangeMarker.dispose();
        if (newElement != null) {
          newEndOffset = newElement.getTextRange().getEndOffset();
          if (!(newElement instanceof PsiReferenceExpression)) {
            PsiReferenceParameterList parameterList = ((PsiJavaCodeReferenceElement)newElement).getParameterList();
            if (parameterList != null) {
              newEndOffset = parameterList.getTextRange().getStartOffset();
            }
          }
          psiClass = classPointer.getElement();

          if (!staticImport &&
              psiClass != null &&
              !psiClass.getManager().areElementsEquivalent(psiClass, resolveReference((PsiReference)newElement)) &&
              !PsiUtil.isInnerClass(psiClass)) {
            String qName = psiClass.getQualifiedName();
            if (qName != null) {
              document.replaceString(newElement.getTextRange().getStartOffset(), newEndOffset, qName);
              newEndOffset = newElement.getTextRange().getStartOffset() + qName.length();
            }
          }
        }
      }
    }

    if (toDelete != null && toDelete.isValid()) {
      document.deleteString(toDelete.getStartOffset(), toDelete.getEndOffset());
    }

    return newEndOffset;
  }
public static int insertClassReference(PsiClass psiClass, PsiFile file, int startOffset, int endOffset)