sourceRequest method

  1. @override
Future<void> sourceRequest(
  1. Request request,
  2. SourceArguments args,
  3. void sendResponse(
    1. SourceResponseBody
    )
)

sourceRequest is called by the client to request source code for a given source.

The client may provide a whole source or just an int sourceReference (the spec originally had only sourceReference but now supports whole sources).

The supplied sourceReference should correspond to a ScriptRef instance that was stored to generate the sourceReference when sent to the client.

Implementation

@override
Future<void> sourceRequest(
  Request request,
  SourceArguments args,
  void Function(SourceResponseBody) sendResponse,
) async {
  final storedData = isolateManager.getStoredData(
    args.source?.sourceReference ?? args.sourceReference,
  );
  if (storedData == null) {
    throw StateError('source reference is no longer valid');
  }
  final thread = storedData.thread;
  final data = storedData.data;
  final scriptRef = data is vm.ScriptRef ? data : null;
  if (scriptRef == null) {
    throw StateError('source reference was not a valid script');
  }

  final script = await thread.getScript(scriptRef);
  final scriptSource = script.source;
  if (scriptSource == null) {
    throw DebugAdapterException('<source not available>');
  }

  sendResponse(
    SourceResponseBody(content: scriptSource, mimeType: dartMimeType),
  );
}