addAt method

void addAt({
  1. required AttributedSpans other,
  2. required int index,
})

Pushes back all the spans in other to index, and then appends the other spans to this AttributedSpans.

The index must be greater than the offset of the final marker within this AttributedSpans.

Implementation

void addAt({
  required AttributedSpans other,
  required int index,
}) {
  if (_markers.isNotEmpty && _markers.last.offset >= index) {
    throw Exception(
        'Another AttributedSpans can only be appended after the final marker in this AttributedSpans. Final marker: ${_markers.last}');
  }

  _log.fine('attributions before pushing them:');
  _log.fine(toString());

  // Push back all the `other` markers to make room for the
  // spans we're putting in front of them.

  final pushDistance = index;
  _log.fine('pushing `other` markers by: $pushDistance');
  _log.fine('`other` attributions before pushing them:');
  _log.fine(other.toString());
  final pushedSpans = other.copy()..pushAttributionsBack(pushDistance);

  // Combine `this` and `other` attributions into one list.
  final List<SpanMarker> combinedAttributions = List.from(_markers)..addAll(pushedSpans._markers);
  _log.fine('combined attributions before merge:');
  for (final marker in combinedAttributions) {
    _log.fine('   - $marker');
  }

  // Clean up the boundary between the two lists of attributions
  // by merging compatible attributions that meet at the boundary.
  _mergeBackToBackAttributions(combinedAttributions, index);

  _log.fine('combined attributions after merge:');
  for (final marker in combinedAttributions) {
    _log.fine('   - $marker');
  }

  _markers
    ..clear()
    ..addAll(combinedAttributions);
}