Skip to content

Commit 0246cc6

Browse files
keertipCommit Queue
authored andcommitted
[Completion] Show enum constants when completion is in the declaration.
Fixes #56971 Change-Id: Ib33a58e774f5540c426f5d5ffb7a387414dcf9f9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/408600 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]>
1 parent 093ab49 commit 0246cc6

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

pkg/analysis_server/lib/src/services/completion/dart/candidate_suggestion.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,17 +328,25 @@ final class FieldSuggestion extends CandidateSuggestion with MemberSuggestion {
328328
@override
329329
final InterfaceElement2? referencingInterface;
330330

331+
/// Indicates the context, whether the completion is in the body of the
332+
/// declaration.
333+
final bool isInDeclaration;
334+
331335
/// Initialize a newly created candidate suggestion to suggest the [element].
332336
FieldSuggestion({
333337
required this.element,
334338
required super.matcherScore,
335339
required this.referencingInterface,
340+
required this.isInDeclaration,
336341
});
337342

338343
@override
339344
String get completion {
340345
if (element.isEnumConstant) {
341346
var constantName = element.name3;
347+
if (isInDeclaration) {
348+
return '$constantName';
349+
}
342350
var enumName = element.enclosingElement2.displayName;
343351
return '$enumName.$constantName';
344352
}

pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,7 @@ class DeclarationHelper {
12471247
_suggestProperty(
12481248
accessor: accessor,
12491249
referencingInterface: referencingInterface,
1250+
isInDeclaration: true,
12501251
);
12511252
}
12521253
}
@@ -1256,13 +1257,18 @@ class DeclarationHelper {
12561257
_suggestProperty(
12571258
accessor: accessor,
12581259
referencingInterface: referencingInterface,
1260+
isInDeclaration: true,
12591261
);
12601262
}
12611263
}
12621264

12631265
for (var field in element.fields2) {
12641266
if (!field.isSynthetic && (!mustBeStatic || field.isStatic)) {
1265-
_suggestField(field: field, referencingInterface: referencingInterface);
1267+
_suggestField(
1268+
field: field,
1269+
referencingInterface: referencingInterface,
1270+
isInDeclaration: true,
1271+
);
12661272
}
12671273
}
12681274

@@ -1777,6 +1783,7 @@ class DeclarationHelper {
17771783
void _suggestField({
17781784
required FieldElement2 field,
17791785
InterfaceElement2? referencingInterface,
1786+
bool isInDeclaration = false,
17801787
}) {
17811788
if (visibilityTracker.isVisible(element: field, importData: null)) {
17821789
if ((mustBeAssignable && field.setter2 == null) ||
@@ -1789,6 +1796,7 @@ class DeclarationHelper {
17891796
element: field,
17901797
matcherScore: matcherScore,
17911798
referencingInterface: referencingInterface,
1799+
isInDeclaration: isInDeclaration,
17921800
);
17931801
collector.addSuggestion(suggestion);
17941802
}
@@ -1933,6 +1941,7 @@ class DeclarationHelper {
19331941
bool ignoreVisibility = false,
19341942
ImportData? importData,
19351943
InterfaceElement2? referencingInterface,
1944+
bool isInDeclaration = false,
19361945
}) {
19371946
if (ignoreVisibility ||
19381947
visibilityTracker.isVisible(
@@ -1959,6 +1968,7 @@ class DeclarationHelper {
19591968
element: variable,
19601969
matcherScore: matcherScore,
19611970
referencingInterface: referencingInterface,
1971+
isInDeclaration: isInDeclaration,
19621972
);
19631973
collector.addSuggestion(suggestion);
19641974
}
@@ -2043,6 +2053,7 @@ class DeclarationHelper {
20432053
element: variable,
20442054
matcherScore: matcherScore,
20452055
referencingInterface: null,
2056+
isInDeclaration: false,
20462057
);
20472058
collector.addSuggestion(suggestion);
20482059
}

pkg/analysis_server/test/services/completion/dart/declaration/enum_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ void main() {
1515

1616
@reflectiveTest
1717
class EnumTest extends AbstractCompletionDriverTest with EnumTestCases {
18-
@failingTest
1918
Future<void> test_inside_implicitThis_constants() async {
2019
await computeSuggestions('''
2120
enum E {

pkg/analysis_server/test/services/completion/dart/location/enum_declaration_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,27 @@ suggestions
375375
Test {}
376376
kind: identifier
377377
selection: 6
378+
''');
379+
}
380+
381+
Future<void> test_values() async {
382+
allowedIdentifiers = {'first', 'last'};
383+
includeKeywords = false;
384+
await computeSuggestions('''
385+
enum E {
386+
first,
387+
last;
388+
389+
List<E> get all => [^];
390+
}
391+
''');
392+
393+
assertResponse(r'''
394+
suggestions
395+
first
396+
kind: enumConstant
397+
last
398+
kind: enumConstant
378399
''');
379400
}
380401
}

0 commit comments

Comments
 (0)