Skip to content

Commit 54af2c5

Browse files
scheglovCommit Bot
authored and
Commit Bot
committed
Completion for fields after 'this.' in enum constructors.
Bug: #48724 Change-Id: If6e8bb61e02066bbdd9b209b8dcd569b98c57e3c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/239736 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 28d77be commit 54af2c5

File tree

2 files changed

+147
-78
lines changed

2 files changed

+147
-78
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,26 @@ class FieldFormalContributor extends DartCompletionContributor {
5252
}
5353
}
5454

55-
var enclosingClass = constructor.thisOrAncestorOfType<ClassDeclaration>();
55+
ClassElement? enclosingClass;
56+
var constructorParent = constructor.parent;
57+
if (constructorParent is ClassDeclaration) {
58+
enclosingClass = constructorParent.declaredElement;
59+
} else if (constructorParent is EnumDeclaration) {
60+
enclosingClass = constructorParent.declaredElement;
61+
} else {
62+
return;
63+
}
5664
if (enclosingClass == null) {
5765
return;
5866
}
5967

6068
// Add suggestions for fields that are not already referenced.
61-
for (var member in enclosingClass.members) {
62-
if (member is FieldDeclaration && !member.isStatic) {
63-
for (var variable in member.fields.variables) {
64-
var field = variable.name.staticElement;
65-
if (field is FieldElement) {
66-
var fieldName = field.name;
67-
if (fieldName.isNotEmpty) {
68-
if (!referencedFields.contains(fieldName)) {
69-
builder.suggestFieldFormalParameter(field);
70-
}
71-
}
69+
for (var field in enclosingClass.fields) {
70+
if (!field.isSynthetic && !field.isEnumConstant && !field.isStatic) {
71+
var fieldName = field.name;
72+
if (fieldName.isNotEmpty) {
73+
if (!referencedFields.contains(fieldName)) {
74+
builder.suggestFieldFormalParameter(field);
7275
}
7376
}
7477
}

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

Lines changed: 132 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -30,66 +30,63 @@ class FieldFormalParameterTest2 extends AbstractCompletionDriverTest
3030
}
3131

3232
mixin SuperFormalParameterTestCases on AbstractCompletionDriverTest {
33-
/// https://github.com/dart-lang/sdk/issues/39028
34-
Future<void> test_mixin_constructor() async {
35-
var response = await getTestCodeSuggestions('''
36-
mixin M {
37-
var field = 0;
38-
M(this.^);
39-
}
40-
''');
41-
42-
check(response).suggestions.isEmpty;
43-
}
44-
45-
Future<void> test_replacement_left() async {
46-
var response = await getTestCodeSuggestions('''
47-
class A {
48-
var field = 0;
49-
A(this.f^);
50-
}
51-
''');
52-
53-
check(response)
54-
..hasReplacement(left: 1)
55-
..suggestions.matchesInAnyOrder([
56-
(suggestion) => suggestion
57-
..completion.isEqualTo('field')
58-
..isField
59-
..returnType.isEqualTo('int'),
60-
]);
33+
Future<void> test_class_replacement_left() async {
34+
_checkContainers(
35+
declarations: 'var field = 0;',
36+
constructorParameters: 'this.f^',
37+
validator: (response) {
38+
check(response)
39+
..hasReplacement(left: 1)
40+
..suggestions.matchesInAnyOrder([
41+
(suggestion) => suggestion
42+
..completion.isEqualTo('field')
43+
..isField
44+
..returnType.isEqualTo('int'),
45+
]);
46+
},
47+
);
6148
}
6249

63-
Future<void> test_replacement_right() async {
64-
var response = await getTestCodeSuggestions('''
65-
class A {
66-
var field = 0;
67-
A(this.^f);
68-
}
69-
''');
70-
71-
check(response)
72-
..hasReplacement(right: 1)
73-
..suggestions.matchesInAnyOrder([
74-
(suggestion) => suggestion
75-
..completion.isEqualTo('field')
76-
..isField
77-
..returnType.isEqualTo('int'),
78-
]);
50+
Future<void> test_class_replacement_right() async {
51+
_checkContainers(
52+
declarations: 'var field = 0;',
53+
constructorParameters: 'this.^f',
54+
validator: (response) {
55+
check(response)
56+
..hasReplacement(right: 1)
57+
..suggestions.matchesInAnyOrder([
58+
(suggestion) => suggestion
59+
..completion.isEqualTo('field')
60+
..isField
61+
..returnType.isEqualTo('int'),
62+
]);
63+
},
64+
);
7965
}
8066

81-
Future<void> test_suggestions_onlyLocal() async {
67+
Future<void> test_class_suggestions_instanceFields_local() async {
8268
var response = await getTestCodeSuggestions('''
8369
class A {
70+
static final superStatic = 0;
8471
var inherited = 0;
72+
73+
void superMethod() {}
74+
int get superGetter => 0;
75+
void superSetter(int _) {}
8576
}
8677
8778
class B extends A {
79+
static final thisStatic = 0;
80+
8881
var first = 0;
8982
var second = 1.2;
83+
9084
B(this.^);
91-
B.constructor() {}
92-
void method() {}
85+
B.otherConstructor() {}
86+
87+
void thisMethod() {}
88+
int get thisGetter => 0;
89+
void thisSetter(int _) {}
9390
}
9491
''');
9592

@@ -107,41 +104,110 @@ class B extends A {
107104
]);
108105
}
109106

110-
Future<void> test_suggestions_onlyNotSpecified_optionalNamed() async {
107+
Future<void> test_class_suggestions_onlyNotSpecified_optionalNamed() async {
108+
_checkContainers(
109+
declarations: 'final int x; final int y;',
110+
constructorParameters: '{this.x, this.^}',
111+
validator: (response) {
112+
check(response)
113+
..hasEmptyReplacement()
114+
..suggestions.matchesInAnyOrder([
115+
(suggestion) => suggestion
116+
..completion.isEqualTo('y')
117+
..isField
118+
..returnType.isEqualTo('int'),
119+
]);
120+
},
121+
);
122+
}
123+
124+
Future<void>
125+
test_class_suggestions_onlyNotSpecified_requiredPositional() async {
126+
_checkContainers(
127+
declarations: 'final int x; final int y;',
128+
constructorParameters: 'this.x, this.^',
129+
validator: (response) {
130+
check(response)
131+
..hasEmptyReplacement()
132+
..suggestions.matchesInAnyOrder([
133+
(suggestion) => suggestion
134+
..completion.isEqualTo('y')
135+
..isField
136+
..returnType.isEqualTo('int'),
137+
]);
138+
},
139+
);
140+
}
141+
142+
Future<void> test_enum_suggestions_instanceFields() async {
111143
var response = await getTestCodeSuggestions('''
112-
class Point {
113-
final int x;
114-
final int y;
115-
Point({this.x, this.^});
144+
enum E {
145+
v();
146+
147+
static final zero = 0;
148+
final int first;
149+
final double second;
150+
151+
E(this.^);
152+
E.otherConstructor();
153+
154+
void myMethod() {}
155+
int get myGetter => 0;
156+
void mySetter(int _) {}
116157
}
117158
''');
118159

119160
check(response)
120161
..hasEmptyReplacement()
121162
..suggestions.matchesInAnyOrder([
122163
(suggestion) => suggestion
123-
..completion.isEqualTo('y')
164+
..completion.isEqualTo('first')
124165
..isField
125166
..returnType.isEqualTo('int'),
167+
(suggestion) => suggestion
168+
..completion.isEqualTo('second')
169+
..isField
170+
..returnType.isEqualTo('double'),
126171
]);
127172
}
128173

129-
Future<void> test_suggestions_onlyNotSpecified_requiredPositional() async {
174+
/// https://github.com/dart-lang/sdk/issues/39028
175+
Future<void> test_mixin_constructor() async {
130176
var response = await getTestCodeSuggestions('''
131-
class Point {
132-
final int x;
133-
final int y;
134-
Point(this.x, this.^);
177+
mixin M {
178+
var field = 0;
179+
M(this.^);
135180
}
136181
''');
137182

138-
check(response)
139-
..hasEmptyReplacement()
140-
..suggestions.matchesInAnyOrder([
141-
(suggestion) => suggestion
142-
..completion.isEqualTo('y')
143-
..isField
144-
..returnType.isEqualTo('int'),
145-
]);
183+
check(response).suggestions.isEmpty;
184+
}
185+
186+
Future<void> _checkContainers({
187+
required String declarations,
188+
required String constructorParameters,
189+
required void Function(CompletionResponseForTesting response) validator,
190+
}) async {
191+
// class
192+
{
193+
var response = await getTestCodeSuggestions('''
194+
class A {
195+
$declarations
196+
A($constructorParameters);
197+
}
198+
''');
199+
validator(response);
200+
}
201+
// enum
202+
{
203+
var response = await getTestCodeSuggestions('''
204+
enum E {
205+
v;
206+
$declarations
207+
E($constructorParameters);
208+
}
209+
''');
210+
validator(response);
211+
}
146212
}
147213
}

0 commit comments

Comments
 (0)