Skip to content

Commit 3c6af25

Browse files
FMorschelCommit Queue
authored and
Commit Queue
committed
[DAS] Fixes auto-complete import combinator update for enum constants
Fixes: #60565 Change-Id: I9bd4740b48569afe86c4a7f02412f5e50e654bfd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/423621 Auto-Submit: Felipe Morschel <git@fmorschel.dev> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Samuel Rawlins <srawlins@google.com> Commit-Queue: Samuel Rawlins <srawlins@google.com>
1 parent dfaf151 commit 3c6af25

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:analysis_server/src/lsp/mapping.dart';
1212
import 'package:analysis_server/src/utilities/element_location2.dart';
1313
import 'package:analyzer/dart/analysis/results.dart';
1414
import 'package:analyzer/dart/analysis/session.dart';
15+
import 'package:analyzer/dart/element/element.dart';
1516
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1617

1718
class CompletionResolveHandler
@@ -93,10 +94,15 @@ class CompletionResolveHandler
9394
).locateIn(session)
9495
: null;
9596

97+
var showName = element?.name3;
98+
if (element?.enclosingElement case InstanceElement(:var name3)) {
99+
showName = name3;
100+
}
101+
96102
var builder = ChangeBuilder(session: session);
97103
await builder.addDartFileEdit(file, (builder) {
98104
for (var uri in importUris) {
99-
builder.importLibraryElement(uri, showName: element?.name3);
105+
builder.importLibraryElement(uri, showName: showName);
100106
}
101107
});
102108

pkg/analysis_server/test/lsp/completion_dart_test.dart

+70
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,76 @@ import 'dart:^';
21032103
expect(res.any((c) => c.label == 'dart:async'), isTrue);
21042104
}
21052105

2106+
Future<void> test_importedSymbol_libraryImported_hidingVariable() async {
2107+
newFile(join(projectFolderPath, 'lib', 'import.dart'), '''
2108+
enum MyEnum {
2109+
value1,
2110+
value2,
2111+
}
2112+
void myFunction(MyEnum _) {}
2113+
var myVariable = 1;
2114+
''');
2115+
2116+
var content = '''
2117+
import 'import.dart' hide myVariable;
2118+
2119+
void main() {
2120+
myFunction(MyE^);
2121+
}
2122+
''';
2123+
2124+
var expectedContent = '''
2125+
import 'import.dart' hide myVariable;
2126+
2127+
void main() {
2128+
myFunction(MyEnum.value1);
2129+
}
2130+
''';
2131+
2132+
var completionLabel = 'MyEnum.value1';
2133+
2134+
await _checkCompletionEdits(
2135+
mainFileUri,
2136+
content,
2137+
completionLabel,
2138+
expectedContent,
2139+
);
2140+
}
2141+
2142+
Future<void> test_importedSymbol_libraryImported_showingEnum() async {
2143+
newFile(join(projectFolderPath, 'lib', 'import.dart'), '''
2144+
enum MyEnum {
2145+
value1,
2146+
value2,
2147+
}
2148+
''');
2149+
2150+
var content = '''
2151+
import 'import.dart' show MyEnum;
2152+
2153+
void main() {
2154+
MyEnum _ = My^;
2155+
}
2156+
''';
2157+
2158+
var expectedContent = '''
2159+
import 'import.dart' show MyEnum;
2160+
2161+
void main() {
2162+
MyEnum _ = MyEnum.value1;
2163+
}
2164+
''';
2165+
2166+
var completionLabel = 'MyEnum.value1';
2167+
2168+
await _checkCompletionEdits(
2169+
mainFileUri,
2170+
content,
2171+
completionLabel,
2172+
expectedContent,
2173+
);
2174+
}
2175+
21062176
Future<void> test_insertReplaceRanges() async {
21072177
setCompletionItemInsertReplaceSupport();
21082178

0 commit comments

Comments
 (0)