| 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 analyzer.file_system.memory_file_system; | 5 library analyzer.file_system.memory_file_system; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:core' hide Resource; | 9 import 'dart:core' hide Resource; |
| 10 | 10 |
| 11 import 'package:analyzer/file_system/file_system.dart'; | 11 import 'package:analyzer/file_system/file_system.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; | |
| 13 import 'package:analyzer/src/generated/source_io.dart'; | 12 import 'package:analyzer/src/generated/source_io.dart'; |
| 13 import 'package:analyzer/src/source/source_resource.dart'; |
| 14 import 'package:analyzer/src/util/absolute_path.dart'; | 14 import 'package:analyzer/src/util/absolute_path.dart'; |
| 15 import 'package:path/path.dart'; | 15 import 'package:path/path.dart'; |
| 16 import 'package:watcher/watcher.dart'; | 16 import 'package:watcher/watcher.dart'; |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * An in-memory implementation of [ResourceProvider]. | 19 * An in-memory implementation of [ResourceProvider]. |
| 20 * Use `/` as a path separator. | 20 * Use `/` as a path separator. |
| 21 */ | 21 */ |
| 22 class MemoryResourceProvider implements ResourceProvider { | 22 class MemoryResourceProvider implements ResourceProvider { |
| 23 final Map<String, _MemoryResource> _pathToResource = | 23 final Map<String, _MemoryResource> _pathToResource = |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 | 272 |
| 273 @override | 273 @override |
| 274 int get modificationStamp { | 274 int get modificationStamp { |
| 275 int stamp = _provider._pathToTimestamp[path]; | 275 int stamp = _provider._pathToTimestamp[path]; |
| 276 if (stamp == null) { | 276 if (stamp == null) { |
| 277 throw new FileSystemException(path, "File does not exist"); | 277 throw new FileSystemException(path, "File does not exist"); |
| 278 } | 278 } |
| 279 return stamp; | 279 return stamp; |
| 280 } | 280 } |
| 281 | 281 |
| 282 String get _content { | |
| 283 throw new FileSystemException(path, 'File could not be read'); | |
| 284 } | |
| 285 | |
| 286 @override | 282 @override |
| 287 Source createSource([Uri uri]) { | 283 Source createSource([Uri uri]) { |
| 288 throw new FileSystemException(path, 'File could not be read'); | 284 throw new FileSystemException(path, 'File could not be read'); |
| 289 } | 285 } |
| 290 | 286 |
| 291 @override | 287 @override |
| 292 void delete() { | 288 void delete() { |
| 293 throw new FileSystemException(path, 'File could not be deleted'); | 289 throw new FileSystemException(path, 'File could not be deleted'); |
| 294 } | 290 } |
| 295 | 291 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 330 |
| 335 @override | 331 @override |
| 336 int get modificationStamp { | 332 int get modificationStamp { |
| 337 int stamp = _provider._pathToTimestamp[path]; | 333 int stamp = _provider._pathToTimestamp[path]; |
| 338 if (stamp == null) { | 334 if (stamp == null) { |
| 339 throw new FileSystemException(path, 'File "$path" does not exist.'); | 335 throw new FileSystemException(path, 'File "$path" does not exist.'); |
| 340 } | 336 } |
| 341 return stamp; | 337 return stamp; |
| 342 } | 338 } |
| 343 | 339 |
| 344 String get _content { | |
| 345 String content = _provider._pathToContent[path]; | |
| 346 if (content == null) { | |
| 347 throw new FileSystemException(path, 'File "$path" does not exist.'); | |
| 348 } | |
| 349 return content; | |
| 350 } | |
| 351 | |
| 352 @override | 340 @override |
| 353 Source createSource([Uri uri]) { | 341 Source createSource([Uri uri]) { |
| 354 if (uri == null) { | 342 uri ??= _provider.pathContext.toUri(path); |
| 355 uri = _provider.pathContext.toUri(path); | 343 return new FileSource(this, uri); |
| 356 } | |
| 357 return new _MemoryFileSource(this, uri); | |
| 358 } | 344 } |
| 359 | 345 |
| 360 @override | 346 @override |
| 361 void delete() { | 347 void delete() { |
| 362 _provider.deleteFile(path); | 348 _provider.deleteFile(path); |
| 363 } | 349 } |
| 364 | 350 |
| 365 @override | 351 @override |
| 366 bool isOrContains(String path) { | 352 bool isOrContains(String path) { |
| 367 return path == this.path; | 353 return path == this.path; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 393 @override | 379 @override |
| 394 Uri toUri() => new Uri.file(path, windows: _provider.pathContext == windows); | 380 Uri toUri() => new Uri.file(path, windows: _provider.pathContext == windows); |
| 395 | 381 |
| 396 @override | 382 @override |
| 397 void writeAsBytesSync(List<int> bytes) { | 383 void writeAsBytesSync(List<int> bytes) { |
| 398 _provider._setFileBytes(this, bytes); | 384 _provider._setFileBytes(this, bytes); |
| 399 } | 385 } |
| 400 } | 386 } |
| 401 | 387 |
| 402 /** | 388 /** |
| 403 * An in-memory implementation of [Source]. | |
| 404 */ | |
| 405 class _MemoryFileSource extends Source { | |
| 406 /** | |
| 407 * Map from encoded URI/filepath pair to a unique integer identifier. This | |
| 408 * identifier is used for equality tests and hash codes. | |
| 409 * | |
| 410 * The URI and filepath are joined into a pair by separating them with an '@' | |
| 411 * character. | |
| 412 */ | |
| 413 static final Map<String, int> _idTable = new HashMap<String, int>(); | |
| 414 | |
| 415 final _MemoryFile file; | |
| 416 | |
| 417 @override | |
| 418 final Uri uri; | |
| 419 | |
| 420 /** | |
| 421 * The unique ID associated with this [_MemoryFileSource]. | |
| 422 */ | |
| 423 final int id; | |
| 424 | |
| 425 _MemoryFileSource(_MemoryFile file, Uri uri) | |
| 426 : uri = uri, | |
| 427 file = file, | |
| 428 id = _idTable.putIfAbsent('$uri@${file.path}', () => _idTable.length); | |
| 429 | |
| 430 @override | |
| 431 TimestampedData<String> get contents { | |
| 432 return new TimestampedData<String>(modificationStamp, file._content); | |
| 433 } | |
| 434 | |
| 435 @override | |
| 436 String get encoding { | |
| 437 return uri.toString(); | |
| 438 } | |
| 439 | |
| 440 @override | |
| 441 String get fullName => file.path; | |
| 442 | |
| 443 @override | |
| 444 int get hashCode => id; | |
| 445 | |
| 446 @override | |
| 447 bool get isInSystemLibrary => uriKind == UriKind.DART_URI; | |
| 448 | |
| 449 @override | |
| 450 int get modificationStamp { | |
| 451 try { | |
| 452 return file.modificationStamp; | |
| 453 } on FileSystemException { | |
| 454 return -1; | |
| 455 } | |
| 456 } | |
| 457 | |
| 458 @override | |
| 459 String get shortName => file.shortName; | |
| 460 | |
| 461 @override | |
| 462 UriKind get uriKind { | |
| 463 String scheme = uri.scheme; | |
| 464 if (scheme == PackageUriResolver.PACKAGE_SCHEME) { | |
| 465 return UriKind.PACKAGE_URI; | |
| 466 } else if (scheme == DartUriResolver.DART_SCHEME) { | |
| 467 return UriKind.DART_URI; | |
| 468 } else if (scheme == ResourceUriResolver.FILE_SCHEME) { | |
| 469 return UriKind.FILE_URI; | |
| 470 } | |
| 471 return UriKind.FILE_URI; | |
| 472 } | |
| 473 | |
| 474 @override | |
| 475 bool operator ==(other) { | |
| 476 if (other is _MemoryFileSource) { | |
| 477 return id == other.id; | |
| 478 } else if (other is Source) { | |
| 479 return uri == other.uri; | |
| 480 } | |
| 481 return false; | |
| 482 } | |
| 483 | |
| 484 @override | |
| 485 bool exists() => file.exists; | |
| 486 | |
| 487 @override | |
| 488 String toString() => file.toString(); | |
| 489 } | |
| 490 | |
| 491 /** | |
| 492 * An in-memory implementation of [Folder]. | 389 * An in-memory implementation of [Folder]. |
| 493 */ | 390 */ |
| 494 class _MemoryFolder extends _MemoryResource implements Folder { | 391 class _MemoryFolder extends _MemoryResource implements Folder { |
| 495 _MemoryFolder(MemoryResourceProvider provider, String path) | 392 _MemoryFolder(MemoryResourceProvider provider, String path) |
| 496 : super(provider, path); | 393 : super(provider, path); |
| 497 | 394 |
| 498 @override | 395 @override |
| 499 bool get exists => _provider._pathToResource[path] is _MemoryFolder; | 396 bool get exists => _provider._pathToResource[path] is _MemoryFolder; |
| 500 | 397 |
| 501 @override | 398 @override |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 bool operator ==(other) { | 515 bool operator ==(other) { |
| 619 if (runtimeType != other.runtimeType) { | 516 if (runtimeType != other.runtimeType) { |
| 620 return false; | 517 return false; |
| 621 } | 518 } |
| 622 return path == other.path; | 519 return path == other.path; |
| 623 } | 520 } |
| 624 | 521 |
| 625 @override | 522 @override |
| 626 String toString() => path; | 523 String toString() => path; |
| 627 } | 524 } |
| OLD | NEW |