escriba-buffer 0.1.30

Rope-backed text buffer for escriba — ropey + undo tree + encoding + line-ending + structural Lisp-aware navigation helpers.
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use escriba_core::{BufferId, Edit, EditKind, Position, Range};
use ropey::Rope;
use serde::{Deserialize, Serialize};

use crate::encoding::Encoding;
use crate::error::BufferError;
use crate::line_ending::LineEnding;
use crate::undo::{UndoEntry, UndoTree};

/// A single open text buffer.
/// A monotonic counter of TEXT changes to one buffer.
///
/// Deliberately distinct from `escriba-core`'s `EditGen`, which is a REFRESH
/// generation: that one bumps on every action, including pure cursor moves,
/// because its job is telling the renderer when to repaint. Keying staleness
/// on it would mark every cached offset stale after any keypress — noise, not
/// staleness.
///
/// This one bumps if and only if the rope changed, which is the question an
/// offset actually needs answered: "is the text I was measured against still
/// the text?"
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct TextRev(pub u64);

impl TextRev {
    #[must_use]
    pub const fn next(self) -> Self {
        Self(self.0.wrapping_add(1))
    }
}

#[derive(Debug, Clone)]
pub struct Buffer {
    pub id: BufferId,
    pub path: Option<PathBuf>,
    pub rope: Rope,
    pub modified: bool,
    pub encoding: Encoding,
    pub line_ending: LineEnding,
    pub undo: UndoTree,
    /// Bumped by every successful [`Buffer::apply`]. Private so it can only
    /// advance through a real mutation — a settable revision is a revision
    /// that lies.
    text_rev: TextRev,
}

impl Buffer {
    #[must_use]
    pub fn empty(id: BufferId) -> Self {
        Self {
            id,
            path: None,
            rope: Rope::new(),
            modified: false,
            encoding: Encoding::default(),
            line_ending: LineEnding::default(),
            undo: UndoTree::new(),
            text_rev: TextRev::default(),
        }
    }

    /// This buffer's current text revision — the token an offset measured
    /// against it should carry.
    #[must_use]
    pub const fn text_rev(&self) -> TextRev {
        self.text_rev
    }

    #[must_use]
    pub fn from_str(id: BufferId, src: &str) -> Self {
        let line_ending = LineEnding::detect(src);
        Self {
            id,
            path: None,
            rope: Rope::from_str(src),
            modified: false,
            encoding: Encoding::default(),
            line_ending,
            undo: UndoTree::new(),
            text_rev: TextRev::default(),
        }
    }

    pub fn open(id: BufferId, path: impl AsRef<Path>) -> Result<Self, BufferError> {
        let path = path.as_ref().to_path_buf();
        let src = if path.exists() {
            std::fs::read_to_string(&path)?
        } else {
            String::new()
        };
        let line_ending = LineEnding::detect(&src);
        Ok(Self {
            id,
            path: Some(path),
            rope: Rope::from_str(&src),
            modified: false,
            encoding: Encoding::default(),
            line_ending,
            undo: UndoTree::new(),
            text_rev: TextRev::default(),
        })
    }

    pub fn save(&mut self) -> Result<(), BufferError> {
        let path = self.path.clone().ok_or(BufferError::NoPath)?;
        let mut src = String::new();
        for line in self.rope.lines() {
            src.push_str(&line.to_string());
        }
        std::fs::write(&path, src)?;
        self.modified = false;
        Ok(())
    }

    pub fn save_as(&mut self, path: impl AsRef<Path>) -> Result<(), BufferError> {
        self.path = Some(path.as_ref().to_path_buf());
        self.save()
    }

    // ── Queries ────────────────────────────────────────────────────

    #[must_use]
    pub fn line_count(&self) -> u32 {
        u32::try_from(self.rope.len_lines()).unwrap_or(u32::MAX)
    }

    #[must_use]
    pub fn byte_count(&self) -> usize {
        self.rope.len_bytes()
    }

    #[must_use]
    pub fn char_count(&self) -> usize {
        self.rope.len_chars()
    }

    pub fn line(&self, n: u32) -> Option<String> {
        let n = n as usize;
        if n >= self.rope.len_lines() {
            return None;
        }
        Some(self.rope.line(n).to_string())
    }

    pub fn line_len_chars(&self, n: u32) -> u32 {
        let n = n as usize;
        if n >= self.rope.len_lines() {
            return 0;
        }
        let line = self.rope.line(n);
        let mut len = line.len_chars();
        // Strip trailing newline from char count for cursor-placement purposes.
        if line.chars().last().is_some_and(|c| c == '\n' || c == '\r') {
            len = len.saturating_sub(1);
        }
        u32::try_from(len).unwrap_or(u32::MAX)
    }

