textDeleteLines function

TextLineCommandResult textDeleteLines({
  1. required List<String> lines,
  2. required TextLineStateSnapshot state,
})

Implementation

TextLineCommandResult textDeleteLines({
  required List<String> lines,
  required TextLineStateSnapshot state,
}) {
  final nextLines = lines.isEmpty ? <String>[''] : List<String>.from(lines);
  final clampedState = _clampLineStateSnapshot(state, nextLines);
  final span = _selectedLineSpan(clampedState);
  final deletedCount = span.endLine - span.startLine + 1;

  if (deletedCount >= nextLines.length) {
    return const TextLineCommandResult(
      lines: [''],
      cursor: TextPosition(line: 0, column: 0),
    );
  }

  nextLines.removeRange(span.startLine, span.endLine + 1);
  final nextRow = clampedState.cursor.line > span.endLine
      ? clampedState.cursor.line - deletedCount
      : clampedState.cursor.line >= span.startLine
      ? span.startLine
      : clampedState.cursor.line;
  return _lineResultFromSnapshot(
    nextLines,
    TextLineStateSnapshot.collapsed(
      cursor: TextPosition(line: nextRow, column: clampedState.cursor.column),
    ),
  );
}