[analysis_server] Handle override completion before getters

Fixes https://github.com/Dart-Code/Dart-Code/issues/3784.

Change-Id: I422f7758f0ae713a37c4b15672828ada67ce9d65
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/228500
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/completion/dart/override_contributor.dart b/pkg/analysis_server/lib/src/services/completion/dart/override_contributor.dart
index 72174fe..128ff49 100644
--- a/pkg/analysis_server/lib/src/services/completion/dart/override_contributor.dart
+++ b/pkg/analysis_server/lib/src/services/completion/dart/override_contributor.dart
@@ -71,6 +71,7 @@
 
   SimpleIdentifier? _getTargetIdFromVarList(VariableDeclarationList fields) {
     var variables = fields.variables;
+    var type = fields.type;
     if (variables.length == 1) {
       var variable = variables[0];
       var targetId = variable.name;
@@ -81,10 +82,23 @@
         //   where _s_ is a synthetic id inserted by the analyzer parser
         return targetId;
       } else if (fields.keyword == null &&
-          fields.type == null &&
+          type == null &&
           variable.initializer == null) {
         // fasta parser does not insert a synthetic identifier
         return targetId;
+      } else if (fields.keyword == null &&
+          type is NamedType &&
+          type.typeArguments == null &&
+          variable.initializer == null) {
+        //  class A extends B {
+        //    m^
+        //
+        //    String foo;
+        //  }
+        // Parses as a variable list where `m` is the type and `String` is a
+        // variable.
+        var name = type.name;
+        return name is SimpleIdentifier ? name : null;
       }
     }
     return null;
diff --git a/pkg/analysis_server/test/services/completion/dart/override_contributor_test.dart b/pkg/analysis_server/test/services/completion/dart/override_contributor_test.dart
index 0e65414..df03716 100644
--- a/pkg/analysis_server/test/services/completion/dart/override_contributor_test.dart
+++ b/pkg/analysis_server/test/services/completion/dart/override_contributor_test.dart
@@ -42,6 +42,56 @@
     _assertNoOverrideContaining('bar');
   }
 
+  Future<void> test_beforeGetter() async {
+    addTestSource('''
+class A {
+  method() {}
+  int? age;
+}
+
+class B extends A {
+  m^
+
+  String value1 = '';
+}
+''');
+    await computeSuggestions();
+    _assertOverride('''
+@override
+  method() {
+    // TODO: implement method
+    return super.method();
+  }''',
+        displayText: 'method() { … }',
+        selectionOffset: 57,
+        selectionLength: 22);
+  }
+
+  Future<void> test_beforeMethod() async {
+    addTestSource('''
+class A {
+  method() {}
+  int? age;
+}
+
+class B extends A {
+  m^
+
+  void b() {}
+}
+''');
+    await computeSuggestions();
+    _assertOverride('''
+@override
+  method() {
+    // TODO: implement method
+    return super.method();
+  }''',
+        displayText: 'method() { … }',
+        selectionOffset: 57,
+        selectionLength: 22);
+  }
+
   Future<void> test_customOperator() async {
     addTestSource('''
 class A {
@@ -386,12 +436,13 @@
 ''');
     await computeSuggestions();
     _assertOverride('''
-method() {
+@override
+  method() {
     // TODO: implement method
     return super.method();
   }''',
         displayText: 'method() { … }',
-        selectionOffset: 45,
+        selectionOffset: 57,
         selectionLength: 22);
   }