continuity_engine/change.rs
1//! Storage-neutral mutation records returned to host adapters.
2
3use continuity_buffer::{BufferId, Revision, UndoGroupId};
4use continuity_text::{EditOp, Selection};
5
6use crate::RopeEditDeltaWithPoints;
7
8/// The high-level operation that produced a [`ChangeBatch`].
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10pub enum MutationKind {
11 /// A caller supplied one atomic edit.
12 RawEdit,
13 /// A caller supplied a grouped list of edits.
14 GroupedEdit,
15 /// The engine planned a selection-aware edit.
16 SelectionEdit,
17 /// An undo group was reversed.
18 Undo,
19 /// An undo group was replayed.
20 Redo,
21}
22
23/// Metadata for an undo group newly minted by a mutation.
24#[derive(Clone, Debug, Eq, PartialEq)]
25pub struct UndoGroupMeta {
26 /// Stable group identifier.
27 pub id: UndoGroupId,
28 /// Parent at the time the group was created.
29 pub parent: Option<UndoGroupId>,
30 /// Host-supplied wall-clock milliseconds.
31 pub timestamp_ms: i64,
32 /// Command name shown in history surfaces.
33 pub command: String,
34}
35
36/// One atomic edit and the information a host persistence adapter needs.
37#[derive(Clone, Debug, Eq, PartialEq)]
38pub struct AppliedChange {
39 /// Applied operation.
40 pub op: EditOp,
41 /// Text removed by the operation, empty for inserts.
42 pub removed_text: String,
43 /// Revision before the operation.
44 pub revision_before: Revision,
45 /// Revision after the operation.
46 pub revision_after: Revision,
47 /// Selection state recorded before the operation.
48 pub selections_before: Vec<Selection>,
49 /// Selection state recorded after the operation.
50 pub selections_after: Vec<Selection>,
51 /// Running checksum immediately after this operation.
52 pub checksum_after: u64,
53}
54
55/// A complete synchronous engine mutation, ready for host-side persistence.
56#[derive(Clone, Debug, Eq, PartialEq)]
57pub struct ChangeBatch {
58 /// Mutated buffer.
59 pub buffer_id: BufferId,
60 /// High-level mutation kind.
61 pub kind: MutationKind,
62 /// Host-supplied wall-clock milliseconds.
63 pub timestamp_ms: i64,
64 /// Command identity for history and host event logs.
65 pub command: String,
66 /// Revision before the batch.
67 pub revision_before: Revision,
68 /// Revision after the batch.
69 pub revision_after: Revision,
70 /// Undo group that owns the edit rows.
71 pub undo_group_id: UndoGroupId,
72 /// Present only when this batch created the group.
73 pub new_undo_group: Option<UndoGroupMeta>,
74 /// Selection state before the complete mutation.
75 pub selections_before: Vec<Selection>,
76 /// Selection state after the complete mutation.
77 pub selections_after: Vec<Selection>,
78 /// Atomic changes in application order.
79 pub changes: Vec<AppliedChange>,
80 /// Incrementally maintained checksum after the batch.
81 pub checksum_after: u64,
82 /// Rope deltas in application order.
83 pub deltas: Vec<RopeEditDeltaWithPoints>,
84}
85
86/// Lightweight revisioned events emitted after synchronous operations.
87#[derive(Clone, Debug, Eq, PartialEq)]
88pub enum EngineEvent {
89 /// A buffer entered this engine.
90 BufferOpened {
91 /// Opened buffer.
92 id: BufferId,
93 },
94 /// A text mutation completed.
95 Changed {
96 /// Mutated buffer.
97 id: BufferId,
98 /// Revision after the mutation.
99 revision: Revision,
100 /// Mutation kind.
101 kind: MutationKind,
102 },
103 /// Selections changed without a text mutation.
104 SelectionsChanged {
105 /// Buffer whose selections changed.
106 id: BufferId,
107 },
108 /// A buffer left this engine.
109 BufferClosed {
110 /// Closed buffer.
111 id: BufferId,
112 },
113}