Skip to content

Commit 6de0ef3

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fix auto-complete update combinators
We already have a quick-fix that allows us to use the same existing import by editing the combinators. The auto-completer now should do the same. Bug: Dart-Code/Dart-Code#5238 Change-Id: I0f3a50299abb9890370a6c8934b49036f7e03196 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404525 Auto-Submit: Felipe Morschel <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 0b11d0c commit 6de0ef3

File tree

6 files changed

+447
-35
lines changed

6 files changed

+447
-35
lines changed

pkg/analysis_server/lib/src/lsp/handlers/handler_completion_resolve.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,17 @@ class CompletionResolveHandler
8686
return cancelled(token);
8787
}
8888

89+
var element =
90+
elementReference != null
91+
? await ElementLocation2.decode(
92+
elementReference,
93+
).locateIn(session)
94+
: null;
95+
8996
var builder = ChangeBuilder(session: session);
9097
await builder.addDartFileEdit(file, (builder) {
9198
for (var uri in importUris) {
92-
builder.importLibraryElement(uri);
99+
builder.importLibraryElement(uri, showName: element?.name3);
93100
}
94101
});
95102

@@ -123,12 +130,6 @@ class CompletionResolveHandler
123130

124131
// Look up documentation if we can get an element for this item.
125132
Either2<MarkupContent, String>? documentation;
126-
var element =
127-
elementReference != null
128-
? await ElementLocation2.decode(
129-
elementReference,
130-
).locateIn(session)
131-
: null;
132133
if (element != null) {
133134
var formats = clientCapabilities.completionDocumentationFormats;
134135
var dartDocInfo = server.getDartdocDirectiveInfoForSession(session);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class ImportLibrary extends MultiCorrectionProducer {
9696
for (var instantiatedExtension in instantiatedExtensions) {
9797
// If the import has a combinator that needs to be updated, then offer
9898
// to update it.
99+
// TODO(FMorschel): We should fix all combinators for the import, if
100+
// we don't, we may not import at all.
99101
var combinators = import.combinators;
100102
if (combinators.length == 1) {
101103
var combinator = combinators[0];
@@ -624,7 +626,7 @@ class _ImportAbsoluteLibrary extends ResolvedCorrectionProducer {
624626
_uriText = builder.importLibraryWithAbsoluteUri(
625627
_library,
626628
prefix: _prefix,
627-
shownName: _show,
629+
showName: _show,
628630
useShow: _show != null,
629631
);
630632
}
@@ -873,7 +875,7 @@ class _ImportRelativeLibrary extends ResolvedCorrectionProducer {
873875
_uriText = builder.importLibraryWithRelativeUri(
874876
_library,
875877
prefix: _prefix,
876-
shownName: _show,
878+
showName: _show,
877879
useShow: _show != null,
878880
);
879881
}

pkg/analysis_server/test/lsp/completion_dart_test.dart

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,6 +3663,131 @@ void f() {
36633663
expect(res.isIncomplete, isTrue);
36643664
}
36653665

3666+
Future<void> test_unimportedSymbols_libraryImported_hidingMultiple() async {
3667+
newFile(join(projectFolderPath, 'lib', 'my_classes.dart'), '''
3668+
class MyClass1 {}
3669+
class MyClass2 {}
3670+
class MyClass3 {}
3671+
''');
3672+
3673+
var content = '''
3674+
import 'package:test/my_classes.dart' hide MyClass1, MyClass2;
3675+
void f() {
3676+
MyClas^
3677+
}
3678+
''';
3679+
3680+
var expectedContent = '''
3681+
import 'package:test/my_classes.dart' hide MyClass2;
3682+
void f() {
3683+
MyClass1
3684+
}
3685+
''';
3686+
3687+
var completionLabel = 'MyClass1';
3688+
3689+
await _checkCompletionEdits(
3690+
mainFileUri,
3691+
content,
3692+
completionLabel,
3693+
expectedContent,
3694+
);
3695+
}
3696+
3697+
Future<void> test_unimportedSymbols_libraryImported_hidingOne() async {
3698+
newFile(join(projectFolderPath, 'lib', 'my_classes.dart'), '''
3699+
class MyClass1 {}
3700+
class MyClass2 {}
3701+
''');
3702+
3703+
var content = '''
3704+
import 'package:test/my_classes.dart' hide MyClass1;
3705+
void f() {
3706+
MyClas^
3707+
}
3708+
''';
3709+
3710+
var expectedContent = '''
3711+
import 'package:test/my_classes.dart';
3712+
void f() {
3713+
MyClass1
3714+
}
3715+
''';
3716+
3717+
var completionLabel = 'MyClass1';
3718+
3719+
await _checkCompletionEdits(
3720+
mainFileUri,
3721+
content,
3722+
completionLabel,
3723+
expectedContent,
3724+
);
3725+
}
3726+
3727+
Future<void> test_unimportedSymbols_libraryImported_showingOther() async {
3728+
newFile(join(projectFolderPath, 'lib', 'my_classes.dart'), '''
3729+
class MyClass1 {}
3730+
class MyClass2 {}
3731+
''');
3732+
3733+
var content = '''
3734+
import 'package:test/my_classes.dart' show MyClass2;
3735+
void f() {
3736+
MyClas^
3737+
}
3738+
''';
3739+
3740+
var expectedContent = '''
3741+
import 'package:test/my_classes.dart' show MyClass1, MyClass2;
3742+
void f() {
3743+
MyClass1
3744+
}
3745+
''';
3746+
3747+
var completionLabel = 'MyClass1';
3748+
3749+
await _checkCompletionEdits(
3750+
mainFileUri,
3751+
content,
3752+
completionLabel,
3753+
expectedContent,
3754+
);
3755+
}
3756+
3757+
// Code completion doesn't include prefixes for auto-imports so when an
3758+
// auto-import is added it must be unprefixed even if the library exists with
3759+
// a prefix (we cannot modify the inserted text during resolve).
3760+
Future<void> test_unimportedSymbols_libraryImported_withPrefix() async {
3761+
newFile(join(projectFolderPath, 'lib', 'my_classes.dart'), '''
3762+
class MyClass1 {}
3763+
class MyClass2 {}
3764+
''');
3765+
3766+
var content = '''
3767+
import 'package:test/my_classes.dart' as p1 show MyClass2;
3768+
void f() {
3769+
MyClas^
3770+
}
3771+
''';
3772+
3773+
var expectedContent = '''
3774+
import 'package:test/my_classes.dart' as p1 show MyClass2;
3775+
import 'package:test/my_classes.dart';
3776+
void f() {
3777+
MyClass1
3778+
}
3779+
''';
3780+
3781+
var completionLabel = 'MyClass1';
3782+
3783+
await _checkCompletionEdits(
3784+
mainFileUri,
3785+
content,
3786+
completionLabel,
3787+
expectedContent,
3788+
);
3789+
}
3790+
36663791
/// This test reproduces a bug where the pathKey hash used in
36673792
/// available_declarations.dart would not change with the contents of the file
36683793
/// (as it always used 0 as the modification stamp) which would prevent

0 commit comments

Comments
 (0)