Skip to content

Commit 4aa8b10

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Makes "Add type annotation" consider explicit type arguments
[email protected] Fixes: #60080 Change-Id: I88bb6c3ece6f6a95bc22aa57e34aa16afa34bc7e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412540 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent e8c8500 commit 4aa8b10

File tree

4 files changed

+75
-14
lines changed

4 files changed

+75
-14
lines changed

pkg/analysis_server/lib/src/services/correction/dart/add_type_annotation.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ class AddTypeAnnotation extends ResolvedCorrectionProducer {
5555
return;
5656
}
5757

58-
if (node is TypedLiteral) {
58+
if (node case TypedLiteral(
59+
:var typeArguments,
60+
) when typeArguments == null || typeArguments.isSynthetic) {
5961
await _typedLiteral(builder, node);
6062
return;
6163
}
@@ -205,7 +207,7 @@ class AddTypeAnnotation extends ResolvedCorrectionProducer {
205207
await builder.addDartFileEdit(file, (builder) {
206208
builder.addInsertion(offset, (builder) {
207209
builder.write('<');
208-
builder.writeTypes(type.typeArguments);
210+
builder.writeTypes(type.typeArguments, shouldWriteDynamic: true);
209211
builder.write('>');
210212
});
211213
});

pkg/analysis_server/test/src/services/correction/assist/add_type_annotation_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,38 @@ void f() {
688688
await assertNoAssistAt('var ');
689689
}
690690

691+
Future<void> test_mapLiteral_notype() async {
692+
await resolveTestCode('''
693+
var map = {};
694+
''');
695+
await assertHasAssistAt('{}', '''
696+
var map = <dynamic, dynamic>{};
697+
''');
698+
}
699+
700+
Future<void> test_mapLiteral_writtenAnnotation() async {
701+
await resolveTestCode('''
702+
var map = <String, int>{};
703+
''');
704+
await assertNoAssistAt('{}');
705+
}
706+
707+
Future<void> test_mapLiteral_writtenAnnotation2() async {
708+
await resolveTestCode('''
709+
var map = <String, int>{};
710+
''');
711+
await assertNoAssistAt('}');
712+
}
713+
714+
Future<void> test_mapLiteral_writtenStaticType() async {
715+
await resolveTestCode('''
716+
Map<String, int> map = {};
717+
''');
718+
await assertHasAssistAt('{}', '''
719+
Map<String, int> map = <String, int>{};
720+
''');
721+
}
722+
691723
Future<void> test_parameter() async {
692724
await resolveTestCode('''
693725
foo(f(int p)) {}

pkg/analyzer_plugin/lib/src/utilities/change_builder/change_builder_dart.dart

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -792,18 +792,27 @@ class DartEditBuilderImpl extends EditBuilderImpl implements DartEditBuilder {
792792
String? groupName,
793793
ExecutableElement2? methodBeingCopied,
794794
bool required = false,
795+
bool shouldWriteDynamic = false,
795796
}) {
796797
var wroteType = false;
797-
if (type != null && type is! DynamicType) {
798+
if (type != null && (shouldWriteDynamic || type is! DynamicType)) {
798799
if (groupName != null) {
799800
addLinkedEdit(groupName, (LinkedEditBuilder builder) {
800-
wroteType = _writeType(type, methodBeingCopied: methodBeingCopied);
801+
wroteType = _writeType(
802+
type,
803+
methodBeingCopied: methodBeingCopied,
804+
required: shouldWriteDynamic,
805+
);
801806
if (wroteType && addSupertypeProposals) {
802807
_addSuperTypeProposals(builder, type, {});
803808
}
804809
});
805810
} else {
806-
wroteType = _writeType(type, methodBeingCopied: methodBeingCopied);
811+
wroteType = _writeType(
812+
type,
813+
methodBeingCopied: methodBeingCopied,
814+
required: shouldWriteDynamic,
815+
);
807816
}
808817
}
809818
if (!wroteType && required) {
@@ -839,17 +848,21 @@ class DartEditBuilderImpl extends EditBuilderImpl implements DartEditBuilder {
839848
}
840849

841850
@override
842-
void writeTypes(Iterable<DartType>? types, {String? prefix}) {
851+
void writeTypes(
852+
Iterable<DartType>? types, {
853+
String? prefix,
854+
bool shouldWriteDynamic = false,
855+
}) {
843856
if (types == null || types.isEmpty) {
844857
return;
845858
}
846859
if (prefix != null) {
847860
write(prefix);
848861
}
849-
writeType(types.first);
862+
writeType(types.first, shouldWriteDynamic: shouldWriteDynamic);
850863
for (var type in types.skip(1)) {
851864
write(', ');
852-
writeType(type);
865+
writeType(type, shouldWriteDynamic: shouldWriteDynamic);
853866
}
854867
}
855868

pkg/analyzer_plugin/lib/utilities/change_builder/change_builder_dart.dart

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ abstract class DartEditBuilder implements EditBuilder {
329329

330330
/// Writes the code for a type annotation for the given [type].
331331
///
332+
/// If [shouldWriteDynamic] is `true`, then the keyword `dynamic` will be
333+
/// written if the type is `dynamic`.
334+
///
332335
/// If the [type] is either `null` or represents the type `dynamic`, then the
333336
/// behavior depends on whether a type is [required]. If [required] is `true`,
334337
/// then the keyword `var` will be written; otherwise, nothing is written.
@@ -344,11 +347,14 @@ abstract class DartEditBuilder implements EditBuilder {
344347
/// types.
345348
///
346349
/// Returns `true` if any text was written.
347-
bool writeType(DartType? type,
348-
{bool addSupertypeProposals = false,
349-
String? groupName,
350-
ExecutableElement2? methodBeingCopied,
351-
bool required = false});
350+
bool writeType(
351+
DartType? type, {
352+
bool addSupertypeProposals = false,
353+
String? groupName,
354+
ExecutableElement2? methodBeingCopied,
355+
bool required = false,
356+
bool shouldWriteDynamic = false,
357+
});
352358

353359
/// Writes the code to declare the given [typeParameter].
354360
///
@@ -374,7 +380,15 @@ abstract class DartEditBuilder implements EditBuilder {
374380
///
375381
/// If the list of [types] is `null` or does not contain any types, then
376382
/// nothing will be written.
377-
void writeTypes(Iterable<DartType>? types, {String? prefix});
383+
///
384+
/// If [shouldWriteDynamic] is `true`, then the keyword `dynamic` will be
385+
/// written if the type is `dynamic`. Otherwise, the `dynamic` type will be
386+
/// ommitted.
387+
void writeTypes(
388+
Iterable<DartType>? types, {
389+
String? prefix,
390+
bool shouldWriteDynamic = false,
391+
});
378392
}
379393

380394
/// A [FileEditBuilder] used to build edits for Dart files.

0 commit comments

Comments
 (0)