Skip to main content

Module buffer

Module buffer 

Source
Expand description

The document text store: a rope.

Storage is a Rope — an augmented SumTree<Chunk>, the one tree every position query rides. The LF-only line model lives in the chunk summary (a '\n' count), so line/point math is a summary fold with no second index to drift; a test pins that only '\n' breaks a line. An edit is O(edit + log chunks) and coordinate lookups are O(log chunks) cursor descents — no operation is bandwidth-bound in the document length. There is no load-size policy: the only bound is the u32 offset space (~4 GiB), checked at load and on the edit path. (The rope is oracle-tested byte-for-byte against an independent reference model.)

The read API is backing-agnostic: every read hands out Cow slices or single chars — Buffer::slice, Buffer::line, Buffer::char_at, Buffer::char_before — so no consumer outside this module assumes the text is one contiguous allocation. A Cow is Borrowed when the requested range lies inside one rope chunk (always, for documents small enough to be a single leaf) and Owned when it crosses chunks. Buffer::text is the one whole-text read: cold-path only (find seeding, whole-document scans, serialization, tests) — it is a genuine O(len) materialization on any document large enough to span chunks, and per-frame/per-keystroke code must not call it.

Internal text is LF-only and the trailing terminator, if any, is an empty final line — never a flag. The original EOL flavor is remembered only to re-expand at Buffer::serialize.

Structs§

Buffer
The single owner of document text.
DocId
Process-unique document identity, minted fresh at each load. Async replies stamped (DocId, Revision) can never collide across reloads.
Revision
Monotonic per-document transaction counter: 0 at load, +1 per committed transaction including undo and redo. Never reused within a document’s lifetime, never runs backwards. The universal cache-key component and async correlation stamp.
Snapshot
An immutable, Send + Sync copy of the document for background consumers (e.g. a debounced compile thread).

Enums§

EolFlavor
The line-ending flavor detected at load; consulted only by Buffer::serialize. Internal text is always LF.
LoadError
Why a Buffer::new load was refused.