pub enum DocumentEvent {
ContentsChanged {
position: usize,
chars_removed: usize,
chars_added: usize,
blocks_affected: usize,
},
TextInserted {
position: usize,
chars_inserted: usize,
origin: InsertionOrigin,
},
FormatChanged {
position: usize,
length: usize,
kind: FormatChangeKind,
},
HighlightPaintChanged {
position: usize,
length: usize,
},
BlockCountChanged(usize),
FlowElementsInserted {
flow_index: usize,
count: usize,
},
FlowElementsRemoved {
flow_index: usize,
count: usize,
},
DocumentReset,
UndoRedoChanged {
can_undo: bool,
can_redo: bool,
},
ModificationChanged(bool),
LongOperationProgress {
operation_id: String,
percent: f64,
message: String,
},
LongOperationFinished {
operation_id: String,
success: bool,
error: Option<String>,
},
}Variants§
ContentsChanged
Text content changed at a specific region.
Emitted by every edit that changes what the document contains: the
text-level ones (insert_text, delete_char, delete_previous_char,
remove_selected_text, insert_formatted_text, insert_block,
insert_html, insert_markdown, insert_fragment, insert_image), the
streaming appends, undo and redo, and every structural table edit
(insert_table_row, insert_table_column, remove_table_row,
remove_table_column, merge_table_cells, split_table_cell,
remove_table, and the cursor-relative wrappers over them).
⚠ The list above was wrong in both directions for a long time, and the
half that mattered was the table edits: they emitted nothing at all, so a
consumer holding offsets kept them across a row insert and a consumer
caching on TextDocument::content_revision never reheated. Nothing
errored and nothing looked wrong.
§Two things a consumer should know about the figures
chars_added and chars_removed are a net delta for the affected
region, not “characters this edit introduced”: replacing a selection
reports both, and a caller wanting to know how much text an edit brought
in cannot get it from here.
For undo, redo and the table edits the delta is computed as a diff
over blocks joined by newlines, which is not the same string
TextDocument::to_plain_text renders when the document contains a
table. Consumers that shift offsets by these figures are consistent with
each other; a consumer reconciling them against to_plain_text is not.
TextInserted
Text arrived, and this is where it came from.
Emitted alongside ContentsChanged, never
instead of it, and only when an insertion actually added characters.
§Why this is not a field on ContentsChanged
Because ContentsChanged carries the wrong number for the question.
Its chars_added is a net delta for the affected region: replacing a
twelve-character selection with a four-character paste reports both a
removal and an addition, and neither figure is “how much text this paste
brought in”. Attaching an origin to a net delta would produce an
attribution that looks precise and is not.
chars_inserted here is the other number: what this insertion
introduced, which is the one a consumer attributing text to a channel
actually wants.
Keeping it a separate event is also what makes it additive — every
existing consumer of ContentsChanged is untouched, and one that does
not care about origins never has to mention this.
Fields
origin: InsertionOriginFormatChanged
Formatting changed without text content change.
Fields
kind: FormatChangeKindDistinguishes block-level changes (relayout needed) from character-level changes (reshaping only).
HighlightPaintChanged
Only paint-level highlight attributes changed (colors, underline
decorations) on a paint-only highlighter. The shaping input
(fragments) is unchanged, so the layout engine can recolor the
cached layout without reshaping or reflowing.
position / length are document-absolute character offsets bounding
the extent that changed, so a view may recolor just the blocks they
cover rather than re-snapshotting the whole document.
A length of 0 means “unknown — assume the whole document”, and
is what the genuinely document-wide operations send: installing or
retiring a highlighter, and a full rehighlight. set_session_ranges
knows its own before/after ranges and reports their union exactly.
A receiver that does not care may keep treating every one of these as
whole-document; that is the safe reading of both cases.
BlockCountChanged(usize)
Block count changed. Carries the new count.
FlowElementsInserted
Flow elements were inserted at the given index in the main
frame’s child_order.
This is a performance optimization — the layout engine can
update incrementally instead of re-querying
TextDocument::flow().
FlowElementsRemoved
Flow elements were removed starting at the given index in the
main frame’s child_order.
DocumentReset
The document was completely replaced (import, clear).
UndoRedoChanged
Undo/redo was performed or availability changed.
ModificationChanged(bool)
The modified flag changed.
LongOperationProgress
A long operation progressed.
LongOperationFinished
A long operation completed or failed.
Trait Implementations§
Source§impl Clone for DocumentEvent
impl Clone for DocumentEvent
Source§fn clone(&self) -> DocumentEvent
fn clone(&self) -> DocumentEvent
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more