moveSelectionLeftByLine method Null safety

void moveSelectionLeftByLine(
  1. SelectionChangedCause cause
)

Move the current selection to the leftmost of the current line.

The given SelectionChangedCause indicates the cause of this change and will be passed to onSelectionChanged.

See also:

Implementation

void moveSelectionLeftByLine(SelectionChangedCause cause) {
  assert(selection != null);

  // If the previous character is the edge of a line, don't do anything.
  final int previousPoint = previousCharacter(selection!.extentOffset, _plainText, true);
  final TextSelection line = _getLineAtOffset(TextPosition(offset: previousPoint));
  if (line.extentOffset == previousPoint) {
    return;
  }

  // When going left, we want to skip over any whitespace before the line,
  // so we go back to the first non-whitespace before asking for the line
  // bounds, since _getLineAtOffset finds the line boundaries without
  // including whitespace (like the newline).
  final int startPoint = previousCharacter(selection!.extentOffset, _plainText, false);
  final TextSelection selectedLine = _getLineAtOffset(TextPosition(offset: startPoint));
  final TextSelection nextSelection = TextSelection.collapsed(
    offset: selectedLine.baseOffset,
    affinity: TextAffinity.downstream,
  );

  _setSelection(nextSelection, cause);
}