convertOrgDartlangSdkToPath method

Uri? convertOrgDartlangSdkToPath(
  1. Uri uri
)

Converts a URI in the form org-dartlang-sdk:///sdk/lib/collection/hash_set.dart to a local file-like URI based on the current SDK.

Implementation

Uri? convertOrgDartlangSdkToPath(Uri uri) {
  // org-dartlang-sdk URIs can be in multiple forms:
  //
  //   - org-dartlang-sdk:///sdk/lib/collection/hash_set.dart
  //   - org-dartlang-sdk:///runtime/lib/convert_patch.dart
  //
  // We currently only handle the sdk folder, as we don't know which runtime
  // is being used (this code is shared) and do not want to map to the wrong
  // sources.
  for (final mapping in orgDartlangSdkMappings.entries) {
    final mapPath = mapping.key;
    final mapUri = mapping.value;
    if (uri.isScheme(mapUri.scheme) && uri.path.startsWith(mapUri.path)) {
      return Uri.file(
        path.joinAll([
          mapPath,
          ...uri.pathSegments.skip(mapUri.pathSegments.length),
        ]),
      );
    }
  }

  return null;
}