    /// Clamp a position to valid coordinates inside this buffer.
    #[must_use]
    pub fn clamp(&self, pos: Position) -> Position {
        let line = pos.line.min(self.line_count().saturating_sub(1));
        let col = pos.column.min(self.line_len_chars(line));
        Position::new(line, col)
    }

    pub fn position_to_char(&self, pos: Position) -> Result<usize, BufferError> {
        let line = pos.line as usize;
        if line >= self.rope.len_lines() {
            return Err(BufferError::InvalidPosition {
                line: pos.line,
                column: pos.column,
                total_lines: self.line_count(),
            });
        }
        let line_start = self.rope.line_to_char(line);
        let line_slice = self.rope.line(line);
        let max_col = line_slice.len_chars();
        let col = (pos.column as usize).min(max_col);
        Ok(line_start + col)
    }

    #[must_use]
    pub fn char_to_position(&self, ch: usize) -> Position {
        let line = self.rope.char_to_line(ch.min(self.rope.len_chars()));
        let line_start = self.rope.line_to_char(line);
        let col = ch.saturating_sub(line_start);
        Position::new(
            u32::try_from(line).unwrap_or(u32::MAX),
            u32::try_from(col).unwrap_or(u32::MAX),
        )
    }

    pub fn slice(&self, range: Range) -> Result<String, BufferError> {
        let r = range.normalized();
        let a = self.position_to_char(r.start)?;
        let b = self.position_to_char(r.end)?;
        Ok(self.rope.slice(a..b).to_string())
    }

    // ── Mutation ───────────────────────────────────────────────────

    pub fn apply(&mut self, edit: &Edit) -> Result<UndoEntry, BufferError> {
        let range = edit.range.normalized();
        let start_char = self.position_to_char(range.start)?;
        let end_char = self.position_to_char(range.end)?;
        let previous_text = self.rope.slice(start_char..end_char).to_string();

        // The rope is about to change, so every offset measured against the
        // old text expires here. Bumping AFTER the fallible prelude means a
        // rejected edit does not invalidate anything.
        self.text_rev = self.text_rev.next();

        let (inserted_len_chars, reverse_kind) = match &edit.kind {
            EditKind::Insert { text } => {
                self.rope.insert(start_char, text);
                (text.chars().count(), EditKind::Delete)
            }
            EditKind::Delete => {
                self.rope.remove(start_char..end_char);
                (
                    0usize,
                    EditKind::Insert {
                        text: previous_text.clone(),
                    },
                )
            }
            EditKind::Replace { text } => {
                self.rope.remove(start_char..end_char);
                self.rope.insert(start_char, text);
                (
                    text.chars().count(),
                    EditKind::Replace {
                        text: previous_text.clone(),
                    },
                )
            }
        };
        self.modified = true;

        // Compute the reverse edit's range — where the insertion now sits.
        let reverse_end_char = start_char + inserted_len_chars;
        let reverse_range = Range::new(
            self.char_to_position(start_char),
            self.char_to_position(reverse_end_char),
        );
        let reverse_edit = Edit {
            range: reverse_range,
            kind: reverse_kind,
        };
        let entry = UndoEntry {
            applied: edit.clone(),
            reverse: reverse_edit,
        };
        self.undo.push(entry.clone());
        Ok(entry)
    }

    pub fn undo(&mut self) -> Result<UndoEntry, BufferError> {
        let entry = self.undo.pop_undo().ok_or(BufferError::NothingToUndo)?;
        // Apply reverse edit without pushing a new undo entry.
        let r = entry.reverse.range.normalized();
        let a = self.position_to_char(r.start)?;
        let b = self.position_to_char(r.end)?;
        // undo/redo change the TEXT, so every offset measured against the old
        // text expires here too — `apply` bumping alone was not enough: an
        // ordinal anchored before an undo read as FRESH and displayed a
        // position in the previous match set.
        //
        // BEFORE the match, not inside an arm: the first attempt at this fix
        // landed in the `Insert` arm only, so undoing an insert (whose reverse
        // is a Delete) still did not bump. After the fallible prelude, so a
        // rejected undo invalidates nothing.
        self.text_rev = self.text_rev.next();
        match &entry.reverse.kind {
            EditKind::Insert { text } => {
                self.rope.insert(a, text);
            }
            EditKind::Delete => {
                self.rope.remove(a..b);
            }
            EditKind::Replace { text } => {
                self.rope.remove(a..b);
                self.rope.insert(a, text);
            }
        }
        self.modified = true;
        Ok(entry)
    }

