wrapSelection function

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

Implementation

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

  final suffix = after ?? before;
  if (before.isEmpty && suffix.isEmpty) {
    return _unchangedResult(
      graphemes,
      cursorOffset: cursorOffset,
      selectionBaseOffset: selectionBaseOffset,
      selectionExtentOffset: selectionExtentOffset,
    );
  }

  final selected = graphemes.sublist(selection.start, selection.end);
  final replacement = <String>[...before, ...selected, ...suffix];
  final result = edit_ops.replaceRange(
    graphemes,
    start: selection.start,
    end: selection.end,
    replacement: replacement,
  );
  final nextSelectionStart = selection.start + before.length;
  final nextSelectionEnd = nextSelectionStart + selected.length;

  return TextCommandResult(
    graphemes: result.graphemes,
    cursorOffset: nextSelectionEnd,
    selectionBaseOffset: nextSelectionStart,
    selectionExtentOffset: nextSelectionEnd,
  );
}