Chromium Code Reviews| Index: pkg/analyzer/lib/src/generated/bazel.dart |
| diff --git a/pkg/analyzer/lib/src/generated/bazel.dart b/pkg/analyzer/lib/src/generated/bazel.dart |
| index 4f4f019966bcb53dc1c4b17536af6c08771bfc6e..ed2b11afac432d4f099fb1a93f8a7d48fbe9f2c2 100644 |
| --- a/pkg/analyzer/lib/src/generated/bazel.dart |
| +++ b/pkg/analyzer/lib/src/generated/bazel.dart |
| @@ -4,6 +4,7 @@ |
| library analyzer.src.generated.bazel; |
| +import 'dart:collection'; |
| import 'dart:core'; |
| import 'package:analyzer/file_system/file_system.dart'; |
| @@ -38,6 +39,42 @@ class BazelFileUriResolver extends ResourceUriResolver { |
| } |
| /** |
| + * The [UriResolver] that can resolve `package` URIs in [BazelWorkspace]. |
| + */ |
| +class BazelPackageUriResolver extends UriResolver { |
| + final BazelWorkspace workspace; |
| + |
| + /** |
| + * The cache of absolute [Uri]s to [Source]s mappings. |
| + */ |
| + final Map<Uri, Source> _sourceCache = new HashMap<Uri, Source>(); |
| + |
| + BazelPackageUriResolver(this.workspace); |
| + |
| + @override |
| + Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| + return _sourceCache.putIfAbsent(uri, () { |
| + _BazelPackageUri bazelUri = _BazelPackageUri.parse(uri); |
| + if (bazelUri == null) { |
| + return null; |
| + } |
| + if (bazelUri.packageName.indexOf('.') == -1) { |
| + String path = '${workspace.root}/third_party/dart/' |
| + '${bazelUri.packageName}/lib/${bazelUri.libPath}'; |
| + File file = workspace.getFile(path); |
| + return file?.createSource(uri); |
| + } else { |
| + String packagePath = bazelUri.packageName |
| + .replaceAll('.', workspace.provider.pathContext.separator); |
| + String path = '${workspace.root}/$packagePath/lib/${bazelUri.libPath}'; |
| + File file = workspace.findFile(path); |
| + return file?.createSource(uri); |
| + } |
| + }); |
| + } |
| +} |
| + |
| +/** |
| * Information about a Bazel workspace. |
| */ |
| class BazelWorkspace { |
| @@ -165,4 +202,74 @@ class BazelWorkspace { |
| return null; |
| } |
| } |
| + |
| + /** |
| + * Return the file with the given [absolutePath] in the workspace [root]. |
| + * The file is returned even if it does not exist. Return `null` if the given |
| + * [absolutePath] is not in the workspace [root]. |
| + */ |
| + File getFile(String absolutePath) { |
| + if (provider.pathContext.isWithin(root, absolutePath)) { |
| + return provider.getFile(absolutePath); |
| + } |
| + return null; |
| + } |
| +} |
| + |
| +/** |
| + * A `package` URI that has been parsed according to Bazel rules. |
| + * |
| + * For example, "package:dart.tools.analyzer/src/foo.dart" will be split |
| + * into "dart.tools.analyzer" (the package) and "src/foo.dart" (the suffix). |
| + */ |
| +class _BazelPackageUri { |
|
Brian Wilkerson
2016/10/06 21:55:52
This class seems like overkill because it does so
scheglov
2016/10/07 16:02:42
Done.
|
| + /** |
| + * The name of the package being imported or exported. |
| + * |
| + * For example, if the URI is "package:foo.bar/baz.dart" then the |
| + * packageName is "foo.bar". |
| + */ |
| + final String packageName; |
| + |
| + /** |
| + * The path from the `lib` folder for the package to the file. |
| + * |
| + * For example, if the URI is "package:foo/bar/baz.dart" then the |
| + * suffix is "bar/baz.dart". |
| + */ |
| + final String libPath; |
| + |
| + _BazelPackageUri(this.packageName, this.libPath); |
| + |
| + @override |
| + String toString() => toUri().toString(); |
| + |
| + /** |
| + * Return the reconstituted `package` URI. |
| + */ |
| + Uri toUri() { |
| + return new Uri(scheme: 'package', path: '$packageName/$libPath'); |
| + } |
| + |
| + /** |
| + * Parses a Dart `package` URI according to Bazel rules. |
| + * |
| + * Returns `null` if it's not a valid Bazel `package` URI. |
| + */ |
| + static _BazelPackageUri parse(Uri uri) { |
| + if (uri.scheme != 'package') { |
| + return null; |
| + } |
| + String path = uri.path; |
| + int slash = path.indexOf('/'); |
| + |
| + // If the path either starts with a slash or has no slash, it is invalid. |
| + if (slash < 1) { |
| + return null; |
| + } |
| + |
| + String packageName = path.substring(0, slash); |
| + String suffix = path.substring(slash + 1); |
| + return new _BazelPackageUri(packageName, suffix); |
| + } |
| } |