Skip to content

Commit 42e7d3a

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes 'Import from' assist to display the existing uri
Fixes: #61417 Change-Id: Ibf545096c8240093ad58ec2a69bc1b159cb85fc9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/447081 Reviewed-by: Brian Wilkerson <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 15f8c42 commit 42e7d3a

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

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

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ class ImportLibrary extends MultiCorrectionProducer {
179179
];
180180
}
181181
var codeStyleOptions = getCodeStyleOptions(unitResult.file);
182-
if (codeStyleOptions.usePackageUris) {
183-
return [
182+
var usePackageUris = codeStyleOptions.usePackageUris;
183+
var useRelativeUris = codeStyleOptions.useRelativeUris;
184+
return [
185+
if (usePackageUris || !useRelativeUris) ...[
184186
_ImportAbsoluteLibrary(fixKind, library, prefix, context: context),
185187
_ImportAbsoluteLibrary(
186188
fixKindShow,
@@ -189,10 +191,8 @@ class ImportLibrary extends MultiCorrectionProducer {
189191
show: name,
190192
context: context,
191193
),
192-
];
193-
}
194-
if (codeStyleOptions.useRelativeUris) {
195-
return [
194+
],
195+
if (useRelativeUris || !usePackageUris) ...[
196196
_ImportRelativeLibrary(fixKind, library, prefix, context: context),
197197
_ImportRelativeLibrary(
198198
fixKindShow,
@@ -201,25 +201,7 @@ class ImportLibrary extends MultiCorrectionProducer {
201201
show: name,
202202
context: context,
203203
),
204-
];
205-
}
206-
return [
207-
_ImportAbsoluteLibrary(fixKind, library, prefix, context: context),
208-
_ImportAbsoluteLibrary(
209-
fixKindShow,
210-
library,
211-
prefix,
212-
show: name,
213-
context: context,
214-
),
215-
_ImportRelativeLibrary(fixKind, library, prefix, context: context),
216-
_ImportRelativeLibrary(
217-
fixKindShow,
218-
library,
219-
prefix,
220-
show: name,
221-
context: context,
222-
),
204+
],
223205
];
224206
}
225207

@@ -237,8 +219,13 @@ class ImportLibrary extends MultiCorrectionProducer {
237219
// Maybe there is an existing import, but it is with prefix and we don't use
238220
// this prefix.
239221
var alreadyImportedWithPrefix = <LibraryElement>{};
240-
for (var import in unitResult.libraryFragment.libraryImports) {
222+
for (var importDirective
223+
in unitResult.unit.directives.whereType<ImportDirective>()) {
241224
// Prepare the element.
225+
var import = importDirective.libraryImport;
226+
if (import == null) {
227+
continue;
228+
}
242229
var libraryElement = import.importedLibrary;
243230
if (libraryElement == null) {
244231
continue;
@@ -260,7 +247,7 @@ class ImportLibrary extends MultiCorrectionProducer {
260247
) = await _importEditCombinators(
261248
import,
262249
libraryElement,
263-
libraryElement.uri.toString(),
250+
importDirective.uri.stringValue!,
264251
name,
265252
prefix: prefix,
266253
);

pkg/analysis_server/test/src/services/correction/fix/import_library_hide_test.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,29 @@ void f(A a) {
554554
''');
555555
}
556556

557+
Future<void> test_relativeImport() async {
558+
newFile('$testPackageLibPath/lib.dart', '''
559+
class C {}
560+
extension E on String {
561+
void m() {}
562+
}
563+
''');
564+
await resolveTestCode('''
565+
import 'lib.dart' as lib hide E;
566+
567+
void f(String s, lib.C c) {
568+
s.m();
569+
}
570+
''');
571+
await assertHasFix('''
572+
import 'lib.dart' as lib;
573+
574+
void f(String s, lib.C c) {
575+
s.m();
576+
}
577+
''', matchFixMessage: "Import 'E' from lib.dart");
578+
}
579+
557580
Future<void> test_static_samePackage() async {
558581
newFile('$testPackageLibPath/lib.dart', '''
559582
class A {}

pkg/analysis_server/test/src/services/correction/fix/import_library_show_test.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,29 @@ void f() {
502502
''');
503503
}
504504

505+
Future<void> test_relativeImport() async {
506+
newFile('$testPackageLibPath/lib.dart', '''
507+
class C {}
508+
extension E on String {
509+
void m() {}
510+
}
511+
''');
512+
await resolveTestCode('''
513+
import 'lib.dart' as lib show C;
514+
515+
void f(String s, lib.C c) {
516+
s.m();
517+
}
518+
''');
519+
await assertHasFix('''
520+
import 'lib.dart' as lib show C, E;
521+
522+
void f(String s, lib.C c) {
523+
s.m();
524+
}
525+
''', matchFixMessage: "Import 'E' from lib.dart");
526+
}
527+
505528
Future<void> test_sdk() async {
506529
await resolveTestCode(r'''
507530
import 'dart:collection' show HashMap;

0 commit comments

Comments
 (0)