diff options
Diffstat (limited to 'core/jj.zig')
| -rw-r--r-- | core/jj.zig | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/core/jj.zig b/core/jj.zig index 176ead49..c1d07662 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -3306,6 +3306,179 @@ test "a description is whole, its title and its author never cut: in a pane narr } } +/// 60 numbered lines, line 20 of 130 numbered words (12 rows or more at +/// 60 columns), and a change to line 40 whose title wraps over several +/// rows: the rows a view scrolls through, the layer's description among +/// them. +const Tall = struct { + const title = "a title long enough to wrap: the description of this change says what it does and why at some length, " ++ + "and then says it again in other words, and once more, so that no pane of sixty columns holds it in fewer than four rows " ++ + "beside its author and its age"; + + fn repo(gpa: std.mem.Allocator) !?Repo { + var base: std.ArrayList(u8) = .empty; + defer base.deinit(gpa); + var changed: std.ArrayList(u8) = .empty; + defer changed.deinit(gpa); + for (0..60) |i| { + const from = base.items.len; + if (i == 20) { + for (0..130) |w| try base.print(gpa, "t{d:0>3} ", .{w}); + try base.append(gpa, '\n'); + } else try base.print(gpa, "line {d:0>2}\n", .{i}); + try changed.appendSlice(gpa, if (i == 40) "line 40 changed\n" else base.items[from..]); + } + return Repo.with(gpa, &.{ .{ base.items, "base" }, .{ changed.items, title } }, null); + } + + /// The buffer's line the description is. + fn description(f: *const File.State) !usize { + for (f.layer.lines) |l| if (std.mem.indexOf(u8, f.content[l.at..][0..l.len], title[0..20]) != null) + return std.mem.count(u8, f.content[0..l.at], "\n"); + return error.NoDescription; + } + + /// The rows of the text above line `line`'s row that shows its byte + /// `col`, in `pane`'s view. + fn rowsAbove(tab: u16, pane: *Pane, line: usize, col: usize) usize { + var n: usize = File.rowOf(tab, pane, line, col); + for (0..line) |l| n += File.lineRows(tab, pane, l); + return n; + } +}; + +test "a view scrolls by rows through a line wrapped over 12 rows and a change's description wrapped over several (a line of the layer): a wheel's notch is one row down the whole text and back, PageDown and PageUp a page of rows, zj and zk one; a held j and k keep the cursor's row three rows inside the body" { + const gpa = testing.allocator; + var repo = try Tall.repo(gpa) orelse return error.SkipZigTest; + defer repo.deinit(gpa); + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 24, .windowed = false }); + defer p.deinit(); + const tab = p.settings.tab; + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) }); + const id = p.view().active; + const pane = p.panes[id].?; + p.update(.{ .command = "Changes @-" }); + p.sync(); + const f = &pane.file.?; + try testing.expect(File.lineRows(tab, pane, 20) >= 12); + try testing.expect(File.lineRows(tab, pane, try Tall.description(f)) >= 4); + var total: usize = 0; + for (0..File.nlines(gpa, f)) |l| total += File.lineRows(tab, pane, l); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const shot = struct { + fn of(q: *Pardes, w: *Pane, a: std.mem.Allocator) ![]const []const u8 { + return Ghosts.rows(q, w, a, try a.alloc([]const u8, 256)); + } + }.of; + const rect = p.view().rects[id]; + const at = .{ .col = rect.x + 10, .row = p.bodyTop(pane, rect) + 5 }; + const notch = struct { + fn of(down: bool, x: u16, y: u16) pardes.Event { + return .{ .mouse = .{ .button = if (down) .wheel_down else .wheel_up, .kind = .press, .col = x, .row = y } }; + } + }.of; + // Every row of the text at the body's top in turn, a notch each: the + // body's rows one up. + var was = try shot(p, pane, arena.allocator()); + const kept = was.len - 1; + var all: std.ArrayList([]const u8) = .empty; + try all.append(arena.allocator(), was[0]); + while (true) { + p.update(notch(true, at.col, at.row)); + const now = try shot(p, pane, arena.allocator()); + if (std.mem.eql(u8, now[0], was[0])) break; + for (now[0 .. kept - 1], was[1..kept]) |n, w| try testing.expectEqualStrings(w, n); + try all.append(arena.allocator(), now[0]); + was = now; + } + try testing.expectEqual(total, all.items.len); + // And back up, a row a notch. + var back = all.items.len - 1; + while (back > 0) : (back -= 1) { + p.update(notch(false, at.col, at.row)); + const now = try shot(p, pane, arena.allocator()); + try testing.expectEqualStrings(all.items[back - 1], now[0]); + for (now[1..kept], was[0 .. kept - 1]) |n, w| try testing.expectEqualStrings(w, n); + was = now; + } + const indexOf = struct { + fn in(rows: []const []const u8, row: []const u8) usize { + for (rows, 0..) |r, i| if (std.mem.eql(u8, r, row)) return i; + return rows.len; + } + }.in; + // PageDown and PageUp: a page of rows each, the text's last row at the + // top the furthest. + var top: usize = 0; + while (top < all.items.len - 1) { + p.update(.{ .key = .{ .cp = pardes.Key.page_down } }); + top = @min(top + pane.rows, all.items.len - 1); + try testing.expectEqual(top, indexOf(all.items, (try shot(p, pane, arena.allocator()))[0])); + } + while (top > 0) { + p.update(.{ .key = .{ .cp = pardes.Key.page_up } }); + top -|= pane.rows; + try testing.expectEqual(top, indexOf(all.items, (try shot(p, pane, arena.allocator()))[0])); + } + // zj and zk: a row each, through line 20's rows and the description's. + for (0..all.items.len - 1) |n| { + for ("zj") |k| p.update(.{ .key = .{ .cp = k, .text = &[_]u8{k} } }); + try testing.expectEqual(n + 1, indexOf(all.items, (try shot(p, pane, arena.allocator()))[0])); + } + for (0..all.items.len - 1) |n| { + for ("zk") |k| p.update(.{ .key = .{ .cp = k, .text = &[_]u8{k} } }); + try testing.expectEqual(all.items.len - 2 - n, indexOf(all.items, (try shot(p, pane, arena.allocator()))[0])); + } + // A held j from the first line to the last and a held k back: the + // cursor's row three rows (`scroll_off`) inside the body's edges, or + // as far as the text before or after it goes. + p.update(.{ .command = "Look a.zig:1" }); + const body = File.shownRows(tab, pane); + for ("jk") |cp| for (0..total) |_| { + p.update(.{ .key = .{ .cp = cp, .text = &[_]u8{cp} } }); + const t = File.top(tab, pane); + const cur = Tall.rowsAbove(tab, pane, @intCast(pane.body.cur_row), @intCast(pane.body.cur_col)); + const on = cur - Tall.rowsAbove(tab, pane, t.line, 0) - t.row; + try testing.expect(on >= @min(pardes.config.scroll_off, cur)); + try testing.expect(on <= body - 1 - @min(pardes.config.scroll_off, total - 1 - cur)); + }; +} + +test "a client wheels its session's view a row a notch into a change's description wrapped over several rows (a line of the layer), agreeing with the session at every notch; a client joined over that view loads its row" { + var repo = try Tall.repo(testing.allocator) orelse return error.SkipZigTest; + defer repo.deinit(testing.allocator); + const R = pardes.replica; + const a = try R.Authority.init(testing.allocator, .{ .cols = 60, .rows = 24 }, .{}); + defer a.deinit(); + const r = try R.Replica.init(testing.allocator, 1, try a.unit(0, 0)); + defer r.deinit(); + var line: [4200]u8 = undefined; + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) })); + r.confirm(try a.local(.{ .command = "Changes @-" })); + r.settle(); + const id = a.core.view().active; + const af = &a.core.panes[id].?.file.?; + const desc = try Tall.description(af); + const rect = a.core.view().rects[id]; + const notch: pardes.Event = .{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = rect.x + 10, .row = rect.y + 5 } }; + var n: usize = 0; + while (af.scroll != desc or af.scroll_row != 2) : (n += 1) { + if (n > 300) return error.NeverThere; + const m = try testing.allocator.dupe(u8, try r.input(notch)); + defer testing.allocator.free(m); + r.confirm(try a.take(m)); + r.settle(); + try testing.expect(r.diverged == null); + try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); + const rf = &r.core().panes[id].?.file.?; + try testing.expectEqual(af.scroll, rf.scroll); + try testing.expectEqual(af.scroll_row, rf.scroll_row); + } + try R.loadsAsSession(a, 2); +} + test "at one place a change's ghost rows come before its description: a line it changed and the next it took away, then its description" { const gpa = testing.allocator; const fn_text = "fn f() void {\n a();\n b();\n c();\n d();\n}\n"; |
