| 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; | |
| 11 | 10 |
| 12 /** | 11 /** |
| 13 * A [UriResolver] implementation for the `package:` scheme that uses a map of | 12 * A [UriResolver] implementation for the `package:` scheme that uses a map of |
| 14 * package names to their directories. | 13 * package names to their directories. |
| 15 */ | 14 */ |
| 16 class PackageMapUriResolver extends UriResolver { | 15 class PackageMapUriResolver extends UriResolver { |
| 17 /** | 16 /** |
| 18 * The name of the `package` scheme. | 17 * The name of the `package` scheme. |
| 19 */ | 18 */ |
| 20 static const String PACKAGE_SCHEME = "package"; | 19 static const String PACKAGE_SCHEME = "package"; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 @override | 76 @override |
| 78 Uri restoreAbsolute(Source source) { | 77 Uri restoreAbsolute(Source source) { |
| 79 String sourcePath = source.fullName; | 78 String sourcePath = source.fullName; |
| 80 Uri bestMatch; | 79 Uri bestMatch; |
| 81 int bestMatchLength = -1; | 80 int bestMatchLength = -1; |
| 82 for (String pkgName in packageMap.keys) { | 81 for (String pkgName in packageMap.keys) { |
| 83 List<Folder> pkgFolders = packageMap[pkgName]; | 82 List<Folder> pkgFolders = packageMap[pkgName]; |
| 84 for (int i = 0; i < pkgFolders.length; i++) { | 83 for (int i = 0; i < pkgFolders.length; i++) { |
| 85 Folder pkgFolder = pkgFolders[i]; | 84 Folder pkgFolder = pkgFolders[i]; |
| 86 String pkgFolderPath = pkgFolder.path; | 85 String pkgFolderPath = pkgFolder.path; |
| 86 // TODO(paulberry): figure out the right thing to do for Windows. |
| 87 if (pkgFolderPath.length > bestMatchLength && | 87 if (pkgFolderPath.length > bestMatchLength && |
| 88 sourcePath.startsWith(pkgFolderPath + pathos.context.separator)) { | 88 sourcePath.startsWith(pkgFolderPath + '/')) { |
| 89 String relPath = sourcePath.substring(pkgFolderPath.length + 1); | 89 String relPath = sourcePath.substring(pkgFolderPath.length + 1); |
| 90 if (_isReversibleTranslation(pkgFolders, i, relPath)) { | 90 if (_isReversibleTranslation(pkgFolders, i, relPath)) { |
| 91 List<String> relPathComponents = pathos.context.split(relPath); | 91 bestMatch = Uri.parse('$PACKAGE_SCHEME:$pkgName/$relPath'); |
| 92 String relUriPath = pathos.posix.joinAll(relPathComponents); | |
| 93 bestMatch = Uri.parse('$PACKAGE_SCHEME:$pkgName/$relUriPath'); | |
| 94 bestMatchLength = pkgFolderPath.length; | 92 bestMatchLength = pkgFolderPath.length; |
| 95 } | 93 } |
| 96 } | 94 } |
| 97 } | 95 } |
| 98 } | 96 } |
| 99 return bestMatch; | 97 return bestMatch; |
| 100 } | 98 } |
| 101 | 99 |
| 102 /** | 100 /** |
| 103 * A translation from file path to package URI has just been found for | 101 * A translation from file path to package URI has just been found for |
| (...skipping 18 matching lines...) Expand all Loading... |
| 122 return true; | 120 return true; |
| 123 } | 121 } |
| 124 | 122 |
| 125 /** | 123 /** |
| 126 * Returns `true` if [uri] is a `package` URI. | 124 * Returns `true` if [uri] is a `package` URI. |
| 127 */ | 125 */ |
| 128 static bool isPackageUri(Uri uri) { | 126 static bool isPackageUri(Uri uri) { |
| 129 return uri.scheme == PACKAGE_SCHEME; | 127 return uri.scheme == PACKAGE_SCHEME; |
| 130 } | 128 } |
| 131 } | 129 } |
| OLD | NEW |