hjkl-buffer 0.0.18

Rope-backed text buffer with cursor and edits. Pre-1.0 churn.
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
use std::collections::BTreeMap;

use crate::search::SearchState;
use crate::{Position, Span, Viewport};

/// In-memory text buffer + cursor + viewport + per-row span cache.
///
/// This is the core type the rest of `sqeel-buffer` builds on.
/// Phase 1 of the migration ships construction + getters only;
/// motion / edit / render APIs land in later phases. The
/// `lines` invariant — at least one entry, never empty — is
/// preserved by every later mutation.
pub struct Buffer {
    /// One entry per visual row. Always non-empty: a freshly
    /// constructed `Buffer` holds a single empty `String` so cursor
    /// positions don't need an "is the buffer empty?" branch.
    lines: Vec<String>,
    /// Charwise cursor. `col` is bound by `lines[row].chars().count()`
    /// in normal mode, one past it in operator-pending / insert.
    cursor: Position,
    /// Last vertical-motion column (vim's `curswant`). `None` until
    /// the first `j`/`k` so the buffer can bootstrap from the live
    /// cursor column.
    sticky_col: Option<usize>,
    /// Where the buffer is scrolled to + how big the visible region
    /// is. Width / height come from the host on each draw.
    viewport: Viewport,
    /// External per-row syntax / marker styling. `spans[row]` is a
    /// `Vec<Span>` for that row; rows beyond `spans.len()` get no
    /// styling (host hasn't published them yet).
    spans: Vec<Vec<Span>>,
    /// Buffer-local marks (`m{a-z}` / `'{a-z}` / `` `{a-z} ``).
    /// `BTreeMap` so iteration is deterministic for snapshot tests.
    marks: BTreeMap<char, Position>,
    /// Bumps on every mutation; render cache keys against this so a
    /// per-row Line gets recomputed when its source row changes.
    dirty_gen: u64,
    /// Lazy per-row match cache for `/` search. Lives next to the
    /// other buffer state so the `Buffer` API can drive `n` / `N`
    /// without the host plumbing a separate index through.
    search: SearchState,
    /// Manual folds — closed ranges hide rows in the render path.
    /// `pub(crate)` so the [`folds`] module can read/write directly.
    pub(crate) folds: Vec<crate::folds::Fold>,
}

impl Default for Buffer {
    fn default() -> Self {
        Self::new()
    }
}

impl Buffer {
    /// Construct an empty buffer with one empty row + cursor at
    /// `(0, 0)`. Caller publishes a viewport size on first draw.
    pub fn new() -> Self {
        Self {
            lines: vec![String::new()],
            cursor: Position::default(),
            sticky_col: None,
            viewport: Viewport::default(),
            spans: Vec::new(),
            marks: BTreeMap::new(),
            dirty_gen: 0,
            search: SearchState::new(),
            folds: Vec::new(),
        }
    }

    /// Build a buffer from a flat string. Splits on `\n`; a trailing
    /// `\n` produces a trailing empty line (matches every text
    /// editor's behaviour and keeps `from_text(buf.as_string())` an
    /// identity round-trip in the common case).
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(text: &str) -> Self {
        let mut lines: Vec<String> = text.split('\n').map(str::to_owned).collect();
        if lines.is_empty() {
            lines.push(String::new());
        }
        Self {
            lines,
            cursor: Position::default(),
            sticky_col: None,
            viewport: Viewport::default(),
            spans: Vec::new(),
            marks: BTreeMap::new(),
            dirty_gen: 0,
            search: SearchState::new(),
            folds: Vec::new(),
        }
    }

    pub fn lines(&self) -> &[String] {
        &self.lines
    }

    pub fn line(&self, row: usize) -> Option<&str> {
        self.lines.get(row).map(String::as_str)
    }

    pub fn cursor(&self) -> Position {
        self.cursor
    }

    pub fn sticky_col(&self) -> Option<usize> {
        self.sticky_col
    }

