| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 analyzer.src.context.source; | 5 library analyzer.src.context.source; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/file_system/file_system.dart'; | 9 import 'package:analyzer/file_system/file_system.dart'; |
| 10 import 'package:analyzer/file_system/physical_file_system.dart'; | 10 import 'package:analyzer/file_system/physical_file_system.dart'; |
| 11 import 'package:analyzer/source/package_map_resolver.dart'; | 11 import 'package:analyzer/source/package_map_resolver.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/java_core.dart'; | 13 import 'package:analyzer/src/generated/java_core.dart'; |
| 14 import 'package:analyzer/src/generated/java_engine.dart'; | 14 import 'package:analyzer/src/generated/java_engine.dart'; |
| 15 import 'package:analyzer/src/generated/sdk.dart'; | 15 import 'package:analyzer/src/generated/sdk.dart'; |
| 16 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:analyzer/src/generated/utilities_dart.dart' as utils; | 17 import 'package:analyzer/src/generated/utilities_dart.dart' as utils; |
| 18 import 'package:analyzer/src/util/fast_uri.dart'; | 18 import 'package:analyzer/src/util/fast_uri.dart'; |
| 19 import 'package:package_config/packages.dart'; | 19 import 'package:package_config/packages.dart'; |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * Instances of the class `SourceFactory` resolve possibly relative URI's | 22 * Instances of the class `SourceFactory` resolve possibly relative URI's |
| 23 * against an existing [Source]. | 23 * against an existing [Source]. |
| 24 */ | 24 */ |
| 25 class SourceFactoryImpl implements SourceFactory { | 25 class SourceFactoryImpl implements SourceFactory { |
| 26 /** | |
| 27 * The analysis context that this source factory is associated with. | |
| 28 */ | |
| 29 @override | 26 @override |
| 30 AnalysisContext context; | 27 AnalysisContext context; |
| 31 | 28 |
| 32 /** | 29 /** |
| 33 * URI processor used to find mappings for `package:` URIs found in a | 30 * URI processor used to find mappings for `package:` URIs found in a |
| 34 * `.packages` config file. | 31 * `.packages` config file. |
| 35 */ | 32 */ |
| 36 final Packages _packages; | 33 final Packages _packages; |
| 37 | 34 |
| 38 /** | 35 /** |
| (...skipping 19 matching lines...) Expand all Loading... |
| 58 | 55 |
| 59 /** | 56 /** |
| 60 * Initialize a newly created source factory with the given absolute URI | 57 * Initialize a newly created source factory with the given absolute URI |
| 61 * [resolvers] and optional [_packages] resolution helper. | 58 * [resolvers] and optional [_packages] resolution helper. |
| 62 */ | 59 */ |
| 63 SourceFactoryImpl(this.resolvers, | 60 SourceFactoryImpl(this.resolvers, |
| 64 [this._packages, ResourceProvider resourceProvider]) | 61 [this._packages, ResourceProvider resourceProvider]) |
| 65 : _resourceProvider = | 62 : _resourceProvider = |
| 66 resourceProvider ?? PhysicalResourceProvider.INSTANCE; | 63 resourceProvider ?? PhysicalResourceProvider.INSTANCE; |
| 67 | 64 |
| 68 /** | |
| 69 * Return the [DartSdk] associated with this [SourceFactory], or `null` if | |
| 70 * there is no such SDK. | |
| 71 * | |
| 72 * @return the [DartSdk] associated with this [SourceFactory], or `null` if | |
| 73 * there is no such SDK | |
| 74 */ | |
| 75 @override | 65 @override |
| 76 DartSdk get dartSdk { | 66 DartSdk get dartSdk { |
| 77 for (UriResolver resolver in resolvers) { | 67 List<UriResolver> resolvers = this.resolvers; |
| 68 int length = resolvers.length; |
| 69 for (int i = 0; i < length; i++) { |
| 70 UriResolver resolver = resolvers[i]; |
| 78 if (resolver is DartUriResolver) { | 71 if (resolver is DartUriResolver) { |
| 79 DartUriResolver dartUriResolver = resolver; | 72 DartUriResolver dartUriResolver = resolver; |
| 80 return dartUriResolver.dartSdk; | 73 return dartUriResolver.dartSdk; |
| 81 } | 74 } |
| 82 } | 75 } |
| 83 return null; | 76 return null; |
| 84 } | 77 } |
| 85 | 78 |
| 86 /** | |
| 87 * Sets the [LocalSourcePredicate]. | |
| 88 * | |
| 89 * @param localSourcePredicate the predicate to determine is [Source] is local | |
| 90 */ | |
| 91 @override | 79 @override |
| 92 void set localSourcePredicate(LocalSourcePredicate localSourcePredicate) { | 80 void set localSourcePredicate(LocalSourcePredicate localSourcePredicate) { |
| 93 this._localSourcePredicate = localSourcePredicate; | 81 this._localSourcePredicate = localSourcePredicate; |
| 94 } | 82 } |
| 95 | 83 |
| 96 /// A table mapping package names to paths of directories containing | |
| 97 /// the package (or [null] if there is no registered package URI resolver). | |
| 98 @override | 84 @override |
| 99 Map<String, List<Folder>> get packageMap { | 85 Map<String, List<Folder>> get packageMap { |
| 100 // Start by looking in .packages. | 86 // Start by looking in .packages. |
| 101 if (_packages != null) { | 87 if (_packages != null) { |
| 102 Map<String, List<Folder>> packageMap = <String, List<Folder>>{}; | 88 Map<String, List<Folder>> packageMap = <String, List<Folder>>{}; |
| 103 _packages.asMap().forEach((String name, Uri uri) { | 89 _packages.asMap().forEach((String name, Uri uri) { |
| 104 if (uri.scheme == 'file' || uri.scheme == '' /* unspecified */) { | 90 if (uri.scheme == 'file' || uri.scheme == '' /* unspecified */) { |
| 105 packageMap[name] = <Folder>[ | 91 packageMap[name] = <Folder>[ |
| 106 _resourceProvider.getFolder(uri.toFilePath()) | 92 _resourceProvider.getFolder(uri.toFilePath()) |
| 107 ]; | 93 ]; |
| 108 } | 94 } |
| 109 }); | 95 }); |
| 110 return packageMap; | 96 return packageMap; |
| 111 } | 97 } |
| 112 | 98 |
| 113 // Default to the PackageMapUriResolver. | 99 // Default to the PackageMapUriResolver. |
| 114 PackageMapUriResolver resolver = resolvers | 100 PackageMapUriResolver resolver = resolvers |
| 115 .firstWhere((r) => r is PackageMapUriResolver, orElse: () => null); | 101 .firstWhere((r) => r is PackageMapUriResolver, orElse: () => null); |
| 116 return resolver?.packageMap; | 102 return resolver?.packageMap; |
| 117 } | 103 } |
| 118 | 104 |
| 119 /** | |
| 120 * Return a source factory that will resolve URI's in the same way that this | |
| 121 * source factory does. | |
| 122 */ | |
| 123 @override | 105 @override |
| 124 SourceFactory clone() { | 106 SourceFactory clone() { |
| 125 SourceFactory factory = | 107 SourceFactory factory = |
| 126 new SourceFactory(resolvers, _packages, _resourceProvider); | 108 new SourceFactory(resolvers, _packages, _resourceProvider); |
| 127 factory.localSourcePredicate = _localSourcePredicate; | 109 factory.localSourcePredicate = _localSourcePredicate; |
| 128 return factory; | 110 return factory; |
| 129 } | 111 } |
| 130 | 112 |
| 131 /** | |
| 132 * Return a source object representing the given absolute URI, or `null` if | |
| 133 * the URI is not a valid URI or if it is not an absolute URI. | |
| 134 * | |
| 135 * @param absoluteUri the absolute URI to be resolved | |
| 136 * @return a source object representing the absolute URI | |
| 137 */ | |
| 138 @override | 113 @override |
| 139 Source forUri(String absoluteUri) { | 114 Source forUri(String absoluteUri) { |
| 140 try { | 115 try { |
| 141 Uri uri = FastUri.parse(absoluteUri); | 116 Uri uri = FastUri.parse(absoluteUri); |
| 142 if (uri.isAbsolute) { | 117 if (uri.isAbsolute) { |
| 143 return _internalResolveUri(null, uri); | 118 return _internalResolveUri(null, uri); |
| 144 } | 119 } |
| 145 } catch (exception, stackTrace) { | 120 } catch (exception, stackTrace) { |
| 146 AnalysisEngine.instance.logger.logError( | 121 AnalysisEngine.instance.logger.logError( |
| 147 "Could not resolve URI: $absoluteUri", | 122 "Could not resolve URI: $absoluteUri", |
| 148 new CaughtException(exception, stackTrace)); | 123 new CaughtException(exception, stackTrace)); |
| 149 } | 124 } |
| 150 return null; | 125 return null; |
| 151 } | 126 } |
| 152 | 127 |
| 153 /** | |
| 154 * Return a source object representing the given absolute URI, or `null` if | |
| 155 * the URI is not an absolute URI. | |
| 156 * | |
| 157 * @param absoluteUri the absolute URI to be resolved | |
| 158 * @return a source object representing the absolute URI | |
| 159 */ | |
| 160 @override | 128 @override |
| 161 Source forUri2(Uri absoluteUri) { | 129 Source forUri2(Uri absoluteUri) { |
| 162 if (absoluteUri.isAbsolute) { | 130 if (absoluteUri.isAbsolute) { |
| 163 try { | 131 try { |
| 164 return _internalResolveUri(null, absoluteUri); | 132 return _internalResolveUri(null, absoluteUri); |
| 165 } on AnalysisException catch (exception, stackTrace) { | 133 } on AnalysisException catch (exception, stackTrace) { |
| 166 AnalysisEngine.instance.logger.logError( | 134 AnalysisEngine.instance.logger.logError( |
| 167 "Could not resolve URI: $absoluteUri", | 135 "Could not resolve URI: $absoluteUri", |
| 168 new CaughtException(exception, stackTrace)); | 136 new CaughtException(exception, stackTrace)); |
| 169 } | 137 } |
| 170 } | 138 } |
| 171 return null; | 139 return null; |
| 172 } | 140 } |
| 173 | 141 |
| 174 /** | |
| 175 * Return a source object that is equal to the source object used to obtain | |
| 176 * the given encoding. | |
| 177 * | |
| 178 * @param encoding the encoding of a source object | |
| 179 * @return a source object that is described by the given encoding | |
| 180 * @throws IllegalArgumentException if the argument is not a valid encoding | |
| 181 * See [Source.encoding]. | |
| 182 */ | |
| 183 @override | 142 @override |
| 184 Source fromEncoding(String encoding) { | 143 Source fromEncoding(String encoding) { |
| 185 Source source = forUri(encoding); | 144 Source source = forUri(encoding); |
| 186 if (source == null) { | 145 if (source == null) { |
| 187 throw new IllegalArgumentException( | 146 throw new IllegalArgumentException( |
| 188 "Invalid source encoding: '$encoding'"); | 147 "Invalid source encoding: '$encoding'"); |
| 189 } | 148 } |
| 190 return source; | 149 return source; |
| 191 } | 150 } |
| 192 | 151 |
| 193 /** | |
| 194 * Determines if the given [Source] is local. | |
| 195 * | |
| 196 * @param source the [Source] to analyze | |
| 197 * @return `true` if the given [Source] is local | |
| 198 */ | |
| 199 @override | 152 @override |
| 200 bool isLocalSource(Source source) => _localSourcePredicate.isLocal(source); | 153 bool isLocalSource(Source source) => _localSourcePredicate.isLocal(source); |
| 201 | 154 |
| 202 /** | |
| 203 * Return a source representing the URI that results from resolving the given | |
| 204 * (possibly relative) [containedUri] against the URI associated with the | |
| 205 * [containingSource], whether or not the resulting source exists, or `null` | |
| 206 * if either the [containedUri] is invalid or if it cannot be resolved against | |
| 207 * the [containingSource]'s URI. | |
| 208 */ | |
| 209 @override | 155 @override |
| 210 Source resolveUri(Source containingSource, String containedUri) { | 156 Source resolveUri(Source containingSource, String containedUri) { |
| 211 if (containedUri == null || containedUri.isEmpty) { | 157 if (containedUri == null || containedUri.isEmpty) { |
| 212 return null; | 158 return null; |
| 213 } | 159 } |
| 214 try { | 160 try { |
| 215 // Force the creation of an escaped URI to deal with spaces, etc. | 161 // Force the creation of an escaped URI to deal with spaces, etc. |
| 216 return _internalResolveUri(containingSource, FastUri.parse(containedUri)); | 162 return _internalResolveUri(containingSource, FastUri.parse(containedUri)); |
| 217 } on URISyntaxException { | 163 } on URISyntaxException { |
| 218 return null; | 164 return null; |
| 219 } catch (exception, stackTrace) { | 165 } catch (exception, stackTrace) { |
| 220 String containingFullName = | 166 String containingFullName = |
| 221 containingSource != null ? containingSource.fullName : '<null>'; | 167 containingSource != null ? containingSource.fullName : '<null>'; |
| 222 AnalysisEngine.instance.logger.logInformation( | 168 AnalysisEngine.instance.logger.logInformation( |
| 223 "Could not resolve URI ($containedUri) " | 169 "Could not resolve URI ($containedUri) " |
| 224 "relative to source ($containingFullName)", | 170 "relative to source ($containingFullName)", |
| 225 new CaughtException(exception, stackTrace)); | 171 new CaughtException(exception, stackTrace)); |
| 226 return null; | 172 return null; |
| 227 } | 173 } |
| 228 } | 174 } |
| 229 | 175 |
| 230 /** | |
| 231 * Return an absolute URI that represents the given source, or `null` if a | |
| 232 * valid URI cannot be computed. | |
| 233 * | |
| 234 * @param source the source to get URI for | |
| 235 * @return the absolute URI representing the given source | |
| 236 */ | |
| 237 @override | 176 @override |
| 238 Uri restoreUri(Source source) { | 177 Uri restoreUri(Source source) { |
| 239 // First see if a resolver can restore the URI. | 178 // First see if a resolver can restore the URI. |
| 240 for (UriResolver resolver in resolvers) { | 179 for (UriResolver resolver in resolvers) { |
| 241 Uri uri = resolver.restoreAbsolute(source); | 180 Uri uri = resolver.restoreAbsolute(source); |
| 242 if (uri != null) { | 181 if (uri != null) { |
| 243 // Now see if there's a package mapping. | 182 // Now see if there's a package mapping. |
| 244 Uri packageMappedUri = _getPackageMapping(uri); | 183 Uri packageMappedUri = _getPackageMapping(uri); |
| 245 if (packageMappedUri != null) { | 184 if (packageMappedUri != null) { |
| 246 return packageMappedUri; | 185 return packageMappedUri; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 for (UriResolver resolver in resolvers) { | 261 for (UriResolver resolver in resolvers) { |
| 323 Source result = resolver.resolveAbsolute(containedUri, actualUri); | 262 Source result = resolver.resolveAbsolute(containedUri, actualUri); |
| 324 if (result != null) { | 263 if (result != null) { |
| 325 return result; | 264 return result; |
| 326 } | 265 } |
| 327 } | 266 } |
| 328 return null; | 267 return null; |
| 329 }); | 268 }); |
| 330 } | 269 } |
| 331 } | 270 } |
| OLD | NEW |