strop-core 0.25.0

strop editor core: rope buffer, byte-offset positions, edit ops
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! The only mutable text capability. Every mutation publishes pre-edit geometry.
use super::Buffer;
use crate::history::{Edit, EditKind};
use crate::id::{BufferRevision, ByteOffset};
use crate::{InputEdit, Range};

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ChangeOrigin {
    User,
    History,
    System,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Change {
    pub revision: BufferRevision,
    pub origin: ChangeOrigin,
    pub edit: InputEdit,
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum EditError {
    #[error("readonly buffer")]
    ReadOnly,
    #[error("invalid edit range")]
    InvalidRange,
    #[error("overlapping replacements in pre-edit coordinates")]
    Overlap,
    #[error("prepared replacement belongs to another buffer")]
    WrongBuffer,
    #[error("stale revision: expected {expected}, found {found}")]
    StaleRevision {
        expected: BufferRevision,
        found: BufferRevision,
    },
    #[error("buffer revision exhausted")]
    RevisionExhausted,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Replacement {
    pub range: Range,
    pub text: String,
}
impl Replacement {
    pub fn new(range: Range, text: impl Into<String>) -> Self {
        Self {
            range,
            text: text.into(),
        }
    }
}

/// Validated, sorted replacements bound to one exact buffer incarnation/revision.
/// The editor may restore prompt selection state before acquiring its write lease.
pub struct PreparedReplacements {
    buffer: crate::diagnostics::BufferTraceId,
    revision: BufferRevision,
    edits: Vec<Replacement>,
}
impl PreparedReplacements {
    pub fn is_empty(&self) -> bool {
        self.edits.is_empty()
    }
}

pub struct UserEdit<'a> {
    buffer: &'a mut Buffer,
}
pub struct SystemEdit<'a> {
    buffer: &'a mut Buffer,
}

impl Buffer {
    pub fn edit(&mut self) -> UserEdit<'_> {
        UserEdit { buffer: self }
    }
    pub fn system_edit(&mut self) -> SystemEdit<'_> {
        SystemEdit { buffer: self }
    }
    pub fn changes(&self) -> &[Change] {
        &self.changes
    }
    pub fn clear_changes(&mut self) {
        self.changes.clear();
    }
    pub fn begin_undo_group(&mut self) {
        self.history.begin();
    }
    pub fn commit_undo_group(&mut self) {
        self.history.commit();
    }

    fn validate_range(&self, range: Range) -> Result<(), EditError> {
        if range.start > range.end || !self.is_boundary(range.start) || !self.is_boundary(range.end)
        {
            return Err(EditError::InvalidRange);
        }
        Ok(())
    }

    /// All replacements refer to one pre-edit snapshot. Validate the whole batch
    /// before opening history or touching text; reversed application preserves coordinates.
    pub fn prepare_replacements(
        &self,
        base: BufferRevision,
        mut replacements: Vec<Replacement>,
    ) -> Result<PreparedReplacements, EditError> {
        if self.readonly {
            return Err(EditError::ReadOnly);
        }
        if base != self.revision() {
            return Err(EditError::StaleRevision {
                expected: base,
                found: self.revision(),
            });
        }
        replacements.retain(|edit| !edit.range.is_empty() || !edit.text.is_empty());
        for edit in &replacements {
            self.validate_range(edit.range)?;
        }
        // The verified geometry kernel (0045, crate::editmap): sorted by
        // start, strictly non-overlapping, in-bounds — proven, and the
        // error here can only be an overlap (bounds checked above).
        let pairs: Vec<(usize, usize)> = replacements
            .iter()
            .map(|edit| (edit.range.start.get(), edit.range.end.get()))
            .collect();
        let sorted = crate::editmap::check_batch(self.len_bytes(), pairs)
            .map_err(|()| EditError::Overlap)?;
        // Validated batches carry unique ranges, so each pair names its
        // Replacement unambiguously.
        let mut rest = replacements;
        let mut ordered = Vec::with_capacity(sorted.len());
        for pair in sorted {
            let at = rest
                .iter()
                .position(|edit| edit.range.start.get() == pair.0 && edit.range.end.get() == pair.1)
                .expect("a validated batch contains only input ranges");
            ordered.push(rest.remove(at));
        }
        replacements = ordered;
        self.epoch
            .checked_add(replacements.len() as u64)
            .ok_or(EditError::RevisionExhausted)?;
        Ok(PreparedReplacements {
            buffer: self.trace_id(),
            revision: base,
            edits: replacements,
        })
    }

    pub fn apply_prepared(
        &mut self,
        prepared: PreparedReplacements,
        undo_open: bool,
    ) -> Result<BufferRevision, EditError> {
        if prepared.buffer != self.trace_id() {
            return Err(EditError::WrongBuffer);
        }
        if self.readonly {
            return Err(EditError::ReadOnly);
        }
        if prepared.revision != self.revision() {
            return Err(EditError::StaleRevision {
                expected: prepared.revision,
                found: self.revision(),
            });
        }
        if prepared.is_empty() {
            return Ok(self.revision());
        }
        self.history.begin();
        for replacement in prepared.edits.into_iter().rev() {
            self.replace_validated(replacement.range, &replacement.text, ChangeOrigin::User);
        }
        if !undo_open {
            self.history.commit();
        }
        Ok(self.revision())
    }

    fn replace_validated(&mut self, range: Range, text: &str, origin: ChangeOrigin) {
        debug_assert!(self.validate_range(range).is_ok());
        debug_assert!(self.epoch < u64::MAX);
        let (start_byte, end_byte) = (range.start.get(), range.end.get());
        let start_point = self.point_of(start_byte);
        let old_end_point = self.point_of(end_byte);
        let extent = Self::point_extent(text);
        let new_end_point = if extent.0 == 0 {
            (start_point.0, start_point.1 + extent.1)
        } else {
            (start_point.0 + extent.0, extent.1)
        };
        let edit = InputEdit {
            start_byte,
            old_end_byte: end_byte,
            new_end_byte: start_byte + text.len(),
            start_point,
            old_end_point,
            new_end_point,
        };
        if origin == ChangeOrigin::User {
            if !range.is_empty() {
                let removed = self.rope.byte_slice(start_byte..end_byte).to_string();
                self.history.record(
                    Edit {
                        at: start_byte,
                        text: removed.clone(),
                        kind: EditKind::Insert,
                    },
                    Edit {
                        at: start_byte,
                        text: removed,
                        kind: EditKind::Delete,
                    },
                );
            }
            if !text.is_empty() {
                self.history.record(
                    Edit {
                        at: start_byte,
                        text: text.into(),
                        kind: EditKind::Delete,
                    },
                    Edit {
                        at: start_byte,
                        text: text.into(),
                        kind: EditKind::Insert,
                    },
                );
            }
        }
        let start = self.rope.byte_to_char(start_byte);
        let end = self.rope.byte_to_char(end_byte);
        if start != end {
            self.rope.remove(start..end);
        }
        if !text.is_empty() {
            self.rope.insert(start, text);
        }
        self.publish_change(edit, origin);
        self.trace_edit(origin, start_byte, range.len(), text);
    }

    fn publish_change(&mut self, edit: InputEdit, origin: ChangeOrigin) {
        debug_assert!(self.epoch < u64::MAX);
        self.epoch += 1;
        self.invalidate_line_layouts(&edit);
        self.dirty |= origin != ChangeOrigin::System;
        self.changes.push(Change {
            revision: self.revision(),
            origin,
            edit,
        });
    }
}

