defaultSplitSpanAtIndex method

List<InlineSpan> defaultSplitSpanAtIndex(
  1. SplitAtIndex index, {
  2. required bool ignoreFloatedWidgetSpans,
  3. required TextSpan copyWithTextSpan(
    1. TextSpan span,
    2. String? text,
    3. List<InlineSpan>? children
    ),
})

Implementation

List<InlineSpan> defaultSplitSpanAtIndex(
  SplitAtIndex index, {
  required bool ignoreFloatedWidgetSpans,
  required TextSpan Function(
          TextSpan span, String? text, List<InlineSpan>? children)
      copyWithTextSpan,
}) {
  if (index.value == 0) return [this];

  final span = this;
  if (span is TextSpan) {
    final text = span.text;
    if (text != null && text.isNotEmpty) {
      if (index.value >= text.length) {
        index.value -= text.length;
      } else {
        final result = [
          copyWithTextSpan(span, text.substring(0, index.value), null),
          copyWithTextSpan(span, text.substring(index.value), span.children),
        ];
        index.value = 0;
        return result;
      }
    }

    final children = span.children;
    if (children != null && children.isNotEmpty) {
      // If the text.length was equal to index.value, split the text and
      // children.
      if (index.value == 0) {
        return [
          copyWithTextSpan(span, text, null),
          copyWithTextSpan(span, null, span.children),
        ];
      }

      final result = children.splitAtCharacterIndex(index,
          ignoreFloatedWidgetSpans: ignoreFloatedWidgetSpans);

      if (index.value == 0) {
        if (result.length == 2) {
          return [
            copyWithTextSpan(span, text, result.first),
            copyWithTextSpan(span, null, result.last),
          ];
        } else if (result.length == 1) {
          // Only true if the number of characters in all the children was
          // equal to index.value.
          assert(listEquals<InlineSpan>(result.first, children));
        } else {
          assert(false);
        }
      }
    }
  } else if (span is WidgetSpan) {
    if (!ignoreFloatedWidgetSpans &&
        span.child is Floatable &&
        (span.child as Floatable).float != FCFloat.none) {
      index.value += 1;
    } else {
      index.value -= 1;
    }
  } else {
    assert(false);
  }

  return [this];
}