copyCells method

String copyCells(
  1. List<Pair<int, int>> origins,
  2. List<Pair<int, int>> destinations
)

It takes two lists of pairs of integers, and returns a string that represents the code that will be executed by the interpreter

Args: origins (List<Pair<int, int>>): The list of cells that will be copied. destinations (List<Pair<int, int>>): The list of cells to be copied to.

Implementation

String copyCells(
  List<Pair<int, int>> origins,
  List<Pair<int, int>> destinations,
) {
  final List<String> copyCommandsBuffer = List<String>.from(
    CatInterpreter().copyCommandsBuffer,
  );

  final List<String> destinationPosition = <String>[];
  for (final Pair<int, int> i in destinations) {
    destinationPosition.add("${rows[i.first]}${i.second + 1}");
  }
  String code = "";
  if (copyCommandsBuffer.isNotEmpty) {
    final String firstDestination =
        copyCommandsBuffer.removeAt(0).replaceAll(RegExp("[go()]"), "");
    destinationPosition.insert(0, firstDestination);
    code = "COPY({${copyCommandsBuffer.joinToString(separator: ",")}},"
        "{${destinationPosition.joinToString(separator: ",")}})";
    copyCommandsBuffer.clear();
  } else {
    final List<String> originsPosition = <String>[];
    for (final Pair<int, int> i in origins) {
      originsPosition.add("${rows[i.first]}${i.second + 1}");
    }
    code = "COPY({${originsPosition.joinToString(separator: ",")}},"
        "{${destinationPosition.joinToString(separator: ",")}})";
  }

  return code;
}