| 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 resolver.package; | 5 library resolver.package; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/resource.dart'; | 7 import 'package:analysis_server/src/resource.dart'; |
| 8 import 'package:analyzer/src/generated/source_io.dart'; | 8 import 'package:analyzer/src/generated/source_io.dart'; |
| 9 | 9 |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * 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 |
| 13 * package names to their directories. | 13 * package names to their directories. |
| 14 */ | 14 */ |
| 15 class PackageMapUriResolver extends UriResolver { | 15 class PackageMapUriResolver extends UriResolver { |
| 16 /** | 16 /** |
| 17 * The name of the `package` scheme. | 17 * The name of the `package` scheme. |
| 18 */ | 18 */ |
| 19 static const String PACKAGE_SCHEME = "package"; | 19 static const String PACKAGE_SCHEME = "package"; |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * A table mapping package names to the path of the directory containing | 22 * A table mapping package names to the path of the directories containing |
| 23 * the package. | 23 * the package. |
| 24 */ | 24 */ |
| 25 final Map<String, Folder> packageMap; | 25 final Map<String, List<Folder>> packageMap; |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * The [ResourceProvider] for this resolver. | 28 * The [ResourceProvider] for this resolver. |
| 29 */ | 29 */ |
| 30 final ResourceProvider resourceProvider; | 30 final ResourceProvider resourceProvider; |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * Create a new [PackageMapUriResolver]. | 33 * Create a new [PackageMapUriResolver]. |
| 34 * | 34 * |
| 35 * [packageMap] is a table mapping package names to the path of the directory | 35 * [packageMap] is a table mapping package names to the paths of the |
| 36 * containing the package | 36 * directories containing the package |
| 37 */ | 37 */ |
| 38 PackageMapUriResolver(this.resourceProvider, this.packageMap); | 38 PackageMapUriResolver(this.resourceProvider, this.packageMap); |
| 39 | 39 |
| 40 @override | 40 @override |
| 41 Source fromEncoding(UriKind kind, Uri uri) { | 41 Source fromEncoding(UriKind kind, Uri uri) { |
| 42 if (kind == UriKind.PACKAGE_URI) { | 42 if (kind == UriKind.PACKAGE_URI) { |
| 43 Resource resource = resourceProvider.getResource(uri.path); | 43 Resource resource = resourceProvider.getResource(uri.path); |
| 44 if (resource is File) { | 44 if (resource is File) { |
| 45 return resource.createSource(kind); | 45 return resource.createSource(kind); |
| 46 } | 46 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 60 String relPath; | 60 String relPath; |
| 61 int index = path.indexOf('/'); | 61 int index = path.indexOf('/'); |
| 62 if (index == -1 || index == 0) { | 62 if (index == -1 || index == 0) { |
| 63 return null; | 63 return null; |
| 64 } else { | 64 } else { |
| 65 // <pkgName>/<relPath> | 65 // <pkgName>/<relPath> |
| 66 pkgName = path.substring(0, index); | 66 pkgName = path.substring(0, index); |
| 67 relPath = path.substring(index + 1); | 67 relPath = path.substring(index + 1); |
| 68 } | 68 } |
| 69 // Try to find an existing file. | 69 // Try to find an existing file. |
| 70 Folder packageDir = packageMap[pkgName]; | 70 List<Folder> packageDirs = packageMap[pkgName]; |
| 71 if (packageDir != null && packageDir.exists) { | 71 if (packageDirs != null) { |
| 72 Resource result = packageDir.getChild(relPath); | 72 for (Folder packageDir in packageDirs) { |
| 73 if (result is File && result.exists) { | 73 if (packageDir.exists) { |
| 74 return result.createSource(UriKind.PACKAGE_URI); | 74 Resource result = packageDir.getChild(relPath); |
| 75 if (result is File && result.exists) { |
| 76 return result.createSource(UriKind.PACKAGE_URI); |
| 77 } |
| 78 } |
| 75 } | 79 } |
| 76 } | 80 } |
| 77 // Return a NonExistingSource instance. | 81 // Return a NonExistingSource instance. |
| 78 // This helps provide more meaningful error messages to users | 82 // This helps provide more meaningful error messages to users |
| 79 // (a missing file error, as opposed to an invalid URI error). | 83 // (a missing file error, as opposed to an invalid URI error). |
| 80 // TODO(scheglov) move NonExistingSource to "source.dart" | 84 // TODO(scheglov) move NonExistingSource to "source.dart" |
| 81 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); | 85 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); |
| 82 } | 86 } |
| 83 | 87 |
| 84 /** | 88 /** |
| 85 * Returns `true` if [uri] is a `package` URI. | 89 * Returns `true` if [uri] is a `package` URI. |
| 86 */ | 90 */ |
| 87 static bool isPackageUri(Uri uri) { | 91 static bool isPackageUri(Uri uri) { |
| 88 return uri.scheme == PACKAGE_SCHEME; | 92 return uri.scheme == PACKAGE_SCHEME; |
| 89 } | 93 } |
| 90 } | 94 } |
| OLD | NEW |