    pub fn viewport(&self) -> Viewport {
        self.viewport
    }

    pub fn viewport_mut(&mut self) -> &mut Viewport {
        // Explicit `mut` access — viewport size lives on a separate
        // axis from buffer mutation, so we don't bump `dirty_gen`.
        &mut self.viewport
    }

    pub fn dirty_gen(&self) -> u64 {
        self.dirty_gen
    }

    /// Set cursor without scrolling. Caller is responsible for calling
    /// [`Buffer::ensure_cursor_visible`] when they want viewport
    /// follow. Clamps `row` and `col` to valid positions so motion
    /// helpers don't have to repeat the bound check.
    pub fn set_cursor(&mut self, pos: Position) {
        let last_row = self.lines.len().saturating_sub(1);
        let row = pos.row.min(last_row);
        let line_chars = self.lines[row].chars().count();
        let col = pos.col.min(line_chars);
        self.cursor = Position::new(row, col);
    }

    /// Replace the sticky col (vim's `curswant`). Motion code sets
    /// this after vertical / horizontal moves; the buffer doesn't
    /// touch it on its own.
    pub fn set_sticky_col(&mut self, col: Option<usize>) {
        self.sticky_col = col;
    }

    /// Bring the cursor into the visible viewport, scrolling by the
    /// minimum amount needed. When `viewport.wrap != Wrap::None` and
    /// `viewport.text_width > 0`, scrolling is screen-line aware:
    /// `top_row` is advanced one visible doc row at a time until the
    /// cursor's screen row falls inside the viewport's height.
    pub fn ensure_cursor_visible(&mut self) {
        let cursor = self.cursor;
        let v = self.viewport;
        let wrap_active = !matches!(v.wrap, crate::Wrap::None) && v.text_width > 0;
        if !wrap_active {
            self.viewport.ensure_visible(cursor);
            return;
        }
        if v.height == 0 {
            return;
        }
        // Cursor above the visible region: snap top_row to it.
        if cursor.row < v.top_row {
            self.viewport.top_row = cursor.row;
            self.viewport.top_col = 0;
            return;
        }
        let height = v.height as usize;
        // Push top_row forward (one visible doc row per iteration)
        // until the cursor's screen row sits inside [0, height).
        loop {
            let csr = self.cursor_screen_row_from(self.viewport.top_row);
            match csr {
                Some(row) if row < height => break,
                _ => {}
            }
            // Advance to the next non-folded doc row up to (but not
            // past) the cursor row. Stop if we ran out of room.
            let mut next = self.viewport.top_row + 1;
            while next <= cursor.row && self.folds.iter().any(|f| f.hides(next)) {
                next += 1;
            }
            if next > cursor.row {
                // Last resort — pin top_row to the cursor row so the
                // cursor lands at the top edge.
                self.viewport.top_row = cursor.row;
                break;
            }
            self.viewport.top_row = next;
        }
        self.viewport.top_col = 0;
    }

    /// Cursor's screen row offset (0-based) from `viewport.top_row`
    /// under the current wrap mode + `text_width`. `None` when wrap
    /// is off, the cursor row is hidden by a fold, or the cursor sits
    /// above `top_row`. Used by host-side scrolloff math.
    pub fn cursor_screen_row(&self) -> Option<usize> {
        if matches!(self.viewport.wrap, crate::Wrap::None) || self.viewport.text_width == 0 {
            return None;
        }
        self.cursor_screen_row_from(self.viewport.top_row)
    }

    /// Number of screen rows the doc range `start..=end` occupies
    /// under the current wrap mode. Skips fold-hidden rows. Empty /
    /// past-end ranges return 0. `Wrap::None` returns the visible
    /// doc-row count (one screen row per doc row).
    pub fn screen_rows_between(&self, start: usize, end: usize) -> usize {
        if start > end {
            return 0;
        }
        let last = self.lines.len().saturating_sub(1);
        let end = end.min(last);
        let v = self.viewport;
        let mut total = 0usize;
        for r in start..=end {
            if self.folds.iter().any(|f| f.hides(r)) {
                continue;
            }
            if matches!(v.wrap, crate::Wrap::None) || v.text_width == 0 {
                total += 1;
            } else {
                let line = self.lines.get(r).map(String::as_str).unwrap_or("");
                total += crate::wrap::wrap_segments(line, v.text_width, v.wrap).len();
            }
        }
        total
    }

