retain method

  1. @override
void retain(
  1. int index,
  2. int? length,
  3. NotusStyle? style
)
override

Format length characters of this node starting from index with specified style style.

Implementation

@override
void retain(int index, int? length, NotusStyle? style) {
  if (style == null) return;
  final thisLength = this.length;

  final local = math.min(thisLength - index, length!);
  // If index is at newline character then this is a line/block style update.
  final isLineFormat = (index + local == thisLength) && local == 1;

  if (isLineFormat) {
    assert(
        style.values.every((attr) => attr.scope == NotusAttributeScope.line),
        'It is not allowed to apply inline attributes to line itself.');
    _formatAndOptimize(style);
  } else {
    // Otherwise forward to children as it's an inline format update.
    assert(index + local != thisLength,
        'It is not allowed to apply inline attributes to line itself.');
    assert(style.values
        .every((attr) => attr.scope == NotusAttributeScope.inline));
    super.retain(index, local, style);
  }

  final remaining = length - local;
  if (remaining > 0) {
    assert(nextLine != null);
    nextLine!.retain(0, remaining, style);
  }
}