deleteByLine method Null safety

void deleteByLine(
  1. SelectionChangedCause cause
)

Deletes a line backwards from the current selection.

If the selection is collapsed, deletes a line before the cursor.

If the selection is not collapsed, deletes the selection.

If obscureText is true, it treats the whole text content as a single word.

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

See also:

Implementation

void deleteByLine(SelectionChangedCause cause) {
  assert(_selection != null);

  if (_readOnly || !_selection!.isValid) {
    return;
  }

  if (!_selection!.isCollapsed) {
    return _deleteSelection(_selection!, cause);
  }

  // When the text is obscured, the whole thing is treated as one big line.
  if (obscureText) {
    return _deleteToStart(_selection!, cause);
  }

  final String text = textSelectionDelegate.textEditingValue.text;
  String textBefore = _selection!.textBefore(text);
  if (textBefore.isEmpty) {
    return;
  }

  // When there is a line break, line delete shouldn't do anything
  final bool isPreviousCharacterBreakLine = textBefore.codeUnitAt(textBefore.length - 1) == 0x0A;
  if (isPreviousCharacterBreakLine) {
    return;
  }

  final TextSelection line = _getLineAtOffset(TextPosition(offset: textBefore.length - 1));
  textBefore = textBefore.substring(0, line.start);

  final String textAfter = _selection!.textAfter(text);
  final TextSelection newSelection = TextSelection.collapsed(offset: textBefore.length);
  _setTextEditingValue(
    TextEditingValue(text: textBefore + textAfter, selection: newSelection),
    cause,
  );
}