pub struct BatchDocument { /* private fields */ }Expand description
A headless document: no thread, no view, no cursor. See the module docs.
Implementations§
Source§impl BatchDocument
impl BatchDocument
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Bootstrap an empty document. No I/O, no thread.
AppContext::new() gives the machinery (store, event hub, undo manager) but no
entities — so, like TextDocument, this seeds the Root → Document the
importer needs to hang its frames from. It stops there: TextDocument also
seeds a Frame and a Block for an empty editor to show a caret in, but an import
rebuilds the document’s frames anyway, and a batch has no caret to place.
stack_id: None — a batch has no undo of its own. Its caller’s transaction is
the unit that gets rolled back.
Sourcepub fn set_djot(&self, djot: &str, options: &DjotImportOptions) -> Result<()>
pub fn set_djot(&self, djot: &str, options: &DjotImportOptions) -> Result<()>
Replace the document’s contents by parsing djot.
Goes through the synchronous import path. The public import_djot hands
the use case to a LongOperationManager, which spawns a thread for it — the
very thing this type exists to avoid.
Sourcepub fn to_djot(&self, options: &DjotExportOptions) -> Result<String>
pub fn to_djot(&self, options: &DjotExportOptions) -> Result<String>
Serialise the document back to Djot.
Sourcepub fn find_all(
&self,
query: &str,
options: &FindOptions,
) -> Result<Vec<FindMatch>>
pub fn find_all( &self, query: &str, options: &FindOptions, ) -> Result<Vec<FindMatch>>
Every match of query in the document’s text.
Positions are char indices into the document’s own text — the same space every other offset in this crate lives in. They are emphatically not byte offsets into the Djot source the document was parsed from: markup means the two disagree on almost any real paragraph, and mixing them corrupts a replace.
Sourcepub fn find_and_replace(
&self,
query: &str,
options: &ReplaceOptions,
decide: impl FnMut(&str, usize) -> Option<String>,
) -> Result<usize>
pub fn find_and_replace( &self, query: &str, options: &ReplaceOptions, decide: impl FnMut(&str, usize) -> Option<String>, ) -> Result<usize>
Find every match of query and let decide choose what each becomes — the whole
thing in one shot.
decide is handed the matched text and the index of the match, and returns the
replacement, or None to leave that occurrence alone. That is exactly what a reviewed
bulk rename needs: skip the occurrences the writer unticked, and preserve the case of
the ones that stay (AURÉLIEN → AURÉLIAN, not aurélian).
This is why a backend batch can stop doing string surgery on markup. The
alternative — export the Djot and rewrite it as a string — rewrites a query that also
occurs inside a link’s URL, and drops the character formatting under every match. Here
the edit happens inside the document, at the offsets the parser itself reports, and
ReplaceOptions::format_policy decides what the replacement wears where it overwrites
formatted prose.
The matched text comes back from the scan itself, sliced from the very text that was
searched — never from a plain-text export, which is the human-readable view and does
not carry the U+FFFC anchor an embedded table occupies.
Sourcepub fn replace_ranges(
&self,
ranges: &[ReplaceRange],
options: &ReplaceOptions,
) -> Result<usize>
pub fn replace_ranges( &self, ranges: &[ReplaceRange], options: &ReplaceOptions, ) -> Result<usize>
Replace an explicit set of ranges, each with its own replacement text.
Ranges that straddle a block boundary, or that overlap one another, are skipped; the
returned count is what was actually applied. Prefer Self::find_and_replace, which
derives the ranges from a scan of the document as it stands, so they cannot address
text that has since moved.