OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library engine.source; |
| 6 |
| 7 import 'dart:collection'; |
| 8 import "dart:math" as math; |
| 9 |
| 10 import 'package:analyzer/file_system/file_system.dart'; |
| 11 import 'package:analyzer/file_system/physical_file_system.dart'; |
| 12 import 'package:analyzer/source/package_map_resolver.dart'; |
| 13 import 'package:analyzer/src/generated/utilities_dart.dart' as utils; |
| 14 import 'package:analyzer/task/model.dart'; |
| 15 import 'package:package_config/packages.dart'; |
| 16 import 'package:path/path.dart' as pathos; |
| 17 |
| 18 import 'engine.dart'; |
| 19 import 'java_core.dart'; |
| 20 import 'java_engine.dart'; |
| 21 import 'java_io.dart' show JavaFile; |
| 22 import 'sdk.dart' show DartSdk; |
| 23 import 'source_io.dart' show FileBasedSource; |
| 24 |
| 25 /** |
| 26 * A function that is used to visit [ContentCache] entries. |
| 27 */ |
| 28 typedef void ContentCacheVisitor(String fullPath, int stamp, String contents); |
| 29 |
| 30 /** |
| 31 * A cache used to override the default content of a [Source]. |
| 32 */ |
| 33 class ContentCache { |
| 34 /** |
| 35 * A table mapping the full path of sources to the contents of those sources. |
| 36 * This is used to override the default contents of a source. |
| 37 */ |
| 38 HashMap<String, String> _contentMap = new HashMap<String, String>(); |
| 39 |
| 40 /** |
| 41 * A table mapping the full path of sources to the modification stamps of |
| 42 * those sources. This is used when the default contents of a source has been |
| 43 * overridden. |
| 44 */ |
| 45 HashMap<String, int> _stampMap = new HashMap<String, int>(); |
| 46 |
| 47 /** |
| 48 * Visit all entries of this cache. |
| 49 */ |
| 50 void accept(ContentCacheVisitor visitor) { |
| 51 _contentMap.forEach((String fullPath, String contents) { |
| 52 int stamp = _stampMap[fullPath]; |
| 53 visitor(fullPath, stamp, contents); |
| 54 }); |
| 55 } |
| 56 |
| 57 /** |
| 58 * Return the contents of the given [source], or `null` if this cache does not |
| 59 * override the contents of the source. |
| 60 * |
| 61 * <b>Note:</b> This method is not intended to be used except by |
| 62 * [AnalysisContext.getContents]. |
| 63 */ |
| 64 String getContents(Source source) => _contentMap[source.fullName]; |
| 65 |
| 66 /** |
| 67 * Return the modification stamp of the given [source], or `null` if this |
| 68 * cache does not override the contents of the source. |
| 69 * |
| 70 * <b>Note:</b> This method is not intended to be used except by |
| 71 * [AnalysisContext.getModificationStamp]. |
| 72 */ |
| 73 int getModificationStamp(Source source) => _stampMap[source.fullName]; |
| 74 |
| 75 /** |
| 76 * Set the contents of the given [source] to the given [contents]. This has |
| 77 * the effect of overriding the default contents of the source. If the |
| 78 * contents are `null` the override is removed so that the default contents |
| 79 * will be returned. |
| 80 */ |
| 81 String setContents(Source source, String contents) { |
| 82 String fullName = source.fullName; |
| 83 if (contents == null) { |
| 84 _stampMap.remove(fullName); |
| 85 return _contentMap.remove(fullName); |
| 86 } else { |
| 87 int newStamp = JavaSystem.currentTimeMillis(); |
| 88 int oldStamp = _stampMap[fullName]; |
| 89 _stampMap[fullName] = newStamp; |
| 90 // Occasionally, if this method is called in rapid succession, the |
| 91 // timestamps are equal. Guard against this by artificially incrementing |
| 92 // the new timestamp. |
| 93 if (newStamp == oldStamp) { |
| 94 _stampMap[fullName] = newStamp + 1; |
| 95 } |
| 96 String oldContent = _contentMap[fullName]; |
| 97 _contentMap[fullName] = contents; |
| 98 return oldContent; |
| 99 } |
| 100 } |
| 101 } |
| 102 |
| 103 class CustomUriResolver extends UriResolver { |
| 104 final Map<String, String> _urlMappings; |
| 105 |
| 106 CustomUriResolver(this._urlMappings); |
| 107 |
| 108 @override |
| 109 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 110 String mapping = _urlMappings[uri.toString()]; |
| 111 if (mapping == null) return null; |
| 112 |
| 113 Uri fileUri = new Uri.file(mapping); |
| 114 if (!fileUri.isAbsolute) return null; |
| 115 |
| 116 JavaFile javaFile = new JavaFile.fromUri(fileUri); |
| 117 return new FileBasedSource(javaFile, actualUri != null ? actualUri : uri); |
| 118 } |
| 119 } |
| 120 |
| 121 /** |
| 122 * Instances of the class `DartUriResolver` resolve `dart` URI's. |
| 123 */ |
| 124 class DartUriResolver extends UriResolver { |
| 125 /** |
| 126 * The name of the `dart` scheme. |
| 127 */ |
| 128 static String DART_SCHEME = "dart"; |
| 129 |
| 130 /** |
| 131 * The prefix of a URI using the dart-ext scheme to reference a native code li
brary. |
| 132 */ |
| 133 static String _DART_EXT_SCHEME = "dart-ext:"; |
| 134 |
| 135 /** |
| 136 * The Dart SDK against which URI's are to be resolved. |
| 137 */ |
| 138 final DartSdk _sdk; |
| 139 |
| 140 /** |
| 141 * Initialize a newly created resolver to resolve Dart URI's against the given
platform within the |
| 142 * given Dart SDK. |
| 143 * |
| 144 * @param sdk the Dart SDK against which URI's are to be resolved |
| 145 */ |
| 146 DartUriResolver(this._sdk); |
| 147 |
| 148 /** |
| 149 * Return the [DartSdk] against which URIs are to be resolved. |
| 150 * |
| 151 * @return the [DartSdk] against which URIs are to be resolved. |
| 152 */ |
| 153 DartSdk get dartSdk => _sdk; |
| 154 |
| 155 @override |
| 156 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 157 if (!isDartUri(uri)) { |
| 158 return null; |
| 159 } |
| 160 return _sdk.mapDartUri(uri.toString()); |
| 161 } |
| 162 |
| 163 /** |
| 164 * Return `true` if the given URI is a `dart-ext:` URI. |
| 165 * |
| 166 * @param uriContent the textual representation of the URI being tested |
| 167 * @return `true` if the given URI is a `dart-ext:` URI |
| 168 */ |
| 169 static bool isDartExtUri(String uriContent) => |
| 170 uriContent != null && uriContent.startsWith(_DART_EXT_SCHEME); |
| 171 |
| 172 /** |
| 173 * Return `true` if the given URI is a `dart:` URI. |
| 174 * |
| 175 * @param uri the URI being tested |
| 176 * @return `true` if the given URI is a `dart:` URI |
| 177 */ |
| 178 static bool isDartUri(Uri uri) => DART_SCHEME == uri.scheme; |
| 179 } |
| 180 |
| 181 /** |
| 182 * Instances of the class `LineInfo` encapsulate information about line and colu
mn information |
| 183 * within a source file. |
| 184 */ |
| 185 class LineInfo { |
| 186 /** |
| 187 * An array containing the offsets of the first character of each line in the
source code. |
| 188 */ |
| 189 final List<int> _lineStarts; |
| 190 |
| 191 /** |
| 192 * The zero-based [_lineStarts] index resulting from the last call to |
| 193 * [getLocation]. |
| 194 */ |
| 195 int _previousLine = 0; |
| 196 |
| 197 /** |
| 198 * Initialize a newly created set of line information to represent the data en
coded in the given |
| 199 * array. |
| 200 * |
| 201 * @param lineStarts the offsets of the first character of each line in the so
urce code |
| 202 */ |
| 203 LineInfo(this._lineStarts) { |
| 204 if (_lineStarts == null) { |
| 205 throw new IllegalArgumentException("lineStarts must be non-null"); |
| 206 } else if (_lineStarts.length < 1) { |
| 207 throw new IllegalArgumentException("lineStarts must be non-empty"); |
| 208 } |
| 209 } |
| 210 |
| 211 /** |
| 212 * Return the location information for the character at the given offset. |
| 213 * |
| 214 * @param offset the offset of the character for which location information is
to be returned |
| 215 * @return the location information for the character at the given offset |
| 216 */ |
| 217 LineInfo_Location getLocation(int offset) { |
| 218 var min = 0; |
| 219 var max = _lineStarts.length - 1; |
| 220 |
| 221 // Subsequent calls to [getLocation] are often for offsets near each other. |
| 222 // To take advantage of that, we cache the index of the line start we found |
| 223 // when this was last called. If the current offset is on that line or |
| 224 // later, we'll skip those early indices completely when searching. |
| 225 if (offset >= _lineStarts[_previousLine]) { |
| 226 min = _previousLine; |
| 227 |
| 228 // Before kicking off a full binary search, do a quick check here to see |
| 229 // if the new offset is on that exact line. |
| 230 if (min == _lineStarts.length - 1 || offset < _lineStarts[min + 1]) { |
| 231 return new LineInfo_Location(min + 1, offset - _lineStarts[min] + 1); |
| 232 } |
| 233 } |
| 234 |
| 235 // Binary search to fine the line containing this offset. |
| 236 while (min < max) { |
| 237 var midpoint = (max - min + 1) ~/ 2 + min; |
| 238 |
| 239 if (_lineStarts[midpoint] > offset) { |
| 240 max = midpoint - 1; |
| 241 } else { |
| 242 min = midpoint; |
| 243 } |
| 244 } |
| 245 |
| 246 _previousLine = min; |
| 247 |
| 248 return new LineInfo_Location(min + 1, offset - _lineStarts[min] + 1); |
| 249 } |
| 250 } |
| 251 |
| 252 /** |
| 253 * Instances of the class `Location` represent the location of a character as a
line and |
| 254 * column pair. |
| 255 */ |
| 256 class LineInfo_Location { |
| 257 /** |
| 258 * The one-based index of the line containing the character. |
| 259 */ |
| 260 final int lineNumber; |
| 261 |
| 262 /** |
| 263 * The one-based index of the column containing the character. |
| 264 */ |
| 265 final int columnNumber; |
| 266 |
| 267 /** |
| 268 * Initialize a newly created location to represent the location of the charac
ter at the given |
| 269 * line and column position. |
| 270 * |
| 271 * @param lineNumber the one-based index of the line containing the character |
| 272 * @param columnNumber the one-based index of the column containing the charac
ter |
| 273 */ |
| 274 LineInfo_Location(this.lineNumber, this.columnNumber); |
| 275 } |
| 276 |
| 277 /** |
| 278 * Instances of interface `LocalSourcePredicate` are used to determine if the gi
ven |
| 279 * [Source] is "local" in some sense, so can be updated. |
| 280 */ |
| 281 abstract class LocalSourcePredicate { |
| 282 /** |
| 283 * Instance of [LocalSourcePredicate] that always returns `false`. |
| 284 */ |
| 285 static final LocalSourcePredicate FALSE = new LocalSourcePredicate_FALSE(); |
| 286 |
| 287 /** |
| 288 * Instance of [LocalSourcePredicate] that always returns `true`. |
| 289 */ |
| 290 static final LocalSourcePredicate TRUE = new LocalSourcePredicate_TRUE(); |
| 291 |
| 292 /** |
| 293 * Instance of [LocalSourcePredicate] that returns `true` for all [Source]s |
| 294 * except of SDK. |
| 295 */ |
| 296 static final LocalSourcePredicate NOT_SDK = |
| 297 new LocalSourcePredicate_NOT_SDK(); |
| 298 |
| 299 /** |
| 300 * Determines if the given [Source] is local. |
| 301 * |
| 302 * @param source the [Source] to analyze |
| 303 * @return `true` if the given [Source] is local |
| 304 */ |
| 305 bool isLocal(Source source); |
| 306 } |
| 307 |
| 308 class LocalSourcePredicate_FALSE implements LocalSourcePredicate { |
| 309 @override |
| 310 bool isLocal(Source source) => false; |
| 311 } |
| 312 |
| 313 class LocalSourcePredicate_NOT_SDK implements LocalSourcePredicate { |
| 314 @override |
| 315 bool isLocal(Source source) => source.uriKind != UriKind.DART_URI; |
| 316 } |
| 317 |
| 318 class LocalSourcePredicate_TRUE implements LocalSourcePredicate { |
| 319 @override |
| 320 bool isLocal(Source source) => true; |
| 321 } |
| 322 |
| 323 /** |
| 324 * An implementation of an non-existing [Source]. |
| 325 */ |
| 326 class NonExistingSource extends Source { |
| 327 @override |
| 328 final String fullName; |
| 329 |
| 330 @override |
| 331 final Uri uri; |
| 332 |
| 333 final UriKind uriKind; |
| 334 |
| 335 NonExistingSource(this.fullName, this.uri, this.uriKind); |
| 336 |
| 337 @override |
| 338 TimestampedData<String> get contents { |
| 339 throw new UnsupportedOperationException('$fullName does not exist.'); |
| 340 } |
| 341 |
| 342 @override |
| 343 String get encoding { |
| 344 throw new UnsupportedOperationException('$fullName does not exist.'); |
| 345 } |
| 346 |
| 347 @override |
| 348 int get hashCode => fullName.hashCode; |
| 349 |
| 350 @override |
| 351 bool get isInSystemLibrary => false; |
| 352 |
| 353 @override |
| 354 int get modificationStamp => -1; |
| 355 |
| 356 @override |
| 357 String get shortName => pathos.basename(fullName); |
| 358 |
| 359 @override |
| 360 bool operator ==(Object other) { |
| 361 if (other is NonExistingSource) { |
| 362 return other.uriKind == uriKind && other.fullName == fullName; |
| 363 } |
| 364 return false; |
| 365 } |
| 366 |
| 367 @override |
| 368 bool exists() => false; |
| 369 |
| 370 @override |
| 371 Uri resolveRelativeUri(Uri relativeUri) { |
| 372 throw new UnsupportedOperationException('$fullName does not exist.'); |
| 373 } |
| 374 } |
| 375 |
| 376 /** |
| 377 * The interface `Source` defines the behavior of objects representing source co
de that can be |
| 378 * analyzed by the analysis engine. |
| 379 * |
| 380 * Implementations of this interface need to be aware of some assumptions made b
y the analysis |
| 381 * engine concerning sources: |
| 382 * * Sources are not required to be unique. That is, there can be multiple insta
nces representing |
| 383 * the same source. |
| 384 * * Sources are long lived. That is, the engine is allowed to hold on to a sour
ce for an extended |
| 385 * period of time and that source must continue to report accurate and up-to-dat
e information. |
| 386 * Because of these assumptions, most implementations will not maintain any stat
e but will delegate |
| 387 * to an authoritative system of record in order to implement this API. For exam
ple, a source that |
| 388 * represents files on disk would typically query the file system to determine t
he state of the |
| 389 * file. |
| 390 * |
| 391 * If the instances that implement this API are the system of record, then they
will typically be |
| 392 * unique. In that case, sources that are created that represent non-existent fi
les must also be |
| 393 * retained so that if those files are created at a later date the long-lived so
urces representing |
| 394 * those files will know that they now exist. |
| 395 */ |
| 396 abstract class Source implements AnalysisTarget { |
| 397 /** |
| 398 * An empty list of sources. |
| 399 */ |
| 400 @deprecated // Use Source.EMPTY_LIST |
| 401 static const List<Source> EMPTY_ARRAY = EMPTY_LIST; |
| 402 |
| 403 /** |
| 404 * An empty list of sources. |
| 405 */ |
| 406 static const List<Source> EMPTY_LIST = const <Source>[]; |
| 407 |
| 408 /** |
| 409 * Get the contents and timestamp of this source. |
| 410 * |
| 411 * Clients should consider using the the method [AnalysisContext.getContents] |
| 412 * because contexts can have local overrides of the content of a source that t
he source is not |
| 413 * aware of. |
| 414 * |
| 415 * @return the contents and timestamp of the source |
| 416 * @throws Exception if the contents of this source could not be accessed |
| 417 */ |
| 418 TimestampedData<String> get contents; |
| 419 |
| 420 /** |
| 421 * Return an encoded representation of this source that can be used to create
a source that is |
| 422 * equal to this source. |
| 423 * |
| 424 * @return an encoded representation of this source |
| 425 * See [SourceFactory.fromEncoding]. |
| 426 */ |
| 427 String get encoding; |
| 428 |
| 429 /** |
| 430 * Return the full (long) version of the name that can be displayed to the use
r to denote this |
| 431 * source. For example, for a source representing a file this would typically
be the absolute path |
| 432 * of the file. |
| 433 * |
| 434 * @return a name that can be displayed to the user to denote this source |
| 435 */ |
| 436 String get fullName; |
| 437 |
| 438 /** |
| 439 * Return a hash code for this source. |
| 440 * |
| 441 * @return a hash code for this source |
| 442 * See [Object.hashCode]. |
| 443 */ |
| 444 @override |
| 445 int get hashCode; |
| 446 |
| 447 /** |
| 448 * Return `true` if this source is in one of the system libraries. |
| 449 * |
| 450 * @return `true` if this is in a system library |
| 451 */ |
| 452 bool get isInSystemLibrary; |
| 453 |
| 454 /** |
| 455 * Return the modification stamp for this source, or a negative value if the |
| 456 * source does not exist. A modification stamp is a non-negative integer with |
| 457 * the property that if the contents of the source have not been modified |
| 458 * since the last time the modification stamp was accessed then the same value |
| 459 * will be returned, but if the contents of the source have been modified one |
| 460 * or more times (even if the net change is zero) the stamps will be different
. |
| 461 * |
| 462 * Clients should consider using the the method |
| 463 * [AnalysisContext.getModificationStamp] because contexts can have local |
| 464 * overrides of the content of a source that the source is not aware of. |
| 465 */ |
| 466 int get modificationStamp; |
| 467 |
| 468 /** |
| 469 * Return a short version of the name that can be displayed to the user to den
ote this source. For |
| 470 * example, for a source representing a file this would typically be the name
of the file. |
| 471 * |
| 472 * @return a name that can be displayed to the user to denote this source |
| 473 */ |
| 474 String get shortName; |
| 475 |
| 476 @override |
| 477 Source get source => this; |
| 478 |
| 479 /** |
| 480 * Return the URI from which this source was originally derived. |
| 481 * |
| 482 * @return the URI from which this source was originally derived |
| 483 */ |
| 484 Uri get uri; |
| 485 |
| 486 /** |
| 487 * Return the kind of URI from which this source was originally derived. If th
is source was |
| 488 * created from an absolute URI, then the returned kind will reflect the schem
e of the absolute |
| 489 * URI. If it was created from a relative URI, then the returned kind will be
the same as the kind |
| 490 * of the source against which the relative URI was resolved. |
| 491 * |
| 492 * @return the kind of URI from which this source was originally derived |
| 493 */ |
| 494 UriKind get uriKind; |
| 495 |
| 496 /** |
| 497 * Return `true` if the given object is a source that represents the same sour
ce code as |
| 498 * this source. |
| 499 * |
| 500 * @param object the object to be compared with this object |
| 501 * @return `true` if the given object is a source that represents the same sou
rce code as |
| 502 * this source |
| 503 * See [Object.==]. |
| 504 */ |
| 505 @override |
| 506 bool operator ==(Object object); |
| 507 |
| 508 /** |
| 509 * Return `true` if this source exists. |
| 510 * |
| 511 * Clients should consider using the the method [AnalysisContext.exists] becau
se |
| 512 * contexts can have local overrides of the content of a source that the sourc
e is not aware of |
| 513 * and a source with local content is considered to exist even if there is no
file on disk. |
| 514 * |
| 515 * @return `true` if this source exists |
| 516 */ |
| 517 bool exists(); |
| 518 |
| 519 /** |
| 520 * Resolve the relative URI against the URI associated with this source object
. |
| 521 * |
| 522 * Note: This method is not intended for public use, it is only visible out of
necessity. It is |
| 523 * only intended to be invoked by a [SourceFactory]. Source factories will |
| 524 * only invoke this method if the URI is relative, so implementations of this
method are not |
| 525 * required to, and generally do not, verify the argument. The result of invok
ing this method with |
| 526 * an absolute URI is intentionally left unspecified. |
| 527 * |
| 528 * @param relativeUri the relative URI to be resolved against this source |
| 529 * @return the URI to which given URI was resolved |
| 530 * @throws AnalysisException if the relative URI could not be resolved |
| 531 */ |
| 532 Uri resolveRelativeUri(Uri relativeUri); |
| 533 } |
| 534 |
| 535 /** |
| 536 * The interface `ContentReceiver` defines the behavior of objects that can rece
ive the |
| 537 * content of a source. |
| 538 */ |
| 539 abstract class Source_ContentReceiver { |
| 540 /** |
| 541 * Accept the contents of a source. |
| 542 * |
| 543 * @param contents the contents of the source |
| 544 * @param modificationTime the time at which the contents were last set |
| 545 */ |
| 546 void accept(String contents, int modificationTime); |
| 547 } |
| 548 |
| 549 /** |
| 550 * The interface `SourceContainer` is used by clients to define a collection of
sources |
| 551 * |
| 552 * Source containers are not used within analysis engine, but can be used by cli
ents to group |
| 553 * sources for the purposes of accessing composite dependency information. For e
xample, the Eclipse |
| 554 * client uses source containers to represent Eclipse projects, which allows it
to easily compute |
| 555 * project-level dependencies. |
| 556 */ |
| 557 abstract class SourceContainer { |
| 558 /** |
| 559 * Determine if the specified source is part of the receiver's collection of s
ources. |
| 560 * |
| 561 * @param source the source in question |
| 562 * @return `true` if the receiver contains the source, else `false` |
| 563 */ |
| 564 bool contains(Source source); |
| 565 } |
| 566 |
| 567 /** |
| 568 * Instances of the class `SourceFactory` resolve possibly relative URI's agains
t an existing |
| 569 * [Source]. |
| 570 */ |
| 571 class SourceFactory { |
| 572 /** |
| 573 * The analysis context that this source factory is associated with. |
| 574 */ |
| 575 AnalysisContext context; |
| 576 |
| 577 /** |
| 578 * URI processor used to find mappings for `package:` URIs found in a `.packag
es` config |
| 579 * file. |
| 580 */ |
| 581 final Packages _packages; |
| 582 |
| 583 /** |
| 584 * Resource provider used in working with package maps. |
| 585 */ |
| 586 final ResourceProvider _resourceProvider; |
| 587 |
| 588 /** |
| 589 * The resolvers used to resolve absolute URI's. |
| 590 */ |
| 591 final List<UriResolver> _resolvers; |
| 592 |
| 593 /** |
| 594 * The predicate to determine is [Source] is local. |
| 595 */ |
| 596 LocalSourcePredicate _localSourcePredicate = LocalSourcePredicate.NOT_SDK; |
| 597 |
| 598 /** |
| 599 * Initialize a newly created source factory with the given absolute URI [reso
lvers] and |
| 600 * optional [packages] resolution helper. |
| 601 */ |
| 602 SourceFactory(this._resolvers, |
| 603 [this._packages, ResourceProvider resourceProvider]) |
| 604 : _resourceProvider = resourceProvider != null |
| 605 ? resourceProvider |
| 606 : PhysicalResourceProvider.INSTANCE; |
| 607 |
| 608 /** |
| 609 * Return the [DartSdk] associated with this [SourceFactory], or `null` if the
re |
| 610 * is no such SDK. |
| 611 * |
| 612 * @return the [DartSdk] associated with this [SourceFactory], or `null` if |
| 613 * there is no such SDK |
| 614 */ |
| 615 DartSdk get dartSdk { |
| 616 for (UriResolver resolver in _resolvers) { |
| 617 if (resolver is DartUriResolver) { |
| 618 DartUriResolver dartUriResolver = resolver; |
| 619 return dartUriResolver.dartSdk; |
| 620 } |
| 621 } |
| 622 return null; |
| 623 } |
| 624 |
| 625 /** |
| 626 * Sets the [LocalSourcePredicate]. |
| 627 * |
| 628 * @param localSourcePredicate the predicate to determine is [Source] is local |
| 629 */ |
| 630 void set localSourcePredicate(LocalSourcePredicate localSourcePredicate) { |
| 631 this._localSourcePredicate = localSourcePredicate; |
| 632 } |
| 633 |
| 634 /// A table mapping package names to paths of directories containing |
| 635 /// the package (or [null] if there is no registered package URI resolver). |
| 636 Map<String, List<Folder>> get packageMap { |
| 637 // Start by looking in .packages. |
| 638 if (_packages != null) { |
| 639 Map<String, List<Folder>> packageMap = <String, List<Folder>>{}; |
| 640 _packages.asMap().forEach((String name, Uri uri) { |
| 641 if (uri.scheme == 'file' || uri.scheme == '' /* unspecified */) { |
| 642 packageMap[name] = |
| 643 <Folder>[_resourceProvider.getFolder(uri.toFilePath())]; |
| 644 } |
| 645 }); |
| 646 return packageMap; |
| 647 } |
| 648 |
| 649 // Default to the PackageMapUriResolver. |
| 650 PackageMapUriResolver resolver = _resolvers.firstWhere( |
| 651 (r) => r is PackageMapUriResolver, orElse: () => null); |
| 652 return resolver != null ? resolver.packageMap : null; |
| 653 } |
| 654 |
| 655 /** |
| 656 * Return a source object representing the given absolute URI, or `null` if th
e URI is not a |
| 657 * valid URI or if it is not an absolute URI. |
| 658 * |
| 659 * @param absoluteUri the absolute URI to be resolved |
| 660 * @return a source object representing the absolute URI |
| 661 */ |
| 662 Source forUri(String absoluteUri) { |
| 663 try { |
| 664 Uri uri = parseUriWithException(absoluteUri); |
| 665 if (uri.isAbsolute) { |
| 666 return _internalResolveUri(null, uri); |
| 667 } |
| 668 } catch (exception, stackTrace) { |
| 669 AnalysisEngine.instance.logger.logError( |
| 670 "Could not resolve URI: $absoluteUri", |
| 671 new CaughtException(exception, stackTrace)); |
| 672 } |
| 673 return null; |
| 674 } |
| 675 |
| 676 /** |
| 677 * Return a source object representing the given absolute URI, or `null` if th
e URI is not |
| 678 * an absolute URI. |
| 679 * |
| 680 * @param absoluteUri the absolute URI to be resolved |
| 681 * @return a source object representing the absolute URI |
| 682 */ |
| 683 Source forUri2(Uri absoluteUri) { |
| 684 if (absoluteUri.isAbsolute) { |
| 685 try { |
| 686 return _internalResolveUri(null, absoluteUri); |
| 687 } on AnalysisException catch (exception, stackTrace) { |
| 688 AnalysisEngine.instance.logger.logError( |
| 689 "Could not resolve URI: $absoluteUri", |
| 690 new CaughtException(exception, stackTrace)); |
| 691 } |
| 692 } |
| 693 return null; |
| 694 } |
| 695 |
| 696 /** |
| 697 * Return a source object that is equal to the source object used to obtain th
e given encoding. |
| 698 * |
| 699 * @param encoding the encoding of a source object |
| 700 * @return a source object that is described by the given encoding |
| 701 * @throws IllegalArgumentException if the argument is not a valid encoding |
| 702 * See [Source.encoding]. |
| 703 */ |
| 704 Source fromEncoding(String encoding) { |
| 705 Source source = forUri(encoding); |
| 706 if (source == null) { |
| 707 throw new IllegalArgumentException( |
| 708 "Invalid source encoding: '$encoding'"); |
| 709 } |
| 710 return source; |
| 711 } |
| 712 |
| 713 /** |
| 714 * Determines if the given [Source] is local. |
| 715 * |
| 716 * @param source the [Source] to analyze |
| 717 * @return `true` if the given [Source] is local |
| 718 */ |
| 719 bool isLocalSource(Source source) => _localSourcePredicate.isLocal(source); |
| 720 |
| 721 /** |
| 722 * Return a source object representing the URI that results from resolving the
given (possibly |
| 723 * relative) contained URI against the URI associated with an existing source
object, whether or |
| 724 * not the resulting source exists, or `null` if either the contained URI is i
nvalid or if |
| 725 * it cannot be resolved against the source object's URI. |
| 726 * |
| 727 * @param containingSource the source containing the given URI |
| 728 * @param containedUri the (possibly relative) URI to be resolved against the
containing source |
| 729 * @return the source representing the contained URI |
| 730 */ |
| 731 Source resolveUri(Source containingSource, String containedUri) { |
| 732 if (containedUri == null || containedUri.isEmpty) { |
| 733 return null; |
| 734 } |
| 735 try { |
| 736 // Force the creation of an escaped URI to deal with spaces, etc. |
| 737 return _internalResolveUri( |
| 738 containingSource, parseUriWithException(containedUri)); |
| 739 } catch (exception, stackTrace) { |
| 740 String containingFullName = |
| 741 containingSource != null ? containingSource.fullName : '<null>'; |
| 742 AnalysisEngine.instance.logger.logError( |
| 743 "Could not resolve URI ($containedUri) relative to source ($containing
FullName)", |
| 744 new CaughtException(exception, stackTrace)); |
| 745 return null; |
| 746 } |
| 747 } |
| 748 |
| 749 /** |
| 750 * Return an absolute URI that represents the given source, or `null` if a val
id URI cannot |
| 751 * be computed. |
| 752 * |
| 753 * @param source the source to get URI for |
| 754 * @return the absolute URI representing the given source |
| 755 */ |
| 756 Uri restoreUri(Source source) { |
| 757 // First see if a resolver can restore the URI. |
| 758 for (UriResolver resolver in _resolvers) { |
| 759 Uri uri = resolver.restoreAbsolute(source); |
| 760 if (uri != null) { |
| 761 // Now see if there's a package mapping. |
| 762 Uri packageMappedUri = _getPackageMapping(uri); |
| 763 if (packageMappedUri != null) { |
| 764 return packageMappedUri; |
| 765 } |
| 766 // Fall back to the resolver's computed URI. |
| 767 return uri; |
| 768 } |
| 769 } |
| 770 |
| 771 return null; |
| 772 } |
| 773 |
| 774 Uri _getPackageMapping(Uri sourceUri) { |
| 775 if (_packages == null) { |
| 776 return null; |
| 777 } |
| 778 if (sourceUri.scheme != 'file') { |
| 779 //TODO(pquitslund): verify this works for non-file URIs. |
| 780 return null; |
| 781 } |
| 782 |
| 783 Uri packageUri; |
| 784 _packages.asMap().forEach((String name, Uri uri) { |
| 785 if (packageUri == null) { |
| 786 if (utils.startsWith(sourceUri, uri)) { |
| 787 packageUri = Uri.parse( |
| 788 'package:$name/${sourceUri.path.substring(uri.path.length)}'); |
| 789 } |
| 790 } |
| 791 }); |
| 792 return packageUri; |
| 793 } |
| 794 |
| 795 /** |
| 796 * Return a source object representing the URI that results from resolving the
given (possibly |
| 797 * relative) contained URI against the URI associated with an existing source
object, or |
| 798 * `null` if the URI could not be resolved. |
| 799 * |
| 800 * @param containingSource the source containing the given URI |
| 801 * @param containedUri the (possibly relative) URI to be resolved against the
containing source |
| 802 * @return the source representing the contained URI |
| 803 * @throws AnalysisException if either the contained URI is invalid or if it c
annot be resolved |
| 804 * against the source object's URI |
| 805 */ |
| 806 Source _internalResolveUri(Source containingSource, Uri containedUri) { |
| 807 if (!containedUri.isAbsolute) { |
| 808 if (containingSource == null) { |
| 809 throw new AnalysisException( |
| 810 "Cannot resolve a relative URI without a containing source: $contain
edUri"); |
| 811 } |
| 812 containedUri = containingSource.resolveRelativeUri(containedUri); |
| 813 } |
| 814 |
| 815 Uri actualUri = containedUri; |
| 816 |
| 817 // Check .packages and update target and actual URIs as appropriate. |
| 818 if (_packages != null && containedUri.scheme == 'package') { |
| 819 Uri packageUri = |
| 820 _packages.resolve(containedUri, notFound: (Uri packageUri) => null); |
| 821 |
| 822 if (packageUri != null) { |
| 823 // Ensure scheme is set. |
| 824 if (packageUri.scheme == '') { |
| 825 packageUri = packageUri.replace(scheme: 'file'); |
| 826 } |
| 827 containedUri = packageUri; |
| 828 } |
| 829 } |
| 830 |
| 831 for (UriResolver resolver in _resolvers) { |
| 832 Source result = resolver.resolveAbsolute(containedUri, actualUri); |
| 833 if (result != null) { |
| 834 return result; |
| 835 } |
| 836 } |
| 837 |
| 838 return null; |
| 839 } |
| 840 } |
| 841 |
| 842 /** |
| 843 * The enumeration `SourceKind` defines the different kinds of sources that are
known to the |
| 844 * analysis engine. |
| 845 */ |
| 846 class SourceKind extends Enum<SourceKind> { |
| 847 /** |
| 848 * A source containing HTML. The HTML might or might not contain Dart scripts. |
| 849 */ |
| 850 static const SourceKind HTML = const SourceKind('HTML', 0); |
| 851 |
| 852 /** |
| 853 * A Dart compilation unit that is not a part of another library. Libraries mi
ght or might not |
| 854 * contain any directives, including a library directive. |
| 855 */ |
| 856 static const SourceKind LIBRARY = const SourceKind('LIBRARY', 1); |
| 857 |
| 858 /** |
| 859 * A Dart compilation unit that is part of another library. Parts contain a pa
rt-of directive. |
| 860 */ |
| 861 static const SourceKind PART = const SourceKind('PART', 2); |
| 862 |
| 863 /** |
| 864 * An unknown kind of source. Used both when it is not possible to identify th
e kind of a source |
| 865 * and also when the kind of a source is not known without performing a comput
ation and the client |
| 866 * does not want to spend the time to identify the kind. |
| 867 */ |
| 868 static const SourceKind UNKNOWN = const SourceKind('UNKNOWN', 3); |
| 869 |
| 870 static const List<SourceKind> values = const [HTML, LIBRARY, PART, UNKNOWN]; |
| 871 |
| 872 const SourceKind(String name, int ordinal) : super(name, ordinal); |
| 873 } |
| 874 |
| 875 /** |
| 876 * A source range defines an [Element]'s source coordinates relative to its [Sou
rce]. |
| 877 */ |
| 878 class SourceRange { |
| 879 /** |
| 880 * An empty [SourceRange] with offset `0` and length `0`. |
| 881 */ |
| 882 static SourceRange EMPTY = new SourceRange(0, 0); |
| 883 |
| 884 /** |
| 885 * The 0-based index of the first character of the source code for this elemen
t, relative to the |
| 886 * source buffer in which this element is contained. |
| 887 */ |
| 888 final int offset; |
| 889 |
| 890 /** |
| 891 * The number of characters of the source code for this element, relative to t
he source buffer in |
| 892 * which this element is contained. |
| 893 */ |
| 894 final int length; |
| 895 |
| 896 /** |
| 897 * Initialize a newly created source range using the given offset and the give
n length. |
| 898 * |
| 899 * @param offset the given offset |
| 900 * @param length the given length |
| 901 */ |
| 902 SourceRange(this.offset, this.length); |
| 903 |
| 904 /** |
| 905 * @return the 0-based index of the after-last character of the source code fo
r this element, |
| 906 * relative to the source buffer in which this element is contained. |
| 907 */ |
| 908 int get end => offset + length; |
| 909 |
| 910 @override |
| 911 int get hashCode => 31 * offset + length; |
| 912 |
| 913 @override |
| 914 bool operator ==(Object obj) { |
| 915 if (obj is! SourceRange) { |
| 916 return false; |
| 917 } |
| 918 SourceRange sourceRange = obj as SourceRange; |
| 919 return sourceRange.offset == offset && sourceRange.length == length; |
| 920 } |
| 921 |
| 922 /** |
| 923 * @return `true` if <code>x</code> is in [offset, offset + length) interval. |
| 924 */ |
| 925 bool contains(int x) => offset <= x && x < offset + length; |
| 926 |
| 927 /** |
| 928 * @return `true` if <code>x</code> is in (offset, offset + length) interval. |
| 929 */ |
| 930 bool containsExclusive(int x) => offset < x && x < offset + length; |
| 931 |
| 932 /** |
| 933 * @return `true` if <code>otherRange</code> covers this [SourceRange]. |
| 934 */ |
| 935 bool coveredBy(SourceRange otherRange) => otherRange.covers(this); |
| 936 |
| 937 /** |
| 938 * @return `true` if this [SourceRange] covers <code>otherRange</code>. |
| 939 */ |
| 940 bool covers(SourceRange otherRange) => |
| 941 offset <= otherRange.offset && otherRange.end <= end; |
| 942 |
| 943 /** |
| 944 * @return `true` if this [SourceRange] ends in <code>otherRange</code>. |
| 945 */ |
| 946 bool endsIn(SourceRange otherRange) { |
| 947 int thisEnd = end; |
| 948 return otherRange.contains(thisEnd); |
| 949 } |
| 950 |
| 951 /** |
| 952 * @return the expanded instance of [SourceRange], which has the same center. |
| 953 */ |
| 954 SourceRange getExpanded(int delta) => |
| 955 new SourceRange(offset - delta, delta + length + delta); |
| 956 |
| 957 /** |
| 958 * @return the instance of [SourceRange] with end moved on "delta". |
| 959 */ |
| 960 SourceRange getMoveEnd(int delta) => new SourceRange(offset, length + delta); |
| 961 |
| 962 /** |
| 963 * @return the expanded translated of [SourceRange], with moved start and the
same length. |
| 964 */ |
| 965 SourceRange getTranslated(int delta) => |
| 966 new SourceRange(offset + delta, length); |
| 967 |
| 968 /** |
| 969 * @return the minimal [SourceRange] that cover this and the given [SourceRang
e]s. |
| 970 */ |
| 971 SourceRange getUnion(SourceRange other) { |
| 972 int newOffset = math.min(offset, other.offset); |
| 973 int newEnd = math.max(offset + length, other.offset + other.length); |
| 974 return new SourceRange(newOffset, newEnd - newOffset); |
| 975 } |
| 976 |
| 977 /** |
| 978 * @return `true` if this [SourceRange] intersects with given. |
| 979 */ |
| 980 bool intersects(SourceRange other) { |
| 981 if (other == null) { |
| 982 return false; |
| 983 } |
| 984 if (end <= other.offset) { |
| 985 return false; |
| 986 } |
| 987 if (offset >= other.end) { |
| 988 return false; |
| 989 } |
| 990 return true; |
| 991 } |
| 992 |
| 993 /** |
| 994 * Return `true` if this [SourceRange] starts in the [otherRange]. |
| 995 */ |
| 996 bool startsIn(SourceRange otherRange) => otherRange.contains(offset); |
| 997 |
| 998 @override |
| 999 String toString() => '[offset=$offset, length=$length]'; |
| 1000 } |
| 1001 |
| 1002 /** |
| 1003 * The enumeration `UriKind` defines the different kinds of URI's that are known
to the |
| 1004 * analysis engine. These are used to keep track of the kind of URI associated w
ith a given source. |
| 1005 */ |
| 1006 class UriKind extends Enum<UriKind> { |
| 1007 /** |
| 1008 * A 'dart:' URI. |
| 1009 */ |
| 1010 static const UriKind DART_URI = const UriKind('DART_URI', 0, 0x64); |
| 1011 |
| 1012 /** |
| 1013 * A 'file:' URI. |
| 1014 */ |
| 1015 static const UriKind FILE_URI = const UriKind('FILE_URI', 1, 0x66); |
| 1016 |
| 1017 /** |
| 1018 * A 'package:' URI. |
| 1019 */ |
| 1020 static const UriKind PACKAGE_URI = const UriKind('PACKAGE_URI', 2, 0x70); |
| 1021 |
| 1022 static const List<UriKind> values = const [DART_URI, FILE_URI, PACKAGE_URI]; |
| 1023 |
| 1024 /** |
| 1025 * The single character encoding used to identify this kind of URI. |
| 1026 */ |
| 1027 final int encoding; |
| 1028 |
| 1029 /** |
| 1030 * Initialize a newly created URI kind to have the given encoding. |
| 1031 * |
| 1032 * @param encoding the single character encoding used to identify this kind of
URI. |
| 1033 */ |
| 1034 const UriKind(String name, int ordinal, this.encoding) : super(name, ordinal); |
| 1035 |
| 1036 /** |
| 1037 * Return the URI kind represented by the given encoding, or `null` if there i
s no kind with |
| 1038 * the given encoding. |
| 1039 * |
| 1040 * @param encoding the single character encoding used to identify the URI kind
to be returned |
| 1041 * @return the URI kind represented by the given encoding |
| 1042 */ |
| 1043 static UriKind fromEncoding(int encoding) { |
| 1044 while (true) { |
| 1045 if (encoding == 0x64) { |
| 1046 return DART_URI; |
| 1047 } else if (encoding == 0x66) { |
| 1048 return FILE_URI; |
| 1049 } else if (encoding == 0x70) { |
| 1050 return PACKAGE_URI; |
| 1051 } |
| 1052 break; |
| 1053 } |
| 1054 return null; |
| 1055 } |
| 1056 } |
| 1057 |
| 1058 /** |
| 1059 * The abstract class `UriResolver` defines the behavior of objects that are use
d to resolve |
| 1060 * URI's for a source factory. Subclasses of this class are expected to resolve
a single scheme of |
| 1061 * absolute URI. |
| 1062 */ |
| 1063 abstract class UriResolver { |
| 1064 /** |
| 1065 * Resolve the given absolute URI. Return a [Source] representing the file to
which |
| 1066 * it was resolved, whether or not the resulting source exists, or `null` if i
t could not be |
| 1067 * resolved because the URI is invalid. |
| 1068 * |
| 1069 * @param uri the URI to be resolved |
| 1070 * @param actualUri the actual uri for this source -- if `null`, the value of
[uri] will be used |
| 1071 * @return a [Source] representing the file to which given URI was resolved |
| 1072 */ |
| 1073 Source resolveAbsolute(Uri uri, [Uri actualUri]); |
| 1074 |
| 1075 /** |
| 1076 * Return an absolute URI that represents the given [source], or `null` if a |
| 1077 * valid URI cannot be computed. |
| 1078 * |
| 1079 * The computation should be based solely on [source.fullName]. |
| 1080 */ |
| 1081 Uri restoreAbsolute(Source source) => null; |
| 1082 } |
OLD | NEW |