moveLineDown method

void moveLineDown()

Moves the current line down by one line.

If the selection spans multiple lines, all selected lines are moved. The selection is adjusted accordingly after the move. Does nothing if the line is already at the bottom or if the controller is read-only.

Implementation

void moveLineDown() {
  if (readOnly) return;
  final selection = this.selection;
  final text = this.text;
  final selStart = selection.start;
  final selEnd = selection.end;
  final lineStart = text.lastIndexOf('\n', selStart - 1) + 1;
  int lineEnd = text.indexOf('\n', selEnd);
  if (lineEnd == -1) lineEnd = text.length;
  final nextLineStart = lineEnd + 1;
  if (nextLineStart >= text.length) return;
  int nextLineEnd = text.indexOf('\n', nextLineStart);
  if (nextLineEnd == -1) nextLineEnd = text.length;

  final currentLines = text.substring(lineStart, lineEnd);
  final nextLine = text.substring(nextLineStart, nextLineEnd);

  replaceRange(lineStart, nextLineEnd, '$nextLine\n$currentLines');

  final offsetDelta = nextLine.length + 1;
  final newSelection = TextSelection(
    baseOffset: selection.baseOffset + offsetDelta,
    extentOffset: selection.extentOffset + offsetDelta,
  );
  setSelectionSilently(newSelection);
}