public static void replace(@NotNull Project project, @NotNull Editor editor) {
    final FindManager findManager = FindManager.getInstance(project);
    final FindModel model = findManager.getFindInFileModel().clone();
    final String s = editor.getSelectionModel().getSelectedText();
    if (!StringUtil.isEmpty(s)) {
      if (s.indexOf('\n') >= 0) {
        model.setGlobal(false);
      }
      else {
        model.setStringToFind(s);
        model.setGlobal(true);
      }
    }
    else {
      model.setGlobal(true);
    }
    model.setReplaceState(true);

    findManager.showFindDialog(model, () -> {
      if (!model.isGlobal() && editor.getSelectionModel().hasSelection()) {
        int offset = model.isForward()
                     ? editor.getSelectionModel().getSelectionStart()
                     : editor.getSelectionModel().getSelectionEnd();
        ScrollType scrollType = model.isForward() ? ScrollType.CENTER_DOWN : ScrollType.CENTER_UP;
        moveCaretAndDontChangeSelection(editor, offset, scrollType);
      }
      int offset;
      if (model.isGlobal()) {
        if (model.isFromCursor()) {
          offset = editor.getCaretModel().getOffset();
          if (!model.isForward()) {
            offset++;
          }
        }
        else {
          offset = model.isForward() ? 0 : editor.getDocument().getTextLength();
        }
      }
      else {
        // in selection

        if (!editor.getSelectionModel().hasSelection()) {
          // TODO[anton] actually, this should never happen - Find dialog should not allow such combination
          findManager.setFindNextModel(null);
          return;
        }

        offset = model.isForward() ? editor.getSelectionModel().getSelectionStart() : editor.getSelectionModel().getSelectionEnd();
      }

      if (s != null && editor.getSelectionModel().hasSelection() && s.equals(model.getStringToFind())) {
        if (model.isFromCursor() && model.isForward()) {
          offset = Math.min(editor.getSelectionModel().getSelectionStart(), offset);
        }
        else if (model.isFromCursor() && !model.isForward()) {
          offset = Math.max(editor.getSelectionModel().getSelectionEnd(), offset);
        }
      }
      findManager.setFindNextModel(null);
      findManager.getFindInFileModel().copyFrom(model);
      replace(project, editor, offset, model);
    });
  }
public static void replace(@NotNull Project project, @NotNull Editor editor)