    /// Earliest `top_row` such that `screen_rows_between(top, last)`
    /// is at least `height`. Lets host-side scrolloff math clamp
    /// `top_row` so the buffer never leaves blank rows below the
    /// content. When the buffer's total screen rows are smaller than
    /// `height` this returns 0.
    pub fn max_top_for_height(&self, height: usize) -> usize {
        if height == 0 {
            return 0;
        }
        let last = self.lines.len().saturating_sub(1);
        let mut total = 0usize;
        let mut row = last;
        loop {
            if !self.folds.iter().any(|f| f.hides(row)) {
                let v = self.viewport;
                total += if matches!(v.wrap, crate::Wrap::None) || v.text_width == 0 {
                    1
                } else {
                    let line = self.lines.get(row).map(String::as_str).unwrap_or("");
                    crate::wrap::wrap_segments(line, v.text_width, v.wrap).len()
                };
            }
            if total >= height {
                return row;
            }
            if row == 0 {
                return 0;
            }
            row -= 1;
        }
    }

    /// Returns the cursor's screen row (0-based, relative to `top`)
    /// under the current wrap mode + text width. `None` when the
    /// cursor row is hidden by a fold or sits above `top`.
    fn cursor_screen_row_from(&self, top: usize) -> Option<usize> {
        let cursor = self.cursor;
        if cursor.row < top {
            return None;
        }
        let v = self.viewport;
        let mut screen = 0usize;
        for r in top..=cursor.row {
            if self.folds.iter().any(|f| f.hides(r)) {
                continue;
            }
            let line = self.lines.get(r).map(String::as_str).unwrap_or("");
            let segs = crate::wrap::wrap_segments(line, v.text_width, v.wrap);
            if r == cursor.row {
                let seg_idx = crate::wrap::segment_for_col(&segs, cursor.col);
                return Some(screen + seg_idx);
            }
            screen += segs.len();
        }
        None
    }

    /// Clamp `pos` to the buffer's content. Out-of-range row gets
    /// pulled to the last row; out-of-range col gets pulled to the
    /// row's char count (one past last char — insertion point).
    pub fn clamp_position(&self, pos: Position) -> Position {
        let last_row = self.lines.len().saturating_sub(1);
        let row = pos.row.min(last_row);
        let line_chars = self.lines[row].chars().count();
        let col = pos.col.min(line_chars);
        Position::new(row, col)
    }

    /// Mutable access to the lines. Crate-internal — edit code uses
    /// this; outside callers go through [`Buffer::apply_edit`].
    pub(crate) fn lines_mut(&mut self) -> &mut Vec<String> {
        &mut self.lines
    }

    /// Crate-internal accessor for the search state. Search code
    /// keeps its lazy match cache here; the public surface is
    /// [`Buffer::set_search_pattern`] etc.
    pub(crate) fn search_state(&self) -> &SearchState {
        &self.search
    }
    pub(crate) fn search_state_mut(&mut self) -> &mut SearchState {
        &mut self.search
    }

    /// Bump the render-cache generation. Crate-internal — every
    /// content mutation calls this so render fingerprints invalidate.
    pub(crate) fn dirty_gen_bump(&mut self) {
        self.dirty_gen = self.dirty_gen.wrapping_add(1);
    }

    /// Replace the per-row syntax span overlay. Used by the host
    /// once tree-sitter (or any other producer) has fresh styling
    /// for the visible window. `spans[row]` corresponds to row
    /// `row`; rows beyond `spans.len()` get no styling.
    pub fn set_spans(&mut self, spans: Vec<Vec<crate::Span>>) {
        self.spans = spans;
        self.dirty_gen_bump();
    }

