Chromium Code Reviews| Index: pkg/analyzer/lib/source/package_map_resolver.dart |
| diff --git a/pkg/analyzer/lib/source/package_map_resolver.dart b/pkg/analyzer/lib/source/package_map_resolver.dart |
| index 37de62a4c5ea15aad016e8a738fb05f1173810a8..80ce2e6d1ff5c7b16db7d4dfc151e02ad4b70116 100644 |
| --- a/pkg/analyzer/lib/source/package_map_resolver.dart |
| +++ b/pkg/analyzer/lib/source/package_map_resolver.dart |
| @@ -79,11 +79,15 @@ class PackageMapUriResolver extends UriResolver { |
| String sourcePath = source.fullName; |
| for (String pkgName in packageMap.keys) { |
| List<Folder> pkgFolders = packageMap[pkgName]; |
| - for (Folder pkgFolder in pkgFolders) { |
| + for (int i = 0; i < pkgFolders.length; i++) { |
| + Folder pkgFolder = pkgFolders[i]; |
| String pkgFolderPath = pkgFolder.path; |
| - if (sourcePath.startsWith(pkgFolderPath)) { |
| - String relPath = sourcePath.substring(pkgFolderPath.length); |
| - return Uri.parse('$PACKAGE_SCHEME:$pkgName$relPath'); |
| + // TODO(paulberry): figure out the right thing to do for Windows. |
|
Paul Berry
2015/02/24 21:58:16
Note: AFAICT this function is broken for Windows b
|
| + if (sourcePath.startsWith(pkgFolderPath + '/')) { |
| + String relPath = sourcePath.substring(pkgFolderPath.length + 1); |
| + if (_isReversibleTranslation(pkgFolders, i, relPath)) { |
| + return Uri.parse('$PACKAGE_SCHEME:$pkgName/$relPath'); |
| + } |
| } |
| } |
| } |
| @@ -91,6 +95,29 @@ class PackageMapUriResolver extends UriResolver { |
| } |
| /** |
| + * A translation from file path to package URI has just been found for |
| + * using the [packageDirIndex]th element of [packageDirs], and appending the |
| + * relative path [relPath]. Determine whether the translation is reversible; |
| + * that is, whether translating the package URI pack to a file path will |
| + * produce the file path we started with. |
| + */ |
| + bool _isReversibleTranslation(List<Folder> packageDirs, int packageDirIndex, |
| + String relPath) { |
| + // The translation is reversible provided there is no prior element of |
| + // [packageDirs] containing a file matching [relPath]. |
| + for (int i = 0; i < packageDirIndex; i++) { |
| + Folder packageDir = packageDirs[i]; |
| + if (packageDir.exists) { |
| + Resource result = packageDir.getChild(relPath); |
| + if (result is File && result.exists) { |
| + return false; |
| + } |
| + } |
| + } |
| + return true; |
| + } |
| + |
| + /** |
| * Returns `true` if [uri] is a `package` URI. |
| */ |
| static bool isPackageUri(Uri uri) { |