insert method

void insert(
  1. int position,
  2. String text
)

Implementation

void insert(int position, String text) {
  if (position < 0 || position > _length) {
    throw RangeError('Invalid position: $position for length $_length');
  }
  if (text.isEmpty) return;

  _lineCache = null;

  if (text.length == 1 && _root != null) {
    final inserted = _tryInsertInLeaf(_root!, position, text);
    if (inserted != null) {
      _root = inserted;
      _length += 1;
      return;
    }
  }

  final pair = _split(_root, position);
  final mid = _buildBalanced(text, 0, text.length);
  _root = _concat(_concat(pair.left, mid), pair.right);
  _length += text.length;
}