syncEditorStateFromOffsets function

void syncEditorStateFromOffsets(
  1. TextDocument document,
  2. EditorState editorState, {
  3. required int cursorOffset,
  4. int? selectionBaseOffset,
  5. int? selectionExtentOffset,
})

Implementation

void syncEditorStateFromOffsets(
  TextDocument document,
  EditorState editorState, {
  required int cursorOffset,
  int? selectionBaseOffset,
  int? selectionExtentOffset,
}) {
  final cursor = document.positionForOffset(
    cursorOffset.clamp(0, document.length),
  );
  editorState.setCursor(line: cursor.line, column: cursor.column);

  if (selectionBaseOffset == null || selectionExtentOffset == null) {
    editorState.clearSelection();
    return;
  }

  editorState.setSelection(
    base: document.positionForOffset(
      selectionBaseOffset.clamp(0, document.length),
    ),
    extent: document.positionForOffset(
      selectionExtentOffset.clamp(0, document.length),
    ),
    cursor: cursor,
  );
}