Suggest pattern variables declared in a for loop
Fixes: https://github.com/dart-lang/sdk/issues/52851
Change-Id: Ie44af3d9a336ba6daa26b52abcf29bada88bf660
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352998
Reviewed-by: Keerti Parthasarathy <keertip@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart b/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart
index 0444838..74a4431 100644
--- a/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart
+++ b/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart
@@ -1400,6 +1400,8 @@
if (declaredElement != null) {
_suggestVariable(declaredElement);
}
+ } else if (node is ForEachPartsWithPattern) {
+ _visitPattern(node.pattern);
} else if (node is ForPartsWithDeclarations) {
var variables = node.variables;
for (var variable in variables.variables) {
@@ -1408,6 +1410,8 @@
_suggestVariable(declaredElement);
}
}
+ } else if (node is ForPartsWithPattern) {
+ _visitPattern(node.variables.pattern);
}
}
diff --git a/pkg/analysis_server/test/services/completion/dart/location/block_test.dart b/pkg/analysis_server/test/services/completion/dart/location/block_test.dart
index d4d83e5..7194e13 100644
--- a/pkg/analysis_server/test/services/completion/dart/location/block_test.dart
+++ b/pkg/analysis_server/test/services/completion/dart/location/block_test.dart
@@ -351,6 +351,140 @@
''');
}
+ Future<void>
+ test_afterLeftBrace_beforeRightBrace_inFunction_withPatternVariables_for() async {
+ await computeSuggestions('''
+void f() {
+ int a0 = 5;
+ int b0 = 5;
+
+ for (var (int a1, :b1) = g(); a1 > 0; a1--) {
+ ^
+ }
+}
+
+(int, {int b1}) g() => (1, b1: 2);
+''');
+ assertResponse(r'''
+suggestions
+ a0
+ kind: localVariable
+ a1
+ kind: localVariable
+ assert
+ kind: keyword
+ b0
+ kind: localVariable
+ b1
+ kind: localVariable
+ break
+ kind: keyword
+ const
+ kind: keyword
+ continue
+ kind: keyword
+ do
+ kind: keyword
+ dynamic
+ kind: keyword
+ false
+ kind: keyword
+ final
+ kind: keyword
+ for
+ kind: keyword
+ if
+ kind: keyword
+ late
+ kind: keyword
+ null
+ kind: keyword
+ return
+ kind: keyword
+ switch
+ kind: keyword
+ throw
+ kind: keyword
+ true
+ kind: keyword
+ try
+ kind: keyword
+ var
+ kind: keyword
+ void
+ kind: keyword
+ while
+ kind: keyword
+''');
+ }
+
+ Future<void>
+ test_afterLeftBrace_beforeRightBrace_inFunction_withPatternVariables_forEach() async {
+ await computeSuggestions('''
+void f() {
+ int a0 = 5;
+ int b0 = 5;
+
+ for (var (int a1, :b1) in g()) {
+ ^
+ }
+}
+
+List<(int, {int b1})> g() => [(1, b1: 2)];
+''');
+ assertResponse(r'''
+suggestions
+ a0
+ kind: localVariable
+ a1
+ kind: localVariable
+ assert
+ kind: keyword
+ b0
+ kind: localVariable
+ b1
+ kind: localVariable
+ break
+ kind: keyword
+ const
+ kind: keyword
+ continue
+ kind: keyword
+ do
+ kind: keyword
+ dynamic
+ kind: keyword
+ false
+ kind: keyword
+ final
+ kind: keyword
+ for
+ kind: keyword
+ if
+ kind: keyword
+ late
+ kind: keyword
+ null
+ kind: keyword
+ return
+ kind: keyword
+ switch
+ kind: keyword
+ throw
+ kind: keyword
+ true
+ kind: keyword
+ try
+ kind: keyword
+ var
+ kind: keyword
+ void
+ kind: keyword
+ while
+ kind: keyword
+''');
+ }
+
Future<void> test_afterLeftBrace_beforeRightBrace_inMethod() async {
await computeSuggestions('''
class A {foo() {for (int x in myList) {^}}}