@NotNull
  private String replaceAndProcessDocument(
      @NotNull final Action action,
      @NotNull final String text,
      @NotNull final PsiFile file,
      @Nullable final Document document)
      throws IncorrectOperationException {
    if (document == null) {
      fail("Don't expect the document to be null");
      return null;
    }
    if (myLineRange != null) {
      final DocumentImpl doc = new DocumentImpl(text);
      myTextRange =
          new TextRange(
              doc.getLineStartOffset(myLineRange.getStartOffset()),
              doc.getLineEndOffset(myLineRange.getEndOffset()));
    }
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
    CommandProcessor.getInstance()
        .executeCommand(
            getProject(),
            new Runnable() {
              @Override
              public void run() {
                ApplicationManager.getApplication()
                    .runWriteAction(
                        new Runnable() {
                          @Override
                          public void run() {
                            document.replaceString(0, document.getTextLength(), text);
                            manager.commitDocument(document);
                            try {
                              TextRange rangeToUse = myTextRange;
                              if (rangeToUse == null) {
                                rangeToUse = file.getTextRange();
                              }
                              ACTIONS
                                  .get(action)
                                  .run(
                                      file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
                            } catch (IncorrectOperationException e) {
                              assertTrue(e.getLocalizedMessage(), false);
                            }
                          }
                        });
              }
            },
            action == Action.REFORMAT ? ReformatCodeProcessor.COMMAND_NAME : "",
            "");

    return document.getText();
  }
 private void initWithCustomLineSeparators(final String text) {
   myFixture.configureByText(getTestName(true) + ".java", "");
   final DocumentImpl document = (DocumentImpl) myFixture.getEditor().getDocument();
   document.setAcceptSlashR(true);
   ApplicationManager.getApplication()
       .runWriteAction(
           new Runnable() {
             @Override
             public void run() {
               document.setText(text);
             }
           });
   myFixture.doHighlighting();
 }
 private void markLinesUnchanged(int startLine, int endLine) {
   if (myDocument.getTextLength() == 0) return; // empty document has no lines
   if (startLine == endLine) return;
   ((DocumentImpl) myDocument).clearLineModificationFlags(startLine, endLine);
 }