public static void addProviderMethod(@NotNull PsiClass targetClass, @NotNull Editor editor, int startOffset) {
    String className = targetClass.getName();
    if (className == null) return;

    String methodStringBeforeCursor = "public static " + className + " " + PsiJavaModule.PROVIDER_METHOD + "() {" +
                                      "return new " + className + "(";
    String methodStringAfterCursor = ");}";

    Document document = editor.getDocument();
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(targetClass.getProject());

    editor.getDocument().insertString(startOffset, methodStringBeforeCursor + methodStringAfterCursor);

    editor.getCaretModel().moveToOffset(startOffset + methodStringBeforeCursor.length());
    documentManager.commitDocument(document);

    PsiFile psiFile = documentManager.getPsiFile(document);
    if (psiFile != null) {
      CodeStyleManager.getInstance(targetClass.getProject())
        .reformatText(psiFile, startOffset, startOffset + methodStringBeforeCursor.length() + methodStringAfterCursor.length() + 1);
    }
  }
public static void addProviderMethod(@NotNull PsiClass targetClass, @NotNull Editor editor, int startOffset)