handleAnalysisUpdateContent method

Future<AnalysisUpdateContentResult> handleAnalysisUpdateContent(
  1. AnalysisUpdateContentParams parameters
)

Handle an 'analysis.updateContent' request. Most subclasses should not override this method, but should instead use the contentCache to access the current content of overlaid files.

Throw a RequestFailure if the request could not be handled.

Implementation

Future<AnalysisUpdateContentResult> handleAnalysisUpdateContent(
    AnalysisUpdateContentParams parameters) async {
  var changedPaths = <String>{};
  var paths = parameters.files;
  paths.forEach((String path, Object? overlay) {
    // Prepare the old overlay contents.
    String? oldContents;
    try {
      if (resourceProvider.hasOverlay(path)) {
        var file = resourceProvider.getFile(path);
        oldContents = file.readAsStringSync();
      }
    } catch (_) {}

    // Prepare the new contents.
    String? newContents;
    if (overlay is AddContentOverlay) {
      newContents = overlay.content;
    } else if (overlay is ChangeContentOverlay) {
      if (oldContents == null) {
        // The server should only send a ChangeContentOverlay if there is
        // already an existing overlay for the source.
        throw RequestFailure(
            RequestErrorFactory.invalidOverlayChangeNoContent());
      }
      try {
        newContents = SourceEdit.applySequence(oldContents, overlay.edits);
      } on RangeError {
        throw RequestFailure(
            RequestErrorFactory.invalidOverlayChangeInvalidEdit());
      }
    } else if (overlay is RemoveContentOverlay) {
      newContents = null;
    }

    if (newContents != null) {
      resourceProvider.setOverlay(
        path,
        content: newContents,
        modificationStamp: _overlayModificationStamp++,
      );
    } else {
      resourceProvider.removeOverlay(path);
    }

    changedPaths.add(path);
  });
  await contentChanged(changedPaths.toList());
  return AnalysisUpdateContentResult();
}