Skip to content

Commit a40403e

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] New assists for wrap with Expanded/Flexible
[email protected] Fixes #56648 Change-Id: I34a697d914e3a480105a4388d77178a96425893a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390622 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 10759dd commit a40403e

File tree

7 files changed

+783
-2
lines changed

7 files changed

+783
-2
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ abstract final class DartAssistKind {
287287
DartAssistKindPriority.FLUTTER_WRAP_SPECIFIC,
288288
'Wrap with Container',
289289
);
290+
static const FLUTTER_WRAP_EXPANDED = AssistKind(
291+
'dart.assist.flutter.wrap.expanded',
292+
DartAssistKindPriority.FLUTTER_WRAP_SPECIFIC,
293+
'Wrap with Expanded',
294+
);
295+
static const FLUTTER_WRAP_FLEXIBLE = AssistKind(
296+
'dart.assist.flutter.wrap.flexible',
297+
DartAssistKindPriority.FLUTTER_WRAP_SPECIFIC,
298+
'Wrap with Flexible',
299+
);
290300
static const FLUTTER_WRAP_PADDING = AssistKind(
291301
'dart.assist.flutter.wrap.padding',
292302
DartAssistKindPriority.FLUTTER_WRAP_SPECIFIC,

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:analysis_server/src/services/correction/selection_analyzer.dart'
77
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
88
import 'package:analyzer/dart/ast/ast.dart';
99
import 'package:analyzer/dart/element/element2.dart';
10+
import 'package:analyzer/dart/element/type.dart';
1011
import 'package:analyzer/source/source_range.dart';
1112
import 'package:analyzer/src/dart/ast/extensions.dart';
1213
import 'package:analyzer/src/utilities/extensions/flutter.dart';
@@ -30,6 +31,14 @@ class FlutterWrap extends MultiCorrectionProducer {
3031
if (!widgetType.isExactWidgetTypeContainer) {
3132
producers.add(_FlutterWrapContainer(widgetExpr, context: context));
3233
}
34+
if (!widgetType.isExactWidgetTypeExpanded &&
35+
(widgetExpr.isParentFlexWidget || !widgetExpr.isParentWidget)) {
36+
producers.add(_FlutterWrapExpanded(widgetExpr, context: context));
37+
}
38+
if (!widgetType.isExactWidgetTypeFlexible &&
39+
(widgetExpr.isParentFlexWidget || !widgetExpr.isParentWidget)) {
40+
producers.add(_FlutterWrapFlexible(widgetExpr, context: context));
41+
}
3342
if (!widgetType.isExactWidgetTypePadding) {
3443
producers.add(_FlutterWrapPadding(widgetExpr, context: context));
3544
}
@@ -138,6 +147,36 @@ class _FlutterWrapContainer extends _WrapSingleWidget {
138147
String get _parentLibraryUri => widgetsUri;
139148
}
140149

150+
/// A correction processor that can make one of the possible changes computed by
151+
/// the [FlutterWrap] producer.
152+
class _FlutterWrapExpanded extends _WrapSingleWidget {
153+
_FlutterWrapExpanded(super.widgetExpr, {required super.context});
154+
155+
@override
156+
AssistKind get assistKind => DartAssistKind.FLUTTER_WRAP_EXPANDED;
157+
158+
@override
159+
String get _parentClassName => 'Expanded';
160+
161+
@override
162+
String get _parentLibraryUri => widgetsUri;
163+
}
164+
165+
/// A correction processor that can make one of the possible changes computed by
166+
/// the [FlutterWrap] producer.
167+
class _FlutterWrapFlexible extends _WrapSingleWidget {
168+
_FlutterWrapFlexible(super.widgetExpr, {required super.context});
169+
170+
@override
171+
AssistKind get assistKind => DartAssistKind.FLUTTER_WRAP_FLEXIBLE;
172+
173+
@override
174+
String get _parentClassName => 'Flexible';
175+
176+
@override
177+
String get _parentLibraryUri => widgetsUri;
178+
}
179+
141180
/// A correction processor that can make one of the possible changes computed by
142181
/// the [FlutterWrap] producer.
143182
class _FlutterWrapGeneric extends _WrapSingleWidget {
@@ -350,3 +389,53 @@ abstract class _WrapSingleWidget extends ResolvedCorrectionProducer {
350389
});
351390
}
352391
}
392+
393+
extension on Expression {
394+
/// Return `true` if the parent is a `Flex` widget creation.
395+
///
396+
/// This is used to determine if the widget is wrapped in a `Row`, `Column`,
397+
/// or `Flex` widget.
398+
bool get isParentFlexWidget {
399+
var parent = _getParentInstanceCreationExpression();
400+
if (parent == null || !parent.isWidgetCreation) {
401+
return false;
402+
}
403+
return parent.staticType.isWidgetFlexType;
404+
}
405+
406+
/// Return `true` if the parent is a widget creation.
407+
///
408+
/// This tells if this is a direct child of a widget creation.
409+
/// It will return `false` if we are assigning this to a variable or
410+
/// returning it from a function or other similar cases.
411+
bool get isParentWidget {
412+
var parent = _getParentInstanceCreationExpression();
413+
return parent != null && parent.isWidgetCreation;
414+
}
415+
416+
/// Return the parent `InstanceCreationExpression` if it exists.
417+
///
418+
/// This is used to find the parent widget creation if it exists.
419+
InstanceCreationExpression? _getParentInstanceCreationExpression() {
420+
var self = this;
421+
NamedExpression? namedExpression;
422+
if (self.parent case ListLiteral listLiteral) {
423+
if (listLiteral.parent case NamedExpression parent) {
424+
namedExpression = parent;
425+
}
426+
}
427+
// NamedExpression (child:), ArgumentList, InstanceCreationExpression
428+
if ((namedExpression ?? self.parent)?.parent?.parent
429+
case InstanceCreationExpression parent?) {
430+
return parent;
431+
}
432+
return null;
433+
}
434+
}
435+
436+
extension on DartType? {
437+
bool get isWidgetFlexType {
438+
var self = this;
439+
return self is InterfaceType && self.element3.isFlexWidget;
440+
}
441+
}

0 commit comments

Comments
 (0)