pub struct Buffer { /* private fields */ }Expand description
The single owner of document text.
Invariants, upheld inside every mutation so no observer sees them disagree:
textis LF-only (no\r). The trailing terminator, if any, is an empty final line intext, not a flag. Line boundaries are the rope’s own bookkeeping (LF-only by feature selection) — there is no second index to drift.revisionbumps once per committed transaction (viaBuffer::bump_revision, called by the transaction engine).
Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the document is empty (a single empty line — never truly zero).
Sourcepub fn text(&self) -> Cow<'_, str>
pub fn text(&self) -> Cow<'_, str>
The whole document text, LF-only, as one slice.
Cold-path read: Borrowed only while the
document is a single rope chunk; an O(len) materialization otherwise.
For whole-document scans (find seeding, select-all-occurrences),
serialization, and tests — per-frame and per-keystroke code reads via
Buffer::slice / Buffer::line / Buffer::char_at /
Buffer::char_before.
Sourcepub fn slice(&self, range: Range<u32>) -> Cow<'_, str>
pub fn slice(&self, range: Range<u32>) -> Cow<'_, str>
The text in range (byte offsets; must lie on char boundaries, like
str slicing). Clamps to the document end. The workhorse ranged read —
Borrowed while the range sits in one rope chunk, Owned across
chunks.
Sourcepub fn line(&self, row: u32) -> Cow<'_, str>
pub fn line(&self, row: u32) -> Cow<'_, str>
The text of one row, excluding its trailing \n. Out-of-range rows
return "".
Sourcepub fn char_at(&self, offset: u32) -> Option<char>
pub fn char_at(&self, offset: u32) -> Option<char>
The char whose first byte is at offset — None at/past the end.
(offset must be a char boundary.) The forward one-char read every
boundary scan uses instead of slicing the whole text. O(log chunks).
Sourcepub fn char_before(&self, offset: u32) -> Option<char>
pub fn char_before(&self, offset: u32) -> Option<char>
The char ending at offset — None at 0. (offset must be a char
boundary.) The backward twin of Buffer::char_at.
Sourcepub fn line_count(&self) -> u32
pub fn line_count(&self) -> u32
Number of lines. Always ≥ 1 (an empty document is one empty line); a
trailing \n yields a final empty line, so line_count("a\nb\n") == 3.
Sourcepub fn line_len(&self, row: u32) -> u32
pub fn line_len(&self, row: u32) -> u32
Byte length of one row’s text (excludes \n). Out-of-range → 0.
Allocation-free (range arithmetic, no line materialization).
Sourcepub fn eol_flavor(&self) -> EolFlavor
pub fn eol_flavor(&self) -> EolFlavor
The EOL flavor detected at load.
Sourcepub fn offset_to_point(&self, offset: u32) -> Point
pub fn offset_to_point(&self, offset: u32) -> Point
Convert a byte offset to a Point. Offsets past the end clamp to the
end of the document. O(log chunks).
Sourcepub fn point_to_offset(&self, point: Point) -> u32
pub fn point_to_offset(&self, point: Point) -> u32
Convert a Point to a byte offset, clamping the row to the last line
and the column to that line’s length. O(log chunks).
Sourcepub fn clip_offset(&self, offset: u32, bias: Bias) -> u32
pub fn clip_offset(&self, offset: u32, bias: Bias) -> u32
Clamp a byte offset into [0, len] and snap it to a char boundary
(direction from bias). Idempotent.
Sourcepub fn clip_point(&self, point: Point, bias: Bias) -> Point
pub fn clip_point(&self, point: Point, bias: Bias) -> Point
Clip a Point to a valid position: clamp the row/col, then snap the
resulting offset to a char boundary.