insertText method

void insertText(
  1. String text, {
  2. bool replaceSelection = true,
  3. bool coalesce = false,
})

Inserts text at the cursor, optionally replacing the current selection.

Implementation

void insertText(
  String text, {
  bool replaceSelection = true,
  bool coalesce = false,
}) {
  if (text.isEmpty) return;
  _runEditFrame(() {
    final hasSelection =
        selectionStart != null &&
        selectionEnd != null &&
        selectionStart != selectionEnd;
    final action = replaceSelection && hasSelection
        ? _TextInputHistoryAction.replace
        : _TextInputHistoryAction.insert;
    _beginHistoryAction(
      action,
      breakChain: !coalesce || action == _TextInputHistoryAction.replace,
    );
    _resetDesiredCol();
    final result = textInsertText(
      document: _document,
      state: _currentOffsetStateSnapshot(),
      text: String.fromCharCodes(uni.codePoints(text)),
      replaceSelection: replaceSelection,
    );
    if (result.changed) {
      _recordUndoSnapshot();
      _applyEditCommandResult(result);
    }
    _updateSuggestions();
    _handleOverflow();
  });
}