Skip to main content

replace_in_block

Function replace_in_block 

Source
pub fn replace_in_block(
    store: &Store,
    block: &Block,
    char_start: i64,
    char_end: i64,
    replacement: &str,
    policy: ReplaceFormatPolicy,
) -> Result<Block, FormatRunError>
Expand description

Replace [char_start..char_end) inside block with replacement, choosing what the replacement wears where it overwrites formatted text — see ReplaceFormatPolicy. Mutates the block’s format runs, image anchors, and the global rope consistently in one step, and returns the updated Block (with a bumped updated_at) for the caller to persist via its own unit of work.

The single shared implementation of “replace a char range inside one block” — originally written for the project-wide replace path (document_search::replace_core::apply_in_block) and moved here so document_editing’s interactive selection-replace path can offer the same format-policy choice instead of being permanently pinned to ReplaceFormatPolicy::InheritPreceding. See ReplaceFormatPolicy’s own doc comment for the failure mode a second, independently-drifting copy of this would risk: a replace used to be an unannounced delete + insert, which silently dropped formatting.

Deliberately takes no unit of work — everything here is store-level (the block’s text lives in the rope, its formatting in format_runs, its images in block_images), so the caller’s UoW only has to persist the returned Block.

Returns Err rather than corrupting the block if the replace would violate the format-run invariants (see FormatRunError) — the caller should refuse the edit and propagate this rather than swallow it.