| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library source.package_map_resolver; | 5 library source.package_map_resolver; |
| 6 | 6 |
| 7 import 'package:analyzer/file_system/file_system.dart'; | 7 import 'package:analyzer/file_system/file_system.dart'; |
| 8 import 'package:analyzer/src/generated/source.dart'; | 8 import 'package:analyzer/src/generated/source.dart'; |
| 9 import 'package:analyzer/src/util/asserts.dart' as asserts; | 9 import 'package:analyzer/src/util/asserts.dart' as asserts; |
| 10 import 'package:path/path.dart' as pathos; |
| 10 | 11 |
| 11 /** | 12 /** |
| 12 * A [UriResolver] implementation for the `package:` scheme that uses a map of | 13 * A [UriResolver] implementation for the `package:` scheme that uses a map of |
| 13 * package names to their directories. | 14 * package names to their directories. |
| 14 */ | 15 */ |
| 15 class PackageMapUriResolver extends UriResolver { | 16 class PackageMapUriResolver extends UriResolver { |
| 16 /** | 17 /** |
| 17 * The name of the `package` scheme. | 18 * The name of the `package` scheme. |
| 18 */ | 19 */ |
| 19 static const String PACKAGE_SCHEME = "package"; | 20 static const String PACKAGE_SCHEME = "package"; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 // This helps provide more meaningful error messages to users | 72 // This helps provide more meaningful error messages to users |
| 72 // (a missing file error, as opposed to an invalid URI error). | 73 // (a missing file error, as opposed to an invalid URI error). |
| 73 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); | 74 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); |
| 74 } | 75 } |
| 75 | 76 |
| 76 @override | 77 @override |
| 77 Uri restoreAbsolute(Source source) { | 78 Uri restoreAbsolute(Source source) { |
| 78 String sourcePath = source.fullName; | 79 String sourcePath = source.fullName; |
| 79 Uri bestMatch; | 80 Uri bestMatch; |
| 80 int bestMatchLength = -1; | 81 int bestMatchLength = -1; |
| 82 pathos.Context pathContext = resourceProvider.pathContext; |
| 81 for (String pkgName in packageMap.keys) { | 83 for (String pkgName in packageMap.keys) { |
| 82 List<Folder> pkgFolders = packageMap[pkgName]; | 84 List<Folder> pkgFolders = packageMap[pkgName]; |
| 83 for (int i = 0; i < pkgFolders.length; i++) { | 85 for (int i = 0; i < pkgFolders.length; i++) { |
| 84 Folder pkgFolder = pkgFolders[i]; | 86 Folder pkgFolder = pkgFolders[i]; |
| 85 String pkgFolderPath = pkgFolder.path; | 87 String pkgFolderPath = pkgFolder.path; |
| 86 // TODO(paulberry): figure out the right thing to do for Windows. | |
| 87 if (pkgFolderPath.length > bestMatchLength && | 88 if (pkgFolderPath.length > bestMatchLength && |
| 88 sourcePath.startsWith(pkgFolderPath + '/')) { | 89 sourcePath.startsWith(pkgFolderPath + pathContext.separator)) { |
| 89 String relPath = sourcePath.substring(pkgFolderPath.length + 1); | 90 String relPath = sourcePath.substring(pkgFolderPath.length + 1); |
| 90 if (_isReversibleTranslation(pkgFolders, i, relPath)) { | 91 if (_isReversibleTranslation(pkgFolders, i, relPath)) { |
| 91 bestMatch = Uri.parse('$PACKAGE_SCHEME:$pkgName/$relPath'); | 92 List<String> relPathComponents = pathContext.split(relPath); |
| 93 String relUriPath = pathos.posix.joinAll(relPathComponents); |
| 94 bestMatch = Uri.parse('$PACKAGE_SCHEME:$pkgName/$relUriPath'); |
| 92 bestMatchLength = pkgFolderPath.length; | 95 bestMatchLength = pkgFolderPath.length; |
| 93 } | 96 } |
| 94 } | 97 } |
| 95 } | 98 } |
| 96 } | 99 } |
| 97 return bestMatch; | 100 return bestMatch; |
| 98 } | 101 } |
| 99 | 102 |
| 100 /** | 103 /** |
| 101 * A translation from file path to package URI has just been found for | 104 * A translation from file path to package URI has just been found for |
| (...skipping 18 matching lines...) Expand all Loading... |
| 120 return true; | 123 return true; |
| 121 } | 124 } |
| 122 | 125 |
| 123 /** | 126 /** |
| 124 * Returns `true` if [uri] is a `package` URI. | 127 * Returns `true` if [uri] is a `package` URI. |
| 125 */ | 128 */ |
| 126 static bool isPackageUri(Uri uri) { | 129 static bool isPackageUri(Uri uri) { |
| 127 return uri.scheme == PACKAGE_SCHEME; | 130 return uri.scheme == PACKAGE_SCHEME; |
| 128 } | 131 } |
| 129 } | 132 } |
| OLD | NEW |