unwrapSelection function
Implementation
TextCommandResult unwrapSelection(
List<String> graphemes, {
required int cursorOffset,
int? selectionBaseOffset,
int? selectionExtentOffset,
required Map<String, String> surroundPairs,
}) {
final selection = normalizedSelectionRange(
selectionBaseOffset,
selectionExtentOffset,
);
if (selection == null || selection.start == selection.end) {
return _unchangedResult(
graphemes,
cursorOffset: cursorOffset,
selectionBaseOffset: selectionBaseOffset,
selectionExtentOffset: selectionExtentOffset,
);
}
if (selection.start < 1 || selection.end >= graphemes.length) {
return _unchangedResult(
graphemes,
cursorOffset: cursorOffset,
selectionBaseOffset: selectionBaseOffset,
selectionExtentOffset: selectionExtentOffset,
);
}
final before = graphemes[selection.start - 1];
final after = graphemes[selection.end];
if (surroundPairs[before] != after) {
return _unchangedResult(
graphemes,
cursorOffset: cursorOffset,
selectionBaseOffset: selectionBaseOffset,
selectionExtentOffset: selectionExtentOffset,
);
}
final selected = graphemes.sublist(selection.start, selection.end);
final result = edit_ops.replaceRange(
graphemes,
start: selection.start - 1,
end: selection.end + 1,
replacement: selected,
);
final nextSelectionStart = selection.start - 1;
final nextSelectionEnd = nextSelectionStart + selected.length;
return TextCommandResult(
graphemes: result.graphemes,
cursorOffset: nextSelectionEnd,
selectionBaseOffset: nextSelectionStart,
selectionExtentOffset: nextSelectionEnd,
);
}