Skip to content

Commit 7e1721d

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate ImportLibrary.
Change-Id: I127e0f0b31e6b0d05727ae518b54971287e37008 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398950 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 8e8a0e3 commit 7e1721d

File tree

4 files changed

+54
-46
lines changed

4 files changed

+54
-46
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ lib/src/services/completion/dart/completion_manager.dart
3131
lib/src/services/completion/dart/declaration_helper.dart
3232
lib/src/services/completion/dart/identifier_helper.dart
3333
lib/src/services/completion/dart/visibility_tracker.dart
34-
lib/src/services/correction/dart/import_library.dart
3534
lib/src/services/correction/namespace.dart
3635
lib/src/services/correction/util.dart
3736
lib/src/services/flutter/class_description.dart

pkg/analysis_server/lib/src/services/correction/dart/import_library.dart

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ import 'package:analysis_server/src/utilities/extensions/element.dart';
1010
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
1111
import 'package:analyzer/dart/ast/ast.dart';
1212
import 'package:analyzer/dart/ast/token.dart';
13-
import 'package:analyzer/dart/element/element.dart';
13+
import 'package:analyzer/dart/element/element2.dart';
1414
import 'package:analyzer/dart/element/type.dart';
1515
import 'package:analyzer/dart/element/type_system.dart';
1616
import 'package:analyzer/source/source_range.dart';
1717
import 'package:analyzer/src/dart/ast/extensions.dart';
1818
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
1919
import 'package:analyzer/src/dart/resolver/applicable_extensions.dart';
20-
import 'package:analyzer/src/utilities/extensions/element.dart';
2120
import 'package:analyzer_plugin/src/utilities/change_builder/change_builder_dart.dart';
2221
import 'package:analyzer_plugin/src/utilities/library.dart';
2322
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
@@ -71,7 +70,7 @@ class ImportLibrary extends MultiCorrectionProducer {
7170
}
7271

