insertAutoPair function
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,
);
}