blob: 5fcd235c3ae98412d17a220828c2961efb42f5b6 [file] [log] [blame]
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analysis_server/src/services/correction/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
class FlutterWrapGeneric extends CorrectionProducer {
@override
AssistKind get assistKind => DartAssistKind.FLUTTER_WRAP_GENERIC;
@override
Future<void> compute(ChangeBuilder builder) async {
if (node is! ListLiteral) {
return;
}
if ((node as ListLiteral).elements.any((CollectionElement exp) =>
!(exp is InstanceCreationExpression &&
flutter.isWidgetCreation(exp)))) {
return;
}
var literalSrc = utils.getNodeText(node);
var newlineIdx = literalSrc.lastIndexOf(eol);
if (newlineIdx < 0 || newlineIdx == literalSrc.length - 1) {
return; // Lists need to be in multi-line format already.
}
var indentOld = utils.getLinePrefix(node.offset + eol.length + newlineIdx);
var indentArg = '$indentOld${utils.getIndent(1)}';
var indentList = '$indentOld${utils.getIndent(2)}';
await builder.addDartFileEdit(file, (builder) {
builder.addReplacement(range.node(node), (builder) {
builder.write('[');
builder.write(eol);
builder.write(indentArg);
builder.addSimpleLinkedEdit('WIDGET', 'widget');
builder.write('(');
builder.write(eol);
builder.write(indentList);
// Linked editing not needed since arg is always a list.
builder.write('children: ');
builder.write(literalSrc.replaceAll(
RegExp('^$indentOld', multiLine: true), '$indentList'));
builder.write(',');
builder.write(eol);
builder.write(indentArg);
builder.write('),');
builder.write(eol);
builder.write(indentOld);
builder.write(']');
});
});
}
/// Return an instance of this class. Used as a tear-off in `AssistProcessor`.
static FlutterWrapGeneric newInstance() => FlutterWrapGeneric();
}