@NotNull
  public PyAstDocStringOwner buildAndInsert(@NotNull String replacementText) {
    Preconditions.checkNotNull(myDocStringOwner, "For this action docstring owner must be supplied");

    final Project project = myDocStringOwner.getProject();
    PyAstElementGenerator elementGenerator = PyAstElementGenerator.getInstance(project);
    final PyAstExpressionStatement replacement = elementGenerator.createDocstring(replacementText);

    final PyAstStringLiteralExpression docStringExpression = getDocStringExpression();
    if (docStringExpression != null) {
      docStringExpression.replace(replacement.getExpression());
    }
    else {
      PyAstStatementListContainer container = ObjectUtils.tryCast(myDocStringOwner, PyAstStatementListContainer.class);
      if (container == null) {
        throw new IllegalStateException("Should be a function or class");
      }
      final PyAstStatementList statements = container.getStatementList();
      final String indentation = PyIndentUtil.getElementIndent(statements);

      PyUtilCore.updateDocumentUnblockedAndCommitted(myDocStringOwner, document -> {
        final PsiElement beforeStatements = statements.getPrevSibling();
        String replacementWithLineBreaks = "\n" + indentation + replacementText;
        if (statements.getStatements().length > 0) {
          replacementWithLineBreaks += "\n" + indentation;
        }
        final TextRange range = beforeStatements.getTextRange();
        if (!(beforeStatements instanceof PsiWhiteSpace)) {
          document.insertString(range.getEndOffset(), replacementWithLineBreaks);
        }
        else if (statements.getStatements().length == 0 && beforeStatements.textContains('\n')) {
          // preserve original spacing, since it probably separates function and other declarations
          document.insertString(range.getStartOffset(), replacementWithLineBreaks);
        }
        else {
          document.replaceString(range.getStartOffset(), range.getEndOffset(), replacementWithLineBreaks);
        }
      });
    }
    return myDocStringOwner;
  }
@NotNull
  public PyAstDocStringOwner buildAndInsert(@NotNull String replacementText)