calculateDiff static method
Implementation
static Future<FileLineDiff> calculateDiff({
required File thisFile,
required String thisName,
required int thisLineNo,
Function()? onThisLineDoesntExist,
required File otherFile,
required String otherName,
}) async {
bool thisExists = thisFile.existsSync();
bool otherExists = otherFile.existsSync();
if (!thisExists && !otherExists) {
return FileLineDiff(
thisPath: thisFile.path,
thisName: thisName,
otherPath: otherFile.path,
otherName: otherName,
thisLineNo: thisLineNo,
thisLine: null,
otherLine: null,
diffType: DiffType.same,
);
}
List<String> thisLines = thisExists ? thisFile.readAsLinesSync() : [];
List<String> otherLines = otherExists ? otherFile.readAsLinesSync() : [];
if (!thisExists) {
return FileLineDiff(
thisPath: thisFile.path,
thisName: thisName,
otherPath: otherFile.path,
otherName: otherName,
thisLineNo: thisLineNo,
thisLine: null,
otherLine: otherLines[thisLineNo],
diffType: DiffType.delete,
);
}
if (!otherExists) {
return FileLineDiff(
thisPath: thisFile.path,
thisName: thisName,
otherPath: otherFile.path,
otherName: otherName,
thisLineNo: thisLineNo,
thisLine: thisLines[thisLineNo],
otherLine: null,
diffType: DiffType.insert,
);
}
if (thisLineNo > thisLines.length - 1) {
onThisLineDoesntExist?.call();
return FileLineDiff(
thisPath: thisFile.path,
thisName: thisName,
otherPath: otherFile.path,
otherName: otherName,
thisLineNo: thisLineNo,
thisLine: null,
otherLine: otherLines[thisLineNo],
diffType: DiffType.delete,
);
}
String thisLine = thisLines[thisLineNo];
if (thisLineNo > otherLines.length - 1) {
return FileLineDiff(
thisPath: thisFile.path,
thisName: thisName,
otherPath: otherFile.path,
otherName: otherName,
thisLineNo: thisLineNo,
thisLine: thisLine,
otherLine: null,
diffType: DiffType.insert,
);
}
String otherLine = otherLines[thisLineNo];
// int diffScore = await levenshteinDistance(thisLine, otherLine);
DiffType diffType = thisLine == otherLine ? DiffType.same : DiffType.modify;
return FileLineDiff(
thisPath: thisFile.path,
thisName: thisName,
otherPath: otherFile.path,
otherName: otherName,
thisLineNo: thisLineNo,
thisLine: thisLine,
otherLine: otherLine,
diffType: diffType,
);
}