GraphemeCountCache
Incremental grapheme-cluster count for a document — the per-keystroke companion of TextMeasure.Count. The count is cached by ITextDocument.Version; PredictCount evaluates a pending edit by re-segmenting only a boundary-safe window around it, and CommitEdit promotes that prediction to the new cached count when the applied EditShape matches. Any mutation the cache was not told about degrades to one full recount on the next query.
Remarks
MaxAnchorScan codepoints (a pathological run of joiners), the prediction falls back to per-part arithmetic and the commit invalidates instead.public int Count(ITextDocument document)Grapheme-cluster count of the whole document. O(1) while the cached version matches; one full segmentation (pooled, allocation-free steady state) otherwise.
public int PredictCount(ITextDocument document, TextRange replacedRange, ReadOnlySpan<char> inserted)Grapheme-cluster count of the document AFTER applying the proposed edit, computed from a boundary-safe window around it — never a whole-document pass while the cache is warm. Follow up with CommitEdit once the edit is applied so the prediction becomes the cached count.
public void CommitEdit(ITextDocument document, in EditShape shape)Applies a mutation to the cached count: an edit matching the last prediction — same shape AND same inserted content (a later hook may have rewritten the text) — promotes it; anything else invalidates, deferring to a full recount on the next query.
