@@ -26,6 +26,7 @@ import 'package:analyzer_plugin/src/utilities/change_builder/change_builder_dart
2626import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart' ;
2727import 'package:analyzer_plugin/utilities/fixes/fixes.dart' ;
2828import 'package:analyzer_plugin/utilities/range_factory.dart' ;
29+ import 'package:collection/collection.dart' ;
2930
3031typedef _ProducersGenerators =
3132 Future <List <ResolvedCorrectionProducer >> Function (
@@ -110,12 +111,9 @@ class ImportLibrary extends MultiCorrectionProducer {
110111 String ? prefix,
111112 }) async {
112113 var combinators = import.combinators;
113- if (combinators.length != 1 ) {
114+ if (combinators.isEmpty ) {
114115 return (null , null );
115116 }
116- _ImportLibraryCombinator ? importCombinator;
117- _ImportLibraryCombinatorMultiple ? importCombinatorMultiple;
118- var combinator = combinators.first;
119117 var otherNames = await _otherUnresolvedNames (prefix, name);
120118 var namesInThisLibrary = < String > [
121119 name,
@@ -125,41 +123,23 @@ class ImportLibrary extends MultiCorrectionProducer {
125123 exportedName,
126124 ];
127125 var importPrefix = import.prefix2? .element;
128- if (combinator is HideElementCombinator ) {
129- importCombinator = _ImportLibraryCombinator (
130- uri,
131- combinator,
132- name,
133- removePrefix: importPrefix == null ,
134- context: context,
135- );
136- if (namesInThisLibrary.length > 1 ) {
137- importCombinatorMultiple = _ImportLibraryCombinatorMultiple (
138- uri,
139- combinator,
140- namesInThisLibrary,
141- removePrefix: importPrefix == null ,
142- context: context,
143- );
144- }
145- } else if (combinator is ShowElementCombinator ) {
146- importCombinator = _ImportLibraryCombinator (
147- uri,
148- combinator,
149- name,
150- removePrefix: importPrefix == null ,
151- context: context,
152- );
153- if (namesInThisLibrary.length > 1 ) {
154- importCombinatorMultiple = _ImportLibraryCombinatorMultiple (
155- uri,
156- combinator,
157- namesInThisLibrary,
158- removePrefix: importPrefix == null ,
159- context: context,
160- );
161- }
126+ var importCombinator = _ImportLibraryCombinator (
127+ uri,
128+ combinators,
129+ name,
130+ removePrefix: importPrefix == null ,
131+ context: context,
132+ );
133+ if (namesInThisLibrary.length == 1 ) {
134+ return (importCombinator, null );
162135 }
136+ var importCombinatorMultiple = _ImportLibraryCombinatorMultiple (
137+ uri,
138+ combinators,
139+ namesInThisLibrary,
140+ removePrefix: importPrefix == null ,
141+ context: context,
142+ );
163143 return (importCombinator, importCombinatorMultiple);
164144 }
165145
@@ -435,8 +415,6 @@ class ImportLibrary extends MultiCorrectionProducer {
435415 for (var instantiatedExtension in instantiatedExtensions) {
436416 // If the import has a combinator that needs to be updated, then offer
437417 // to update it.
438- // TODO(FMorschel): We should fix all combinators for the import, if
439- // we don't, we may not import at all.
440418 var libraryElement = import.importedLibrary2;
441419 if (libraryElement == null ) {
442420 continue ;
@@ -857,11 +835,11 @@ enum _ImportKind {
857835class _ImportLibraryCombinator extends _ImportLibraryCombinatorMultiple {
858836 _ImportLibraryCombinator (
859837 String libraryName,
860- NamespaceCombinator combinator ,
838+ List < NamespaceCombinator > combinators ,
861839 String updatedName, {
862840 super .removePrefix,
863841 required super .context,
864- }) : super (libraryName, combinator , [updatedName]);
842+ }) : super (libraryName, combinators , [updatedName]);
865843
866844 @override
867845 List <String > get fixArguments => [_updatedNames.first, _libraryName];
@@ -875,15 +853,15 @@ class _ImportLibraryCombinator extends _ImportLibraryCombinatorMultiple {
875853class _ImportLibraryCombinatorMultiple extends ResolvedCorrectionProducer {
876854 final String _libraryName;
877855
878- final NamespaceCombinator _combinator ;
856+ final List < NamespaceCombinator > _combinators ;
879857
880858 final List <String > _updatedNames;
881859
882860 final bool _removePrefix;
883861
884862 _ImportLibraryCombinatorMultiple (
885863 this ._libraryName,
886- this ._combinator ,
864+ this ._combinators ,
887865 this ._updatedNames, {
888866 bool removePrefix = false ,
889867 required super .context,
@@ -910,50 +888,57 @@ class _ImportLibraryCombinatorMultiple extends ResolvedCorrectionProducer {
910888
911889 @override
912890 Future <void > compute (ChangeBuilder builder) async {
913- Set <String > finalNames = SplayTreeSet <String >();
914- int offset;
915- int length;
916- Keyword keyword;
917- if (_combinator case ShowElementCombinator (shownNames: var names)) {
918- finalNames.addAll (names);
919- offset = _combinator.offset;
920- length = _combinator.end - offset;
921- finalNames.addAll (_updatedNames);
922- keyword = Keyword .SHOW ;
923- } else if (_combinator case HideElementCombinator (hiddenNames: var names)) {
924- finalNames.addAll (names);
925- offset = _combinator.offset;
926- length = _combinator.end - offset;
927- finalNames.removeAll (_updatedNames);
928- keyword = Keyword .HIDE ;
929- } else {
930- return ;
931- }
932- var newCombinatorCode = '' ;
933- if (finalNames.isNotEmpty) {
934- newCombinatorCode = ' ${keyword .lexeme } ${finalNames .join (', ' )}' ;
935- }
936- var libraryPath = unitResult.libraryElement2.firstFragment.source.fullName;
937- await builder.addDartFileEdit (libraryPath, (builder) {
938- builder.addSimpleReplacement (
939- SourceRange (offset - 1 , length + 1 ),
940- newCombinatorCode,
941- );
942- if (_removePrefix) {
943- AstNode ? prefix;
944- if (node case NamedType (: var importPrefix? )) {
945- prefix = importPrefix;
946- } else if (node case PrefixedIdentifier (: var prefix)) {
947- prefix = prefix;
948- } else {
949- return ;
950- }
951- if (prefix == null ) {
952- return ;
953- }
954- builder.addDeletion (range.node (prefix));
891+ var codeStyleOptions = getCodeStyleOptions (unitResult.file);
892+
893+ for (var combinator in _combinators) {
894+ var combinatorNames = < String > {};
895+ var offset = combinator.offset;
896+ var length = combinator.end - offset;
897+
898+ Keyword keyword;
899+ switch (combinator) {
900+ case ShowElementCombinator (shownNames: var names):
901+ combinatorNames.addAll (names);
902+ combinatorNames.addAll (_updatedNames);
903+ keyword = Keyword .SHOW ;
904+ case HideElementCombinator (hiddenNames: var names):
905+ combinatorNames.addAll (names);
906+ combinatorNames.removeAll (_updatedNames);
907+ keyword = Keyword .HIDE ;
955908 }
956- });
909+
910+ var names =
911+ codeStyleOptions.sortCombinators
912+ ? combinatorNames.sorted ()
913+ : combinatorNames;
914+
915+ var newCombinatorCode = '' ;
916+ if (names.isNotEmpty) {
917+ newCombinatorCode = ' ${keyword .lexeme } ${names .join (', ' )}' ;
918+ }
919+ var libraryPath =
920+ unitResult.libraryElement2.firstFragment.source.fullName;
921+ await builder.addDartFileEdit (libraryPath, (builder) {
922+ builder.addSimpleReplacement (
923+ SourceRange (offset - 1 , length + 1 ),
924+ newCombinatorCode,
925+ );
926+ if (_removePrefix) {
927+ AstNode ? prefix;
928+ if (node case NamedType (: var importPrefix? )) {
929+ prefix = importPrefix;
930+ } else if (node case PrefixedIdentifier (: var prefix)) {
931+ prefix = prefix;
932+ } else {
933+ return ;
934+ }
935+ if (prefix == null ) {
936+ return ;
937+ }
938+ builder.addDeletion (range.node (prefix));
939+ }
940+ });
941+ }
957942 }
958943}
959944
0 commit comments