ochrepad-core 0.3.0

Text model and editing engine of the ochrepad text editor (internal; no stable API)
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
//! Display rows: how a view's document lines map to rows on screen
//! (PLAN_v0.2.0 3.2). A row is addressed relative to its line, as a
//! [`RowPos`], so nothing needs a global row index: with word wrap that
//! would mean wrapping the whole file. Lines hidden by a fold have no rows;
//! with word wrap on, a line has one row per wrapped piece, otherwise one.
//!
//! A compare view's side has [`Padding`]: blank rows before some lines (and
//! after the last), opposite lines only the other side has. They count as
//! rows of the line they precede; carets never stand on them.
//!
//! The editor widget scrolls, paints and hit-tests through a [`DisplayMap`],
//! and vertical motions resolve through one, so a macro's `Up` moves one
//! row in whatever view plays it.

use std::collections::HashMap;
use std::ops::Range;
use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::buffer::Buffer;
use crate::fold::FoldSet;
use crate::history::Revision;
use crate::position::{
    ByteOffset, VisualCol, Wrapped, byte_to_visual_col, prev_grapheme_boundary, visual_col_to_byte,
    wrap_line,
};
use crate::text::Document;
use crate::view::View;

/// One display row: the `sub`-th row of document line `line`. Rows order by
/// line, then by row within the line.
#[derive(
    Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub struct RowPos {
    pub line: usize,
    pub sub: usize,
}

impl RowPos {
    pub fn new(line: usize, sub: usize) -> Self {
        Self { line, sub }
    }
}

/// Wrapped lines of one view, for one (width, tab width). An entry made at
/// an older revision is still good if the line's text is unchanged, so an
/// edit only rewraps the lines it touched (or shifted).
#[derive(Clone, Debug, Default)]
pub(crate) struct WrapCache {
    key: Option<(usize, usize)>,
    lines: HashMap<usize, WrapEntry>,
}

#[derive(Clone, Debug)]
struct WrapEntry {
    /// The revision this entry was last checked at.
    revision: Revision,
    text: String,
    wrapped: Arc<Wrapped>,
}

/// Lines kept in a [`WrapCache`] before it starts over.
const WRAP_CACHE_LINES: usize = 4096;

/// Blank rows that align a view with another: `before[line]` rows above
/// each line (missing entries are 0), `after_last` rows after the last.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Padding {
    pub before: Vec<usize>,
    pub after_last: usize,
}

/// Folds do not apply to a padded view.
static NO_FOLDS: std::sync::LazyLock<FoldSet> = std::sync::LazyLock::new(FoldSet::default);

/// A view's rows over its document.
#[derive(Clone, Copy)]
pub struct DisplayMap<'a> {
    doc: &'a Document,
    revision: Revision,
    folds: &'a FoldSet,
    wrap: Option<usize>,
    cache: &'a std::cell::RefCell<WrapCache>,
    padding: Option<&'a Padding>,
    tab_width: usize,
}

impl<'a> DisplayMap<'a> {
    pub fn new(buffer: &'a Buffer, view: &'a View) -> Self {
        let tab_width = buffer.indent.width;
        let padding = view.padding.as_deref();
        Self {
            doc: buffer.doc(),
            revision: buffer.revision(),
            folds: if padding.is_some() {
                &NO_FOLDS
            } else {
                &view.folds
            },
            wrap: view.wrap_cols.filter(|_| padding.is_none()),
            cache: &view.wrap_cache,
            padding,
            tab_width,
        }
    }

