apply method
Returns original, the content of file, with this change applied.
Implementation
@override
Future<String> apply(String original) async {
final result = parseString(content: original);
final unit = result.unit;
final body = functionBodyIn(unit, function);
final childrenMatches = <ListLiteral>[];
body.visitChildren(
NamedListVisitor(
name: listVariableMatch.replaceAll(':', ''),
parentMatch: parentExpressionMatch,
onMatch: childrenMatches.add,
),
);
if (childrenMatches.length <= index) {
throw Exception('List match not found at index $index');
}
final targetList = childrenMatches[index];
final elements = ListInsert.parse(insert);
if (elements?.isIn(targetList) ?? false) return original;
final first = targetList.elements.firstOrNull;
final insertion = first == null
? insertionAfter(original, targetList.leftBracket, insert)
// A comma separates the insert from the element that follows it.
: insertionBefore(
original,
first.beginToken,
elements?.withTrailingComma(comma: true) ?? insert,
);
return dartFormater.format(applyInsertions(original, [insertion]));
}