diff options
Diffstat (limited to 'core/Tables.zig')
| -rw-r--r-- | core/Tables.zig | 196 |
1 files changed, 65 insertions, 131 deletions
diff --git a/core/Tables.zig b/core/Tables.zig index dc667b7c..a0256d8a 100644 --- a/core/Tables.zig +++ b/core/Tables.zig @@ -85,23 +85,10 @@ fences: std.ArrayList([2]u32) = .empty, garbage: usize = 0, /// How many times the whole text was read (for the numbers and the tests). scans: u32 = 0, -/// Where a reading of the whole text goes on from, until it reaches the -/// end: it is read a frame's budget at a time from the top (`frame_ns`), as -/// Places finds, so opening a big file never holds a frame; rows past it -/// are shown as their source until it gets there. -pending: ?Resume = null, -/// A scan's place: the next line's byte and number, the fence it is in, +/// Where a scan starts: the line's byte and number, the fence it is in, /// and whether the line before ended a block. -const Resume = struct { at: usize, n: u32, fence: ?Fence, prev_blank: bool }; - -/// What one frame spends reading a big text from the top: on the session's -/// tick, by the clock (core/tape.zig keeps a replica's reading in step). -pub const frame_ns: u64 = 4 * std.time.ns_per_ms; -/// What a new text's first look reads at once, wherever it is asked for -/// (the render, or a step reading the view's geometry): a count, never the -/// clock, so whoever asks first finds the same tables. -pub const first_lines: u32 = 4096; +const Start = struct { at: usize, n: u32, fence: ?Fence, prev_blank: bool }; pub fn deinit(t: *Tables, gpa: std.mem.Allocator) void { t.list.deinit(gpa); @@ -143,65 +130,18 @@ pub fn ensure(t: *Tables, tab: u16, gpa: std.mem.Allocator, f: *File.State) void } if (f.output != null or f.mini != null or !isMarkdown(f.path)) { t.list.clearRetainingCapacity(); - t.pending = null; return; } - const starts = File.lineIndex(gpa, f) catch { - t.list.clearRetainingCapacity(); - t.pending = null; - return; - }; + // Read whole, at once, whoever asks first (a frame, or a step reading + // the view's geometry): what is found is the text's alone, never how + // far a reading had got, which is no core's state (derived: a core + // that drew and one that did not, or one joined from a dump since, + // would find two answers). ReleaseFast: 40 ns a line of prose, 18 ms + // for 2.7 MB of 2000 tables (docs/next-tables.md's stress text). const one_edit = t.revision != null and t.revision.? +% 1 == f.revision and f.splice.revision == f.revision and f.splice.old_len == t.len; - if (one_edit and t.garbage <= 4 * t.widths.items.len + 1024) { - if (t.pending) |r| { - // An edit past where the reading has got to changes nothing it - // read: it goes on. - if (lineHolding(starts, f.splice.head) >= r.n) { - t.lines = starts.len; - t.go(tab, gpa, f.content, 0, first_lines); - return; - } - } else if (t.reflow(tab, gpa, f)) |_| return else |_| {} - } - t.begin(starts.len); - t.go(tab, gpa, f.content, 0, first_lines); -} - -/// A frame's more of a reading under way: on the session's tick, so a core -/// that draws reads exactly as far as one that does not. -pub fn step(t: *Tables, tab: u16, gpa: std.mem.Allocator, f: *File.State) void { - t.ensure(tab, gpa, f); - if (t.pending != null) t.go(tab, gpa, f.content, frame_ns, std.math.maxInt(u32)); -} - -/// Whether a reading of the text is still under way (Pardes.nextWake). -pub fn busy(t: *const Tables) bool { - return t.pending != null; -} - -/// Start reading the whole text again, from the top. -fn begin(t: *Tables, lines: usize) void { - t.list.clearRetainingCapacity(); - t.widths.clearRetainingCapacity(); - t.aligns.clearRetainingCapacity(); - t.fences.clearRetainingCapacity(); - t.garbage = 0; - t.scans +%= 1; - t.lines = lines; - t.pending = .{ .at = 0, .n = 0, .fence = null, .prev_blank = true }; -} - -/// Read on for about `budget_ns` (0: no clock) or `max_lines`, whichever -/// ends first. On no memory there are no tables (the text is shown as it is). -fn go(t: *Tables, tab: u16, gpa: std.mem.Allocator, content: []const u8, budget_ns: u64, max_lines: u32) void { - var at = t.pending orelse return; - const done = t.scanFrom(tab, gpa, content, &at, std.math.maxInt(u32), true, budget_ns, max_lines) catch { - t.list.clearRetainingCapacity(); - t.pending = null; - return; - }; - t.pending = if (done) null else at; + if (one_edit and t.garbage <= 4 * t.widths.items.len + 1024) if (t.reflow(tab, gpa, f)) |_| return else |_| {}; + t.scan(tab, gpa, f.content) catch t.list.clearRetainingCapacity(); } /// Line `n` of a text whose line index is `starts`. @@ -299,8 +239,8 @@ fn reflow(t: *Tables, tab: u16, gpa: std.mem.Allocator, f: *File.State) !void { // Any fence line closes a block (a header may follow it). const prev_blank = r0 == 0 or blank(prev) or fenceOf(prev) != null or std.mem.startsWith(u8, prev[@min(indentOf(prev), prev.len)..], "#"); const head = lineOf(content, starts, r0); - var at: Resume = .{ .at = head.start, .n = head.n, .fence = null, .prev_blank = prev_blank }; - _ = try t.scanFrom(tab, gpa, content, &at, @intCast(r1), false, 0, std.math.maxInt(u32)); + const at: Start = .{ .at = head.start, .n = head.n, .fence = null, .prev_blank = prev_blank }; + try t.scanFrom(tab, gpa, content, at, @intCast(r1), false); const fresh = try gpa.dupe(Table, t.list.items[before..]); defer gpa.free(fresh); t.list.shrinkRetainingCapacity(before); @@ -502,28 +442,21 @@ pub fn scan(t: *Tables, tab: u16, gpa: std.mem.Allocator, content: []const u8) ! t.list.clearRetainingCapacity(); t.widths.clearRetainingCapacity(); t.aligns.clearRetainingCapacity(); - t.begin(std.mem.count(u8, content, "\n") + 1); - var at = t.pending.?; - _ = try t.scanFrom(tab, gpa, content, &at, std.math.maxInt(u32), true, 0, std.math.maxInt(u32)); - t.pending = null; + t.fences.clearRetainingCapacity(); + t.garbage = 0; + t.scans +%= 1; + t.lines = std.mem.count(u8, content, "\n") + 1; + try t.scanFrom(tab, gpa, content, .{ .at = 0, .n = 0, .fence = null, .prev_blank = true }, std.math.maxInt(u32), true); } /// Find the tables whose header is on a line from `at` to `stop`, -/// appending them; with `fences`, keep the fences. With a budget, stop -/// when it is spent, `at` where to go on from. Whether it reached the end -/// (or `stop`). -fn scanFrom(t: *Tables, tab: u16, gpa: std.mem.Allocator, content: []const u8, at: *Resume, stop: u32, fences: bool, budget_ns: u64, max_lines: u32) !bool { +/// appending them; with `fences`, keep the fences. +fn scanFrom(t: *Tables, tab: u16, gpa: std.mem.Allocator, content: []const u8, at: Start, stop: u32, fences: bool) !void { var fence = at.fence; var prev_blank = at.prev_blank; var line = lineAt(content, at.at, at.n); - const t0 = if (budget_ns > 0) pardes.FrameStats.nowNs() else 0; - var lines: usize = 0; - while (line) |l| : (lines += 1) { + while (line) |l| { if (l.n > stop) break; - if (lines % 256 == 255 and (lines >= max_lines or (budget_ns > 0 and pardes.tape.over(t0, budget_ns)))) { - at.* = .{ .at = l.start, .n = l.n, .fence = fence, .prev_blank = prev_blank }; - return false; - } const following = nextLine(content, l); if (fence) |open| { if (closes(l.text, open)) { @@ -563,7 +496,6 @@ fn scanFrom(t: *Tables, tab: u16, gpa: std.mem.Allocator, content: []const u8, a prev_blank = is_blank or std.mem.startsWith(u8, l.text[@min(indentOf(l.text), l.text.len)..], "#"); line = following; } - return true; } /// Take the table whose header is `header`: its rows, its columns' widths @@ -1026,49 +958,6 @@ test "tables: an edit's tables, shifted and found again around it, are the whole try testing.expect(reflowed > 1000); } -test "tables: a big text's tables are read a frame's budget at a time from the top, edits on the way kept" { - var big: std.ArrayList(u8) = .empty; - defer big.deinit(testing.allocator); - for (0..1500) |i| try big.print(testing.allocator, "## {d}\n\n| a | b {d} |\n|---|--:|\n| one | two |\n| three | {d} |\n\n", .{ i, i, i * 7 }); - const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); - defer p.deinit(); - var frame = std.heap.ArenaAllocator.init(testing.allocator); - defer frame.deinit(); - const pane = try testPane(p, big.items, frame.allocator()); - const f = &pane.file.?; - // One frame did not read it all, and asks for the next. - try testing.expect(f.tables.busy()); - try testing.expect(p.nextWake() != null); - // The top's tables are found already: the view is right at once. - try testing.expect(File.rowTable(pane, 2) != null); - const reached = f.tables.pending.?.n; - // An edit past where it got to: it goes on; one before it: from the top. - var edits: usize = 0; - var frames: usize = 0; - while (f.tables.busy()) : (frames += 1) { - if (edits < 2) { - const at = if (edits == 0) f.content.len - 3 else 10; - const new = try std.mem.concat(p.gpa, u8, &.{ f.content[0..at], "x", f.content[at..] }); - File.setContent(p, f, new); - edits += 1; - } - p.update(.tick); - _ = frame.reset(.retain_capacity); - _ = try p.render(frame.allocator()); - try testing.expect(frames < 10_000); - } - try testing.expect(reached < File.nlines(p.gpa, f)); - var whole: Tables = .{}; - defer whole.deinit(testing.allocator); - try whole.scan(config.tab_width, testing.allocator, f.content); - try testing.expectEqual(whole.list.items.len, f.tables.list.items.len); - for (whole.list.items, f.tables.list.items) |w, mine| { - try testing.expectEqual(w.first, mine.first); - try testing.expectEqual(w.end, mine.end); - try testing.expectEqual(w.width, mine.width); - } -} - test "tables: a table wider than a wrapping pane is shown as its source, laid out again with Wrap off" { const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 34, .rows = 24 }); defer p.deinit(); @@ -1087,3 +976,48 @@ test "tables: a table wider than a wrapping pane is shown as its source, laid ou try testing.expect(!pane.body_wrapped); try testing.expect(File.rowTable(pane, 2) != null); } + +// How far a big text's tables have been read is derived (File.State.tables: +// in no digest and no dump), and a frame reads them too: here the session +// draws once before an edit and the replica joined from its dump draws +// nothing, then the same edit and a click on a table's row go to both. +test "tables: a click on a big text's table is the same state whichever frame this process drew last" { + const gpa = testing.allocator; + var big: std.ArrayList(u8) = .empty; + defer big.deinit(gpa); + for (0..5000) |_| try big.appendSlice(gpa, "x\n"); + const table = big.items.len / 2; + try big.appendSlice(gpa, "\n| a | b |\n|---|---|\n| xyz | 1 |\n| q | 12345 |\n"); + const a = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 16 }); + defer a.deinit(); + const pane_a = try a.setTestFile(big.items); + a.gpa.free(pane_a.file.?.path); + pane_a.file.?.path = try a.gpa.dupe(u8, "/notes.md"); + const last: i32 = @intCast(table + 4); + pane_a.file.?.scroll = table - 4; + pane_a.body.cur_row = @intCast(table - 1); + pane_a.body.cur_col = 0; + a.sync(); + const bytes = try pardes.dump.dumpBytes(a); + defer gpa.free(bytes); + const b = try pardes.dump.load(gpa, a.opts, bytes, null); + defer b.deinit(); + var frame = std.heap.ArenaAllocator.init(gpa); + defer frame.deinit(); + _ = try a.render(frame.allocator()); + for ([_]*Pardes{ a, b }) |p| p.update(.{ .paste = "y" }); + const r = a.view().rects[0]; + for (0..14) |col| { + var at: [2]i32 = undefined; + for ([_]*Pardes{ a, b }, &at) |p, *got| { + const pane = p.panes[0].?; + const x: u16 = @intCast(r.x + config.GUTTER + File.gutterWidth(pane) + col); + const y: u16 = @intCast(p.bodyTop(pane, r) + @as(u16, @intCast(last - @as(i32, @intCast(pane.file.?.scroll))))); + p.update(.{ .mouse = .{ .button = config.select_button, .kind = .press, .col = x, .row = y } }); + p.update(.{ .mouse = .{ .button = config.select_button, .kind = .release, .col = x, .row = y } }); + try testing.expectEqual(last, pane.body.cur_row); + got.* = pane.body.cur_col; + } + try testing.expectEqual(at[0], at[1]); + } +} |
