replaceSelectionOrInsert function

TextCommandResult replaceSelectionOrInsert(
  1. List<String> graphemes, {
  2. required int cursorOffset,
  3. int? selectionBaseOffset,
  4. int? selectionExtentOffset,
  5. List<String> replacement = const <String>[],
  6. bool replaceSelection = true,
})

Implementation

TextCommandResult replaceSelectionOrInsert(
  List<String> graphemes, {
  required int cursorOffset,
  int? selectionBaseOffset,
  int? selectionExtentOffset,
  List<String> replacement = const <String>[],
  bool replaceSelection = true,
}) {
  final selection = normalizedSelectionRange(
    selectionBaseOffset,
    selectionExtentOffset,
  );
  final hasSelection = selection != null && selection.start != selection.end;

  if (replaceSelection && hasSelection) {
    final result = edit_ops.replaceRange(
      graphemes,
      start: selection.start,
      end: selection.end,
      replacement: replacement,
    );
    return TextCommandResult(
      graphemes: result.graphemes,
      cursorOffset: result.cursorOffset,
      changed: selection.start != selection.end || replacement.isNotEmpty,
    );
  }

  final result = edit_ops.insertAtCursor(graphemes, cursorOffset, replacement);
  return TextCommandResult(
    graphemes: result.graphemes,
    cursorOffset: result.cursorOffset,
    changed: replacement.isNotEmpty,
  );
}