Skip to main content

DocumentBuilder

Struct DocumentBuilder 

Source
pub struct DocumentBuilder { /* private fields */ }
Expand description

Builder for a Document with a path and/or initial content.

Content precedence: a buffer from Self::with_buffer wins over text from Self::with_text; with neither, the document is empty. Nothing here touches the filesystem — Document::open reads, the builder only assembles. Every built document gets a fresh [DocumentId], starts at version 0, and is clean.

§Examples

use lattice_core::DocumentBuilder;

let doc = DocumentBuilder::default()
    .with_path("/tmp/notes.txt")
    .with_text("draft\n")
    .build();
assert_eq!(doc.text(), "draft\n");
assert_eq!(doc.path().and_then(|p| p.to_str()), Some("/tmp/notes.txt"));
assert!(!doc.dirty());

Implementations§

Source§

impl DocumentBuilder

Source

pub fn with_path(self, path: impl Into<PathBuf>) -> Self

Set the file path the document will report and Document::save will write to. Not checked for existence.

Source

pub fn with_text(self, text: impl Into<String>) -> Self

Set the initial text. Ignored if Self::with_buffer is also set.

Source

pub fn with_buffer(self, buffer: Buffer) -> Self

Build the Document around a pre-existing Buffer instead of going through a String. Used by callers that already hold a Buffer (typically from DocumentSnapshot.buffer.clone() — Buffer::clone is Rope::clone which is Arc-backed and O(1)) and want to avoid the Buffer → as_string() → from_text(&str) round- trip. K.4.11’s MultibufferDocumentHandle::dispatch_with_cancel is the primary consumer: every multibuffer keystroke ran the round-trip, allocating O(composed_size) bytes on the App thread per motion. After this fix the per-keystroke cost is one Arc bump.

Slice: K.4.11.perf-fix (2026-06-02).

Source

pub fn build(self) -> Document

Build the document: fresh [DocumentId], version 0, a single cursor at the origin, an empty undo stack, and clean (the initial content counts as the saved state).

Trait Implementations§

Source§

impl Debug for DocumentBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DocumentBuilder

Source§

fn default() -> DocumentBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.