@@ -227,6 +227,7 @@ class ImportLibrary extends MultiCorrectionProducer {
227
227
String name,
228
228
List <ElementKind > kinds, {
229
229
String ? prefix,
230
+ bool canBePrefixed = true ,
230
231
}) async {
231
232
// Ignore the element if the name is private.
232
233
if (name.startsWith ('_' )) {
@@ -252,7 +253,6 @@ class ImportLibrary extends MultiCorrectionProducer {
252
253
if (! kinds.contains (element.kind)) {
253
254
continue ;
254
255
}
255
- var importPrefix = import.prefix? .element;
256
256
// Maybe update a "show"/"hide" directive.
257
257
var (
258
258
combinatorProducer,
@@ -265,7 +265,8 @@ class ImportLibrary extends MultiCorrectionProducer {
265
265
prefix: prefix,
266
266
);
267
267
// Maybe apply a prefix.
268
- if (importPrefix != null ) {
268
+ var importPrefix = import.prefix? .element;
269
+ if (canBePrefixed && importPrefix != null ) {
269
270
producers.add (
270
271
_ImportLibraryPrefix (
271
272
libraryElement,
@@ -397,74 +398,52 @@ class ImportLibrary extends MultiCorrectionProducer {
397
398
// additional analysis.
398
399
var foundImport = false ;
399
400
var names = < _PrefixedName > [];
401
+ var extensionsInLibrary =
402
+ < LibraryImport ? , List <InstantiatedExtensionWithMember >> {};
400
403
for (var import in unitResult.libraryFragment.libraryImports) {
401
- // prepare element
402
404
var importedLibrary = import.importedLibrary;
403
405
if (importedLibrary == null || importedLibrary != libraryToImport) {
404
406
continue ;
405
407
}
406
408
foundImport = true ;
407
- var instantiatedExtensions = importedLibrary.exportedExtensions
409
+ extensionsInLibrary[import] = importedLibrary.exportedExtensions
408
410
.havingMemberWithBaseName (memberName)
409
411
.applicableTo (
410
412
targetLibrary: libraryElement2,
411
413
targetType: targetType as TypeImpl ,
412
414
);
413
- for (var instantiatedExtension in instantiatedExtensions) {
414
- // If the import has a combinator that needs to be updated, then offer
415
- // to update it.
416
- var libraryElement = import.importedLibrary;
417
- if (libraryElement == null ) {
418
- continue ;
419
- }
415
+ }
416
+
417
+ // If the library at the URI is not already imported, we return a correction
418
+ // producer that will either add an import or not based on the result of
419
+ // analyzing the library.
420
+ if (! foundImport) {
421
+ extensionsInLibrary[null ] = libraryToImport.exportedExtensions
422
+ .havingMemberWithBaseName (memberName)
423
+ .applicableTo (
424
+ targetLibrary: libraryElement2,
425
+ targetType: targetType as TypeImpl ,
426
+ );
427
+ }
428
+ for (var entry in extensionsInLibrary.entries) {
429
+ var extensionsInLibrary = entry.value;
430
+ for (var instantiatedExtension in extensionsInLibrary) {
420
431
names.add (
421
432
_PrefixedName (
422
433
name: instantiatedExtension.extension .name! ,
423
434
ignorePrefix: true ,
424
435
producerGenerators: (prefix, name) async {
425
- var producers = < ResolvedCorrectionProducer > [];
426
- var (
427
- importLibraryCombinator,
428
- importLibraryCombinatorMultiple,
429
- ) = await _importEditCombinators (
430
- import,
431
- libraryElement,
432
- libraryToImport.uri.toString (),
436
+ return await _importLibraryForElement (
433
437
name,
438
+ prefix: prefix,
439
+ canBePrefixed: false ,
440
+ [ElementKind .EXTENSION ],
434
441
);
435
- if (importLibraryCombinator != null ) {
436
- producers.add (importLibraryCombinator);
437
- if (importLibraryCombinatorMultiple != null ) {
438
- producers.add (importLibraryCombinatorMultiple);
439
- }
440
- }
441
- return producers;
442
442
},
443
443
),
444
444
);
445
445
}
446
446
}
447
-
448
- // If the library at the URI is not already imported, we return a correction
449
- // producer that will either add an import or not based on the result of
450
- // analyzing the library.
451
- if (! foundImport) {
452
- names.add (
453
- _PrefixedName (
454
- name: memberName.name,
455
- producerGenerators: (prefix, name) async {
456
- return [
457
- _ImportLibraryContainingExtension (
458
- libraryToImport,
459
- targetType,
460
- memberName,
461
- context: context,
462
- ),
463
- ];
464
- },
465
- ),
466
- );
467
- }
468
447
return names;
469
448
}
470
449
@@ -914,8 +893,7 @@ class _ImportLibraryCombinatorMultiple extends ResolvedCorrectionProducer {
914
893
if (names.isNotEmpty) {
915
894
newCombinatorCode = ' ${keyword .lexeme } ${names .join (', ' )}' ;
916
895
}
917
- var libraryPath =
918
- unitResult.libraryElement.firstFragment.source.fullName;
896
+ var libraryPath = unitResult.libraryElement.firstFragment.source.fullName;
919
897
await builder.addDartFileEdit (libraryPath, (builder) {
920
898
builder.addSimpleReplacement (
921
899
SourceRange (offset - 1 , length + 1 ),
@@ -940,55 +918,6 @@ class _ImportLibraryCombinatorMultiple extends ResolvedCorrectionProducer {
940
918
}
941
919
}
942
920
943
- /// A correction processor that can add an import of a library containing an
944
- /// extension, but which does so only if the extension applies to a given type.
945
- class _ImportLibraryContainingExtension extends ResolvedCorrectionProducer {
946
- /// The library defining the extension.
947
- LibraryElement library;
948
-
949
- /// The type of the target that the extension must apply to.
950
- DartType targetType;
951
-
952
- /// The name of the member that the extension must declare.
953
- Name memberName;
954
-
955
- /// The URI that is being proposed for the import directive.
956
- String _uriText = '' ;
957
-
958
- _ImportLibraryContainingExtension (
959
- this .library,
960
- this .targetType,
961
- this .memberName, {
962
- required super .context,
963
- });
964
-
965
- @override
966
- CorrectionApplicability get applicability =>
967
- // TODO(applicability): comment on why.
968
- CorrectionApplicability .singleLocation;
969
-
970
- @override
971
- List <String > get fixArguments => [_uriText];
972
-
973
- @override
974
- FixKind get fixKind => DartFixKind .IMPORT_LIBRARY_PROJECT1 ;
975
-
976
- @override
977
- Future <void > compute (ChangeBuilder builder) async {
978
- var instantiatedExtensions = library.exportedExtensions
979
- .havingMemberWithBaseName (memberName)
980
- .applicableTo (
981
- targetLibrary: libraryElement2,
982
- targetType: targetType as TypeImpl ,
983
- );
984
- if (instantiatedExtensions.isNotEmpty) {
985
- await builder.addDartFileEdit (file, (builder) {
986
- _uriText = builder.importLibrary (library.uri);
987
- });
988
- }
989
- }
990
- }
991
-
992
921
/// A correction processor that can add a prefix to an identifier defined in a
993
922
/// library that is already imported but that is imported with a prefix.
994
923
class _ImportLibraryPrefix extends ResolvedCorrectionProducer {
0 commit comments