findOrReplace method

  1. @override
ConstructorManagerResultData findOrReplace({
  1. String? replacementText,
})
override

Implementation

@override
ConstructorManagerResultData findOrReplace({String? replacementText}) {
  final finder = ConstructorVisitor(className, constructorName!);
  parsedFileUnit.visitChildren(finder);

  // final task = dataSingleton.currentFnAsTask();

  if (finder.targetNode != null) {
    final node = finder.targetNode!;
    final offset = node.offset;
    final end = node.end;

    // 4. Define the replacement
    // Perhaps we want to rename the event or add a parameter
    if (replacementText != null) {
      // 5. Replace
      final newSource = parsedFileContent.replaceRange(
        node.offset,
        node.end,
        replacementText.trim(),
      );

      dataSingleton.addTaskResultValue({
        "foundNode": node.toSource(),
        "foundOffset": offset,
        "foundEnd": end,
        "newSourceCode": newSource,
      });
      return dataSingleton;
    }

    dataSingleton.addTaskResultValue({
      "foundNode": node.toSource(),
      "foundOffset": offset,
      "foundEnd": end,
    });
    return dataSingleton;
  }
  // node not found
  // printWarning('[findOrReplace] constructor "$fullIdentifier" not found.');
  // dataInstance.update(foundNode: null, foundOffset: 0, foundEnd: 0);
  dataSingleton.addTaskResultValue({
    "foundNode": null,
    "foundOffset": 0,
    "foundEnd": 0,
  });
  return dataSingleton;
}