impl UserEdit<'_> {
    pub fn insert(&mut self, at: impl Into<ByteOffset>, text: &str) -> Result<(), EditError> {
        let at = at.into();
        self.replace(Range::charwise(at, at), text)
    }
    pub fn delete(&mut self, range: Range) -> Result<String, EditError> {
        if self.buffer.readonly {
            return Err(EditError::ReadOnly);
        }
        self.buffer.validate_range(range)?;
        let removed = self.buffer.slice_string(range);
        self.replace(range, "")?;
        Ok(removed)
    }
    pub fn replace(&mut self, range: Range, text: &str) -> Result<(), EditError> {
        if self.buffer.readonly {
            return Err(EditError::ReadOnly);
        }
        self.buffer.validate_range(range)?;
        if range.is_empty() && text.is_empty() {
            return Ok(());
        }
        self.buffer
            .epoch
            .checked_add(1)
            .ok_or(EditError::RevisionExhausted)?;
        self.buffer
            .replace_validated(range, text, ChangeOrigin::User);
        Ok(())
    }
}

impl SystemEdit<'_> {
    /// A partial generated-surface edit (0049 §5 collection view
    /// splices): system origin, never undoable, and it never dirties the
    /// buffer.
    pub fn replace(&mut self, range: Range, text: &str) -> Result<(), EditError> {
        self.buffer.validate_range(range)?;
        if range.is_empty() && text.is_empty() {
            return Ok(());
        }
        self.buffer
            .epoch
            .checked_add(1)
            .ok_or(EditError::RevisionExhausted)?;
        self.buffer
            .replace_validated(range, text, ChangeOrigin::System);
        Ok(())
    }

    /// A generated surface is a new system snapshot, never an undoable user edit.
    pub fn replace_all(&mut self, text: &str) -> Result<(), EditError> {
        self.buffer
            .epoch
            .checked_add(1)
            .ok_or(EditError::RevisionExhausted)?;
        self.buffer.replace_validated(
            Range::charwise(0, self.buffer.len_bytes()),
            text,
            ChangeOrigin::System,
        );
        self.buffer.history = Default::default();
        Ok(())
    }

    /// Publish worker-built text without flattening or rebuilding a rope on the
    /// event loop. Identity, readonly policy and monotonic revision stay local.
    pub fn replace_rope(&mut self, rope: ropey::Rope) -> Result<(), EditError> {
        self.buffer
            .epoch
            .checked_add(1)
            .ok_or(EditError::RevisionExhausted)?;
        let old_end_byte = self.buffer.len_bytes();
        let old_end_point = self.buffer.point_of(old_end_byte);
        let new_end_byte = rope.len_bytes();
        let line = rope.byte_to_line(new_end_byte);
        let new_end_point = (line, new_end_byte - rope.line_to_byte(line));
        self.buffer.rope = rope;
        self.buffer.publish_change(
            InputEdit {
                start_byte: 0,
                old_end_byte,
                new_end_byte,
                start_point: (0, 0),
                old_end_point,
                new_end_point,
            },
            ChangeOrigin::System,
        );
        self.buffer.history = Default::default();
        self.buffer.trace_snapshot(old_end_byte);
        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
pub struct HistoryMove {
    pub start: ByteOffset,
    pub end: ByteOffset,
}

impl Buffer {
    pub fn undo(&mut self) -> Result<Option<HistoryMove>, EditError> {
        self.move_history(crate::history::HistoryAction::Undo)
    }
    pub fn redo(&mut self) -> Result<Option<HistoryMove>, EditError> {
        self.move_history(crate::history::HistoryAction::Redo)
    }
    pub fn restore_revision(&mut self, revision: usize) -> Result<Option<HistoryMove>, EditError> {
        self.move_history(crate::history::HistoryAction::Jump(revision))
    }
    fn move_history(
        &mut self,
        action: crate::history::HistoryAction,
    ) -> Result<Option<HistoryMove>, EditError> {
        if self.readonly {
            return Err(EditError::ReadOnly);
        }
        let Some(cost) = self.history.movement_cost(action) else {
            return Ok(None);
        };
        self.epoch
            .checked_add(cost as u64)
            .ok_or(EditError::RevisionExhausted)?;
        // A valid history is guaranteed by construction and validated at restore.
        let Some(ops) = self.history.navigate(action) else {
            return Ok(None);
        };
        debug_assert_eq!(cost, ops.len());
        let start = ops.iter().map(|edit| edit.at).min().unwrap_or(0);
        let end = ops
            .last()
            .map(|edit| {
                edit.at
                    + if edit.kind == EditKind::Insert {
                        edit.text.len()
                    } else {
                        0
                    }
            })
            .unwrap_or(start);
        for op in &ops {
            let (range, text) = match op.kind {
                EditKind::Insert => (Range::charwise(op.at, op.at), op.text.as_str()),
                EditKind::Delete => (Range::charwise(op.at, op.at + op.text.len()), ""),
            };
            debug_assert!(self.validate_range(range).is_ok(), "sealed history range");
            debug_assert!(
                op.kind != EditKind::Delete
                    || self.rope.byte_slice(range.start.get()..range.end.get()) == op.text,
                "sealed history content"
            );
            self.replace_validated(range, text, ChangeOrigin::History);
        }
        self.trace_history(&ops);
        Ok(Some(HistoryMove {
            start: start.into(),
            end: end.into(),
        }))
    }
}