Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a2d1136

Browse files
scheglovCommit Bot
authored andcommitted
Simplify 'Import Library' support in Cider.
Change-Id: Ife204362816bcddaaa0757e07b86e0393bebbfbe Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/223260 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Keerti Parthasarathy <keertip@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 5aa240a commit a2d1136

File tree

4 files changed

+18
-103
lines changed

4 files changed

+18
-103
lines changed

pkg/analysis_server/lib/src/cider/fixes.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ class CiderFixesComputer {
7272
String name) async {
7373
var result = <LibraryElement, List<Element>>{};
7474
var files = _fileResolver.getFilesWithTopLevelDeclarations(name);
75-
for (var fileWithKind in files) {
76-
var file = fileWithKind.file;
75+
for (var file in files) {
7776
if (file.partOfLibrary == null) {
7877
var libraryElement = _fileResolver.getLibraryByUri(
7978
uriStr: file.uriStr,

pkg/analyzer/lib/src/dart/micro/library_graph.dart

Lines changed: 15 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,9 @@ void computeLibraryCycle(Uint32List linkedSalt, FileState file) {
4343
libraryWalker.walk(libraryWalker.getNode(file));
4444
}
4545

46-
class CiderUnitTopLevelDeclarations {
47-
final List<String> extensionNames;
48-
final List<String> functionNames;
49-
final List<String> typeNames;
50-
final List<String> variableNames;
51-
52-
CiderUnitTopLevelDeclarations({
53-
required this.extensionNames,
54-
required this.functionNames,
55-
required this.typeNames,
56-
required this.variableNames,
57-
});
58-
59-
factory CiderUnitTopLevelDeclarations.read(SummaryDataReader reader) {
60-
return CiderUnitTopLevelDeclarations(
61-
extensionNames: reader.readStringUtf8List(),
62-
functionNames: reader.readStringUtf8List(),
63-
typeNames: reader.readStringUtf8List(),
64-
variableNames: reader.readStringUtf8List(),
65-
);
66-
}
67-
68-
void write(BufferedSink sink) {
69-
sink.writeStringUtf8Iterable(extensionNames);
70-
sink.writeStringUtf8Iterable(functionNames);
71-
sink.writeStringUtf8Iterable(typeNames);
72-
sink.writeStringUtf8Iterable(variableNames);
73-
}
74-
}
75-
7646
class CiderUnlinkedUnit {
7747
/// Top-level declarations of the unit.
78-
final CiderUnitTopLevelDeclarations topLevelDeclarations;
48+
final Set<String> topLevelDeclarations;
7949

8050
/// Unlinked summary of the compilation unit.
8151
final UnlinkedUnit unit;
@@ -93,7 +63,7 @@ class CiderUnlinkedUnit {
9363

9464
factory CiderUnlinkedUnit.read(SummaryDataReader reader) {
9565
return CiderUnlinkedUnit(
96-
topLevelDeclarations: CiderUnitTopLevelDeclarations.read(reader),
66+
topLevelDeclarations: reader.readStringUtf8Set(),
9767
unit: UnlinkedUnit.read(reader),
9868
);
9969
}
@@ -106,7 +76,7 @@ class CiderUnlinkedUnit {
10676
}
10777

10878
void write(BufferedSink sink) {
109-
topLevelDeclarations.write(sink);
79+
sink.writeStringUtf8Iterable(topLevelDeclarations);
11080
unit.write(sink);
11181
}
11282
}
@@ -386,40 +356,12 @@ class FileSystemState {
386356
}
387357

388358
/// Return files that have a top-level declaration with the [name].
389-
List<FileWithTopLevelDeclaration> getFilesWithTopLevelDeclarations(
390-
String name,
391-
) {
392-
var result = <FileWithTopLevelDeclaration>[];
393-
359+
List<FileState> getFilesWithTopLevelDeclarations(String name) {
360+
var result = <FileState>[];
394361
for (var file in _pathToFile.values) {
395-
void addDeclaration(
396-
List<String> names,
397-
FileTopLevelDeclarationKind kind,
398-
) {
399-
if (names.contains(name)) {
400-
result.add(
401-
FileWithTopLevelDeclaration(file: file, kind: kind),
402-
);
403-
}
362+
if (file._unlinked.unlinked.topLevelDeclarations.contains(name)) {
363+
result.add(file);
404364
}
405-
406-
var topLevelDeclarations = file._unlinked.unlinked.topLevelDeclarations;
407-
addDeclaration(
408-
topLevelDeclarations.extensionNames,
409-
FileTopLevelDeclarationKind.extension,
410-
);
411-
addDeclaration(
412-
topLevelDeclarations.functionNames,
413-
FileTopLevelDeclarationKind.function,
414-
);
415-
addDeclaration(
416-
topLevelDeclarations.typeNames,
417-
FileTopLevelDeclarationKind.type,
418-
);
419-
addDeclaration(
420-
topLevelDeclarations.variableNames,
421-
FileTopLevelDeclarationKind.variable,
422-
);
423365
}
424366
return result;
425367
}
@@ -527,20 +469,6 @@ class FileSystemStateTimers {
527469
}
528470
}
529471

530-
/// The kind in [FileWithTopLevelDeclaration].
531-
enum FileTopLevelDeclarationKind { extension, function, type, variable }
532-
533-
/// The data structure for top-level declarations response.
534-
class FileWithTopLevelDeclaration {
535-
final FileState file;
536-
final FileTopLevelDeclarationKind kind;
537-
538-
FileWithTopLevelDeclaration({
539-
required this.file,
540-
required this.kind,
541-
});
542-
}
543-
544472
/// Information about libraries that reference each other, so form a cycle.
545473
class LibraryCycle {
546474
/// The libraries that belong to this cycle.
@@ -1004,27 +932,24 @@ class _FileStateUnlinked {
1004932
);
1005933
}
1006934

1007-
var declaredExtensions = <String>[];
1008-
var declaredFunctions = <String>[];
1009-
var declaredTypes = <String>[];
1010-
var declaredVariables = <String>[];
935+
var topLevelDeclarations = <String>{};
1011936
for (var declaration in unit.declarations) {
1012937
if (declaration is ClassDeclaration) {
1013-
declaredTypes.add(declaration.name.name);
938+
topLevelDeclarations.add(declaration.name.name);
1014939
} else if (declaration is EnumDeclaration) {
1015-
declaredTypes.add(declaration.name.name);
940+
topLevelDeclarations.add(declaration.name.name);
1016941
} else if (declaration is ExtensionDeclaration) {
1017942
var name = declaration.name;
1018943
if (name != null) {
1019-
declaredExtensions.add(name.name);
944+
topLevelDeclarations.add(name.name);
1020945
}
1021946
} else if (declaration is FunctionDeclaration) {
1022-
declaredFunctions.add(declaration.name.name);
947+
topLevelDeclarations.add(declaration.name.name);
1023948
} else if (declaration is MixinDeclaration) {
1024-
declaredTypes.add(declaration.name.name);
949+
topLevelDeclarations.add(declaration.name.name);
1025950
} else if (declaration is TopLevelVariableDeclaration) {
1026951
for (var variable in declaration.variables.variables) {
1027-
declaredVariables.add(variable.name.name);
952+
topLevelDeclarations.add(variable.name.name);
1028953
}
1029954
}
1030955
}
@@ -1044,12 +969,7 @@ class _FileStateUnlinked {
1044969

1045970
return CiderUnlinkedUnit(
1046971
unit: unlinkedUnit,
1047-
topLevelDeclarations: CiderUnitTopLevelDeclarations(
1048-
extensionNames: declaredExtensions,
1049-
functionNames: declaredFunctions,
1050-
typeNames: declaredTypes,
1051-
variableNames: declaredVariables,
1052-
),
972+
topLevelDeclarations: topLevelDeclarations,
1053973
);
1054974
}
1055975

pkg/analyzer/lib/src/dart/micro/resolve_file.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,7 @@ class FileResolver {
310310
}
311311

312312
/// Return files that have a top-level declaration with the [name].
313-
List<FileWithTopLevelDeclaration> getFilesWithTopLevelDeclarations(
314-
String name,
315-
) {
313+
List<FileState> getFilesWithTopLevelDeclarations(String name) {
316314
final fsState = this.fsState;
317315
if (fsState == null) {
318316
return const [];

pkg/analyzer/test/src/dart/micro/simple_file_resolver_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:analyzer/source/line_info.dart';
77
import 'package:analyzer/src/dart/ast/utilities.dart';
88
import 'package:analyzer/src/dart/error/syntactic_errors.dart';
99
import 'package:analyzer/src/dart/micro/cider_byte_store.dart';
10-
import 'package:analyzer/src/dart/micro/library_graph.dart';
1110
import 'package:analyzer/src/dart/micro/resolve_file.dart';
1211
import 'package:analyzer/src/dart/micro/utils.dart';
1312
import 'package:analyzer/src/error/codes.dart';
@@ -768,8 +767,7 @@ var b = 1 + 2;
768767
var files = fileResolver.getFilesWithTopLevelDeclarations('a');
769768
expect(files, hasLength(1));
770769
var file = files.single;
771-
expect(file.file.path, result.path);
772-
expect(file.kind, FileTopLevelDeclarationKind.variable);
770+
expect(file.path, result.path);
773771
}
774772

775773
// Ask to check that it works when parsed.

0 commit comments

Comments
 (0)