inferDiffWithCursorHint function

  1. @visibleForTesting
Diff? inferDiffWithCursorHint(
  1. String previous,
  2. String current,
  3. int cursor
)

Implementation

@visibleForTesting
Diff? inferDiffWithCursorHint(String previous, String current, int cursor) {
  if (previous.length < current.length) {
    int numAdded = current.length - previous.length;
    int offset = cursor - numAdded;
    if (offset >= 0 &&
        current.substring(0, offset) + current.substring(cursor) == previous) {
      _maybePrint('inferDiffWithCursorHint: AddedChars(offset: $offset, '
          'addedChars: ${current.substring(offset, cursor)})');
      return AddedChars(
          offset: offset, addedChars: current.substring(offset, cursor));
    }
  } else if (previous.length > current.length) {
    int numRemoved = previous.length - current.length;
    int endOffset = cursor + numRemoved;
    if (endOffset <= previous.length &&
        previous.substring(0, cursor) + previous.substring(endOffset) ==
            current) {
      _maybePrint('inferDiffWithCursorHint: RemovedChars(offset: $cursor, '
          'numChars: $numRemoved)');
      return RemovedChars(offset: cursor, numChars: numRemoved);
    }
  }

  return null;
}