diff options
Diffstat (limited to 'core/views.zig')
| -rw-r--r-- | core/views.zig | 153 |
1 files changed, 149 insertions, 4 deletions
diff --git a/core/views.zig b/core/views.zig index f414eb6c..e5f2038e 100644 --- a/core/views.zig +++ b/core/views.zig @@ -63,9 +63,11 @@ pub const PaneView = struct { follow_scroll: usize = 0, scrolled_to: ?usize = null, body: Part(Text, &body_fields) = .{}, - /// A document's scroll and size; a terminal's size is its pty's, which - /// every view shows (sized by its last typist, `Pardes.sync`). + /// A document's scroll (its top: File.Top) and size; a terminal's size + /// is its pty's, which every view shows (sized by its last typist, + /// `Pardes.sync`). scroll: usize = 0, + scroll_row: u32 = 0, cols: u16 = 1, rows: u16 = 1, @@ -80,6 +82,12 @@ pub const Shares = struct { k: u8, panes: []const Share = &.{}, + /// Its view, a slot (walk.zig `.index`). + pub const replica = .{ .index = .{.{ "k", "slots" }} }; + pub fn slots(_: *const Shares, k: usize) bool { + return k < max; + } + pub const Share = struct { id: u16, pv: PaneView }; }; @@ -252,7 +260,10 @@ fn extract(pane: *const Pane) PaneView { var v: PaneView = .{ .cols = pane.cols, .rows = pane.rows }; copy(&pane_fields, &v, pane); copy(&body_fields, &v.body, &pane.body); - if (pane.file) |f| v.scroll = f.scroll; + if (pane.file) |f| { + v.scroll = f.scroll; + v.scroll_row = f.scroll_row; + } return v; } @@ -269,6 +280,7 @@ fn apply(pane: *Pane, v: *const PaneView) void { // brought back inside it. const lines = File.nlines(pane.gpa, f); f.scroll = @min(v.scroll, lines -| 1); + f.scroll_row = v.scroll_row; if (pane.body.cur_row >= lines) { pane.body.vsel.active = false; pane.body.nsel = 0; @@ -552,7 +564,10 @@ pub fn within(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pa pane.cols = pv.cols; pane.rows = pv.rows; } - if (pane.file) |*file| file.scroll = pv.scroll; + if (pane.file) |*file| { + file.scroll = pv.scroll; + file.scroll_row = pv.scroll_row; + } pv.body.sels = &.{}; pv.body.nsel = 0; }; @@ -883,6 +898,136 @@ test "a view drawn while another is live keeps its own cursors" { try testing.expectEqual(@as(u16, 1), pane.body.nsel); } +test "a view drawn after another view's step moved the top into a long wrapped line, the two alike before it: drawn from its own top, line 0, not from the row the step found in the other's line" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + // A short first line, and line 10 wrapped over more rows than the body. + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + try text.appendSlice(gpa, "short\n"); + for (1..10) |i| try text.print(gpa, "line {d}\n", .{i}); + for (0..500) |w| try text.print(gpa, "w{d:0>4} ", .{w}); + try text.appendSlice(gpa, "\nlast\n"); + const pane = try p.setTestFile(text.items); + const k = p.openView().?; + // View 0's step puts the cursor at line 10's end: the top goes into + // that line's rows, a row far past line 0's five bytes. View k is + // where view 0 was: its top line 0, its size view 0's. + pane.body.cur_row = 10; + pane.body.cur_col = @intCast(pardes.panes.File.sourceLine(pane, 10).len - 1); + pane.body.cur_pinned = true; + pane.ensureCursorVisible(p.settings.tab); + p.sync(); + try testing.expectEqual(@as(usize, 10), pane.file.?.scroll); + try testing.expect(pane.file.?.scroll_row > 0); + const Seen = struct { + short: bool = false, + fn present(ctx: ?*anyopaque, s: *const pardes.Surface) bool { + const seen: *@This() = @ptrCast(@alignCast(ctx.?)); + var row: [512]u8 = undefined; + for (0..s.rows) |y| { + var n: usize = 0; + for (s.cells[y * s.cols ..][0..s.cols]) |c| { + if (n + c.len > row.len) break; + @memcpy(row[n..][0..c.len], c.text[0..c.len]); + n += c.len; + } + if (std.mem.indexOf(u8, row[0..n], "short") != null) seen.short = true; + } + return true; + } + }; + var seen: Seen = .{}; + _ = try drawOne(p, k.index(), .{ .ctx = &seen, .f = Seen.present }); + try testing.expect(seen.short); +} + +test "a view's step after another view's step moved the top, the two alike before it: it scrolls from its own top's row, as a core loaded from the session (none of its geometry) does" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + // Line 3 wrapped over several rows, line 10 over more than the body. + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + for (0..12) |i| switch (i) { + 3, 10 => { + for (0..@as(usize, if (i == 3) 60 else 500)) |w| try text.print(gpa, "w{d:0>4} ", .{w}); + try text.append(gpa, '\n'); + }, + else => try text.print(gpa, "line {d}\n", .{i}), + }; + const pane = try p.setTestFile(text.items); + const f = &pane.file.?; + // Both views' top on line 3's third row, the cursor on it. + pane.body.cur_row = 3; + pane.body.cur_col = 150; + f.scroll = 3; + f.scroll_row = 2; + p.sync(); + const k = p.openView().?; + // View 0's step: the cursor to line 10's end, the top into its rows. + pane.body.cur_row = 10; + pane.body.cur_col = @intCast(pardes.panes.File.sourceLine(pane, 10).len - 1); + pane.body.cur_pinned = true; + pane.ensureCursorVisible(p.settings.tab); + p.sync(); + try testing.expect(f.scroll == 10 and f.scroll_row > 2); + // The session as a client joining it loads it: no geometry made. + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes, null); + defer loaded.deinit(); + // View k's PageDown in each, from line 3's third row. + for ([_]*Pardes{ p, loaded }) |core| { + core.toView(k); + core.update(.{ .key = .{ .cp = pardes.Key.page_down } }); + } + const lf = &loaded.panes[0].?.file.?; + try testing.expectEqual(lf.scroll, f.scroll); + try testing.expectEqual(lf.scroll_row, f.scroll_row); +} + +test "a view's top on a wrapped line's third row, the line taken out by another view and the next one empty: its top row past its line's, read as its last, and the session's dump loads as the session" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + for (0..30) |i| switch (i) { + 3 => { + for (0..60) |w| try text.print(gpa, "w{d:0>4} ", .{w}); + try text.append(gpa, '\n'); + }, + 4 => try text.append(gpa, '\n'), + else => try text.print(gpa, "line {d}\n", .{i}), + }; + const pane = try p.setTestFile(text.items); + const f = &pane.file.?; + // View k: its top on line 3's third row, its cursor well inside the body. + pane.body.cur_row = 10; + f.scroll = 3; + f.scroll_row = 2; + p.sync(); + const k = p.openView().?; + // View 0 takes line 3 out: view k's top line is the empty one after it. + cursorTo(p, pane, 3); + typeKeys(p, "xd"); + p.toView(k); + // A step of view k's own: its cursor inside the band, its top kept. + escape(p); + try testing.expectEqual(@as(usize, 3), f.scroll); + try testing.expectEqual(@as(usize, 0), pardes.panes.File.sourceLine(pane, 3).len); + try testing.expectEqual(@as(u32, 2), f.scroll_row); + try testing.expectEqual(pardes.panes.File.Top{ .line = 3, .row = 0 }, pardes.panes.File.top(p.settings.tab, pane)); + // A client joining loads the session's dump as the session. + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes, null); + defer loaded.deinit(); + try testing.expectEqual(pardes.walk.digest(Pardes, p), pardes.walk.digest(Pardes, loaded)); +} + test "a view not live keeps its place in a text another view edits" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); |