    /// Replace the buffer's full text in place. Cursor + sticky col
    /// are clamped to the new content; viewport stays put. Used
    /// during the migration off tui-textarea so the buffer can mirror
    /// the textarea's content after every edit without rebuilding
    /// the whole struct.
    pub fn replace_all(&mut self, text: &str) {
        let mut lines: Vec<String> = text.split('\n').map(str::to_owned).collect();
        if lines.is_empty() {
            lines.push(String::new());
        }
        self.lines = lines;
        // Clamp cursor to surviving content.
        let cursor = self.clamp_position(self.cursor);
        self.cursor = cursor;
        self.dirty_gen_bump();
    }

    /// Same as [`Buffer::set_spans`] but exposed for in-crate tests
    /// without crossing the dirty-gen / lifetime boundaries the
    /// pub method advertises.
    #[cfg(test)]
    pub(crate) fn set_spans_for_test(&mut self, spans: Vec<Vec<crate::Span>>) {
        self.spans = spans;
    }

    pub fn marks(&self) -> &BTreeMap<char, Position> {
        &self.marks
    }

    pub fn spans(&self) -> &[Vec<Span>] {
        &self.spans
    }

    /// Concatenate the rows into a single `String` joined by `\n`.
    /// Inverse of [`Buffer::from_str`] for content built without a
    /// trailing newline.
    pub fn as_string(&self) -> String {
        self.lines.join("\n")
    }

