calculateDiff static method

Future<FileDiff> calculateDiff({
  1. required File thisFile,
  2. required String thisName,
  3. required File otherFile,
  4. required String otherName,
})

Implementation

static Future<FileDiff> calculateDiff({
  required File thisFile,
  required String thisName,
  required File otherFile,
  required String otherName,
}) async {
  if (!thisFile.existsSync()) {
    return FileDiff(
      thisFile: thisFile,
      thisName: thisName,
      otherFile: otherFile,
      otherName: otherName,
      linesDiff: {},
      diffType: DiffType.delete,
    );
  }

  List<String> thisTotalLines = thisFile.readAsLinesSync();
  if (!otherFile.existsSync()) {
    Map<int, FileLineDiff> linesDiff = {};
    for (int i = 0; i < thisTotalLines.length; i++) {
      int lineNo = i + 1;
      linesDiff.putIfAbsent(
        lineNo,
        () => FileLineDiff(
          thisPath: thisFile.path,
          thisName: thisName,
          otherPath: otherFile.path,
          otherName: otherName,
          thisLineNo: lineNo,
          thisLine: thisTotalLines[lineNo],
          otherLine: null,
          diffType: DiffType.insert,
        ),
      );
    }

    return FileDiff(
      thisFile: thisFile,
      thisName: thisName,
      otherFile: otherFile,
      otherName: otherName,
      linesDiff: linesDiff,
      diffType: DiffType.insert,
    );
  }

  Map<int, FileLineDiff> linesDiff = {};
  int otherLines = otherFile.readAsLinesSync().length;
  int maxLines = max(thisTotalLines.length, otherLines);

  for (int lineNo = 0; lineNo < maxLines; lineNo++) {
    FileLineDiff fileLineDiff = await FileLineDiff.calculateDiff(
      thisFile: thisFile,
      thisName: thisName,
      thisLineNo: lineNo,
      otherFile: otherFile,
      otherName: otherName,
    );
    linesDiff.putIfAbsent(lineNo, () => fileLineDiff);
  }

  return FileDiff(
    thisFile: thisFile,
    thisName: thisName,
    otherFile: otherFile,
    otherName: otherName,
    linesDiff: linesDiff,
    diffType: DiffType.modify,
  );
}