insertAutoPair function

TextCommandResult insertAutoPair(
  1. List<String> graphemes, {
  2. required int cursorOffset,
  3. int? selectionBaseOffset,
  4. int? selectionExtentOffset,
  5. required List<String> opening,
  6. required List<String> closing,
})

Implementation

TextCommandResult insertAutoPair(
  List<String> graphemes, {
  required int cursorOffset,
  int? selectionBaseOffset,
  int? selectionExtentOffset,
  required List<String> opening,
  required List<String> closing,
}) {
  final selection = normalizedSelectionRange(
    selectionBaseOffset,
    selectionExtentOffset,
  );
  final hasSelection = selection != null && selection.start != selection.end;
  if (hasSelection) {
    return wrapSelection(
      graphemes,
      cursorOffset: cursorOffset,
      selectionBaseOffset: selectionBaseOffset,
      selectionExtentOffset: selectionExtentOffset,
      before: opening,
      after: closing,
    );
  }

  final inserted = <String>[...opening, ...closing];
  if (inserted.isEmpty) {
    return _unchangedResult(
      graphemes,
      cursorOffset: cursorOffset,
      selectionBaseOffset: selectionBaseOffset,
      selectionExtentOffset: selectionExtentOffset,
    );
  }

  final result = edit_ops.insertAtCursor(graphemes, cursorOffset, inserted);
  final nextCursor = cursorOffset.clamp(0, graphemes.length) + opening.length;
  return TextCommandResult(
    graphemes: result.graphemes,
    cursorOffset: nextCursor,
  );
}