Skip to content

Commit 10db3de

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] "Update library import" edit hide
[email protected] Fixes #30503 Change-Id: I2d16e18a3644333f57f77c1b2ead86c8734c2cd4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390630 Auto-Submit: Felipe Morschel <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent ea94702 commit 10db3de

File tree

5 files changed

+355
-62
lines changed

5 files changed

+355
-62
lines changed

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

Lines changed: 80 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ class ImportLibrary extends MultiCorrectionProducer {
101101
if (combinators.length == 1) {
102102
var combinator = combinators[0];
103103
if (combinator is HideElementCombinator) {
104-
// TODO(brianwilkerson): Support removing the extension name from a
105-
// hide combinator.
104+
producers.add(_ImportLibraryCombinator(
105+
libraryToImport.source.uri.toString(),
106+
combinator,
107+
instantiatedExtension.extension.name!,
108+
context: context,
109+
));
106110
} else if (combinator is ShowElementCombinator) {
107-
producers.add(_ImportLibraryShow(
111+
producers.add(_ImportLibraryCombinator(
108112
libraryToImport.source.uri.toString(),
109113
combinator,
110114
instantiatedExtension.extension.name!,
@@ -197,23 +201,25 @@ class ImportLibrary extends MultiCorrectionProducer {
197201
_ImportLibraryPrefix(libraryElement, prefix, context: context));
198202
continue;
199203
}
200-
// Maybe update a "show" directive.
204+
// Maybe update a "show"/"hide" directive.
201205
var combinators = import.combinators;
202206
if (combinators.length == 1) {
203-
var combinator = combinators[0];
207+
// Prepare library name - unit name or 'dart:name' for SDK library.
208+
var libraryName =
209+
libraryElement.definingCompilationUnit.source.uri.toString();
210+
if (libraryElement.isInSdk) {
211+
libraryName = libraryElement.source.shortName;
212+
}
213+
var combinator = combinators.first;
204214
if (combinator is HideElementCombinator) {
205-
// TODO(brianwilkerson): Support removing the element name from a
206-
// hide combinator.
215+
// Don't add this library again.
216+
alreadyImportedWithPrefix.add(libraryElement);
217+
producers.add(_ImportLibraryCombinator(libraryName, combinator, name,
218+
context: context));
207219
} else if (combinator is ShowElementCombinator) {
208-
// Prepare library name - unit name or 'dart:name' for SDK library.
209-
var libraryName =
210-
libraryElement.definingCompilationUnit.source.uri.toString();
211-
if (libraryElement.isInSdk) {
212-
libraryName = libraryElement.source.shortName;
213-
}
214220
// Don't add this library again.
215221
alreadyImportedWithPrefix.add(libraryElement);
216-
producers.add(_ImportLibraryShow(libraryName, combinator, name,
222+
producers.add(_ImportLibraryCombinator(libraryName, combinator, name,
217223
context: context));
218224
}
219225
}
@@ -447,6 +453,66 @@ enum _ImportKind {
447453
forType
448454
}
449455

456+
/// A correction processor that can add/remove a name to/from the show/hide
457+
/// combinator of an existing import.
458+
class _ImportLibraryCombinator extends ResolvedCorrectionProducer {
459+
final String _libraryName;
460+
461+
final NamespaceCombinator _combinator;
462+
463+
final String _updatedName;
464+
465+
_ImportLibraryCombinator(
466+
this._libraryName,
467+
this._combinator,
468+
this._updatedName, {
469+
required super.context,
470+
});
471+
472+
@override
473+
CorrectionApplicability get applicability =>
474+
// TODO(applicability): comment on why.
475+
CorrectionApplicability.singleLocation;
476+
477+
@override
478+
List<String> get fixArguments => [_libraryName];
479+
480+
@override
481+
FixKind get fixKind => DartFixKind.IMPORT_LIBRARY_COMBINATOR;
482+
483+
@override
484+
Future<void> compute(ChangeBuilder builder) async {
485+
Set<String> finalNames = SplayTreeSet<String>();
486+
int offset;
487+
int length;
488+
Keyword keyword;
489+
if (_combinator case ShowElementCombinator(shownNames: var names)) {
490+
finalNames.addAll(names);
491+
offset = _combinator.offset;
492+
length = _combinator.end - offset;
493+
finalNames.add(_updatedName);
494+
keyword = Keyword.SHOW;
495+
} else if (_combinator case HideElementCombinator(hiddenNames: var names)) {
496+
finalNames.addAll(names);
497+
offset = _combinator.offset;
498+
length = _combinator.end - offset;
499+
finalNames.remove(_updatedName);
500+
keyword = Keyword.HIDE;
501+
} else {
502+
return;
503+
}
504+
var newCombinatorCode = '';
505+
if (finalNames.isNotEmpty) {
506+
newCombinatorCode = ' ${keyword.lexeme} ${finalNames.join(', ')}';
507+
}
508+
var libraryFile = unitResult.libraryElement.source.fullName;
509+
await builder.addDartFileEdit(libraryFile, (builder) {
510+
builder.addSimpleReplacement(
511+
SourceRange(offset - 1, length + 1), newCombinatorCode);
512+
});
513+
}
514+
}
515+
450516
/// A correction processor that can add an import of a library containing an
451517
/// extension, but which does so only if the extension applies to a given type.
452518
class _ImportLibraryContainingExtension extends ResolvedCorrectionProducer {
@@ -535,48 +601,6 @@ class _ImportLibraryPrefix extends ResolvedCorrectionProducer {
535601
}
536602
}
537603

538-
/// A correction processor that can add a name to the show combinator of an
539-
/// existing import.
540-
class _ImportLibraryShow extends ResolvedCorrectionProducer {
541-
final String _libraryName;
542-
543-
final ShowElementCombinator _showCombinator;
544-
545-
final String _addedName;
546-
547-
_ImportLibraryShow(
548-
this._libraryName,
549-
this._showCombinator,
550-
this._addedName, {
551-
required super.context,
552-
});
553-
554-
@override
555-
CorrectionApplicability get applicability =>
556-
// TODO(applicability): comment on why.
557-
CorrectionApplicability.singleLocation;
558-
559-
@override
560-
List<String> get fixArguments => [_libraryName];
561-
562-
@override
563-
FixKind get fixKind => DartFixKind.IMPORT_LIBRARY_SHOW;
564-
565-
@override
566-
Future<void> compute(ChangeBuilder builder) async {
567-
Set<String> showNames = SplayTreeSet<String>();
568-
showNames.addAll(_showCombinator.shownNames);
569-
showNames.add(_addedName);
570-
var newShowCode = 'show ${showNames.join(', ')}';
571-
var offset = _showCombinator.offset;
572-
var length = _showCombinator.end - offset;
573-
var libraryFile = unitResult.libraryElement.source.fullName;
574-
await builder.addDartFileEdit(libraryFile, (builder) {
575-
builder.addSimpleReplacement(SourceRange(offset, length), newShowCode);
576-
});
577-
}
578-
}
579-
580604
/// A correction processor that can add an import using a relative URI.
581605
class _ImportRelativeLibrary extends ResolvedCorrectionProducer {
582606
final FixKind _fixKind;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,11 @@ abstract final class DartFixKind {
819819
49,
820820
"Import 'dart:async'",
821821
);
822+
static const IMPORT_LIBRARY_COMBINATOR = FixKind(
823+
'dart.fix.import.libraryCombinator',
824+
DartFixKindPriority.standard + 5,
825+
"Update library '{0}' import",
826+
);
822827
static const IMPORT_LIBRARY_PREFIX = FixKind(
823828
'dart.fix.import.libraryPrefix',
824829
DartFixKindPriority.standard + 5,
@@ -844,11 +849,6 @@ abstract final class DartFixKind {
844849
DartFixKindPriority.standard + 4,
845850
"Import library '{0}'",
846851
);
847-
static const IMPORT_LIBRARY_SHOW = FixKind(
848-
'dart.fix.import.libraryShow',
849-
DartFixKindPriority.standard + 5,
850-
"Update library '{0}' import",
851-
);
852852
static const INLINE_INVOCATION = FixKind(
853853
'dart.fix.inlineInvocation',
854854
DartFixKindPriority.standard - 20,

0 commit comments

Comments
 (0)