compareLines method

  1. @override
Iterable<String> compareLines(
  1. Iterable<String> source,
  2. Iterable<String> target, {
  3. String? sourceLabel,
  4. String? targetLabel,
})
override

Compares two iterables of lines from source to target.

The optional arguments sourceLabel and targetLabel are used by some differs to print additional information about where the data is coming from, i.e. the filename and the last modification date.

Implementation

@override
Iterable<String> compareLines(
    Iterable<String> source, Iterable<String> target,
    {String? sourceLabel, String? targetLabel}) sync* {
  if (sourceLabel != null) yield '--- $sourceLabel';
  if (targetLabel != null) yield '+++ $targetLabel';
  final matcher = SequenceMatcher(source: source, target: target);
  for (final group in matcher.groupedOperations(context: context)) {
    final sourceRange = _range(group.first.sourceStart, group.last.sourceEnd);
    final targetRange = _range(group.first.targetStart, group.last.targetEnd);
    yield '@@ -$sourceRange +$targetRange @@';
    for (final operation in group) {
      if (operation.type == OperationType.equal) {
        yield* _dump(' ', source, operation.sourceStart, operation.sourceEnd);
      }
      if (operation.type == OperationType.replace ||
          operation.type == OperationType.delete) {
        yield* _dump('-', source, operation.sourceStart, operation.sourceEnd);
      }
      if (operation.type == OperationType.replace ||
          operation.type == OperationType.insert) {
        yield* _dump('+', target, operation.targetStart, operation.targetEnd);
      }
    }
  }
}