insertCells method

void insertCells(
  1. int start,
  2. int count, [
  3. CursorStyle? style
])

Inserts count cells at start. New cells are initialized with style.

Implementation

void insertCells(int start, int count, [CursorStyle? style]) {
  style ??= CursorStyle.empty;

  if (start > 0 && getWidth(start - 1) == 2) {
    _eraseCellRaw(start - 1, style);
  }

  if (start + count < _length) {
    final moveStart = start * _cellSize;
    final moveEnd = (_length - count) * _cellSize;
    final moveOffset = count * _cellSize;
    _data.setRange(moveStart + moveOffset, moveEnd + moveOffset, _data, moveStart);
    // Content shifted right: the high-water mark moves with it.
    if (_occ > start) _occ = min(_occ + count, _length);
  }

  final end = min(start + count, _length);
  for (var i = start; i < end; i++) {
    _eraseCellRaw(i, style);
  }

  if (getWidth(_length - 1) == 2) {
    _eraseCellRaw(_length - 1, style);
  }

  // Update anchors, move anchors that are after the inserted range.
  // Iterate backwards: disposing an anchor removes it from [_anchors].
  for (var i = _anchors.length - 1; i >= 0; i--) {
    final anchor = _anchors[i];
    if (anchor.x >= start + count) {
      anchor.reposition(anchor.x + count);

      // Remove anchors that are now outside the buffer.
      if (anchor.x >= _length) {
        anchor.dispose();
      }
    }
  }
  version++;
}