Revert "Revert "Revert "Don't do implicit new/const code generation with --preview-dart-2 enabled."""

This reverts commit 4089111540ec7c35922106f9e6d2b123d926f545.

R=brianwilkerson@google.com

Change-Id: I66297466f8baafe6e070acbcb1776b41f3d9fedc
Reviewed-on: https://dart-review.googlesource.com/55506
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analysis_server/lib/src/services/completion/dart/static_member_contributor.dart b/pkg/analysis_server/lib/src/services/completion/dart/static_member_contributor.dart
index 8239f37..593de24 100644
--- a/pkg/analysis_server/lib/src/services/completion/dart/static_member_contributor.dart
+++ b/pkg/analysis_server/lib/src/services/completion/dart/static_member_contributor.dart
@@ -68,6 +68,13 @@
   }
 
   @override
+  visitConstructorElement(ConstructorElement element) {
+    if (element.context.analysisOptions.previewDart2) {
+      _addSuggestion(element);
+    }
+  }
+
+  @override
   visitFieldElement(FieldElement element) {
     if (element.isStatic) {
       _addSuggestion(element);
diff --git a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
index 6eba6d5..19c6a82 100644
--- a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
@@ -1664,7 +1664,9 @@
     DartChangeBuilder changeBuilder = new DartChangeBuilder(session);
     await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
       builder.addReplacement(range.node(widgetExpr), (DartEditBuilder builder) {
-        builder.write('new ');
+        if (!driver.analysisOptions.previewDart2) {
+          builder.write('new ');
+        }
         if (parentClassElement == null) {
           builder.addSimpleLinkedEdit('WIDGET', 'widget');
         } else {
@@ -1743,7 +1745,9 @@
       DartChangeBuilder changeBuilder = new DartChangeBuilder(session);
       await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
         builder.addReplacement(selectedRange, (DartEditBuilder builder) {
-          builder.write('new ');
+          if (!driver.analysisOptions.previewDart2) {
+            builder.write('new ');
+          }
           builder.writeType(parentClassElement.type);
           builder.write('(');
 
diff --git a/pkg/analysis_server/test/completion_test.dart b/pkg/analysis_server/test/completion_test.dart
index 1e1aec8..9d2093d 100644
--- a/pkg/analysis_server/test/completion_test.dart
+++ b/pkg/analysis_server/test/completion_test.dart
@@ -732,21 +732,17 @@
         <String>["1+fooConst", "1-fooNotConst", "1-bar"],
         failingTests: '1');
 
-    buildTests(
-        'testCompletion_annotation_type',
-        '''
+    buildTests('testCompletion_annotation_type', '''
 class AAA {
   const AAA({int a, int b});
   const AAA.nnn(int c, int d);
 }
 @AAA!1
 main() {
-}''',
-        <String>[
-          "1+AAA" /*":" + ProposalKind.CONSTRUCTOR*/,
-          "1+AAA.nnn" /*":" + ProposalKind.CONSTRUCTOR*/
-        ],
-        failingTests: '1');
+}''', <String>[
+      "1+AAA" /*":" + ProposalKind.CONSTRUCTOR*/,
+      "1+AAA.nnn" /*":" + ProposalKind.CONSTRUCTOR*/
+    ]);
 
     buildTests('testCompletion_annotation_type_inClass_withoutMember', '''
 class AAA {
diff --git a/pkg/analysis_server/test/domain_completion_test.dart b/pkg/analysis_server/test/domain_completion_test.dart
index 078a2bb..07c23fa 100644
--- a/pkg/analysis_server/test/domain_completion_test.dart
+++ b/pkg/analysis_server/test/domain_completion_test.dart
@@ -619,8 +619,11 @@
     assertHasResult(CompletionSuggestionKind.INVOCATION, 'A',
         elementKind: ElementKind.CLASS);
 
-    // No constructors suggested.
-    assertNoResult('A.named');
+    // Both constructors - default and named, are suggested.
+    assertHasResult(CompletionSuggestionKind.INVOCATION, 'A',
+        elementKind: ElementKind.CONSTRUCTOR);
+    assertHasResult(CompletionSuggestionKind.INVOCATION, 'A.named',
+        elementKind: ElementKind.CONSTRUCTOR);
   }
 
   test_local_named_constructor() {
diff --git a/pkg/analysis_server/test/services/completion/dart/completion_contributor_util.dart b/pkg/analysis_server/test/services/completion/dart/completion_contributor_util.dart
index 558129a..f02389c 100644
--- a/pkg/analysis_server/test/services/completion/dart/completion_contributor_util.dart
+++ b/pkg/analysis_server/test/services/completion/dart/completion_contributor_util.dart
@@ -47,7 +47,7 @@
    * Return `true` if contributors should suggest constructors in contexts where
    * there is no `new` or `const` keyword.
    */
-  bool get suggestConstructorsWithoutNew => false;
+  bool get suggestConstructorsWithoutNew => true;
 
   bool get usingFastaParser => analyzer.Parser.useFasta;
 
diff --git a/pkg/analysis_server/test/services/completion/dart/imported_reference_contributor_test.dart b/pkg/analysis_server/test/services/completion/dart/imported_reference_contributor_test.dart
index 8f6efea..60b262c 100644
--- a/pkg/analysis_server/test/services/completion/dart/imported_reference_contributor_test.dart
+++ b/pkg/analysis_server/test/services/completion/dart/imported_reference_contributor_test.dart
@@ -2449,12 +2449,12 @@
     await computeSuggestions();
 
     assertSuggestClass('A');
-    assertNotSuggested('A.a1');
-    assertNotSuggested('A.a2');
+    assertSuggestConstructor('A.a1');
+    assertSuggestConstructor('A.a2');
 
     assertSuggestClass('B');
-    assertNotSuggested('B.b1');
-    assertNotSuggested('B.b2');
+    assertSuggestConstructor('B.b1');
+    assertSuggestConstructor('B.b2');
   }
 
   test_ImportDirective_dart() async {
diff --git a/pkg/analysis_server/test/services/completion/dart/static_member_contributor_test.dart b/pkg/analysis_server/test/services/completion/dart/static_member_contributor_test.dart
index 8aebb55..2987710 100644
--- a/pkg/analysis_server/test/services/completion/dart/static_member_contributor_test.dart
+++ b/pkg/analysis_server/test/services/completion/dart/static_member_contributor_test.dart
@@ -114,8 +114,8 @@
 ''');
     await computeSuggestions();
 
-    assertNotSuggested('foo');
-    assertNotSuggested('bar');
+    assertSuggestConstructor('foo', elementName: 'foo');
+    assertSuggestConstructor('bar', elementName: 'bar');
   }
 
   test_keyword() async {
diff --git a/pkg/analysis_server/test/services/correction/assist_test.dart b/pkg/analysis_server/test/services/correction/assist_test.dart
index cf816ea..c80d7e9 100644
--- a/pkg/analysis_server/test/services/correction/assist_test.dart
+++ b/pkg/analysis_server/test/services/correction/assist_test.dart
@@ -38,7 +38,7 @@
   String resultCode;
   LinkedEditGroup linkedPositionGroup;
 
-  bool get omitNew => false;
+  bool get omitNew => true;
 
   /**
    * Asserts that there is an [Assist] of the given [kind] at [offset] which
@@ -3672,7 +3672,7 @@
 import 'package:flutter/widgets.dart';
 class FakeFlutter {
   main() {
-    return /*caret*/new Container();
+    return /*caret*/Container();
   }
 }
 ''');
@@ -3681,7 +3681,7 @@
 import 'package:flutter/widgets.dart';
 class FakeFlutter {
   main() {
-    return /*caret*/new Center(child: new Container());
+    return /*caret*/Center(child: Container());
   }
 }
 ''');
@@ -3714,7 +3714,7 @@
 }
 
 main() {
-  return /*caret*/Center(child: MyWidget.named());
+  return Center(child: MyWidget./*caret*/named());
 }
 ''');
     } else {
@@ -3855,7 +3855,7 @@
 
 main() {
   return Container(
-    child: /*caret*/new Text('aaa'),
+    child: /*caret*/Text('aaa'),
   );
 }
 ''');
@@ -3865,9 +3865,9 @@
 
 main() {
   return Container(
-    child: /*caret*/new Column(
+    child: /*caret*/Column(
       children: <Widget>[
-        new Text('aaa'),
+        Text('aaa'),
       ],
     ),
   );
diff --git a/pkg/analyzer_plugin/lib/src/utilities/completion/optype.dart b/pkg/analyzer_plugin/lib/src/utilities/completion/optype.dart
index a7a52c0..da284db 100644
--- a/pkg/analyzer_plugin/lib/src/utilities/completion/optype.dart
+++ b/pkg/analyzer_plugin/lib/src/utilities/completion/optype.dart
@@ -121,6 +121,16 @@
     optype.inStaticMethodBody =
         mthDecl is MethodDeclaration && mthDecl.isStatic;
 
+    // If a value should be suggested, suggest also constructors.
+    if (optype.includeReturnValueSuggestions) {
+      // Careful: in angular plugin, `target.unit` may be null!
+      CompilationUnitElement unitElement = target.unit?.element;
+      if (unitElement != null &&
+          unitElement.context.analysisOptions.previewDart2) {
+        optype.includeConstructorSuggestions = true;
+      }
+    }
+
     // Compute the type required by the context and set filters.
     optype._computeRequiredTypeAndFilters(target);