areFileEditsEquivalent function

bool areFileEditsEquivalent(
  1. List<FileEdit> edits1,
  2. List<FileEdit> edits2,
  3. String originalContent
)

Compare two sets of edits to determine if they are equivalent by applying both sets to the original content and comparing results.

Implementation

bool areFileEditsEquivalent(
  List<FileEdit> edits1,
  List<FileEdit> edits2,
  String originalContent,
) {
  // Fast path: check if edits are literally identical
  if (edits1.length == edits2.length) {
    var allMatch = true;
    for (var i = 0; i < edits1.length; i++) {
      if (edits1[i].oldString != edits2[i].oldString ||
          edits1[i].newString != edits2[i].newString ||
          edits1[i].replaceAll != edits2[i].replaceAll) {
        allMatch = false;
        break;
      }
    }
    if (allMatch) return true;
  }

  // Try applying both sets of edits
  String? result1;
  String? error1;
  String? result2;
  String? error2;

  try {
    result1 = applyEditsToFile(originalContent, edits1);
  } catch (e) {
    error1 = e.toString();
  }

  try {
    result2 = applyEditsToFile(originalContent, edits2);
  } catch (e) {
    error2 = e.toString();
  }

  // If both threw errors, they are equal only if errors are the same
  if (error1 != null && error2 != null) return error1 == error2;
  // If one threw and the other didn't, not equal
  if (error1 != null || error2 != null) return false;
  // Both succeeded -- compare results
  return result1 == result2;
}