textInsertGraphemes function

TextCommandResult textInsertGraphemes({
  1. required TextDocument document,
  2. required TextOffsetStateSnapshot state,
  3. required List<String> graphemes,
  4. bool replaceSelection = true,
})

Implementation

TextCommandResult textInsertGraphemes({
  required TextDocument document,
  required TextOffsetStateSnapshot state,
  required List<String> graphemes,
  bool replaceSelection = true,
}) {
  if (graphemes.isEmpty) {
    return _unchangedDocumentCommandResult(document, state);
  }

  final working = document.copy();
  final selection = normalizedSelectionRange(
    state.selectionBaseOffset,
    state.selectionExtentOffset,
  );
  final hasSelection = selection != null && selection.start != selection.end;

  final result = replaceSelection && hasSelection
      ? edit_ops.replaceDocumentRange(
          working,
          start: selection.start,
          end: selection.end,
          replacement: graphemes,
        )
      : edit_ops.insertIntoDocument(working, state.cursorOffset, graphemes);

  return _documentCommandResult(
    working,
    cursorOffset: result.cursorOffset,
    documentChange: result.change,
    changed: result.changed,
  );
}