    pub fn redo(&mut self) -> Result<UndoEntry, BufferError> {
        let entry = self.undo.pop_redo().ok_or(BufferError::NothingToRedo)?;
        let r = entry.applied.range.normalized();
        let a = self.position_to_char(r.start)?;
        let b = self.position_to_char(r.end)?;
        // undo/redo change the TEXT, so every offset measured against the old
        // text expires here too — `apply` bumping alone was not enough: an
        // ordinal anchored before an undo read as FRESH and displayed a
        // position in the previous match set.
        //
        // BEFORE the match, not inside an arm: the first attempt at this fix
        // landed in the `Insert` arm only, so undoing an insert (whose reverse
        // is a Delete) still did not bump. After the fallible prelude, so a
        // rejected undo invalidates nothing.
        self.text_rev = self.text_rev.next();
        match &entry.applied.kind {
            EditKind::Insert { text } => {
                self.rope.insert(a, text);
            }
            EditKind::Delete => {
                self.rope.remove(a..b);
            }
            EditKind::Replace { text } => {
                self.rope.remove(a..b);
                self.rope.insert(a, text);
            }
        }
        self.modified = true;
        Ok(entry)
    }

    #[must_use]
    pub fn to_string(&self) -> String {
        self.rope.to_string()
    }
}

/// A registry of open buffers keyed by id — phase 1 single-threaded view.
#[derive(Debug, Default, Clone)]
pub struct BufferSet {
    buffers: HashMap<BufferId, Buffer>,
    next_id: u64,
}

impl BufferSet {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    pub fn next_id(&mut self) -> BufferId {
        self.next_id += 1;
        BufferId(self.next_id)
    }

    pub fn open(&mut self, path: impl AsRef<Path>) -> Result<BufferId, BufferError> {
        let id = self.next_id();
        let buf = Buffer::open(id, path)?;
        self.buffers.insert(id, buf);
        Ok(id)
    }

    pub fn scratch(&mut self, src: &str) -> BufferId {
        let id = self.next_id();
        self.buffers.insert(id, Buffer::from_str(id, src));
        id
    }

    #[must_use]
    pub fn get(&self, id: BufferId) -> Option<&Buffer> {
        self.buffers.get(&id)
    }

    pub fn get_mut(&mut self, id: BufferId) -> Option<&mut Buffer> {
        self.buffers.get_mut(&id)
    }

    #[must_use]
    pub fn ids(&self) -> Vec<BufferId> {
        let mut v: Vec<_> = self.buffers.keys().copied().collect();
        v.sort();
        v
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BufferSummary {
    pub id: BufferId,
    pub path: Option<PathBuf>,
    pub line_count: u32,
    pub modified: bool,
}

#[cfg(test)]
mod tests {
    use super::*;
    use escriba_core::{Edit, Position, Range};

    fn buf(src: &str) -> Buffer {
        Buffer::from_str(BufferId(1), src)
    }

    #[test]
    fn empty_has_one_line() {
        let b = Buffer::empty(BufferId(1));
        assert_eq!(b.line_count(), 1);
    }

    #[test]
    fn line_counts() {
        let b = buf("a\nb\nc\n");
        assert_eq!(b.line_count(), 4); // trailing \n leaves a final empty line
    }

    #[test]
    fn position_char_round_trip() {
        let b = buf("hello\nworld\n");
        let p = Position::new(1, 3);
        let c = b.position_to_char(p).unwrap();
        assert_eq!(b.char_to_position(c), p);
    }

    #[test]
    fn insert_then_undo() {
        let mut b = buf("hello");
        let e = Edit::insert(Position::new(0, 5), " world");
        b.apply(&e).unwrap();
        assert_eq!(b.to_string(), "hello world");
        b.undo().unwrap();
        assert_eq!(b.to_string(), "hello");
    }

    #[test]
    fn delete_then_redo() {
        let mut b = buf("hello world");
        let e = Edit::delete(Range::new(Position::new(0, 5), Position::new(0, 11)));
        b.apply(&e).unwrap();
        assert_eq!(b.to_string(), "hello");
        b.undo().unwrap();
        assert_eq!(b.to_string(), "hello world");
        b.redo().unwrap();
        assert_eq!(b.to_string(), "hello");
    }

    #[test]
    fn replace_is_delete_plus_insert() {
        let mut b = buf("hello world");
        let e = Edit::replace(
            Range::new(Position::new(0, 6), Position::new(0, 11)),
            "tatara",
        );
        b.apply(&e).unwrap();
        assert_eq!(b.to_string(), "hello tatara");
        b.undo().unwrap();
        assert_eq!(b.to_string(), "hello world");
    }

    #[test]
    fn clamp_constrains_position() {
        let b = buf("ab\ncd");
        assert_eq!(b.line_count(), 2);
        assert_eq!(b.clamp(Position::new(0, 99)), Position::new(0, 2));
        assert_eq!(b.clamp(Position::new(99, 0)), Position::new(1, 0));
    }

    #[test]
    fn slice_returns_text() {
        let b = buf("hello world");
        let s = b
            .slice(Range::new(Position::new(0, 6), Position::new(0, 11)))
            .unwrap();
        assert_eq!(s, "world");
    }

    #[test]
    fn save_round_trip() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("demo.txt");
        let mut b = Buffer::from_str(BufferId(1), "hello\n");
        b.save_as(&path).unwrap();
        let b2 = Buffer::open(BufferId(2), &path).unwrap();
        assert_eq!(b2.to_string(), "hello\n");
    }

    #[test]
    fn buffer_set_tracks_ids() {
        let mut set = BufferSet::new();
        let a = set.scratch("one");
        let b = set.scratch("two");
        assert_ne!(a, b);
        assert_eq!(set.ids().len(), 2);
        assert_eq!(set.get(a).unwrap().to_string(), "one");
    }
}

#[cfg(test)]
mod text_rev_tests {
    use super::*;
    use escriba_core::{Edit, Position, Range};

