Issue 52219. Fix AddTypeAnnotation for const literals.

Bug: https://github.com/dart-lang/sdk/issues/52219
Change-Id: Iab4d63142f08789fa7bd1b46e2f4ba8e696a1fe8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/300162
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart
index 21b63ad..0ff5f79 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart
@@ -59,16 +59,7 @@
     }
 
     if (node is TypedLiteral) {
-      final type = node.staticType;
-      if (type is InterfaceType) {
-        await builder.addDartFileEdit(file, (builder) {
-          builder.addInsertion(node.offset, (builder) {
-            builder.write('<');
-            builder.writeTypes(type.typeArguments);
-            builder.write('>');
-          });
-        });
-      }
+      await _typedLiteral(builder, node);
       return;
     }
 
@@ -193,6 +184,31 @@
     await _applyChange(builder, declarationList.keyword, variable.name, type);
   }
 
+  Future<void> _typedLiteral(ChangeBuilder builder, TypedLiteral node) async {
+    final type = node.staticType;
+    if (type is! InterfaceType) {
+      return;
+    }
+
+    final int offset;
+    switch (node) {
+      case ListLiteral():
+        offset = node.leftBracket.offset;
+      case SetOrMapLiteral():
+        offset = node.leftBracket.offset;
+      default:
+        return;
+    }
+
+    await builder.addDartFileEdit(file, (builder) {
+      builder.addInsertion(offset, (builder) {
+        builder.write('<');
+        builder.writeTypes(type.typeArguments);
+        builder.write('>');
+      });
+    });
+  }
+
   DartType? _typeForVariable(VariableDeclaration variable) {
     var initializer = variable.initializer;
     if (initializer != null) {
diff --git a/pkg/analysis_server/test/src/services/correction/fix/add_type_annotation_test.dart b/pkg/analysis_server/test/src/services/correction/fix/add_type_annotation_test.dart
index 685072f..0ddc345 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/add_type_annotation_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/add_type_annotation_test.dart
@@ -280,6 +280,19 @@
 ''');
   }
 
+  Future<void> test_setOrMapLiteral_map_const() async {
+    await resolveTestCode('''
+void f() {
+  (const {0: true});
+}
+''');
+    await assertHasFix(r'''
+void f() {
+  (const <int, bool>{0: true});
+}
+''');
+  }
+
   Future<void> test_setOrMapLiteral_set() async {
     await resolveTestCode('''
 void f() {
@@ -292,6 +305,19 @@
 }
 ''');
   }
+
+  Future<void> test_setOrMapLiteral_set_const() async {
+    await resolveTestCode('''
+void f() {
+  (const {0, 1, 2});
+}
+''');
+    await assertHasFix(r'''
+void f() {
+  (const <int>{0, 1, 2});
+}
+''');
+  }
 }
 
 @reflectiveTest