| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:convert'; | 5 import 'dart:convert'; |
| 6 import 'dart:typed_data'; | 6 import 'dart:typed_data'; |
| 7 | 7 |
| 8 import 'package:analyzer/dart/ast/ast.dart'; | 8 import 'package:analyzer/dart/ast/ast.dart'; |
| 9 import 'package:analyzer/dart/ast/token.dart'; | 9 import 'package:analyzer/dart/ast/token.dart'; |
| 10 import 'package:analyzer/error/listener.dart'; | 10 import 'package:analyzer/error/listener.dart'; |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 final AnalysisOptions _analysisOptions; | 541 final AnalysisOptions _analysisOptions; |
| 542 final Uint32List _salt; | 542 final Uint32List _salt; |
| 543 final String _sdkApiSignature; | 543 final String _sdkApiSignature; |
| 544 | 544 |
| 545 /** | 545 /** |
| 546 * Mapping from a URI to the corresponding [FileState]. | 546 * Mapping from a URI to the corresponding [FileState]. |
| 547 */ | 547 */ |
| 548 final Map<Uri, FileState> _uriToFile = {}; | 548 final Map<Uri, FileState> _uriToFile = {}; |
| 549 | 549 |
| 550 /** | 550 /** |
| 551 * All known file paths. |
| 552 */ |
| 553 final Set<String> knownFilePaths = new Set<String>(); |
| 554 |
| 555 /** |
| 551 * Mapping from a path to the corresponding [FileState]s, canonical or not. | 556 * Mapping from a path to the corresponding [FileState]s, canonical or not. |
| 552 */ | 557 */ |
| 553 final Map<String, List<FileState>> _pathToFiles = {}; | 558 final Map<String, List<FileState>> _pathToFiles = {}; |
| 554 | 559 |
| 555 /** | 560 /** |
| 556 * Mapping from a path to the corresponding canonical [FileState]. | 561 * Mapping from a path to the corresponding canonical [FileState]. |
| 557 */ | 562 */ |
| 558 final Map<String, FileState> _pathToCanonicalFile = {}; | 563 final Map<String, FileState> _pathToCanonicalFile = {}; |
| 559 | 564 |
| 560 /** | 565 /** |
| 561 * Mapping from a part to the libraries it is a part of. | 566 * Mapping from a part to the libraries it is a part of. |
| 562 */ | 567 */ |
| 563 final Map<FileState, List<FileState>> _partToLibraries = {}; | 568 final Map<FileState, List<FileState>> _partToLibraries = {}; |
| 564 | 569 |
| 565 FileSystemStateTestView _testView; | 570 FileSystemStateTestView _testView; |
| 566 | 571 |
| 567 FileSystemState( | 572 FileSystemState( |
| 568 this._logger, | 573 this._logger, |
| 569 this._byteStore, | 574 this._byteStore, |
| 570 this._contentOverlay, | 575 this._contentOverlay, |
| 571 this._resourceProvider, | 576 this._resourceProvider, |
| 572 this._sourceFactory, | 577 this._sourceFactory, |
| 573 this._analysisOptions, | 578 this._analysisOptions, |
| 574 this._salt, | 579 this._salt, |
| 575 this._sdkApiSignature) { | 580 this._sdkApiSignature) { |
| 576 _testView = new FileSystemStateTestView(this); | 581 _testView = new FileSystemStateTestView(this); |
| 577 } | 582 } |
| 578 | 583 |
| 579 /** | 584 /** |
| 580 * Return the set of known file paths. | |
| 581 */ | |
| 582 Set<String> get knownFilePaths => _pathToFiles.keys.toSet(); | |
| 583 | |
| 584 /** | |
| 585 * Return the known files. | 585 * Return the known files. |
| 586 */ | 586 */ |
| 587 Iterable<FileState> get knownFiles => | 587 Iterable<FileState> get knownFiles => |
| 588 _pathToFiles.values.map((files) => files.first); | 588 _pathToFiles.values.map((files) => files.first); |
| 589 | 589 |
| 590 @visibleForTesting | 590 @visibleForTesting |
| 591 FileSystemStateTestView get test => _testView; | 591 FileSystemStateTestView get test => _testView; |
| 592 | 592 |
| 593 /** | 593 /** |
| 594 * Return the canonical [FileState] for the given absolute [path]. The | 594 * Return the canonical [FileState] for the given absolute [path]. The |
| (...skipping 12 matching lines...) Expand all Loading... |
| 607 file = _uriToFile[uri]; | 607 file = _uriToFile[uri]; |
| 608 // If we have a file, call it the canonical one and return it. | 608 // If we have a file, call it the canonical one and return it. |
| 609 if (file != null) { | 609 if (file != null) { |
| 610 _pathToCanonicalFile[path] = file; | 610 _pathToCanonicalFile[path] = file; |
| 611 return file; | 611 return file; |
| 612 } | 612 } |
| 613 // Create a new file. | 613 // Create a new file. |
| 614 FileSource uriSource = new FileSource(resource, uri); | 614 FileSource uriSource = new FileSource(resource, uri); |
| 615 file = new FileState._(this, path, uri, uriSource); | 615 file = new FileState._(this, path, uri, uriSource); |
| 616 _uriToFile[uri] = file; | 616 _uriToFile[uri] = file; |
| 617 _pathToFiles.putIfAbsent(path, () => <FileState>[]).add(file); | 617 _addFileWithPath(path, file); |
| 618 _pathToCanonicalFile[path] = file; | 618 _pathToCanonicalFile[path] = file; |
| 619 file.refresh(); | 619 file.refresh(); |
| 620 } | 620 } |
| 621 return file; | 621 return file; |
| 622 } | 622 } |
| 623 | 623 |
| 624 /** | 624 /** |
| 625 * Return the [FileState] for the given absolute [uri]. May return `null` if | 625 * Return the [FileState] for the given absolute [uri]. May return `null` if |
| 626 * the [uri] is invalid, e.g. a `package:` URI without a package name. The | 626 * the [uri] is invalid, e.g. a `package:` URI without a package name. The |
| 627 * returned file has the last known state since if was last refreshed. | 627 * returned file has the last known state since if was last refreshed. |
| 628 */ | 628 */ |
| 629 FileState getFileForUri(Uri uri) { | 629 FileState getFileForUri(Uri uri) { |
| 630 FileState file = _uriToFile[uri]; | 630 FileState file = _uriToFile[uri]; |
| 631 if (file == null) { | 631 if (file == null) { |
| 632 Source uriSource = _sourceFactory.resolveUri(null, uri.toString()); | 632 Source uriSource = _sourceFactory.resolveUri(null, uri.toString()); |
| 633 // If the URI is invalid, for example package:/test/d.dart (note the | 633 // If the URI is invalid, for example package:/test/d.dart (note the |
| 634 // leading '/'), then `null` is returned. We should ignore this URI. | 634 // leading '/'), then `null` is returned. We should ignore this URI. |
| 635 if (uriSource == null) { | 635 if (uriSource == null) { |
| 636 return null; | 636 return null; |
| 637 } | 637 } |
| 638 String path = uriSource.fullName; | 638 String path = uriSource.fullName; |
| 639 File resource = _resourceProvider.getFile(path); | 639 File resource = _resourceProvider.getFile(path); |
| 640 FileSource source = new FileSource(resource, uri); | 640 FileSource source = new FileSource(resource, uri); |
| 641 file = new FileState._(this, path, uri, source); | 641 file = new FileState._(this, path, uri, source); |
| 642 _uriToFile[uri] = file; | 642 _uriToFile[uri] = file; |
| 643 _pathToFiles.putIfAbsent(path, () => <FileState>[]).add(file); | 643 _addFileWithPath(path, file); |
| 644 file.refresh(); | 644 file.refresh(); |
| 645 } | 645 } |
| 646 return file; | 646 return file; |
| 647 } | 647 } |
| 648 | 648 |
| 649 /** | 649 /** |
| 650 * Return the list of all [FileState]s corresponding to the given [path]. The | 650 * Return the list of all [FileState]s corresponding to the given [path]. The |
| 651 * list has at least one item, and the first item is the canonical file. | 651 * list has at least one item, and the first item is the canonical file. |
| 652 */ | 652 */ |
| 653 List<FileState> getFilesForPath(String path) { | 653 List<FileState> getFilesForPath(String path) { |
| 654 FileState canonicalFile = getFileForPath(path); | 654 FileState canonicalFile = getFileForPath(path); |
| 655 List<FileState> allFiles = _pathToFiles[path].toList(); | 655 List<FileState> allFiles = _pathToFiles[path].toList(); |
| 656 if (allFiles.length == 1) { | 656 if (allFiles.length == 1) { |
| 657 return allFiles; | 657 return allFiles; |
| 658 } | 658 } |
| 659 return allFiles | 659 return allFiles |
| 660 ..remove(canonicalFile) | 660 ..remove(canonicalFile) |
| 661 ..insert(0, canonicalFile); | 661 ..insert(0, canonicalFile); |
| 662 } | 662 } |
| 663 |
| 664 void _addFileWithPath(String path, FileState file) { |
| 665 var files = _pathToFiles[path]; |
| 666 if (files == null) { |
| 667 knownFilePaths.add(path); |
| 668 files = <FileState>[]; |
| 669 _pathToFiles[path] = files; |
| 670 } |
| 671 files.add(file); |
| 672 } |
| 663 } | 673 } |
| 664 | 674 |
| 665 @visibleForTesting | 675 @visibleForTesting |
| 666 class FileSystemStateTestView { | 676 class FileSystemStateTestView { |
| 667 final FileSystemState state; | 677 final FileSystemState state; |
| 668 | 678 |
| 669 FileSystemStateTestView(this.state); | 679 FileSystemStateTestView(this.state); |
| 670 | 680 |
| 671 Set<FileState> get filesWithoutTransitiveFiles { | 681 Set<FileState> get filesWithoutTransitiveFiles { |
| 672 return state._uriToFile.values | 682 return state._uriToFile.values |
| 673 .where((f) => f._transitiveFiles == null) | 683 .where((f) => f._transitiveFiles == null) |
| 674 .toSet(); | 684 .toSet(); |
| 675 } | 685 } |
| 676 | 686 |
| 677 Set<FileState> get filesWithoutTransitiveSignature { | 687 Set<FileState> get filesWithoutTransitiveSignature { |
| 678 return state._uriToFile.values | 688 return state._uriToFile.values |
| 679 .where((f) => f._transitiveSignature == null) | 689 .where((f) => f._transitiveSignature == null) |
| 680 .toSet(); | 690 .toSet(); |
| 681 } | 691 } |
| 682 } | 692 } |
| OLD | NEW |