7372
List<ResolvedCorrectionProducer> _importExtensionInLibrary(
74-
LibraryElement libraryToImport,
73+
LibraryElement2 libraryToImport,
7574
DartType targetType,
7675
Name memberName,
7776
) {
@@ -80,19 +79,16 @@ class ImportLibrary extends MultiCorrectionProducer {
8079
// additional analysis.
8180
var foundImport = false;
8281
var producers = <ResolvedCorrectionProducer>[];
83-
for (var import in libraryElement.definingCompilationUnit.libraryImports) {
82+
for (var import in unitResult.libraryFragment.libraryImports2) {
8483
// prepare element
85-
var importedLibrary = import.importedLibrary;
84+
var importedLibrary = import.importedLibrary2;
8685
if (importedLibrary == null || importedLibrary != libraryToImport) {
8786
continue;
8887
}
8988
foundImport = true;
90-
var instantiatedExtensions = importedLibrary.asElement2.exportedExtensions
89+
var instantiatedExtensions = importedLibrary.exportedExtensions
9190
.havingMemberWithBaseName(memberName)
92-
.applicableTo(
93-
targetLibrary: libraryElement2,
94-
targetType: targetType,
95-
);
91+
.applicableTo(targetLibrary: libraryElement2, targetType: targetType);
9692
for (var instantiatedExtension in instantiatedExtensions) {
9793
// If the import has a combinator that needs to be updated, then offer
9894
// to update it.
@@ -102,7 +98,7 @@ class ImportLibrary extends MultiCorrectionProducer {
10298
if (combinator is HideElementCombinator) {
10399
producers.add(
104100
_ImportLibraryCombinator(
105-
libraryToImport.source.uri.toString(),
101+
libraryToImport.uri.toString(),
106102
combinator,
107103
instantiatedExtension.extension.name3!,
108104
context: context,
@@ -111,7 +107,7 @@ class ImportLibrary extends MultiCorrectionProducer {
111107
} else if (combinator is ShowElementCombinator) {
112108
producers.add(
113109
_ImportLibraryCombinator(
114-
libraryToImport.source.uri.toString(),
110+
libraryToImport.uri.toString(),
115111
combinator,
116112
instantiatedExtension.extension.name3!,
117113
context: context,
@@ -224,19 +220,19 @@ class ImportLibrary extends MultiCorrectionProducer {
224220
var producers = <ResolvedCorrectionProducer>[];
225221
// Maybe there is an existing import, but it is with prefix and we don't use
226222
// this prefix.
227-
var alreadyImportedWithPrefix = <LibraryElement>{};
228-
for (var import in libraryElement.definingCompilationUnit.libraryImports) {
223+
var alreadyImportedWithPrefix = <LibraryElement2>{};
224+
for (var import in unitResult.libraryFragment.libraryImports2) {
229225
// Prepare the element.
230-
var libraryElement = import.importedLibrary;
226+
var libraryElement = import.importedLibrary2;
231227
if (libraryElement == null) {
232228
continue;
233229
}
234-
var element = getExportedElement(libraryElement, name);
230+
var element = getExportedElement2(libraryElement, name);
235231
if (element == null) {
236232
continue;
237233
}
238-
if (element is PropertyAccessorElement) {
239-
element = element.variable2;
234+
if (element is PropertyAccessorElement2) {
235+
element = element.variable3;
240236
if (element == null) {
241237
continue;
242238
}
@@ -245,16 +241,12 @@ class ImportLibrary extends MultiCorrectionProducer {
245241
continue;
246242
}
247243
_ImportLibraryCombinator? combinatorProducer;
248-
var importPrefix = import.prefix?.element;
244+
var importPrefix = import.prefix2?.element;
249245
// Maybe update a "show"/"hide" directive.
250246
var combinators = import.combinators;
251247
if (combinators.length == 1) {
252248
// Prepare library name - unit name or 'dart:name' for SDK library.
253-
var libraryName =
254-
libraryElement.definingCompilationUnit.source.uri.toString();
255-
if (libraryElement.isInSdk) {
256-
libraryName = libraryElement.source.shortName;
257-
}
249+
var libraryName = libraryElement.uri.toString();
258250
var combinator = combinators.first;
259251
if (combinator is HideElementCombinator) {
260252
// Don't add this library again.
@@ -295,11 +287,11 @@ class ImportLibrary extends MultiCorrectionProducer {
295287
}
296288
}
297289
// Find new top-level declarations.
298-
var librariesWithElements = await getTopLevelDeclarations(name);
290+
var librariesWithElements = await getTopLevelDeclarations2(name);
299291
for (var libraryEntry in librariesWithElements.entries) {
300292
var libraryElement = libraryEntry.key;
301293
var declaration = libraryEntry.value;
302-
var librarySource = libraryElement.source;
294+
var librarySource = libraryElement.firstFragment.source;
303295
// Check the kind.
304296
if (!kinds.contains(declaration.kind)) {
305297
continue;
@@ -334,7 +326,7 @@ class ImportLibrary extends MultiCorrectionProducer {
334326
prefix.isEmptyOrNull
335327
? DartFixKind.IMPORT_LIBRARY_PROJECT3_SHOW
336328
: DartFixKind.IMPORT_LIBRARY_PROJECT3_PREFIXED_SHOW;
337-
} else if (declaration.library != libraryElement) {
329+
} else if (declaration.library2 != libraryElement) {
338330
// Ugly: exports.
339331
fixKind =
340332
prefix.isEmptyOrNull
@@ -359,7 +351,7 @@ class ImportLibrary extends MultiCorrectionProducer {
359351
// relative import.
360352
var includeRelativeUri = canBeRelativeImport(
361353
librarySource.uri,
362-
this.libraryElement.librarySource.uri,
354+
libraryElement2.uri,
363355
);
364356
// Add the fix(es).
365357
producers.addAll(
@@ -429,12 +421,12 @@ class ImportLibrary extends MultiCorrectionProducer {
429421
return const [];
430422
}
431423

432-
var name = Name(
433-
dartFixContext.unitResult.libraryElement.source.uri,
424+
var name = Name.forLibrary(
425+
dartFixContext.unitResult.libraryElement2,
434426
memberName,
435427
);
436428
var producers = <ResolvedCorrectionProducer>[];
437-
await for (var libraryToImport in librariesWithExtensions(memberName)) {
429+
await for (var libraryToImport in librariesWithExtensions2(memberName)) {
438430
producers.addAll(
439431
_importExtensionInLibrary(libraryToImport, targetType, name),
440432
);
@@ -527,7 +519,7 @@ class ImportLibrary extends MultiCorrectionProducer {
527519
prefix = prefixed.prefix.name;
528520
}
529521
if (targetNode case Annotation(:var name)) {
530-
if (name.staticElement == null) {
522+
if (name.element == null) {
531523
if (targetNode.arguments != null) {
532524
return const [];
533525
}
@@ -557,7 +549,7 @@ class ImportLibrary extends MultiCorrectionProducer {
557549
}
558550
var targetNode = node;
559551
if (targetNode case Annotation(:var name)) {
560-
if (name.staticElement == null) {
552+
if (name.element == null) {
561553
if (targetNode.period != null && targetNode.arguments == null) {
562554
return const [];
563555
}
@@ -701,8 +693,8 @@ class _ImportLibraryCombinator extends ResolvedCorrectionProducer {
701693
if (finalNames.isNotEmpty) {
702694
newCombinatorCode = ' ${keyword.lexeme} ${finalNames.join(', ')}';
703695
}
704-
var libraryFile = unitResult.libraryElement.source.fullName;
705-
await builder.addDartFileEdit(libraryFile, (builder) {
696+
var libraryPath = unitResult.libraryElement2.firstFragment.source.fullName;
697+
await builder.addDartFileEdit(libraryPath, (builder) {
706698
builder.addSimpleReplacement(
707699
SourceRange(offset - 1, length + 1),
708700
newCombinatorCode,
@@ -729,7 +721,7 @@ class _ImportLibraryCombinator extends ResolvedCorrectionProducer {
729721
/// extension, but which does so only if the extension applies to a given type.
730722
class _ImportLibraryContainingExtension extends ResolvedCorrectionProducer {
731723
/// The library defining the extension.
732-
LibraryElement library;
724+
LibraryElement2 library;
733725

734726
/// The type of the target that the extension must apply to.
735727
DartType targetType;
@@ -761,12 +753,12 @@ class _ImportLibraryContainingExtension extends ResolvedCorrectionProducer {
761753

762754
@override
763755
Future<void> compute(ChangeBuilder builder) async {
764-
var instantiatedExtensions = library.asElement2.exportedExtensions
756+
var instantiatedExtensions = library.exportedExtensions
765757
.havingMemberWithBaseName(memberName)
766758
.applicableTo(targetLibrary: libraryElement2, targetType: targetType);
767759
if (instantiatedExtensions.isNotEmpty) {
768760
await builder.addDartFileEdit(file, (builder) {
769-
_uriText = builder.importLibrary(library.source.uri);
761+
_uriText = builder.importLibrary(library.uri);
770762
});
771763
}
772764
}
@@ -775,8 +767,8 @@ class _ImportLibraryContainingExtension extends ResolvedCorrectionProducer {
775767
/// A correction processor that can add a prefix to an identifier defined in a
776768
/// library that is already imported but that is imported with a prefix.
777769
class _ImportLibraryPrefix extends ResolvedCorrectionProducer {
778-
final LibraryElement _importedLibrary;
779-
final PrefixElement _importPrefix;
770+
final LibraryElement2 _importedLibrary;
771+
final PrefixElement2 _importPrefix;
780772
final String? _nodePrefix;
781773
final _ImportLibraryCombinator? _editCombinator;
782774

@@ -796,14 +788,14 @@ class _ImportLibraryPrefix extends ResolvedCorrectionProducer {
796788

797789
@override
798790
List<String> get fixArguments {
799-
var uriStr = _importedLibrary.source.uri.toString();
791+
var uriStr = _importedLibrary.uri.toString();
800792
return [uriStr, _prefixName];
801793
}
802794

803795
@override
804796
FixKind get fixKind => DartFixKind.IMPORT_LIBRARY_PREFIX;
805797

806-
String get _prefixName => _importPrefix.name;
798+
String get _prefixName => _importPrefix.name3!;
807799

808800
@override
809801
Future<void> compute(ChangeBuilder builder) async {
@@ -945,11 +937,11 @@ extension on SimpleIdentifier {
945937
DartType? enclosingThisType(AstNode node) {
946938
var parent = node.parent;
947939
if (parent is ClassDeclaration) {
948-
return parent.declaredElement?.thisType;
940+
return parent.declaredFragment?.element.thisType;
949941
} else if (parent is ExtensionDeclaration) {
950942
return parent.onClause?.extendedType.type;
951943
} else if (parent is MixinDeclaration) {
952-
return parent.declaredElement?.thisType;
944+
return parent.declaredFragment?.element.thisType;
953945
} else {
954946
return null;
955947
}

pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import 'package:analyzer/src/dart/element/element.dart';
2727
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
2828
import 'package:analyzer/src/dart/element/type.dart';
2929
import 'package:analyzer/src/utilities/extensions/ast.dart';
30+
import 'package:analyzer/src/utilities/extensions/element.dart';
3031
import 'package:analyzer_plugin/utilities/assist/assist.dart';
3132
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
3233
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -779,6 +780,18 @@ sealed class _AbstractCorrectionProducer<T extends ParsedUnitResult> {
779780
) =>
780781
_context.dartFixContext!.getTopLevelDeclarations(baseName);
781782

783+
/// Returns the mapping from a library (that is available to this context) to
784+
/// a top-level declaration that is exported (not necessary declared) by this
785+
/// library, and has the requested base name.
786+
///
787+
/// For getters and setters the corresponding top-level variable is returned.
788+
Future<Map<LibraryElement2, Element2>> getTopLevelDeclarations2(
789+
String baseName,
790+
) async {
791+
var map = await _context.dartFixContext!.getTopLevelDeclarations(baseName);
792+
return map.map((key, value) => MapEntry(key.asElement2, value.asElement2!));
793+
}
794+
782795
/// Returns whether the selection covers an operator of the given
783796
/// [binaryExpression].
784797
bool isOperatorSelected(BinaryExpression binaryExpression) {

pkg/analysis_server_plugin/lib/edit/fix/dart_fix_context.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ class DartFixContext implements FixContext {
9393
// TODO(srawlins): The documentation above is wrong; `memberName` is unused.
9494
Stream<LibraryElement2> librariesWithExtensions2(String memberName) async* {
9595
var analysisContext = unitResult.session.analysisContext;
96-
var analysisDriver = (analysisContext as DriverBasedAnalysisContext).driver;
96+
if (analysisContext is! DriverBasedAnalysisContext) {
97+
return;
98+
}
99+
100+
var analysisDriver = analysisContext.driver;
97101
await analysisDriver.discoverAvailableFiles();
98102

99103
var fsState = analysisDriver.fsState;

0 commit comments

Comments
 (0)