expandAttributionToSpan method

AttributionSpan expandAttributionToSpan({
  1. required Attribution attribution,
  2. required int offset,
})

Calculates and returns the full AttributionSpan, which contains the given attribution at the given offset.

For example, imagine spans applied to text like this: "Hello, |world!|". The text between the bars has a "bold" attribution. Invoking this method with the "bold" attribution and an offset of 10 would return an AttributionSpan of "bold" from 7 to 14.

Implementation

AttributionSpan expandAttributionToSpan({
  required Attribution attribution,
  required int offset,
}) {
  if (!hasAttributionAt(offset, attribution: attribution)) {
    throw Exception(
        'Tried to expand attribution ($attribution) at offset "$offset" but the given attribution does not exist at that offset.');
  }

  // The following methods should be guaranteed to produce non-null
  // values because we already verified that the given attribution
  // exists at the given offset.
  SpanMarker markerBefore = _getStartingMarkerAtOrBefore(offset, attribution: attribution)!;
  SpanMarker markerAfter = _getEndingMarkerAtOrAfter(markerBefore.offset, attribution: attribution)!;

  return AttributionSpan(
    attribution: attribution,
    start: markerBefore.offset,
    end: markerAfter.offset,
  );
}