Skip to content

Commit 652a419

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes completion for collection ifs and fors
Fixes: #60604 Change-Id: I55a8426ae54a92fb26fd5a04e3dc11c267c26acf Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/428940 Auto-Submit: Felipe Morschel <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 62d40cc commit 652a419

File tree

6 files changed

+110
-24
lines changed

6 files changed

+110
-24
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,16 @@ class _ContextTypeVisitor extends SimpleAstVisitor<DartType> {
765765
return null;
766766
}
767767

768+
@override
769+
DartType? visitForElement(ForElement node) {
770+
if (range
771+
.endStart(node.leftParenthesis, node.rightParenthesis)
772+
.contains(offset)) {
773+
return node.forLoopParts.accept(this);
774+
}
775+
return node.parent!.accept(this);
776+
}
777+
768778
@override
769779
DartType? visitForPartsWithDeclarations(ForPartsWithDeclarations node) {
770780
if (range
@@ -812,7 +822,7 @@ class _ContextTypeVisitor extends SimpleAstVisitor<DartType> {
812822
.contains(offset)) {
813823
return typeProvider.boolType;
814824
}
815-
return null;
825+
return node.parent?.accept(this);
816826
}
817827

818828
@override

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ f() => [for (var e in c) ^];
1919
''');
2020
assertResponse(r'''
2121
suggestions
22+
false
23+
kind: keyword
24+
null
25+
kind: keyword
26+
true
27+
kind: keyword
2228
if
2329
kind: keyword
2430
const
2531
kind: keyword
26-
false
27-
kind: keyword
2832
for
2933
kind: keyword
30-
null
31-
kind: keyword
3234
switch
3335
kind: keyword
34-
true
35-
kind: keyword
3636
''');
3737
}
3838
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ f() => [if (true) 1 else ^];
3939
''');
4040
assertResponse(r'''
4141
suggestions
42+
false
43+
kind: keyword
44+
null
45+
kind: keyword
46+
true
47+
kind: keyword
4248
if
4349
kind: keyword
4450
for
4551
kind: keyword
4652
const
4753
kind: keyword
48-
false
49-
kind: keyword
50-
null
51-
kind: keyword
5254
switch
5355
kind: keyword
54-
true
55-
kind: keyword
5656
''');
5757
}
5858

@@ -77,20 +77,20 @@ f() => [if (true) ^];
7777
''');
7878
assertResponse(r'''
7979
suggestions
80+
false
81+
kind: keyword
82+
null
83+
kind: keyword
84+
true
85+
kind: keyword
8086
const
8187
kind: keyword
8288
for
8389
kind: keyword
84-
false
85-
kind: keyword
8690
if
8791
kind: keyword
88-
null
89-
kind: keyword
9092
switch
9193
kind: keyword
92-
true
93-
kind: keyword
9494
''');
9595
}
9696

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,58 @@ suggestions
130130
''');
131131
}
132132

133+
Future<void> test_expectedType_namedField_forElement() async {
134+
await computeSuggestions('''
135+
List<({int f01})> f() {
136+
return [
137+
for (int i = 0; i < 10; i++)
138+
(^),
139+
];
140+
}
141+
''');
142+
assertResponse(r'''
143+
suggestions
144+
|f01: |
145+
kind: namedArgument
146+
const
147+
kind: keyword
148+
false
149+
kind: keyword
150+
null
151+
kind: keyword
152+
switch
153+
kind: keyword
154+
true
155+
kind: keyword
156+
''');
157+
}
158+
159+
Future<void> test_expectedType_namedField_ifElement() async {
160+
await computeSuggestions('''
161+
List<({int f01})> f() {
162+
return [
163+
if (1 == 1)
164+
(^),
165+
];
166+
}
167+
''');
168+
assertResponse(r'''
169+
suggestions
170+
|f01: |
171+
kind: namedArgument
172+
const
173+
kind: keyword
174+
false
175+
kind: keyword
176+
null
177+
kind: keyword
178+
switch
179+
kind: keyword
180+
true
181+
kind: keyword
182+
''');
183+
}
184+
133185
Future<void> test_matchingContext_namedField_name() async {
134186
await computeSuggestions('''
135187
void f(({int f01, int f02, int g01}) x0) {

pkg/analysis_server/test/services/completion/dart/relevance/locality_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ replacement
6666
suggestions
6767
v02
6868
kind: localVariable
69-
relevance: 572
69+
relevance: 594
7070
v01
7171
kind: parameter
72-
relevance: 556
72+
relevance: 578
7373
''');
7474
}
7575

@@ -86,13 +86,13 @@ replacement
8686
suggestions
8787
v02
8888
kind: localVariable
89-
relevance: 572
89+
relevance: 594
9090
v03
9191
kind: localVariable
92-
relevance: 566
92+
relevance: 588
9393
v01
9494
kind: parameter
95-
relevance: 551
95+
relevance: 573
9696
''');
9797
}
9898

pkg/analysis_server/test/src/services/completion/dart/feature_computer_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,14 @@ void f() {
562562
''', 'Iterable<dynamic>');
563563
}
564564

565+
Future<void> test_forElement() async {
566+
await assertContextType('''
567+
List<String> f() => [
568+
for (int i = 0; i < 10; i ++) ^
569+
];
570+
''', 'String');
571+
}
572+
565573
Future<void> test_forPartsWithPattern_condition() async {
566574
await assertContextType('''
567575
void f() {
@@ -578,6 +586,14 @@ void f(bool b, int e) {
578586
''', 'bool');
579587
}
580588

589+
Future<void> test_ifElement_else() async {
590+
await assertContextType('''
591+
List<String> f(bool b) => [
592+
if (b) '' else ^
593+
];
594+
''', 'String');
595+
}
596+
581597
Future<void> test_ifElement_identifier() async {
582598
await assertContextType('''
583599
void f(bool b, int e) {
@@ -586,6 +602,14 @@ void f(bool b, int e) {
586602
''', 'bool');
587603
}
588604

605+
Future<void> test_ifElement_then() async {
606+
await assertContextType('''
607+
List<String> f(bool b) => [
608+
if (b) ^
609+
];
610+
''', 'String');
611+
}
612+
589613
Future<void> test_ifStatement_condition() async {
590614
await assertContextType('''
591615
void foo() {

0 commit comments

Comments
 (0)