insertImmutable method

Rope insertImmutable(
  1. int position,
  2. String text
)

Creates a new Rope with text inserted at position (immutable)

Implementation

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

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