Skip to content

Commit

Permalink
[DAS] Adds new import with show option
Browse files Browse the repository at this point in the history
R= [email protected]

Fixes #32234
Fixes #55842

Change-Id: I3acb03cc6d9a5f8bc1ebaf590a45ac5ee10388f8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396564
Auto-Submit: Felipe Morschel <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
Reviewed-by: Samuel Rawlins <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
  • Loading branch information
FMorschel authored and Commit Queue committed Dec 2, 2024
1 parent 0197bea commit 06caa3f
Show file tree
Hide file tree
Showing 7 changed files with 881 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ import 'package:analyzer_plugin/utilities/range_factory.dart';
class ImportLibrary extends MultiCorrectionProducer {
final _ImportKind _importKind;

/// Initialize a newly created instance that will add an import of
/// `dart:async`.
ImportLibrary.dartAsync({required super.context})
: _importKind = _ImportKind.dartAsync;

/// Initialize a newly created instance that will add an import for an
/// extension.
ImportLibrary.forExtension({required super.context})
Expand Down Expand Up @@ -65,10 +60,6 @@ class ImportLibrary extends MultiCorrectionProducer {
@override
Future<List<ResolvedCorrectionProducer>> get producers async {
return switch (_importKind) {
_ImportKind.dartAsync => _importLibrary(
DartFixKind.IMPORT_ASYNC,
Uri.parse('dart:async'),
),
_ImportKind.forExtension => await _producersForExtension(),
_ImportKind.forExtensionMember => await _producersForExtensionMember(),
_ImportKind.forExtensionType => await _producersForExtensionType(),
Expand Down Expand Up @@ -154,29 +145,66 @@ class ImportLibrary extends MultiCorrectionProducer {
/// Otherwise, both are returned in the order: absolute, relative.
List<ResolvedCorrectionProducer> _importLibrary(
FixKind fixKind,
Uri library, {
FixKind fixKindShow,
Uri library,
String name, {
String? prefix,
bool includeRelativeFix = false,
}) {
if (!includeRelativeFix) {
return [
_ImportAbsoluteLibrary(fixKind, library, prefix, context: context),
_ImportAbsoluteLibrary(
fixKindShow,
library,
prefix,
show: name,
context: context,
),
];
}
var codeStyleOptions = getCodeStyleOptions(unitResult.file);
if (codeStyleOptions.usePackageUris) {
return [
_ImportAbsoluteLibrary(fixKind, library, prefix, context: context),
_ImportAbsoluteLibrary(
fixKindShow,
library,
prefix,
show: name,
context: context,
),
];
}
if (codeStyleOptions.useRelativeUris) {
return [
_ImportRelativeLibrary(fixKind, library, prefix, context: context),
_ImportRelativeLibrary(
fixKindShow,
library,
prefix,
show: name,
context: context,
),
];
}
return [
_ImportAbsoluteLibrary(fixKind, library, prefix, context: context),
_ImportAbsoluteLibrary(
fixKindShow,
library,
prefix,
show: name,
context: context,
),
_ImportRelativeLibrary(fixKind, library, prefix, context: context),
_ImportRelativeLibrary(
fixKindShow,
library,
prefix,
show: name,
context: context,
),
];
}

Expand Down Expand Up @@ -282,29 +310,46 @@ class ImportLibrary extends MultiCorrectionProducer {
}
// Compute the fix kind.
FixKind fixKind;
FixKind fixKindShow;
if (libraryElement.isInSdk) {
fixKind =
prefix.isEmptyOrNull
? DartFixKind.IMPORT_LIBRARY_SDK
: DartFixKind.IMPORT_LIBRARY_SDK_PREFIXED;
fixKindShow =
prefix.isEmptyOrNull
? DartFixKind.IMPORT_LIBRARY_SDK_SHOW
: DartFixKind.IMPORT_LIBRARY_SDK_PREFIXED_SHOW;
} else if (_isLibSrcPath(librarySource.fullName)) {
// Bad: non-API.
fixKind =
prefix.isEmptyOrNull
? DartFixKind.IMPORT_LIBRARY_PROJECT3
: DartFixKind.IMPORT_LIBRARY_PROJECT3_PREFIXED;
fixKindShow =
prefix.isEmptyOrNull
? DartFixKind.IMPORT_LIBRARY_PROJECT3_SHOW
: DartFixKind.IMPORT_LIBRARY_PROJECT3_PREFIXED_SHOW;
} else if (declaration.library != libraryElement) {
// Ugly: exports.
fixKind =
prefix.isEmptyOrNull
? DartFixKind.IMPORT_LIBRARY_PROJECT2
: DartFixKind.IMPORT_LIBRARY_PROJECT2_PREFIXED;
fixKindShow =
prefix.isEmptyOrNull
? DartFixKind.IMPORT_LIBRARY_PROJECT2_SHOW
: DartFixKind.IMPORT_LIBRARY_PROJECT2_PREFIXED_SHOW;
} else {
// Good: direct declaration.
fixKind =
prefix.isEmptyOrNull
? DartFixKind.IMPORT_LIBRARY_PROJECT1
: DartFixKind.IMPORT_LIBRARY_PROJECT1_PREFIXED;
fixKindShow =
prefix.isEmptyOrNull
? DartFixKind.IMPORT_LIBRARY_PROJECT1_SHOW
: DartFixKind.IMPORT_LIBRARY_PROJECT1_PREFIXED_SHOW;
}
// If both files are in the same package's 'lib' folder, also include a
// relative import.
Expand All @@ -316,7 +361,9 @@ class ImportLibrary extends MultiCorrectionProducer {
producers.addAll(
_importLibrary(
fixKind,
fixKindShow,
librarySource.uri,
name,
prefix: prefix,
includeRelativeFix: includeRelativeUri,
),
Expand Down Expand Up @@ -543,15 +590,17 @@ class _ImportAbsoluteLibrary extends ResolvedCorrectionProducer {
final FixKind _fixKind;
final String? _prefix;
final Uri _library;
final String? _show;

String _uriText = '';

_ImportAbsoluteLibrary(
this._fixKind,
this._library,
this._prefix, {
String? show,
required super.context,
});
}) : _show = show;

@override
CorrectionApplicability get applicability =>
Expand All @@ -572,14 +621,18 @@ class _ImportAbsoluteLibrary extends ResolvedCorrectionProducer {
Future<void> compute(ChangeBuilder builder) async {
await builder.addDartFileEdit(file, (builder) {
if (builder is DartFileEditBuilderImpl) {
_uriText = builder.importLibraryWithAbsoluteUri(_library, _prefix);
_uriText = builder.importLibraryWithAbsoluteUri(
_library,
prefix: _prefix,
shownName: _show,
useShow: _show != null,
);
}
});
}
}

enum _ImportKind {
dartAsync,
forExtension,
forExtensionMember,
forExtensionType,
Expand Down Expand Up @@ -783,15 +836,17 @@ class _ImportRelativeLibrary extends ResolvedCorrectionProducer {
final FixKind _fixKind;
final String? _prefix;
final Uri _library;
final String? _show;

String _uriText = '';

_ImportRelativeLibrary(
this._fixKind,
this._library,
this._prefix, {
String? show,
required super.context,
});
}) : _show = show;

@override
CorrectionApplicability get applicability =>
Expand All @@ -812,7 +867,12 @@ class _ImportRelativeLibrary extends ResolvedCorrectionProducer {
Future<void> compute(ChangeBuilder builder) async {
await builder.addDartFileEdit(file, (builder) {
if (builder is DartFileEditBuilderImpl) {
_uriText = builder.importLibraryWithRelativeUri(_library, _prefix);
_uriText = builder.importLibraryWithRelativeUri(
_library,
prefix: _prefix,
shownName: _show,
useShow: _show != null,
);
}
});
}
Expand Down
42 changes: 41 additions & 1 deletion pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,16 @@ abstract final class DartFixKind {
DartFixKindPriority.standard + 3,
"Import library '{0}' with prefix '{1}'",
);
static const IMPORT_LIBRARY_PROJECT1_PREFIXED_SHOW = FixKind(
'dart.fix.import.libraryProject1PrefixedShow',
DartFixKindPriority.standard + 3,
"Import library '{0}' with prefix '{1}' and 'show'",
);
static const IMPORT_LIBRARY_PROJECT1_SHOW = FixKind(
'dart.fix.import.libraryProject1Show',
DartFixKindPriority.standard + 3,
"Import library '{0}' with 'show'",
);
static const IMPORT_LIBRARY_PROJECT2 = FixKind(
'dart.fix.import.libraryProject2',
DartFixKindPriority.standard + 2,
Expand All @@ -854,6 +864,16 @@ abstract final class DartFixKind {
DartFixKindPriority.standard + 2,
"Import library '{0}' with prefix '{1}'",
);
static const IMPORT_LIBRARY_PROJECT2_PREFIXED_SHOW = FixKind(
'dart.fix.import.libraryProject2PrefixedShow',
DartFixKindPriority.standard + 2,
"Import library '{0}' with prefix '{1}' and 'show'",
);
static const IMPORT_LIBRARY_PROJECT2_SHOW = FixKind(
'dart.fix.import.libraryProject2Show',
DartFixKindPriority.standard + 2,
"Import library '{0}' with 'show'",
);
static const IMPORT_LIBRARY_PROJECT3 = FixKind(
'dart.fix.import.libraryProject3',
DartFixKindPriority.standard + 1,
Expand All @@ -864,16 +884,36 @@ abstract final class DartFixKind {
DartFixKindPriority.standard + 1,
"Import library '{0}' with prefix '{1}'",
);
static const IMPORT_LIBRARY_PROJECT3_PREFIXED_SHOW = FixKind(
'dart.fix.import.libraryProject3PrefixedShow',
DartFixKindPriority.standard + 1,
"Import library '{0}' with prefix '{1}' and 'show'",
);
static const IMPORT_LIBRARY_PROJECT3_SHOW = FixKind(
'dart.fix.import.libraryProject3Show',
DartFixKindPriority.standard + 1,
"Import library '{0}' with 'show'",
);
static const IMPORT_LIBRARY_SDK = FixKind(
'dart.fix.import.librarySdk',
DartFixKindPriority.standard + 4,
"Import library '{0}'",
);
static const IMPORT_LIBRARY_SDK_PREFIXED = FixKind(
'dart.fix.import.librarySdk',
'dart.fix.import.librarySdkPrefixed',
DartFixKindPriority.standard + 4,
"Import library '{0}' with prefix '{1}'",
);
static const IMPORT_LIBRARY_SDK_PREFIXED_SHOW = FixKind(
'dart.fix.import.librarySdkPrefixedShow',
DartFixKindPriority.standard + 4,
"Import library '{0}' with prefix '{1}' and 'show'",
);
static const IMPORT_LIBRARY_SDK_SHOW = FixKind(
'dart.fix.import.librarySdkShow',
DartFixKindPriority.standard + 4,
"Import library '{0}' with 'show'",
);
static const INLINE_INVOCATION = FixKind(
'dart.fix.inlineInvocation',
DartFixKindPriority.standard - 20,
Expand Down
7 changes: 4 additions & 3 deletions pkg/analysis_server/test/edit/fixes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ void f() {
var errorFixes = await _getFixesAt(testFile, 'Completer<String>');
expect(errorFixes, hasLength(1));
var fixes = errorFixes[0].fixes;
expect(fixes, hasLength(3));
expect(fixes, hasLength(4));
expect(fixes[0].message, matches('Import library'));
expect(fixes[1].message, matches('Create class'));
expect(fixes[2].message, matches('Create mixin'));
expect(fixes[1].message, matches("Import library .+ with 'show'"));
expect(fixes[2].message, matches('Create class'));
expect(fixes[3].message, matches('Create mixin'));
}

Future<void> test_fromPlugins() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ FutureOr f;
// expect a suggestion to add the dart:async import
expect(fix.fixes, isNotEmpty);

var change = fix.fixes.singleWhere(
var change = fix.fixes.firstWhere(
(SourceChange change) => change.message.startsWith('Import '),
);
expect(change.edits, hasLength(1));
Expand Down
Loading

0 comments on commit 06caa3f

Please sign in to comment.