[DAS] Fixes 'Import from' assist to display the existing uri Fixes: https://github.com/dart-lang/sdk/issues/61417 Change-Id: Ibf545096c8240093ad58ec2a69bc1b159cb85fc9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/447081 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Auto-Submit: Felipe Morschel <git@fmorschel.dev> Reviewed-by: Samuel Rawlins <srawlins@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart b/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart index a135e49..02d87d0 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart
@@ -179,8 +179,10 @@ ]; } var codeStyleOptions = getCodeStyleOptions(unitResult.file); - if (codeStyleOptions.usePackageUris) { - return [ + var usePackageUris = codeStyleOptions.usePackageUris; + var useRelativeUris = codeStyleOptions.useRelativeUris; + return [ + if (usePackageUris || !useRelativeUris) ...[ _ImportAbsoluteLibrary(fixKind, library, prefix, context: context), _ImportAbsoluteLibrary( fixKindShow, @@ -189,10 +191,8 @@ show: name, context: context, ), - ]; - } - if (codeStyleOptions.useRelativeUris) { - return [ + ], + if (useRelativeUris || !usePackageUris) ...[ _ImportRelativeLibrary(fixKind, library, prefix, context: context), _ImportRelativeLibrary( fixKindShow, @@ -201,25 +201,7 @@ 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, - ), + ], ]; } @@ -237,8 +219,13 @@ // Maybe there is an existing import, but it is with prefix and we don't use // this prefix. var alreadyImportedWithPrefix = <LibraryElement>{}; - for (var import in unitResult.libraryFragment.libraryImports) { + for (var importDirective + in unitResult.unit.directives.whereType<ImportDirective>()) { // Prepare the element. + var import = importDirective.libraryImport; + if (import == null) { + continue; + } var libraryElement = import.importedLibrary; if (libraryElement == null) { continue; @@ -260,7 +247,7 @@ ) = await _importEditCombinators( import, libraryElement, - libraryElement.uri.toString(), + importDirective.uri.stringValue!, name, prefix: prefix, );
diff --git a/pkg/analysis_server/test/src/services/correction/fix/import_library_hide_test.dart b/pkg/analysis_server/test/src/services/correction/fix/import_library_hide_test.dart index f98bd4f..0b41bb1 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/import_library_hide_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/import_library_hide_test.dart
@@ -554,6 +554,29 @@ '''); } + Future<void> test_relativeImport() async { + newFile('$testPackageLibPath/lib.dart', ''' +class C {} +extension E on String { + void m() {} +} +'''); + await resolveTestCode(''' +import 'lib.dart' as lib hide E; + +void f(String s, lib.C c) { + s.m(); +} +'''); + await assertHasFix(''' +import 'lib.dart' as lib; + +void f(String s, lib.C c) { + s.m(); +} +''', matchFixMessage: "Import 'E' from lib.dart"); + } + Future<void> test_static_samePackage() async { newFile('$testPackageLibPath/lib.dart', ''' class A {}
diff --git a/pkg/analysis_server/test/src/services/correction/fix/import_library_show_test.dart b/pkg/analysis_server/test/src/services/correction/fix/import_library_show_test.dart index 202cb71..d64e4b8 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/import_library_show_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/import_library_show_test.dart
@@ -502,6 +502,29 @@ '''); } + Future<void> test_relativeImport() async { + newFile('$testPackageLibPath/lib.dart', ''' +class C {} +extension E on String { + void m() {} +} +'''); + await resolveTestCode(''' +import 'lib.dart' as lib show C; + +void f(String s, lib.C c) { + s.m(); +} +'''); + await assertHasFix(''' +import 'lib.dart' as lib show C, E; + +void f(String s, lib.C c) { + s.m(); +} +''', matchFixMessage: "Import 'E' from lib.dart"); + } + Future<void> test_sdk() async { await resolveTestCode(r''' import 'dart:collection' show HashMap;