    /// Number of rows in the buffer. Always `>= 1`.
    pub fn row_count(&self) -> usize {
        self.lines.len()
    }
}

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

    #[test]
    fn new_has_one_empty_row() {
        let b = Buffer::new();
        assert_eq!(b.row_count(), 1);
        assert_eq!(b.line(0), Some(""));
        assert_eq!(b.cursor(), Position::default());
    }

    #[test]
    fn from_str_splits_on_newline() {
        let b = Buffer::from_str("foo\nbar\nbaz");
        assert_eq!(b.row_count(), 3);
        assert_eq!(b.line(0), Some("foo"));
        assert_eq!(b.line(2), Some("baz"));
    }

    #[test]
    fn from_str_trailing_newline_keeps_empty_row() {
        let b = Buffer::from_str("foo\n");
        assert_eq!(b.row_count(), 2);
        assert_eq!(b.line(1), Some(""));
    }

    #[test]
    fn from_str_empty_input_keeps_one_row() {
        let b = Buffer::from_str("");
        assert_eq!(b.row_count(), 1);
        assert_eq!(b.line(0), Some(""));
    }

    #[test]
    fn as_string_round_trips() {
        let b = Buffer::from_str("a\nb\nc");
        assert_eq!(b.as_string(), "a\nb\nc");
    }

    #[test]
    fn dirty_gen_starts_at_zero() {
        assert_eq!(Buffer::new().dirty_gen(), 0);
    }

    #[test]
    fn ensure_cursor_visible_wrap_scrolls_when_cursor_below_screen() {
        let mut b = Buffer::from_str("aaaaaaaaaa\nb\nc");
        {
            let v = b.viewport_mut();
            v.height = 3;
            v.width = 4;
            v.text_width = 4;
            v.wrap = crate::Wrap::Char;
        }
        // Cursor on row 2 col 0. Doc rows 0-2 occupy 3+1+1=5 screen
        // rows; only 3 fit. ensure_cursor_visible should advance
        // top_row past row 0 so cursor lands inside the viewport.
        b.set_cursor(Position::new(2, 0));
        b.ensure_cursor_visible();
        assert_eq!(b.viewport().top_row, 1);
    }

    #[test]
    fn ensure_cursor_visible_wrap_no_scroll_when_visible() {
        let mut b = Buffer::from_str("aaaaaaaaaa\nb");
        {
            let v = b.viewport_mut();
            v.height = 4;
            v.width = 4;
            v.text_width = 4;
            v.wrap = crate::Wrap::Char;
        }
        // Cursor in row 0 segment 1 (col 5). Doc row 0 wraps to 3
        // screen rows; cursor's screen row is 1 (< height). No scroll.
        b.set_cursor(Position::new(0, 5));
        b.ensure_cursor_visible();
        assert_eq!(b.viewport().top_row, 0);
    }

    #[test]
    fn ensure_cursor_visible_wrap_snaps_top_when_cursor_above() {
        let mut b = Buffer::from_str("a\nb\nc\nd\ne");
        {
            let v = b.viewport_mut();
            v.height = 2;
            v.width = 4;
            v.text_width = 4;
            v.wrap = crate::Wrap::Char;
            v.top_row = 3;
        }
        b.set_cursor(Position::new(1, 0));
        b.ensure_cursor_visible();
        assert_eq!(b.viewport().top_row, 1);
    }

    #[test]
    fn screen_rows_between_sums_segments_under_wrap() {
        // 9-char first row + 1-char second row + empty third.
        let mut b = Buffer::from_str("aaaaaaaaa\nb\n");
        {
            let v = b.viewport_mut();
            v.wrap = crate::Wrap::Char;
            v.text_width = 4;
        }
        // Row 0 wraps to 3 segments; row 1 → 1; row 2 (empty) → 1.
        assert_eq!(b.screen_rows_between(0, 0), 3);
        assert_eq!(b.screen_rows_between(0, 1), 4);
        assert_eq!(b.screen_rows_between(0, 2), 5);
        assert_eq!(b.screen_rows_between(1, 2), 2);
    }

    #[test]
    fn screen_rows_between_one_per_doc_row_when_wrap_off() {
        let b = Buffer::from_str("aaaaa\nb\nc");
        assert_eq!(b.screen_rows_between(0, 2), 3);
    }

    #[test]
    fn max_top_for_height_walks_back_until_height_reached() {
        // 5 rows, last row wraps to 3 segments under width 4.
        let mut b = Buffer::from_str("a\nb\nc\nd\neeeeeeee");
        {
            let v = b.viewport_mut();
            v.wrap = crate::Wrap::Char;
            v.text_width = 4;
        }
        // Last row alone = 2 segments; with row 3 added = 3 screen
        // rows; with row 2 = 4. height=4 → max_top = row 2.
        assert_eq!(b.max_top_for_height(4), 2);
        // Larger than total rows → 0.
        assert_eq!(b.max_top_for_height(99), 0);
    }

    #[test]
    fn cursor_screen_row_returns_none_when_wrap_off() {
        let b = Buffer::from_str("a");
        assert!(b.cursor_screen_row().is_none());
    }

    #[test]
    fn cursor_screen_row_under_wrap() {
        let mut b = Buffer::from_str("aaaaaaaaaa\nb");
        {
            let v = b.viewport_mut();
            v.wrap = crate::Wrap::Char;
            v.text_width = 4;
        }
        b.set_cursor(Position::new(0, 5));
        // Cursor on row 0 segment 1 → screen row 1.
        assert_eq!(b.cursor_screen_row(), Some(1));
        b.set_cursor(Position::new(1, 0));
        // Row 0 wraps to 3 segments + row 1's first segment = 3.
        assert_eq!(b.cursor_screen_row(), Some(3));
    }

    #[test]
    fn ensure_cursor_visible_falls_back_when_wrap_disabled() {
        let mut b = Buffer::from_str("a\nb\nc\nd\ne");
        {
            let v = b.viewport_mut();
            v.height = 2;
            v.width = 4;
            v.text_width = 4;
            v.wrap = crate::Wrap::None;
        }
        b.set_cursor(Position::new(4, 0));
        b.ensure_cursor_visible();
        // Without wrap the existing doc-row math runs: cursor at row 4
        // with height 2 → top_row = 3.
        assert_eq!(b.viewport().top_row, 3);
    }
}