textUnwrapSelection function
TextCommandResult
textUnwrapSelection({
- required TextDocument document,
- required TextOffsetStateSnapshot state,
- required Map<
String, String> surroundPairs,
Implementation
TextCommandResult textUnwrapSelection({
required TextDocument document,
required TextOffsetStateSnapshot state,
required Map<String, String> surroundPairs,
}) {
final selection = normalizedSelectionRange(
state.selectionBaseOffset,
state.selectionExtentOffset,
);
if (selection == null || selection.start == selection.end) {
return _unchangedDocumentCommandResult(document, state);
}
if (selection.start < 1 || selection.end >= document.length) {
return _unchangedDocumentCommandResult(document, state);
}
final leading = document.graphemeAt(selection.start - 1);
final trailing = document.graphemeAt(selection.end);
if (surroundPairs[leading] != trailing) {
return _unchangedDocumentCommandResult(document, state);
}
final selected = document.textInRange(
startOffset: selection.start,
endOffset: selection.end,
);
final working = document.copy();
final result = edit_ops.replaceDocumentTextRange(
working,
start: selection.start - 1,
end: selection.end + 1,
replacement: selected,
);
final nextSelectionStart = selection.start - 1;
final nextSelectionEnd = nextSelectionStart + selected.characters.length;
return _documentCommandResult(
working,
cursorOffset: nextSelectionEnd,
selectionBaseOffset: nextSelectionStart,
selectionExtentOffset: nextSelectionEnd,
documentChange: result.change,
changed: result.changed,
);
}