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) {
    eraseCell(start - 1, style);
  }

  if (start + count < _length) {
    final moveStart = start * _cellSize;
    final moveEnd = (_length - count) * _cellSize;
    final moveOffset = count * _cellSize;
    for (var i = moveEnd - 1; i >= moveStart; i--) {
      _data[i + moveOffset] = _data[i];
    }
  }

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

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

  // Update anchors, move anchors that are after the inserted range.
  for (var i = 0; i < _anchors.length; 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();
      }
    }
  }
}