    pub fn doc(&self) -> &'a Document {
        self.doc
    }

    /// Whether lines wrap.
    pub fn wraps(&self) -> bool {
        self.wrap.is_some()
    }

    /// How `line` wraps, or `None` when wrap is off.
    fn wrapped(&self, line: usize) -> Option<Arc<Wrapped>> {
        let cols = self.wrap?;
        let mut cache = self.cache.borrow_mut();
        let key = (cols, self.tab_width);
        if cache.key != Some(key) || cache.lines.len() > WRAP_CACHE_LINES {
            cache.key = Some(key);
            cache.lines.clear();
        }
        if let Some(e) = cache.lines.get_mut(&line) {
            if e.revision == self.revision {
                return Some(e.wrapped.clone());
            }
            let text = self.doc.line_text(line);
            if text == e.text {
                e.revision = self.revision;
                return Some(e.wrapped.clone());
            }
        }
        let text = self.doc.line_text(line);
        let wrapped = Arc::new(wrap_line(&text, cols, self.tab_width));
        let entry = WrapEntry {
            revision: self.revision,
            text,
            wrapped: wrapped.clone(),
        };
        cache.lines.insert(line, entry);
        Some(wrapped)
    }

    /// Rows that `line` takes on screen: 0 for a hidden line.
    pub fn rows_in_line(&self, line: usize) -> usize {
        if self.folds.is_hidden(line) {
            0
        } else {
            self.pad_before(line) + self.text_rows(line) + self.pad_after(line)
        }
    }

    /// Rows of `line`'s text (not counting padding).
    fn text_rows(&self, line: usize) -> usize {
        self.wrapped(line).map_or(1, |w| w.starts.len())
    }

    fn pad_before(&self, line: usize) -> usize {
        self.padding
            .map_or(0, |p| p.before.get(line).copied().unwrap_or(0))
    }

    fn pad_after(&self, line: usize) -> usize {
        match self.padding {
            Some(p) if line + 1 == self.doc.line_count() => p.after_last,
            _ => 0,
        }
    }

    /// Which row of its line's text `row` is, or `None` for a blank
    /// (padding) row.
    pub fn text_sub(&self, row: RowPos) -> Option<usize> {
        let sub = row.sub.checked_sub(self.pad_before(row.line))?;
        (sub < self.text_rows(row.line)).then_some(sub)
    }

    /// Whether `row` is a blank padding row.
    pub fn is_filler(&self, row: RowPos) -> bool {
        self.text_sub(row).is_none()
    }

    /// Lines that are not hidden. Sizes the scrollbar.
    pub fn visible_line_count(&self) -> usize {
        self.doc.line_count() - self.folds.hidden_count()
    }

    /// The `index`-th line that is not hidden (0-based), or the last one.
    pub fn visible_line_at(&self, index: usize) -> usize {
        self.folds.visible_line(index, self.doc.line_count())
    }

    /// `line`, or if it is hidden the shown line above it (a fold's header),
    /// or below it when nothing above is shown (a filter can hide the first
    /// lines).
    fn shown_line(&self, line: usize) -> usize {
        match self.folds.hidden_range(line) {
            Some((start, end)) => start
                .checked_sub(1)
                .unwrap_or_else(|| (end + 1).min(self.doc.line_count() - 1)),
            None => line,
        }
    }

    /// How many visible lines come before `line` (which must be visible).
    fn visible_index(&self, line: usize) -> usize {
        line - self.folds.hidden_before(line)
    }

    fn next_visible_line(&self, line: usize) -> Option<usize> {
        let next = match self.folds.hidden_range(line + 1) {
            Some((_, end)) => end + 1,
            None => line + 1,
        };
        (next < self.doc.line_count()).then_some(next)
    }

    fn prev_visible_line(&self, line: usize) -> Option<usize> {
        let prev = line.checked_sub(1)?;
        match self.folds.hidden_range(prev) {
            Some((start, _)) => start.checked_sub(1),
            None => Some(prev),
        }
    }

    pub fn first_row(&self) -> RowPos {
        RowPos::new(self.shown_line(0), 0)
    }

    pub fn last_row(&self) -> RowPos {
        let line = self.shown_line(self.doc.line_count() - 1);
        RowPos::new(line, self.rows_in_line(line) - 1)
    }

    /// The nearest real row: a line past the end becomes the last line, a
    /// hidden line its fold's header, and a row past the line's last row
    /// (after the text or the width changed) that last row.
    pub fn clamp_row(&self, row: RowPos) -> RowPos {
        let line = self.shown_line(row.line.min(self.doc.line_count() - 1));
        RowPos::new(line, row.sub.min(self.rows_in_line(line) - 1))
    }

    /// The row `n` rows after `row` (before it for negative `n`), stopping at
    /// the first and last rows, and how many rows it actually moved.
    pub fn step(&self, row: RowPos, n: isize) -> (RowPos, isize) {
        if !self.wraps() && self.padding.is_none() {
            // One row per visible line: count in visible lines.
            let from = self.visible_index(self.shown_line(row.line)) as isize;
            let last = self.visible_line_count() as isize - 1;
            let to = (from + n).clamp(0, last);
            let line = self.folds.visible_line(to as usize, self.doc.line_count());
            return (RowPos::new(line, 0), to - from);
        }
        let mut row = self.clamp_row(row);
        let mut moved = 0;
        while moved < n {
            if row.sub + 1 < self.rows_in_line(row.line) {
                row.sub += 1;
            } else if let Some(line) = self.next_visible_line(row.line) {
                row = RowPos::new(line, 0);
            } else {
                break;
            }
            moved += 1;
        }
        while moved > n {
            if row.sub > 0 {
                row.sub -= 1;
            } else if let Some(line) = self.prev_visible_line(row.line) {
                row = RowPos::new(line, self.rows_in_line(line) - 1);
            } else {
                break;
            }
            moved -= 1;
        }
        (row, moved)
    }

    /// The row `n` rows after `row`, clamped to the document.
    pub fn advance(&self, row: RowPos, n: isize) -> RowPos {
        self.step(row, n).0
    }

    /// The text shown on `row`, without the line break. A row that ends at a
    /// wrap point ends where the next row starts.
    pub fn row_range(&self, row: RowPos) -> Range<ByteOffset> {
        let start = self.doc.line_start(row.line);
        let end = self.doc.line_end(row.line);
        let Some(sub) = self.text_sub(row) else {
            // Padding: empty, before or after the line.
            let at = if row.sub < self.pad_before(row.line) {
                start
            } else {
                end
            };
            return at..at;
        };
        match self.wrapped(row.line) {
            None => start..end,
            Some(w) => {
                let at = |i: usize| ByteOffset(start.0 + w.starts[i]);
                let sub = sub.min(w.starts.len() - 1);
                at(sub)..w.starts.get(sub + 1).map_or(end, |_| at(sub + 1))
            }
        }
    }

    /// Whether `row` is the last row of its line's text (padding rows count
    /// as last).
    pub fn is_last_in_line(&self, row: RowPos) -> bool {
        self.text_sub(row)
            .is_none_or(|s| s + 1 >= self.text_rows(row.line))
    }

    /// The last caret position on `row`. A position at a wrap point shows
    /// at the start of the next row, so a row that wraps ends one grapheme
    /// before it.
    pub fn row_end(&self, row: RowPos) -> ByteOffset {
        let range = self.row_range(row);
        if self.is_last_in_line(row) {
            range.end
        } else {
            prev_grapheme_boundary(self.doc, range.end).max(range.start)
        }
    }

    /// The line's visual column where `row` starts, and the cells the row
    /// is indented by on screen (continuation rows only).
    pub fn row_cols(&self, row: RowPos) -> (usize, usize) {
        match (self.wrapped(row.line), self.text_sub(row)) {
            (Some(w), Some(sub)) if sub > 0 => {
                let sub = sub.min(w.starts.len() - 1);
                (w.start_cols[sub], w.indent)
            }
            _ => (0, 0),
        }
    }

    /// Cells the text of `row` is indented by (continuation rows only).
    pub fn row_indent(&self, row: RowPos) -> usize {
        self.row_cols(row).1
    }

    /// The row showing `pos`; for a position hidden by a fold, the fold's
    /// header row.
    pub fn row_of(&self, pos: ByteOffset) -> RowPos {
        let line = self.doc.line_of(pos);
        let shown = self.shown_line(line);
        if shown != line {
            return RowPos::new(shown, self.rows_in_line(shown) - 1);
        }
        let sub = match self.wrapped(line) {
            None => 0,
            Some(w) => {
                let off = pos.0 - self.doc.line_start(line).0;
                w.starts.partition_point(|&s| s <= off) - 1
            }
        };
        RowPos::new(line, self.pad_before(line) + sub)
    }

    /// The on-screen column of `pos` within its row, counting a
    /// continuation row's indent, so that Up and Down keep carets above
    /// each other.
    pub fn col_in_row(&self, pos: ByteOffset) -> VisualCol {
        let col = byte_to_visual_col(self.doc, pos, self.tab_width).0;
        let (start_col, indent) = self.row_cols(self.row_of(pos));
        VisualCol(indent + col - start_col)
    }

    /// The position at on-screen column `col` of `row`, or the row's end if
    /// it is shorter.
    pub fn byte_at_col(&self, row: RowPos, col: VisualCol) -> ByteOffset {
        if self.is_filler(row) {
            return self.row_range(row).start;
        }
        let (start_col, indent) = self.row_cols(row);
        let target = if start_col > 0 || indent > 0 {
            start_col + col.0.saturating_sub(indent)
        } else {
            col.0
        };
        let at = visual_col_to_byte(self.doc, row.line, VisualCol(target), self.tab_width);
        at.clamp(self.row_range(row).start, self.row_end(row))
    }

    /// A scroll position as a number of visible lines from the top, for the
    /// scrollbar: the visible lines above `row`'s line, plus the part of that
    /// line scrolled past.
    pub fn offset_of(&self, row: RowPos, frac: f32) -> f64 {
        let rows = self.rows_in_line(row.line).max(1) as f64;
        self.visible_index(self.shown_line(row.line)) as f64 + (row.sub as f64 + frac as f64) / rows
    }

    /// The inverse of [`offset_of`](Self::offset_of): the top row and fraction
    /// for a scrollbar offset.
    pub fn pos_at_offset(&self, offset: f64) -> (RowPos, f32) {
        let max = self.visible_line_count().saturating_sub(1) as f64;
        let offset = offset.clamp(0.0, max + 0.999_999);
        let index = offset.floor();
        let line = self
            .folds
            .visible_line(index as usize, self.doc.line_count());
        let rows = self.rows_in_line(line).max(1) as f64;
        let within = (offset - index) * rows;
        let sub = (within.floor() as usize).min(self.rows_in_line(line).max(1) - 1);
        (RowPos::new(line, sub), (within - sub as f64) as f32)
    }

    /// Scrolls a (top row, fraction) position by `rows`, which may be
    /// fractional. Hitting the first row leaves no fraction; the caller
    /// clamps the bottom with [`max_top`](Self::max_top).
    pub fn scroll_by(&self, row: RowPos, frac: f32, rows: f64) -> (RowPos, f32) {
        let total = frac as f64 + rows;
        let whole = total.floor();
        let (to, moved) = self.step(row, whole as isize);
        if moved as f64 != whole {
            // Stopped at the first or last row.
            (to, 0.0)
        } else {
            (to, (total - whole) as f32)
        }
    }

    /// The lowest top row that still fills a viewport of `rows_full` rows
    /// (the first row when the document is shorter).
    pub fn max_top(&self, rows_full: usize) -> RowPos {
        self.advance(self.last_row(), -(rows_full.saturating_sub(1) as isize))
    }

    /// Rows from `top` (inclusive) down, at most `limit`.
    pub fn rows_from(&self, top: RowPos, limit: usize) -> impl Iterator<Item = RowPos> + '_ {
        let last = self.last_row();
        std::iter::successors(Some(top), move |&r| (r < last).then(|| self.advance(r, 1)))
            .take(limit)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::editor::Editor;

    fn with_map(text: &str, wrap: Option<usize>, f: impl FnOnce(DisplayMap)) {
        let mut ed = Editor::new();
        let buffer = ed.add_buffer(text, Default::default());
        let view = ed.add_view(buffer).unwrap();
        ed.view_mut(view).unwrap().wrap_cols = wrap;
        let v = ed.view(view).unwrap();
        f(DisplayMap::new(ed.buffer(buffer).unwrap(), v));
    }

    #[test]
    fn identity_rows_are_lines() {
        with_map("a\nbb\n\tc", None, |m| {
            assert_eq!(m.last_row(), RowPos::new(2, 0));
            assert_eq!(m.row_of(ByteOffset(3)), RowPos::new(1, 0));
            assert_eq!(m.row_range(RowPos::new(1, 0)), ByteOffset(2)..ByteOffset(4));
            assert_eq!(m.step(RowPos::new(1, 0), 5), (RowPos::new(2, 0), 1));
            assert_eq!(m.step(RowPos::new(1, 0), -5), (RowPos::new(0, 0), -1));
            assert_eq!(m.col_in_row(ByteOffset(6)), VisualCol(4));
            assert_eq!(
                m.byte_at_col(RowPos::new(2, 0), VisualCol(2)),
                ByteOffset(5)
            );
            let rows: Vec<_> = m.rows_from(RowPos::new(1, 0), 10).collect();
            assert_eq!(rows, vec![RowPos::new(1, 0), RowPos::new(2, 0)]);
        });
    }

    #[test]
    fn scrolling_clamps_and_round_trips_offsets() {
        with_map(&"x\n".repeat(99), None, |m| {
            assert_eq!(m.max_top(10), RowPos::new(90, 0));
            let (row, frac) = m.scroll_by(RowPos::new(5, 0), 0.25, 2.5);
            assert_eq!((row, frac), (RowPos::new(7, 0), 0.75));
            assert_eq!(
                m.scroll_by(RowPos::new(1, 0), 0.5, -3.0),
                (RowPos::new(0, 0), 0.0)
            );
            assert_eq!(m.offset_of(row, frac), 7.75);
            assert_eq!(m.pos_at_offset(7.75), (row, frac));
            assert_eq!(m.pos_at_offset(-1.0), (RowPos::new(0, 0), 0.0));
        });
    }

    #[test]
    fn wrapped_rows_step_and_map_positions() {
        // "  aaaa bbbb cccc" at width 10: "  aaaa " / "bbbb " / "cccc",
        // the continuation rows indented by 2.
        with_map("x\n  aaaa bbbb cccc\ny", Some(10), |m| {
            assert_eq!(m.rows_in_line(1), 3);
            let line = 2; // start of line 1
            assert_eq!(
                m.row_range(RowPos::new(1, 1)),
                ByteOffset(line + 7)..ByteOffset(line + 12)
            );
            assert_eq!(
                m.row_of(ByteOffset(line + 7)),
                RowPos::new(1, 1),
                "wrap point"
            );
            assert_eq!(m.row_end(RowPos::new(1, 1)), ByteOffset(line + 11));
            assert_eq!(m.row_end(RowPos::new(1, 2)), ByteOffset(line + 16));
            assert_eq!(m.row_indent(RowPos::new(1, 2)), 2);
            // The second "b" (line column 8) is at column 3 of its row,
            // counting the indent.
            assert_eq!(m.col_in_row(ByteOffset(line + 8)), VisualCol(3));
            assert_eq!(
                m.byte_at_col(RowPos::new(1, 2), VisualCol(3)),
                ByteOffset(line + 13)
            );
            assert_eq!(m.step(RowPos::new(0, 0), 3), (RowPos::new(1, 2), 3));
            assert_eq!(m.step(RowPos::new(2, 0), -2), (RowPos::new(1, 1), -2));
            assert_eq!(m.last_row(), RowPos::new(2, 0));
            assert_eq!(m.max_top(3), RowPos::new(1, 1));
            assert_eq!(m.clamp_row(RowPos::new(1, 9)), RowPos::new(1, 2));
        });
    }
}