[analysis_server] Handle prefixed Flutter widget creation expressions for assists

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

Change-Id: I3b462c69520593cbb9db2dc30ae19b8c93deed20
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/260601
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Danny Tuppeny 2022-09-22 18:28:28 +00:00 committed by Commit Bot
parent eb17780ef1
commit 3e9a99df6a
2 changed files with 60 additions and 12 deletions

View file

@ -231,21 +231,23 @@ class Flutter {
/// Return the instance creation expression that surrounds the given
/// [node], if any, else null. The [node] may be the instance creation
/// expression itself or the identifier that names the constructor.
/// expression itself or an (optionally prefixed) identifier that names the
/// constructor.
InstanceCreationExpression? identifyNewExpression(AstNode? node) {
InstanceCreationExpression? newExpr;
if (node is SimpleIdentifier) {
var parent = node.parent;
var grandParent = parent?.parent;
var greatGrandParent = grandParent?.parent;
if (parent is ConstructorName &&
grandParent is InstanceCreationExpression) {
newExpr = grandParent;
} else if (grandParent is ConstructorName &&
greatGrandParent is InstanceCreationExpression) {
newExpr = greatGrandParent;
}
} else if (node is InstanceCreationExpression) {
node = node.parent;
}
if (node is PrefixedIdentifier) {
node = node.parent;
}
if (node is NamedType) {
node = node.parent;
}
if (node is ConstructorName) {
node = node.parent;
}
if (node is InstanceCreationExpression) {
newExpr = node;
}
return newExpr;

View file

@ -301,6 +301,52 @@ void f() {
],
);
}
''');
}
Future<void> test_prefixedConstructor_onConstructor() async {
await resolveTestCode('''
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as m;
void f() {
Center(
child: m./*caret*/Center(
child: Text(''),
),
);
}
''');
await assertHasAssist('''
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as m;
void f() {
Center(
child: Text(''),
);
}
''');
}
Future<void> test_prefixedConstructor_onPrefix() async {
await resolveTestCode('''
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as m;
void f() {
Center(
child: /*caret*/m.Center(
child: Text(''),
),
);
}
''');
await assertHasAssist('''
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as m;
void f() {
Center(
child: Text(''),
);
}
''');
}
}