| 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 | 10 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 } | 70 } |
| 71 // Return a NonExistingSource instance. | 71 // Return a NonExistingSource instance. |
| 72 // This helps provide more meaningful error messages to users | 72 // This helps provide more meaningful error messages to users |
| 73 // (a missing file error, as opposed to an invalid URI error). | 73 // (a missing file error, as opposed to an invalid URI error). |
| 74 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); | 74 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); |
| 75 } | 75 } |
| 76 | 76 |
| 77 @override | 77 @override |
| 78 Uri restoreAbsolute(Source source) { | 78 Uri restoreAbsolute(Source source) { |
| 79 String sourcePath = source.fullName; | 79 String sourcePath = source.fullName; |
| 80 Uri bestMatch; |
| 81 int bestMatchLength = -1; |
| 80 for (String pkgName in packageMap.keys) { | 82 for (String pkgName in packageMap.keys) { |
| 81 List<Folder> pkgFolders = packageMap[pkgName]; | 83 List<Folder> pkgFolders = packageMap[pkgName]; |
| 82 for (int i = 0; i < pkgFolders.length; i++) { | 84 for (int i = 0; i < pkgFolders.length; i++) { |
| 83 Folder pkgFolder = pkgFolders[i]; | 85 Folder pkgFolder = pkgFolders[i]; |
| 84 String pkgFolderPath = pkgFolder.path; | 86 String pkgFolderPath = pkgFolder.path; |
| 85 // TODO(paulberry): figure out the right thing to do for Windows. | 87 // TODO(paulberry): figure out the right thing to do for Windows. |
| 86 if (sourcePath.startsWith(pkgFolderPath + '/')) { | 88 if (pkgFolderPath.length > bestMatchLength && sourcePath.startsWith(pkgF
olderPath + '/')) { |
| 87 String relPath = sourcePath.substring(pkgFolderPath.length + 1); | 89 String relPath = sourcePath.substring(pkgFolderPath.length + 1); |
| 88 if (_isReversibleTranslation(pkgFolders, i, relPath)) { | 90 if (_isReversibleTranslation(pkgFolders, i, relPath)) { |
| 89 return Uri.parse('$PACKAGE_SCHEME:$pkgName/$relPath'); | 91 bestMatch = Uri.parse('$PACKAGE_SCHEME:$pkgName/$relPath'); |
| 92 bestMatchLength = pkgFolderPath.length; |
| 90 } | 93 } |
| 91 } | 94 } |
| 92 } | 95 } |
| 93 } | 96 } |
| 94 return null; | 97 return bestMatch; |
| 95 } | 98 } |
| 96 | 99 |
| 97 /** | 100 /** |
| 98 * 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 |
| 99 * using the [packageDirIndex]th element of [packageDirs], and appending the | 102 * using the [packageDirIndex]th element of [packageDirs], and appending the |
| 100 * relative path [relPath]. Determine whether the translation is reversible; | 103 * relative path [relPath]. Determine whether the translation is reversible; |
| 101 * that is, whether translating the package URI pack to a file path will | 104 * that is, whether translating the package URI pack to a file path will |
| 102 * produce the file path we started with. | 105 * produce the file path we started with. |
| 103 */ | 106 */ |
| 104 bool _isReversibleTranslation(List<Folder> packageDirs, int packageDirIndex, | 107 bool _isReversibleTranslation(List<Folder> packageDirs, int packageDirIndex, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 117 return true; | 120 return true; |
| 118 } | 121 } |
| 119 | 122 |
| 120 /** | 123 /** |
| 121 * Returns `true` if [uri] is a `package` URI. | 124 * Returns `true` if [uri] is a `package` URI. |
| 122 */ | 125 */ |
| 123 static bool isPackageUri(Uri uri) { | 126 static bool isPackageUri(Uri uri) { |
| 124 return uri.scheme == PACKAGE_SCHEME; | 127 return uri.scheme == PACKAGE_SCHEME; |
| 125 } | 128 } |
| 126 } | 129 } |
| OLD | NEW |