offsetSnapshotFromEditorState function
TextOffsetStateSnapshot
offsetSnapshotFromEditorState(
- TextDocument document,
- EditorState editorState, {
- required int textLength,
- bool preserveCollapsedSelection = false,
Implementation
TextOffsetStateSnapshot offsetSnapshotFromEditorState(
TextDocument document,
EditorState editorState, {
required int textLength,
bool preserveCollapsedSelection = false,
}) {
final cursorOffset = document
.offsetForPosition(editorState.cursor)
.clamp(0, textLength);
final selection = editorState.selection;
if (selection == null ||
(selection.isCollapsed && !preserveCollapsedSelection)) {
return TextOffsetStateSnapshot(cursorOffset: cursorOffset);
}
return TextOffsetStateSnapshot(
cursorOffset: cursorOffset,
selectionBaseOffset: document
.offsetForPosition(selection.base)
.clamp(0, textLength),
selectionExtentOffset: document
.offsetForPosition(selection.extent)
.clamp(0, textLength),
);
}