mirror of
https://github.com/dart-lang/sdk
synced 2024-10-02 00:40:13 +00:00
Use patterns and null safety to omprove request routing code style.
Change-Id: I2747e7cfd982430f0d1c72a29b58945722533638 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/346905 Reviewed-by: Phil Quitslund <pquitslund@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
parent
f98574ee10
commit
b9c922998c
|
@ -11,7 +11,7 @@ import 'package:analyzer/dart/ast/visitor.dart';
|
|||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'package:analyzer/src/dart/ast/utilities.dart';
|
||||
import 'package:analyzer/src/utilities/extensions/map.dart';
|
||||
import 'package:analyzer/src/utilities/extensions/collection.dart';
|
||||
import 'package:analyzer_plugin/utilities/assist/assist.dart';
|
||||
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
|
||||
import 'package:analyzer_plugin/utilities/range_factory.dart';
|
||||
|
|
|
@ -1159,8 +1159,7 @@ class AnalysisDriver {
|
|||
_discoverDartCore();
|
||||
_discoverLibraries();
|
||||
|
||||
if (_resolveForCompletionRequests.isNotEmpty) {
|
||||
final request = _resolveForCompletionRequests.removeLast();
|
||||
if (_resolveForCompletionRequests.removeLastOrNull() case var request?) {
|
||||
try {
|
||||
final result = await _resolveForCompletion(request);
|
||||
request.completer.complete(result);
|
||||
|
@ -1173,55 +1172,45 @@ class AnalysisDriver {
|
|||
}
|
||||
|
||||
// Analyze a requested file.
|
||||
if (_requestedFiles.isNotEmpty) {
|
||||
final path = _requestedFiles.keys.first;
|
||||
if (_requestedFiles.firstKey case var path?) {
|
||||
await _analyzeFile(path);
|
||||
return;
|
||||
}
|
||||
|
||||
// Analyze a requested library.
|
||||
if (_requestedLibraries.isNotEmpty) {
|
||||
final library = _requestedLibraries.keys.first;
|
||||
if (_requestedLibraries.firstKey case var library?) {
|
||||
await _getResolvedLibrary(library);
|
||||
return;
|
||||
}
|
||||
|
||||
// Process an error request.
|
||||
if (_errorsRequestedFiles.isNotEmpty) {
|
||||
final path = _errorsRequestedFiles.keys.first;
|
||||
if (_errorsRequestedFiles.firstKey case var path?) {
|
||||
await _getErrors(path);
|
||||
return;
|
||||
}
|
||||
|
||||
// Process an index request.
|
||||
if (_indexRequestedFiles.isNotEmpty) {
|
||||
final path = _indexRequestedFiles.keys.first;
|
||||
if (_indexRequestedFiles.firstKey case var path?) {
|
||||
await _getIndex(path);
|
||||
return;
|
||||
}
|
||||
|
||||
// Process a unit element request.
|
||||
if (_unitElementRequestedFiles.isNotEmpty) {
|
||||
String path = _unitElementRequestedFiles.keys.first;
|
||||
var completers = _unitElementRequestedFiles.remove(path)!;
|
||||
final result = await _computeUnitElement(path);
|
||||
for (var completer in completers) {
|
||||
completer.complete(result);
|
||||
}
|
||||
if (_unitElementRequestedFiles.firstKey case var path?) {
|
||||
await _getUnitElement(path);
|
||||
return;
|
||||
}
|
||||
|
||||
// Discover available files.
|
||||
if (_discoverAvailableFilesTask != null &&
|
||||
!_discoverAvailableFilesTask!.isCompleted) {
|
||||
_discoverAvailableFilesTask!.perform();
|
||||
return;
|
||||
if (_discoverAvailableFilesTask case var task?) {
|
||||
if (!task.isCompleted) {
|
||||
task.perform();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Compute files defining a name.
|
||||
if (_definingClassMemberNameTasks.isNotEmpty) {
|
||||
_FilesDefiningClassMemberNameTask task =
|
||||
_definingClassMemberNameTasks.first;
|
||||
if (_definingClassMemberNameTasks.firstOrNull case var task?) {
|
||||
bool isDone = task.perform();
|
||||
if (isDone) {
|
||||
_definingClassMemberNameTasks.remove(task);
|
||||
|
@ -1230,8 +1219,7 @@ class AnalysisDriver {
|
|||
}
|
||||
|
||||
// Compute files referencing a name.
|
||||
if (_referencingNameTasks.isNotEmpty) {
|
||||
_FilesReferencingNameTask task = _referencingNameTasks.first;
|
||||
if (_referencingNameTasks.firstOrNull case var task?) {
|
||||
bool isDone = task.perform();
|
||||
if (isDone) {
|
||||
_referencingNameTasks.remove(task);
|
||||
|
@ -1240,18 +1228,15 @@ class AnalysisDriver {
|
|||
}
|
||||
|
||||
// Analyze a priority file.
|
||||
if (_priorityFiles.isNotEmpty) {
|
||||
for (String path in _priorityFiles) {
|
||||
if (_fileTracker.isFilePending(path)) {
|
||||
await _analyzeFile(path);
|
||||
return;
|
||||
}
|
||||
for (var path in _priorityFiles) {
|
||||
if (_fileTracker.isFilePending(path)) {
|
||||
await _analyzeFile(path);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Analyze a general file.
|
||||
if (_fileTracker.hasPendingFiles) {
|
||||
String path = _fileTracker.anyPendingFile;
|
||||
if (_fileTracker.anyPendingFile case var path?) {
|
||||
await _produceErrors(path);
|
||||
return;
|
||||
}
|
||||
|
@ -1520,28 +1505,6 @@ class AnalysisDriver {
|
|||
_resolvedLibraryCache.clear();
|
||||
}
|
||||
|
||||
Future<UnitElementResult> _computeUnitElement(String path) async {
|
||||
FileState file = _fsState.getFileForPath(path);
|
||||
|
||||
// Prepare the library - the file itself, or the known library.
|
||||
final kind = file.kind;
|
||||
final library = kind.library ?? kind.asLibrary;
|
||||
|
||||
return _logger.runAsync('Compute unit element for $path', () async {
|
||||
_logger.writeln('Work in $name');
|
||||
await libraryContext.load(
|
||||
targetLibrary: library,
|
||||
performance: OperationPerformanceImpl('<root>'),
|
||||
);
|
||||
var element = libraryContext.computeUnitElement(library, file);
|
||||
return UnitElementResultImpl(
|
||||
session: currentSession,
|
||||
fileState: file,
|
||||
element: element,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
ErrorsResultImpl _createErrorsResultFromBytes(
|
||||
FileState file,
|
||||
LibraryFileKind library,
|
||||
|
@ -1793,6 +1756,28 @@ class AnalysisDriver {
|
|||
return signature.toHex();
|
||||
}
|
||||
|
||||
Future<void> _getUnitElement(String path) async {
|
||||
FileState file = _fsState.getFileForPath(path);
|
||||
|
||||
// Prepare the library - the file itself, or the known library.
|
||||
final kind = file.kind;
|
||||
final library = kind.library ?? kind.asLibrary;
|
||||
|
||||
await libraryContext.load(
|
||||
targetLibrary: library,
|
||||
performance: OperationPerformanceImpl('<root>'),
|
||||
);
|
||||
|
||||
var element = libraryContext.computeUnitElement(library, file);
|
||||
var result = UnitElementResultImpl(
|
||||
session: currentSession,
|
||||
fileState: file,
|
||||
element: element,
|
||||
);
|
||||
|
||||
_unitElementRequestedFiles.completeAll(path, result);
|
||||
}
|
||||
|
||||
bool _hasLibraryByUri(String uriStr) {
|
||||
var uri = uriCache.parse(uriStr);
|
||||
var fileOr = _fsState.getFileForUri(uri);
|
||||
|
|
|
@ -49,19 +49,12 @@ class FileTracker {
|
|||
|
||||
FileTracker(this._logger, this._fsState, this._fileContentStrategy);
|
||||
|
||||
/// Returns the path to exactly one that needs analysis. Throws a
|
||||
/// [StateError] if no files need analysis.
|
||||
String get anyPendingFile {
|
||||
if (_pendingChangedFiles.isNotEmpty) {
|
||||
return _pendingChangedFiles.first;
|
||||
}
|
||||
if (_pendingImportFiles.isNotEmpty) {
|
||||
return _pendingImportFiles.first;
|
||||
}
|
||||
if (_pendingErrorFiles.isNotEmpty) {
|
||||
return _pendingErrorFiles.first;
|
||||
}
|
||||
return _pendingFiles.first;
|
||||
/// Returns the path to exactly one that needs analysis.
|
||||
String? get anyPendingFile {
|
||||
return _pendingChangedFiles.firstOrNull ??
|
||||
_pendingImportFiles.firstOrNull ??
|
||||
_pendingErrorFiles.firstOrNull ??
|
||||
_pendingFiles.firstOrNull;
|
||||
}
|
||||
|
||||
/// Returns a boolean indicating whether there are any files that have
|
||||
|
|
|
@ -92,6 +92,13 @@ extension ListExtension<E> on List<E> {
|
|||
}
|
||||
}
|
||||
|
||||
E? removeLastOrNull() {
|
||||
if (isNotEmpty) {
|
||||
return removeLast();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Returns a new list with all elements of the target, arranged such that
|
||||
/// all elements for which the [predicate] specified returns `true` come
|
||||
/// before those for which the [predicate] returns `false`. The partitioning
|
||||
|
@ -110,6 +117,18 @@ extension ListQueueExtension<T> on ListQueue<T> {
|
|||
}
|
||||
}
|
||||
|
||||
extension MapExtension<K, V> on Map<K, V> {
|
||||
K? get firstKey {
|
||||
return keys.firstOrNull;
|
||||
}
|
||||
}
|
||||
|
||||
extension MapOfListValuesExtension<K, V> on Map<K, List<V>> {
|
||||
void add(K key, V value) {
|
||||
(this[key] ??= []).add(value);
|
||||
}
|
||||
}
|
||||
|
||||
extension SetExtension<E> on Set<E> {
|
||||
void addIfNotNull(E? element) {
|
||||
if (element != null) {
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// TODO(pq): move to collections
|
||||
extension MapExtension<K, V> on Map<K, List<V>> {
|
||||
void add(K key, V value) {
|
||||
(this[key] ??= []).add(value);
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@ main() {
|
|||
defineReflectiveTests(IterableIterableExtensionTest);
|
||||
defineReflectiveTests(IterableMapEntryExtensionTest);
|
||||
defineReflectiveTests(ListExtensionTest);
|
||||
defineReflectiveTests(MapExtensionTest);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -100,6 +101,12 @@ class ListExtensionTest {
|
|||
expect(elements.nextOrNull(3), null);
|
||||
}
|
||||
|
||||
test_removeLastOrNull() {
|
||||
expect([0, 1, 2].removeLastOrNull(), 2);
|
||||
expect([0].removeLastOrNull(), 0);
|
||||
expect(<int>[].removeLastOrNull(), isNull);
|
||||
}
|
||||
|
||||
test_stablePartition() {
|
||||
expect(
|
||||
[0, 1, 2, 3, 4, 5].stablePartition((e) => e.isEven),
|
||||
|
@ -119,6 +126,14 @@ class ListExtensionTest {
|
|||
}
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class MapExtensionTest {
|
||||
test_firstKey() {
|
||||
expect({0: 1, 2: 3}.firstKey, 0);
|
||||
expect(<int, int>{}.firstKey, isNull);
|
||||
}
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class SetExtensionTest {
|
||||
test_addIfNotNull_notNull() {
|
||||
|
|
Loading…
Reference in a new issue