calculateDiff static method

Future<FileLineDiff> calculateDiff({
  1. required File thisFile,
  2. required int thisLineNo,
  3. dynamic onThisLineDoesntExist()?,
  4. required File otherFile,
})

Implementation

static Future<FileLineDiff> calculateDiff({
  required File thisFile,
  required int thisLineNo,
  Function()? onThisLineDoesntExist,
  required File otherFile,
}) async {
  if (!thisFile.existsSync()) {
    return FileLineDiff(
      thisPath: thisFile.path,
      otherPath: otherFile.path,
      thisLineNo: thisLineNo,
      diffScore: maxDiffScore,
      diffType: DiffType.delete,
    );
  }

  List<String> thisTotalLines = thisFile.readAsLinesSync();
  if (thisLineNo > thisTotalLines.length - 1) {
    onThisLineDoesntExist?.call();
    return FileLineDiff(
      thisPath: thisFile.path,
      otherPath: otherFile.path,
      thisLineNo: thisLineNo,
      diffScore: maxDiffScore,
      diffType: DiffType.delete,
    );
  }
  String thisLine = thisTotalLines[thisLineNo];
  if (!otherFile.existsSync()) {
    return FileLineDiff(
      thisPath: thisFile.path,
      otherPath: otherFile.path,
      thisLineNo: thisLineNo,
      diffScore: maxDiffScore,
      diffType: DiffType.insert,
    );
  }

  List<String> otherTotalLines = otherFile.readAsLinesSync();
  if (thisLineNo > otherTotalLines.length - 1) {
    return FileLineDiff(
      thisPath: thisFile.path,
      otherPath: otherFile.path,
      thisLineNo: thisLineNo,
      diffScore: maxDiffScore,
      diffType: DiffType.insert,
    );
  }

  String otherLine = otherTotalLines[thisLineNo];
  int diffScore = await levenshteinDistance(thisLine, otherLine);

  return FileLineDiff(
    thisPath: thisFile.path,
    otherPath: otherFile.path,
    thisLineNo: thisLineNo,
    diffScore: diffScore,
    diffType: diffScore == 0 ? DiffType.same : DiffType.modify,
  );
}