handleAnalysisUpdateContent method

Future<AnalysisUpdateContentResult> handleAnalysisUpdateContent(
  1. AnalysisUpdateContentParams parameters
)
inherited

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 files = parameters.files;
  files.forEach((String filePath, Object? overlay) {
    // Prepare the old overlay contents.
    String? oldContents;
    try {
      if (resourceProvider.hasOverlay(filePath)) {
        var file = resourceProvider.getFile(filePath);
        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(
        filePath,
        content: newContents,
        modificationStamp: _overlayModificationStamp++,
      );
    } else {
      resourceProvider.removeOverlay(filePath);
    }

    contentChanged(filePath);
  });
  return AnalysisUpdateContentResult();
}