removeCells method

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

Remove count cells starting at start. Cells that are empty after the removal are filled with style.

Implementation

void removeCells(int start, int count, [CursorStyle? style, int? end]) {
  end ??= _length;
  assert(start >= 0 && start < _length);
  assert(end >= start && end <= _length);
  assert(count >= 0 && start + count <= end);

  style ??= CursorStyle.empty;
  final combiningCharacters = Map<int, String>.of(_combiningCharacters);
  final underlineColors = Map<int, int>.of(_underlineColors);

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

  for (var i = end - count; i < end; i++) {
    eraseCell(i, style);
  }

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

  _combiningCharacters.clear();
  _underlineColors.clear();
  for (final entry in combiningCharacters.entries) {
    if (entry.key < start) {
      if (getCodePoint(entry.key) != 0) {
        _combiningCharacters[entry.key] = entry.value;
      }
      continue;
    }

    if (entry.key < start + count) continue;
    final newIndex = entry.key - count;
    if (entry.key < end &&
        newIndex < _length &&
        getCodePoint(newIndex) != 0) {
      _combiningCharacters[newIndex] = entry.value;
      continue;
    }

    if (entry.key >= end && getCodePoint(entry.key) != 0) {
      _combiningCharacters[entry.key] = entry.value;
    }
  }
  for (final entry in underlineColors.entries) {
    if (entry.key < start) {
      if (getCodePoint(entry.key) != 0) {
        _underlineColors[entry.key] = entry.value;
      }
      continue;
    }

    if (entry.key < start + count) continue;
    final newIndex = entry.key - count;
    if (entry.key < end &&
        newIndex < _length &&
        getCodePoint(newIndex) != 0) {
      _underlineColors[newIndex] = entry.value;
      continue;
    }

    if (entry.key >= end && getCodePoint(entry.key) != 0) {
      _underlineColors[entry.key] = entry.value;
    }
  }

  // Update anchors, remove anchors that are inside the removed range.
  for (final anchor in _anchors.toList()) {
    if (anchor.x >= start) {
      if (anchor.x < start + count) {
        anchor.dispose();
      } else if (anchor.x < end) {
        anchor.reposition(anchor.x - count);
      }
    }
  }
}