Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Unified Diff: pkg/analyzer/lib/src/generated/bazel.dart

Issue 2393963004: Add BazelPackageUriResolver with resolveAbsolute(). (Closed)
Patch Set: Fixes for review comments. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/bazel_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..92c6cc2334d6e79125ba5dcff4d2e28e0ad2e196 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,56 @@ class BazelFileUriResolver extends ResourceUriResolver {
}
/**
+ * The [UriResolver] that can resolve `package` URIs in [BazelWorkspace].
+ */
+class BazelPackageUriResolver extends UriResolver {
+ final BazelWorkspace _workspace;
+ final Context _context;
+
+ /**
+ * The cache of absolute [Uri]s to [Source]s mappings.
+ */
+ final Map<Uri, Source> _sourceCache = new HashMap<Uri, Source>();
+
+ BazelPackageUriResolver(BazelWorkspace workspace)
+ : _workspace = workspace,
+ _context = workspace.provider.pathContext;
+
+ @override
+ Source resolveAbsolute(Uri uri, [Uri actualUri]) {
+ return _sourceCache.putIfAbsent(uri, () {
+ if (uri.scheme != 'package') {
+ return null;
+ }
+ String uriPath = uri.path;
+ int slash = uriPath.indexOf('/');
+
+ // If the path either starts with a slash or has no slash, it is invalid.
+ if (slash < 1) {
+ return null;
+ }
+
+ String packageName = uriPath.substring(0, slash);
+ String fileUriPart = uriPath.substring(slash + 1);
+ String filePath = fileUriPart.replaceAll('/', _context.separator);
+
+ if (packageName.indexOf('.') == -1) {
+ String path =
+ _context.join('third_party', 'dart', packageName, 'lib', filePath);
+ File file = _workspace.getFile(path);
+ return file?.createSource(uri);
+ } else {
+ String packagePath = packageName.replaceAll('.', _context.separator);
+ String path =
+ _context.join(_workspace.root, packagePath, 'lib', filePath);
+ File file = _workspace.findFile(path);
+ return file?.createSource(uri);
+ }
+ });
+ }
+}
+
+/**
* Information about a Bazel workspace.
*/
class BazelWorkspace {
@@ -129,7 +180,7 @@ class BazelWorkspace {
/**
* Return the file with the given [absolutePath], looking first into
- * directories for generated files: `bazel-bin` and `bazel-genfiles`, and
+ * directories for generated files: `bazel-genfiles` and `bazel-bin`, and
* then into the workspace root. The file in the workspace root is returned
* even if it does not exist. Return `null` if the given [absolutePath] is
* not in the workspace [root].
@@ -165,4 +216,12 @@ class BazelWorkspace {
return null;
}
}
+
+ /**
+ * Return the file for the given [pathInWorkspace]. The file is returned even
+ * if it does not exist.
+ */
+ File getFile(String pathInWorkspace) {
+ return provider.getFile(provider.pathContext.join(root, pathInWorkspace));
+ }
}
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/bazel_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698