| 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:async'; | |
| 6 import 'dart:convert'; | 5 import 'dart:convert'; |
| 7 import 'dart:typed_data'; | 6 import 'dart:typed_data'; |
| 8 | 7 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 8 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/token.dart'; | 9 import 'package:analyzer/dart/ast/token.dart'; |
| 11 import 'package:analyzer/error/listener.dart'; | 10 import 'package:analyzer/error/listener.dart'; |
| 12 import 'package:analyzer/file_system/file_system.dart'; | 11 import 'package:analyzer/file_system/file_system.dart'; |
| 13 import 'package:analyzer/src/dart/analysis/defined_names.dart'; | 12 import 'package:analyzer/src/dart/analysis/defined_names.dart'; |
| 14 import 'package:analyzer/src/dart/analysis/referenced_names.dart'; | 13 import 'package:analyzer/src/dart/analysis/referenced_names.dart'; |
| 15 import 'package:analyzer/src/dart/analysis/top_level_declaration.dart'; | 14 import 'package:analyzer/src/dart/analysis/top_level_declaration.dart'; |
| (...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 * Mapping from a URI to the corresponding [FileState]. | 679 * Mapping from a URI to the corresponding [FileState]. |
| 681 */ | 680 */ |
| 682 final Map<Uri, FileState> _uriToFile = {}; | 681 final Map<Uri, FileState> _uriToFile = {}; |
| 683 | 682 |
| 684 /** | 683 /** |
| 685 * All known file paths. | 684 * All known file paths. |
| 686 */ | 685 */ |
| 687 final Set<String> knownFilePaths = new Set<String>(); | 686 final Set<String> knownFilePaths = new Set<String>(); |
| 688 | 687 |
| 689 /** | 688 /** |
| 690 * The paths of files that were added to the set of known files since the | |
| 691 * last [knownFilesSetChanges] notification. | |
| 692 */ | |
| 693 final Set<String> _addedKnownFiles = new Set<String>(); | |
| 694 | |
| 695 /** | |
| 696 * If not `null`, this delay will be awaited instead of the default one. | |
| 697 */ | |
| 698 Duration _knownFilesSetChangesDelay; | |
| 699 | |
| 700 /** | |
| 701 * The instance of timer that is scheduled to send a new update to the | |
| 702 * [knownFilesSetChanges] stream, or `null` if there are no changes to the | |
| 703 * set of known files to notify the stream about. | |
| 704 */ | |
| 705 Timer _knownFilesSetChangesTimer; | |
| 706 | |
| 707 /** | |
| 708 * Whether the [knownFilesSetChanges] stream is requested. | |
| 709 */ | |
| 710 bool _knownFilesSetChangesRequested = false; | |
| 711 | |
| 712 /** | |
| 713 * The controller for the [knownFilesSetChanges] stream. | |
| 714 */ | |
| 715 final StreamController<KnownFilesSetChange> _knownFilesSetChangesController = | |
| 716 new StreamController<KnownFilesSetChange>(); | |
| 717 | |
| 718 /** | |
| 719 * Mapping from a path to the flag whether there is a URI for the path. | 689 * Mapping from a path to the flag whether there is a URI for the path. |
| 720 */ | 690 */ |
| 721 final Map<String, bool> _hasUriForPath = {}; | 691 final Map<String, bool> _hasUriForPath = {}; |
| 722 | 692 |
| 723 /** | 693 /** |
| 724 * Mapping from a path to the corresponding [FileState]s, canonical or not. | 694 * Mapping from a path to the corresponding [FileState]s, canonical or not. |
| 725 */ | 695 */ |
| 726 final Map<String, List<FileState>> _pathToFiles = {}; | 696 final Map<String, List<FileState>> _pathToFiles = {}; |
| 727 | 697 |
| 728 /** | 698 /** |
| (...skipping 24 matching lines...) Expand all Loading... |
| 753 {this.externalSummaries}) { | 723 {this.externalSummaries}) { |
| 754 _testView = new FileSystemStateTestView(this); | 724 _testView = new FileSystemStateTestView(this); |
| 755 } | 725 } |
| 756 | 726 |
| 757 /** | 727 /** |
| 758 * Return the known files. | 728 * Return the known files. |
| 759 */ | 729 */ |
| 760 List<FileState> get knownFiles => | 730 List<FileState> get knownFiles => |
| 761 _pathToFiles.values.map((files) => files.first).toList(); | 731 _pathToFiles.values.map((files) => files.first).toList(); |
| 762 | 732 |
| 763 /** | |
| 764 * Return the [Stream] that is periodically notified about changes to the | |
| 765 * known files set. | |
| 766 */ | |
| 767 Stream<KnownFilesSetChange> get knownFilesSetChanges { | |
| 768 // If this is the first (and actually the only) time when the stream is | |
| 769 // requested, schedule the timer to send updates. | |
| 770 if (!_knownFilesSetChangesRequested) { | |
| 771 _knownFilesSetChangesRequested = true; | |
| 772 _scheduleKnownFilesSetChange(); | |
| 773 } | |
| 774 return _knownFilesSetChangesController.stream; | |
| 775 } | |
| 776 | |
| 777 @visibleForTesting | 733 @visibleForTesting |
| 778 FileSystemStateTestView get test => _testView; | 734 FileSystemStateTestView get test => _testView; |
| 779 | 735 |
| 780 /** | 736 /** |
| 781 * Return the [FileState] instance that correspond to an unresolved URI. | 737 * Return the [FileState] instance that correspond to an unresolved URI. |
| 782 */ | 738 */ |
| 783 FileState get unresolvedFile { | 739 FileState get unresolvedFile { |
| 784 if (_unresolvedFile == null) { | 740 if (_unresolvedFile == null) { |
| 785 _unresolvedFile = new FileState._(this, null, null, null); | 741 _unresolvedFile = new FileState._(this, null, null, null); |
| 786 _unresolvedFile.refresh(); | 742 _unresolvedFile.refresh(); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 _pathToCanonicalFile.clear(); | 859 _pathToCanonicalFile.clear(); |
| 904 _partToLibraries.clear(); | 860 _partToLibraries.clear(); |
| 905 } | 861 } |
| 906 | 862 |
| 907 void _addFileWithPath(String path, FileState file) { | 863 void _addFileWithPath(String path, FileState file) { |
| 908 var files = _pathToFiles[path]; | 864 var files = _pathToFiles[path]; |
| 909 if (files == null) { | 865 if (files == null) { |
| 910 knownFilePaths.add(path); | 866 knownFilePaths.add(path); |
| 911 files = <FileState>[]; | 867 files = <FileState>[]; |
| 912 _pathToFiles[path] = files; | 868 _pathToFiles[path] = files; |
| 913 // Schedule the stream update. | |
| 914 _addedKnownFiles.add(path); | |
| 915 _scheduleKnownFilesSetChange(); | |
| 916 } | 869 } |
| 917 files.add(file); | 870 files.add(file); |
| 918 } | 871 } |
| 919 | |
| 920 void _scheduleKnownFilesSetChange() { | |
| 921 // Schedule the timer only if there is a client who listens the stream. | |
| 922 if (!_knownFilesSetChangesRequested) { | |
| 923 return; | |
| 924 } | |
| 925 | |
| 926 Duration delay = _knownFilesSetChangesDelay ?? new Duration(seconds: 1); | |
| 927 _knownFilesSetChangesTimer ??= new Timer(delay, () { | |
| 928 Set<String> addedFiles = _addedKnownFiles.toSet(); | |
| 929 Set<String> removedFiles = new Set<String>(); | |
| 930 _knownFilesSetChangesController | |
| 931 .add(new KnownFilesSetChange(addedFiles, removedFiles)); | |
| 932 _addedKnownFiles.clear(); | |
| 933 _knownFilesSetChangesTimer = null; | |
| 934 }); | |
| 935 } | |
| 936 } | 872 } |
| 937 | 873 |
| 938 @visibleForTesting | 874 @visibleForTesting |
| 939 class FileSystemStateTestView { | 875 class FileSystemStateTestView { |
| 940 final FileSystemState state; | 876 final FileSystemState state; |
| 941 | 877 |
| 942 FileSystemStateTestView(this.state); | 878 FileSystemStateTestView(this.state); |
| 943 | 879 |
| 944 Set<FileState> get filesWithoutTransitiveFiles { | 880 Set<FileState> get filesWithoutTransitiveFiles { |
| 945 return state._uriToFile.values | 881 return state._uriToFile.values |
| 946 .where((f) => f._transitiveFiles == null) | 882 .where((f) => f._transitiveFiles == null) |
| 947 .toSet(); | 883 .toSet(); |
| 948 } | 884 } |
| 949 | 885 |
| 950 Set<FileState> get filesWithoutTransitiveSignature { | 886 Set<FileState> get filesWithoutTransitiveSignature { |
| 951 return state._uriToFile.values | 887 return state._uriToFile.values |
| 952 .where((f) => f._transitiveSignature == null) | 888 .where((f) => f._transitiveSignature == null) |
| 953 .toSet(); | 889 .toSet(); |
| 954 } | 890 } |
| 955 | |
| 956 void set knownFilesDelay(Duration value) { | |
| 957 state._knownFilesSetChangesDelay = value; | |
| 958 } | |
| 959 } | 891 } |
| 960 | |
| 961 /** | |
| 962 * Information about changes to the known file set. | |
| 963 */ | |
| 964 class KnownFilesSetChange { | |
| 965 final Set<String> added; | |
| 966 final Set<String> removed; | |
| 967 | |
| 968 KnownFilesSetChange(this.added, this.removed); | |
| 969 } | |
| OLD | NEW |