duplicateLinesAtSelections method

bool duplicateLinesAtSelections()

Duplicates every touched line below the original.

Implementation

bool duplicateLinesAtSelections() {
  return _runEditFrame(() {
    _beginHistoryAction(_TextAreaHistoryAction.transform, breakChain: true);
    _refreshDocumentSnapshot();
    final lines = _selectedLineIndexes().toList()
      ..sort((a, b) => b.compareTo(a));
    if (lines.isEmpty) return false;
    _recordUndoSnapshot();
    final graphemes = _document.flattenWithNewlines();
    var next = selections;
    var changed = false;
    for (final line in lines) {
      final start = _document.lineStartOffset(line);
      final end = _document.lineEndOffset(line);
      final content = graphemes.sublist(start, end);
      final insertion = ['\n', ...content];
      graphemes.replaceRange(end, end, insertion);
      next = next.applyInsertion(offset: end, length: insertion.length);
      changed = true;
    }
    if (!changed) return false;
    _replaceText(graphemes.join());
    setSelections(next);
    return true;
  });
}