Back to API Reference
Struct

TextSelection

sealed

Anchor / focus / affinity selection over codepoint indices. Anchor is where the selection began; Focus is the current caret position (always where the caret is rendered). Affinity disambiguates caret rendering at soft-wrap and BiDi boundaries.

Remarks

Indices are codepoint offsets — the same space used by codepoints and cluster. Cursor movement at the consumer level is grapheme-cluster-aware (via GraphemeNavigator), but the storage representation is codepoints (D-006). Direction is implicit in the Anchor/Focus ordering — matches the W3C Selection API and Flutter TextSelection conventions. Multi-cursor (P3 code-editor) is reserved as a non-breaking IReadOnlyList<TextSelection> overload alongside the single-region property (D-001 revised). Immutable — mutations go through UniTextSelectable named methods (SetCaret / SetSelection / ExtendSelection). Direct construction is allowed for read-only producers (parsers, deserializers, snapshot diffs).
public int Anchor{ get }

Codepoint index where the selection originated. Equals Focus when the selection is collapsed (caret only).

public int Focus{ get }

Current codepoint index of the cursor — the moving end of the selection. The caret is rendered here.

public CaretAffinity Affinity{ get }

Visual side of the caret at a soft-wrap or BiDi run boundary. See CaretAffinity.

public bool IsCollapsed{ get }

True when Anchor equals Focus — there is no selected range, only a caret. Matches the W3C Selection.isCollapsed name and semantics.

public int Start{ get }

Smaller of Anchor and Focus — start of the selected range (inclusive).

public int End{ get }

Larger of Anchor and Focus — end of the selected range (exclusive).

public int Length{ get }

Length of the selected range in codepoints. Zero when collapsed.

[ctor]public TextSelection()

Constructs a selection. anchor equal to focus produces a collapsed caret at that position.

public static TextSelection Caret()

Caret-only selection at codepointIndex with the given affinity.

public TextSelection Clamp()

Returns a new TextSelection with both endpoints clamped to [0, codepointCount]. Returns the original instance when no clamping is needed.

public bool Equals()
public override bool Equals()
public override int GetHashCode()
public override string ToString()