backspace method

void backspace()

Remove the selection or last char if the selection is empty (backspace key)

Implementation

void backspace() {
  if (_undoController?.isUndoRedoInProgress ?? false) return;

  _flushBuffer();

  final selectionBefore = _selection;
  final sel = _selection;
  String deletedText;

  if (sel.start < sel.end) {
    deletedText = _rope.substring(sel.start, sel.end);
    _rope.delete(sel.start, sel.end);
    _currentVersion++;
    _selection = TextSelection.collapsed(offset: sel.start);
    dirtyLine = _rope.getLineAtOffset(sel.start);

    _recordDeletion(sel.start, deletedText, selectionBefore, _selection);
  } else if (sel.start > 0) {
    deletedText = _rope.charAt(sel.start - 1);
    _rope.delete(sel.start - 1, sel.start);
    _currentVersion++;
    _selection = TextSelection.collapsed(offset: sel.start - 1);
    dirtyLine = _rope.getLineAtOffset(sel.start - 1);

    _recordDeletion(sel.start - 1, deletedText, selectionBefore, _selection);
  } else {
    return;
  }

  _syncToConnection();
  notifyListeners();
}