protected void restoreState(@NotNull final V psiField) {
    if (!ReadonlyStatusHandler.ensureDocumentWritable(
        myProject, InjectedLanguageUtil.getTopLevelEditor(myEditor).getDocument())) return;
    ApplicationManager.getApplication()
        .runWriteAction(
            () -> {
              final PsiFile containingFile = psiField.getContainingFile();
              final RangeMarker exprMarker = getExprMarker();
              if (exprMarker != null) {
                myExpr = restoreExpression(containingFile, psiField, exprMarker, myExprText);
              }

              if (myLocalMarker != null) {
                final PsiElement refVariableElement =
                    containingFile.findElementAt(myLocalMarker.getStartOffset());
                if (refVariableElement != null) {
                  final PsiElement parent = refVariableElement.getParent();
                  if (parent instanceof PsiNamedElement) {
                    ((PsiNamedElement) parent).setName(myLocalName);
                  }
                }

                final V localVariable = getLocalVariable();
                if (localVariable != null && localVariable.isPhysical()) {
                  myLocalVariable = localVariable;
                  final PsiElement nameIdentifier = localVariable.getNameIdentifier();
                  if (nameIdentifier != null) {
                    myLocalMarker = createMarker(nameIdentifier);
                  }
                }
              }
              final List<RangeMarker> occurrenceMarkers = getOccurrenceMarkers();
              for (int i = 0, occurrenceMarkersSize = occurrenceMarkers.size();
                  i < occurrenceMarkersSize;
                  i++) {
                RangeMarker marker = occurrenceMarkers.get(i);
                if (getExprMarker() != null
                    && marker.getStartOffset() == getExprMarker().getStartOffset()
                    && myExpr != null) {
                  myOccurrences[i] = myExpr;
                  continue;
                }
                final E psiExpression =
                    restoreExpression(
                        containingFile,
                        psiField,
                        marker,
                        getLocalVariable() != null ? myLocalName : myExprText);
                if (psiExpression != null) {
                  myOccurrences[i] = psiExpression;
                }
              }

              if (myExpr != null && myExpr.isPhysical()) {
                myExprMarker = createMarker(myExpr);
              }
              myOccurrenceMarkers = null;
              deleteTemplateField(psiField);
            });
  }
  protected void runOverEditor(
      @NotNull final Project project,
      @NotNull final Editor editor,
      @NotNull final PsiFile psiFile) {
    final Document document = editor.getDocument();
    if (!ReadonlyStatusHandler.ensureDocumentWritable(project, document)) return;

    final Runnable runnable =
        () -> {
          final int caretOffset = editor.getCaretModel().getOffset();
          final int lineLength = getRightMargin(project);

          DartAnalysisServerService.getInstance().updateFilesContent();
          DartAnalysisServerService.FormatResult formatResult =
              DartAnalysisServerService.getInstance()
                  .edit_format(psiFile.getVirtualFile(), caretOffset, 0, lineLength);

          if (formatResult == null) {
            showHintLater(editor, DartBundle.message("dart.style.hint.failed"), true);
            LOG.warn("Unexpected response from edit_format, formatResult is null");
            return;
          }

          final List<SourceEdit> edits = formatResult.getEdits();
          if (edits == null || edits.size() == 0) {
            showHintLater(editor, DartBundle.message("dart.style.hint.already.good"), false);
          } else if (edits.size() == 1) {
            final String replacement =
                StringUtil.convertLineSeparators(edits.get(0).getReplacement());
            document.replaceString(0, document.getTextLength(), replacement);
            final int offset =
                DartAnalysisServerService.getInstance()
                    .getConvertedOffset(psiFile.getVirtualFile(), formatResult.getOffset());
            editor.getCaretModel().moveToOffset(offset);
            showHintLater(editor, DartBundle.message("dart.style.hint.success"), false);
          } else {
            showHintLater(editor, DartBundle.message("dart.style.hint.failed"), true);
            LOG.warn(
                "Unexpected response from edit_format, formatResult.getEdits().size() = "
                    + edits.size());
          }
        };

    ApplicationManager.getApplication()
        .runWriteAction(
            () ->
                CommandProcessor.getInstance()
                    .executeCommand(
                        project, runnable, DartBundle.message("dart.style.action.name"), null));
  }
  protected void runOverEditor(
      @NotNull final Project project,
      @NotNull final Editor editor,
      @NotNull final PsiFile psiFile) {
    final Document document = editor.getDocument();
    if (!ReadonlyStatusHandler.ensureDocumentWritable(project, document)) return;

    final Runnable runnable =
        new Runnable() {
          public void run() {
            final String path = psiFile.getVirtualFile().getPath();

            final DartAnalysisServerService service = DartAnalysisServerService.getInstance();
            service.updateFilesContent();
            final SourceFileEdit fileEdit = service.edit_sortMembers(path);

            if (fileEdit == null) {
              showHintLater(editor, DartBundle.message("dart.sort.members.hint.failed"), true);
              LOG.warn("Unexpected response from edit_sortMembers, fileEdit is null");
              return;
            }

            final List<SourceEdit> edits = fileEdit.getEdits();
            if (edits == null || edits.size() == 0) {
              showHintLater(
                  editor, DartBundle.message("dart.sort.members.hint.already.good"), false);
            } else {
              AssistUtils.applySourceEdits(document, edits);
              showHintLater(editor, DartBundle.message("dart.sort.members.hint.success"), false);
            }
          }
        };

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                CommandProcessor.getInstance()
                    .executeCommand(
                        project,
                        runnable,
                        DartBundle.message("dart.sort.members.action.name"),
                        null);
              }
            });
  }
  public void performReplaceAll(Editor e) {
    if (!ReadonlyStatusHandler.ensureDocumentWritable(e.getProject(), e.getDocument())) return;
    if (mySearchResults.getFindModel() != null) {
      final FindModel copy = new FindModel();
      copy.copyFrom(mySearchResults.getFindModel());

      final SelectionModel selectionModel = mySearchResults.getEditor().getSelectionModel();

      final int offset;
      if ((!selectionModel.hasSelection() && !selectionModel.hasBlockSelection())
          || copy.isGlobal()) {
        copy.setGlobal(true);
        offset = 0;
      } else {
        offset = selectionModel.getBlockSelectionStarts()[0];
      }
      FindUtil.replace(e.getProject(), e, offset, copy, this);
    }
  }
 @Nullable
 public TextRange performReplace(
     final FindResult occurrence, final String replacement, final Editor editor) {
   if (myReplaceDenied
       || !ReadonlyStatusHandler.ensureDocumentWritable(editor.getProject(), editor.getDocument()))
     return null;
   FindModel findModel = mySearchResults.getFindModel();
   TextRange result =
       FindUtil.doReplace(
           editor.getProject(),
           editor.getDocument(),
           findModel,
           new FindResultImpl(occurrence.getStartOffset(), occurrence.getEndOffset()),
           replacement,
           true,
           new ArrayList<Pair<TextRange, String>>());
   myLivePreview.inSmartUpdate();
   mySearchResults.updateThreadSafe(findModel, true, result, mySearchResults.getStamp());
   return result;
 }
  public void patch(final FlooPatch res) {
    final TextBuf b = this;
    Flog.info("Got _on_patch");

    String text;
    String md5FromDoc;
    final Document d;

    String oldText = buf;
    VirtualFile virtualFile = b.getVirtualFile();
    if (virtualFile == null) {
      Flog.warn("VirtualFile is null, no idea what do do. Aborting everything %s", this);
      getBuf();
      return;
    }
    d = Buf.getDocumentForVirtualFile(virtualFile);
    if (d == null) {
      Flog.warn("Document not found for %s", virtualFile);
      getBuf();
      return;
    }
    String viewText;
    if (virtualFile.exists()) {
      viewText = d.getText();
      if (viewText.equals(oldText)) {
        b.forced_patch = false;
      } else if (!b.forced_patch) {
        b.forced_patch = true;
        oldText = viewText;
        b.send_patch(viewText);
        Flog.warn("Sending force patch for %s. this is dangerous!", b.path);
      }
    } else {
      viewText = oldText;
    }

    b.cancelTimeout();

    String md5Before = DigestUtils.md5Hex(viewText);
    if (!md5Before.equals(res.md5_before)) {
      Flog.warn("starting md5s don't match for %s. this is dangerous!", b.path);
    }

    List<diff_match_patch.Patch> patches = dmp.patch_fromText(res.patch);
    final Object[] results = dmp.patch_apply((LinkedList<diff_match_patch.Patch>) patches, oldText);
    final String patchedContents = (String) results[0];
    final boolean[] patchesClean = (boolean[]) results[1];
    final FlooPatchPosition[] positions = (FlooPatchPosition[]) results[2];

    for (boolean clean : patchesClean) {
      if (!clean) {
        Flog.log("Patch not clean for %s. Sending get_buf and setting readonly.", d);
        getBuf();
        return;
      }
    }
    // XXX: If patchedContents have carriage returns this will be a problem:
    String md5After = DigestUtils.md5Hex(patchedContents);
    if (!md5After.equals(res.md5_after)) {
      Flog.info("MD5 after mismatch (ours %s remote %s)", md5After, res.md5_after);
    }

    if (!d.isWritable()) {
      d.setReadOnly(false);
    }
    if (!ReadonlyStatusHandler.ensureDocumentWritable(context.project, d)) {
      Flog.info("Document: %s is not writable.", d);
      return;
    }

    final Editor[] editors = EditorFactory.getInstance().getEditors(d, context.project);
    final HashMap<ScrollingModel, Integer[]> original = new HashMap<ScrollingModel, Integer[]>();
    for (Editor editor : editors) {
      if (editor.isDisposed()) {
        continue;
      }
      ScrollingModel scrollingModel = editor.getScrollingModel();
      original.put(
          scrollingModel,
          new Integer[] {
            scrollingModel.getHorizontalScrollOffset(), scrollingModel.getVerticalScrollOffset()
          });
    }
    for (FlooPatchPosition flooPatchPosition : positions) {
      int start = Math.max(0, flooPatchPosition.start);
      int end_ld = Math.max(start + flooPatchPosition.end, start);
      end_ld = Math.min(end_ld, d.getTextLength());
      String contents = NEW_LINE.matcher(flooPatchPosition.text).replaceAll("\n");
      Throwable e = null;
      try {
        Listener.flooDisable();
        d.replaceString(start, end_ld, contents);
      } catch (Throwable exception) {
        e = exception;
      } finally {
        Listener.flooEnable();
      }

      if (e != null) {
        Flog.warn(e);
        getBuf();
        return;
      }
    }
    text = d.getText();
    md5FromDoc = DigestUtils.md5Hex(text);
    if (!md5FromDoc.equals(res.md5_after)) {
      Flog.info("md5FromDoc mismatch (ours %s remote %s)", md5FromDoc, res.md5_after);
      b.setGetBufTimeout();
    }

    for (Map.Entry<ScrollingModel, Integer[]> entry : original.entrySet()) {
      ScrollingModel model = entry.getKey();
      Integer[] offsets = entry.getValue();
      model.scrollHorizontally(offsets[0]);
      model.scrollVertically(offsets[1]);
    }

    b.set(text, md5FromDoc);
    Flog.log("Patched %s", res.path);
  }