    fn buf(src: &str) -> Buffer {
        Buffer::from_str(BufferId(0), src)
    }

    #[test]
    fn a_fresh_buffer_starts_at_revision_zero() {
        assert_eq!(buf("hello").text_rev(), TextRev(0));
    }

    #[test]
    fn an_applied_edit_advances_the_revision() {
        let mut b = buf("hello");
        let before = b.text_rev();
        b.apply(&Edit::insert(Position::new(0, 0), "X".to_string()))
            .expect("insert applies");
        assert_ne!(
            b.text_rev(),
            before,
            "a text change must expire old offsets"
        );
    }

    #[test]
    fn each_edit_advances_it_again() {
        let mut b = buf("hello");
        let mut seen = vec![b.text_rev()];
        for _ in 0..3 {
            b.apply(&Edit::insert(Position::new(0, 0), "X".to_string()))
                .expect("insert applies");
            let now = b.text_rev();
            assert!(!seen.contains(&now), "revisions must not repeat: {now:?}");
            seen.push(now);
        }
    }

    #[test]
    fn a_rejected_edit_does_not_advance_the_revision() {
        // The property that makes this usable for staleness: an edit that never
        // touched the rope must not invalidate offsets that are still correct.
        let mut b = buf("hello");
        let before = b.text_rev();
        let out_of_range = Range {
            start: Position::new(99, 0),
            end: Position::new(99, 1),
        };
        assert!(
            b.apply(&Edit::delete(out_of_range)).is_err(),
            "the edit must fail"
        );
        assert_eq!(b.text_rev(), before, "a failed edit changed no text");
    }

    #[test]
    fn reading_the_buffer_does_not_advance_the_revision() {
        // Contrast with `EditGen`, which bumps on every action. If merely
        // looking moved this counter it would be a refresh generation again.
        let b = buf("hello");
        let before = b.text_rev();
        let _ = b.to_string();
        let _ = b.text_rev();
        assert_eq!(b.text_rev(), before);
    }
}

#[cfg(test)]
mod undo_rev_tests {
    use super::*;
    use escriba_core::{Edit, Position};

    #[test]
    fn undo_and_redo_advance_the_revision() {
        // They mutate the rope, so they expire offsets exactly as `apply`
        // does. The first fix for this landed inside one match arm only, so
        // undoing an INSERT — whose reverse is a Delete — still did not bump.
        let mut b = Buffer::from_str(BufferId(0), "hello");
        b.apply(&Edit::insert(Position::new(0, 0), "X".to_string()))
            .expect("insert applies");
        let after_edit = b.text_rev();

        b.undo().expect("undo applies");
        assert_ne!(b.text_rev(), after_edit, "undo must advance the revision");
        let after_undo = b.text_rev();

        b.redo().expect("redo applies");
        assert_ne!(b.text_rev(), after_undo, "redo must advance it again");
    }

    #[test]
    fn a_rejected_undo_does_not_advance_the_revision() {
        let mut b = Buffer::from_str(BufferId(0), "hello");
        let before = b.text_rev();
        assert!(b.undo().is_err(), "nothing to undo");
        assert_eq!(b.text_rev(), before, "a failed undo changed no text");
    }
}