From ce6ba3d8a9e1f5c1e31892639049317f6a27b1bc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 13:59:21 -0300 Subject: Tables 1/3: a markdown file's pipe tables laid out in their rows, cells padded with virtual width before a byte (a tab's extra cells, not a parallel map): File.rowPads under displayOffset/displayEndOffset/byteAtRowDisplay, fillBody, recolorSyntax, the pointer sweep, j/k; tree-sitter-markdown as the detector's oracle --- src/Tables.zig | 641 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 641 insertions(+) create mode 100644 src/Tables.zig (limited to 'src/Tables.zig') diff --git a/src/Tables.zig b/src/Tables.zig new file mode 100644 index 00000000..ca4a4b57 --- /dev/null +++ b/src/Tables.zig @@ -0,0 +1,641 @@ +//! A markdown file's pipe tables (GFM), laid out in their own rows. Every +//! byte of a table stays in its row as the text it is: B1, B2, B3 and the +//! chords work on it as on any text, and nothing is drawn over it. What +//! changes is the width some bytes take. A cell narrower than its column +//! is given the cells it lacks as virtual width, a *pad*, before one of its +//! bytes (the `|` that ends it, or for a right-aligned column the first +//! byte after the `|` that starts it), so every row's `|` of a column falls +//! on the same screen column whatever the source spacing. A pad is to the +//! byte it stands before what a tab's extra cells are to the tab: drawn as +//! blanks, every one of them maps back to that byte (a click there puts +//! the caret before it), and the caret before that byte is drawn after the +//! pad, against its glyph. +//! +//! The pads are the only mapping. File.zig's pane-aware column functions +//! (`displayOffset`, `displayEndOffset`, `byteAtRowDisplay`) and the row +//! walks that draw a body (`fillBody`, `recolorSyntax`) add them, so the +//! caret, every selection, a click and a Look map between a cell's x and +//! its byte as they map a tab's. The look itself (`paint`: the `|` as rules, +//! the delimiter row as one, the header bold) restyles cells that are +//! there; it moves nothing. +//! +//! Which lines are tables is found by lines, as tree-sitter-markdown's +//! scanner finds a `pipe_table` (its PIPE_TABLE_START: a header row's cells +//! counted by its pipes, a delimiter row of `:?-+:?` cells, the same count), +//! once per revision of the text, over the whole text: the highlighter +//! parses only the lines around the view, which start mid-table as soon as +//! a long one is scrolled, and fenced code is known only from the top. The +//! grammar is the oracle in a test ("tables are the pipe_tables tree-sitter +//! finds"). +const std = @import("std"); +const panes = @import("panes.zig"); +const pardes = @import("pardes.zig"); +const colors = @import("colors.zig"); +const Surface = @import("surface.zig").Surface; +const Pardes = pardes.Pardes; +const Pane = panes.Pane; +const File = panes.File; + +const Tables = @This(); + +pub const Align = enum(u2) { left, right, center }; + +pub const Table = struct { + /// The header row's line; the delimiter row is the next. + first: u32, + /// One past the last row's line. + end: u32, + /// Where its columns' widths and alignments start in `widths`/`aligns`. + col0: u32, + /// Columns: the most cells any row ends with a `|`. + cols: u16, + /// The widest indentation before a leading `|`, in cells. + lead: u16, + /// The widest row laid out, in cells: a table wider than a wrapping + /// pane is shown as its source (`File.rowPads`). + width: u32, +}; + +/// `cells` of virtual width before the byte at `at` of its line. +pub const Pad = struct { at: u32, cells: u16 }; + +/// Columns past this many are not aligned (their pipes take no pad). +pub const max_cols = 64; +/// A centred column pads both sides of a cell, and a leading `|` its indent. +pub const max_pads = 2 * max_cols + 1; + +/// The text these tables are of: its revision, where it is and how long. +revision: ?u32 = null, +content: usize = 0, +len: usize = 0, +list: std.ArrayList(Table) = .empty, +widths: std.ArrayList(u16) = .empty, +aligns: std.ArrayList(Align) = .empty, + +pub fn deinit(t: *Tables, gpa: std.mem.Allocator) void { + t.list.deinit(gpa); + t.widths.deinit(gpa); + t.aligns.deinit(gpa); + t.* = .{}; +} + +/// Whether these tables are of `f`'s text as it is now. +pub fn current(t: *const Tables, f: *const File.State) bool { + return t.revision == f.revision and t.content == @intFromPtr(f.content.ptr) and t.len == f.content.len; +} + +/// The table `line` is a row of, if any. +pub fn find(t: *const Tables, line: usize) ?*const Table { + const items = t.list.items; + var lo: usize = 0; + var hi: usize = items.len; + while (lo < hi) { + const mid = (lo + hi) / 2; + if (items[mid].end <= line) lo = mid + 1 else hi = mid; + } + if (lo < items.len and items[lo].first <= line) return &items[lo]; + return null; +} + +/// Find the tables of `f`'s text again unless they are of it already. Only +/// a markdown file has any; on no memory there are none (the text is shown +/// as it is). +pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *const File.State) void { + if (t.current(f)) return; + t.revision = f.revision; + t.content = @intFromPtr(f.content.ptr); + t.len = f.content.len; + t.list.clearRetainingCapacity(); + if (f.output != null or f.mini != null or !isMarkdown(f.path)) return; + scan(t, gpa, f.content) catch { + t.list.clearRetainingCapacity(); + t.widths.clearRetainingCapacity(); + t.aligns.clearRetainingCapacity(); + }; +} + +pub fn isMarkdown(path: []const u8) bool { + return std.ascii.endsWithIgnoreCase(path, ".md") or std.ascii.endsWithIgnoreCase(path, ".markdown"); +} + +/// One line of the text: its bytes and its number. +const Line = struct { text: []const u8, start: usize, n: u32 }; + +fn lineAt(content: []const u8, start: usize, n: u32) ?Line { + if (start > content.len) return null; + const end = std.mem.indexOfScalarPos(u8, content, start, '\n') orelse content.len; + const text = content[start..end]; + return .{ .text = if (text.len > 0 and text[text.len - 1] == '\r') text[0 .. text.len - 1] else text, .start = start, .n = n }; +} + +fn nextLine(content: []const u8, l: Line) ?Line { + const nl = std.mem.indexOfScalarPos(u8, content, l.start, '\n') orelse return null; + return lineAt(content, nl + 1, l.n + 1); +} + +fn indentOf(text: []const u8) usize { + var i: usize = 0; + while (i < text.len and (text[i] == ' ' or text[i] == '\t')) i += 1; + return i; +} + +fn blank(text: []const u8) bool { + return indentOf(text) == text.len; +} + +/// A fence's opener (```` ``` ```` or `~~~`, three or more, up to three +/// spaces in): its character and length. +const Fence = struct { ch: u8, len: usize }; + +fn fenceOf(text: []const u8) ?Fence { + const in = indentOf(text); + if (in > 3 or in >= text.len) return null; + const ch = text[in]; + if (ch != '`' and ch != '~') return null; + var n: usize = 0; + while (in + n < text.len and text[in + n] == ch) n += 1; + if (n < 3) return null; + // A backtick fence's info string has no backtick (CommonMark 4.5). + if (ch == '`' and std.mem.indexOfScalar(u8, text[in + n ..], '`') != null) return null; + return .{ .ch = ch, .len = n }; +} + +fn closes(text: []const u8, open: Fence) bool { + const f = fenceOf(text) orelse return false; + return f.ch == open.ch and f.len >= open.len and blank(text[indentOf(text) + f.len ..]); +} + +/// A line that ends a table's rows by starting a block of its own (what +/// the grammar's paragraph_interrupt_symbols are): a heading, a fence, a +/// quote, a list item, a thematic break, HTML. +fn interrupts(text: []const u8) bool { + const in = indentOf(text); + if (in > 3 or in >= text.len) return false; + const rest = text[in..]; + if (fenceOf(text) != null) return true; + switch (rest[0]) { + '#', '>', '<' => return true, + '-', '*', '+' => { + if (rest.len == 1 or rest[1] == ' ' or rest[1] == '\t') return true; + // A thematic break: three or more of one of -*_ and spaces. + var n: usize = 0; + for (rest) |b| { + if (b == rest[0]) n += 1 else if (b != ' ' and b != '\t') return false; + } + return n >= 3; + }, + '0'...'9' => { + var i: usize = 0; + while (i < rest.len and i < 9 and std.ascii.isDigit(rest[i])) i += 1; + return i < rest.len and (rest[i] == '.' or rest[i] == ')') and (i + 1 == rest.len or rest[i + 1] == ' '); + }, + else => return false, + } +} + +/// A row's unescaped pipes, in order, and whether the row starts with one +/// (after its indentation) and ends with one (before trailing blanks). +const Pipes = struct { + at: [max_cols + 2]u32 = undefined, + n: usize = 0, + /// Pipes past `at`'s room: counted, not kept. + more: usize = 0, + leading: bool = false, + trailing: bool = false, + + fn of(text: []const u8) Pipes { + var p: Pipes = .{}; + const in = indentOf(text); + p.leading = in < text.len and text[in] == '|'; + var last_ink: usize = text.len; + var i: usize = 0; + while (i < text.len) : (i += 1) { + const b = text[i]; + if (b == '\\' and i + 1 < text.len and std.ascii.isPrint(text[i + 1]) and !std.ascii.isAlphanumeric(text[i + 1]) and text[i + 1] != ' ') { + last_ink = i + 1; + i += 1; + continue; + } + if (b == '|') { + if (p.n < p.at.len) { + p.at[p.n] = @intCast(i); + p.n += 1; + } else p.more += 1; + } + if (b != ' ' and b != '\t') last_ink = i; + } + p.trailing = p.n > 0 and last_ink == p.at[p.n - 1] and p.more == 0; + return p; + } + + /// The cells the grammar counts: the pipes after a leading one, plus + /// the last cell when no pipe ends the row. + fn cells(p: *const Pipes) usize { + const pipes = p.n + p.more - @intFromBool(p.leading); + return pipes + @intFromBool(!p.trailing); + } + + /// The pipe that ends cell `k` (the pipes after a leading one). + fn end(p: *const Pipes, k: usize) ?usize { + const i = k + @intFromBool(p.leading); + return if (i < p.n) p.at[i] else null; + } + + /// Where cell `k` starts: after the pipe before it, or after the + /// indentation when it is the first and no pipe leads. + fn start(p: *const Pipes, k: usize) usize { + if (k == 0 and !p.leading) return 0; + return p.at[k - @intFromBool(!p.leading)] + 1; + } +}; + +/// A delimiter row's cells (`:?-+:?` between pipes), or null if `text` is +/// none: as the scanner reads one after a header. +fn delimiterCells(text: []const u8, out: *[max_cols]Align) ?usize { + var i = indentOf(text); + if (i > 3) return null; + var cells: usize = 0; + var pipes: usize = 0; + if (i < text.len and text[i] == '|') { + i += 1; + pipes += 1; + } + while (true) { + while (i < text.len and (text[i] == ' ' or text[i] == '\t')) i += 1; + if (i >= text.len) break; + if (text[i] == '|') { + cells += 1; + pipes += 1; + i += 1; + continue; + } + var left = false; + if (text[i] == ':') { + left = true; + i += 1; + if (i >= text.len or text[i] != '-') return null; + } + var dashes: usize = 0; + while (i < text.len and text[i] == '-') : (i += 1) dashes += 1; + if (dashes == 0) return null; + var right = false; + if (i < text.len and text[i] == ':') { + right = true; + i += 1; + } + if (cells < max_cols) out[cells] = if (left and right) .center else if (right) .right else .left; + cells += 1; + while (i < text.len and (text[i] == ' ' or text[i] == '\t')) i += 1; + if (i >= text.len) break; + if (text[i] != '|') return null; + pipes += 1; + i += 1; + } + if (pipes == 0) return null; + return cells; +} + +/// The display width of a cell's bytes; ASCII is a cell a byte. +fn widthOf(text: []const u8) usize { + for (text) |b| if (b < 0x20 or b >= 0x7f) return File.displayWidth(text); + return text.len; +} + +/// Find the tables of `content`, in place of the ones there were. +pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void { + t.list.clearRetainingCapacity(); + t.widths.clearRetainingCapacity(); + t.aligns.clearRetainingCapacity(); + var fence: ?Fence = null; + var prev_blank = true; + var line = lineAt(content, 0, 0); + while (line) |l| { + const following = nextLine(content, l); + if (fence) |open| { + if (closes(l.text, open)) fence = null; + prev_blank = fence == null; + line = following; + continue; + } + if (fenceOf(l.text)) |open| { + fence = open; + prev_blank = false; + line = following; + continue; + } + const is_blank = blank(l.text); + var aligns: [max_cols]Align = undefined; + // A header starts a block: after a blank line, a heading or a fence + // (the grammar tries PIPE_TABLE_START where a paragraph could start). + if (!is_blank and prev_blank and indentOf(l.text) <= 3 and std.mem.indexOfScalar(u8, l.text, '|') != null) { + if (following) |delimiter| if (delimiterCells(delimiter.text, &aligns)) |count| { + const header = Pipes.of(l.text); + // A header's one pipe may not be its leading one alone, + // unless one ends it too (`| a |`, not `| a`). + const lone = header.n + header.more == @intFromBool(header.leading) and !(header.leading and header.trailing); + if (!lone and header.cells() == count) { + const last = try t.add(gpa, content, l, delimiter, aligns[0..@min(count, max_cols)]); + line = nextLine(content, last); + prev_blank = false; + continue; + } + }; + } + // A heading line ends a block too: a header may follow it. + prev_blank = is_blank or std.mem.startsWith(u8, l.text[@min(indentOf(l.text), l.text.len)..], "#"); + line = following; + } +} + +/// Take the table whose header is `header`: its rows, its columns' widths +/// and its width. Returns its last row's line. +fn add(t: *Tables, gpa: std.mem.Allocator, content: []const u8, header: Line, delimiter: Line, aligns: []const Align) !Line { + var last = delimiter; + while (nextLine(content, last)) |row| { + if (blank(row.text) or interrupts(row.text)) break; + last = row; + } + const col0: u32 = @intCast(t.widths.items.len); + var table: Table = .{ .first = header.n, .end = last.n + 1, .col0 = col0, .cols = 0, .lead = 0, .width = 0 }; + // Widths: each column's widest cell, the cell's bytes up to the pipe + // that ends it; a row's last cell with no pipe after it aligns nothing. + var row: ?Line = header; + while (row) |r| : (row = if (r.n == last.n) null else nextLine(content, r)) { + const pipes = Pipes.of(r.text); + if (pipes.leading) table.lead = @max(table.lead, @as(u16, @intCast(@min(widthOf(r.text[0..pipes.at[0]]), std.math.maxInt(u16))))); + var k: usize = 0; + while (k < max_cols) : (k += 1) { + const stop = pipes.end(k) orelse break; + const w: u16 = @intCast(@min(widthOf(r.text[pipes.start(k)..stop]), std.math.maxInt(u16))); + if (k >= table.cols) { + try t.widths.append(gpa, w); + try t.aligns.append(gpa, if (k < aligns.len) aligns[k] else .left); + table.cols += 1; + } else t.widths.items[col0 + k] = @max(t.widths.items[col0 + k], w); + } + } + // The width: each row laid out, its source width and its pads. + row = header; + var buf: [max_pads]Pad = undefined; + while (row) |r| : (row = if (r.n == last.n) null else nextLine(content, r)) { + var width = widthOf(r.text); + for (t.padsOf(&table, r.text, &buf)) |pad| width += pad.cells; + table.width = @max(table.width, @as(u32, @intCast(@min(width, std.math.maxInt(u32))))); + } + try t.list.append(gpa, table); + return last; +} + +/// The pads of one of `table`'s rows, by byte, in order. No pad is ever at +/// byte 0: a row is drawn from its first byte, so there is no cell before +/// it to give. +pub fn padsOf(t: *const Tables, table: *const Table, text: []const u8, out: *[max_pads]Pad) []Pad { + const pipes = Pipes.of(text); + var n: usize = 0; + if (pipes.leading and pipes.at[0] > 0) { + const w = widthOf(text[0..pipes.at[0]]); + if (w < table.lead) { + out[n] = .{ .at = pipes.at[0], .cells = @intCast(table.lead - w) }; + n += 1; + } + } + var k: usize = 0; + while (k < table.cols) : (k += 1) { + const stop = pipes.end(k) orelse break; + const from = pipes.start(k); + const w = widthOf(text[from..stop]); + const want = t.widths.items[table.col0 + k]; + if (w >= want) continue; + const missing: u16 = @intCast(want - w); + // The first cell with no leading pipe starts at byte 0: it pads at + // its end, whatever its alignment. + const how: Align = if (from == 0) .left else t.aligns.items[table.col0 + k]; + const before: u16 = switch (how) { + .left => 0, + .right => missing, + .center => missing / 2, + }; + // An empty cell's start is its pipe: one pad there. + if (before > 0 and from < stop) { + out[n] = .{ .at = @intCast(from), .cells = before }; + n += 1; + } + const after = if (from < stop) missing - before else missing; + if (after > 0) { + out[n] = .{ .at = @intCast(stop), .cells = after }; + n += 1; + } + } + return out[0..n]; +} + +/// The cells of virtual width between byte `from` (excluded: its pad is +/// before the row's first shown byte) and byte `to` (included: its pad is +/// before its glyph), negative when `to` is before `from`. +pub fn between(pads: []const Pad, from: usize, to: usize) i32 { + const lo = @min(from, to); + const hi = @max(from, to); + var sum: i32 = 0; + for (pads) |pad| { + if (pad.at > hi) break; + if (pad.at > lo) sum += pad.cells; + } + return if (to >= from) sum else -sum; +} + +/// The byte at display column `col` of a row shown from byte `from`, its +/// pads counted: a pad's cells are its byte's, as a tab's are the tab's. +/// Past the end, a virtual column a cell (File.rawAtDisplay). +pub fn byteAt(text: []const u8, pads: []const Pad, from_raw: usize, col: usize) usize { + if (from_raw >= text.len) return from_raw +| col; + var at = @import("modal.zig").graphemeStart(text, from_raw); + var k: usize = 0; + while (k < pads.len and pads[k].at <= at) k += 1; + var used: usize = 0; + while (at < text.len) { + if (k < pads.len and pads[k].at == at) { + used += pads[k].cells; + k += 1; + if (col < used) return at; + } + const end = @import("modal.zig").nextGrapheme(text, at); + used += File.graphemeDisplayWidth(text[at..end]); + if (col < used) return at; + at = end; + } + return text.len + (col - used); +} + +// ---- tests ---- + +const testing = std.testing; +const config = @import("config.zig"); +const Key = pardes.Key; + +const sample = + "# Fruit\n" ++ + "\n" ++ + "| name | qty | note |\n" ++ + "|:-----|----:|:----:|\n" ++ + "| apple | 3 | red |\n" ++ + "| kiwi | 12 | small and fuzzy |\n" ++ + "| fig || |\n" ++ + "\n" ++ + "after\n"; + +/// A pane on `content` as a markdown file, drawn once. +fn testPane(p: *Pardes, content: []const u8, frame: std.mem.Allocator) !*Pane { + const pane = try p.setTestFile(content); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, "/notes.md"); + _ = try p.render(frame); + return pane; +} + +test "tables: a pipe table's columns, alignments and pads, by line" { + var t: Tables = .{}; + defer t.deinit(testing.allocator); + try t.scan(testing.allocator, sample); + try testing.expectEqual(@as(usize, 1), t.list.items.len); + const table = t.list.items[0]; + try testing.expectEqual(@as(u32, 2), table.first); + try testing.expectEqual(@as(u32, 7), table.end); + try testing.expectEqualSlices(u16, &.{ 7, 5, 17 }, t.widths.items); + try testing.expectEqualSlices(Align, &.{ .left, .right, .center }, t.aligns.items); + // `| fig || |`: the first cell short by 2 (left: before its `|`), the + // empty second by 5 (one pad at its pipe), the third by 16 (centred). + var buf: [max_pads]Pad = undefined; + const fig = "| fig || |"; + try testing.expectEqualSlices(Pad, &.{ + .{ .at = 6, .cells = 2 }, .{ .at = 7, .cells = 5 }, .{ .at = 8, .cells = 8 }, .{ .at = 9, .cells = 8 }, + }, t.padsOf(&table, fig, &buf)); + // `| apple | 3 | red |`: right-aligned 3 pads before its first byte. + try testing.expectEqualSlices(Pad, &.{ + .{ .at = 9, .cells = 2 }, .{ .at = 13, .cells = 6 }, .{ .at = 18, .cells = 6 }, + }, t.padsOf(&table, "| apple | 3 | red |", &buf)); + // Every row laid out is one width: the table's. + try testing.expectEqual(@as(u32, 1 + 7 + 1 + 5 + 1 + 17 + 1), table.width); + // Not tables: a fence's rows, counts that differ, a delimiter alone. + for ([_][]const u8{ + "```\n| a | b |\n|---|---|\n```\n", + "| a | b |\n|---|\n", + "|---|---|\n", + "| a\n|---\n", + }) |text| { + try t.scan(testing.allocator, text); + try testing.expectEqual(@as(usize, 0), t.list.items.len); + } +} + +test "tables: every byte's cell maps back to it, and every pad cell to the byte after it" { + var t: Tables = .{}; + defer t.deinit(testing.allocator); + try t.scan(testing.allocator, sample); + const table = &t.list.items[0]; + var lines = std.mem.splitScalar(u8, sample, '\n'); + var n: usize = 0; + while (lines.next()) |text| : (n += 1) { + if (n < table.first or n >= table.end) continue; + var buf: [max_pads]Pad = undefined; + const pads = t.padsOf(table, text, &buf); + var col: usize = 0; + for (text, 0..) |_, b| { + // The pad before b: each of its cells is b's. + const start = File.displayWidth(text[0..b]) + @as(usize, @intCast(between(pads, 0, b))); + while (col < start) : (col += 1) try testing.expectEqual(b, byteAt(text, pads, 0, col)); + try testing.expectEqual(b, byteAt(text, pads, 0, col)); + col += 1; + } + // Past the end, a virtual column a cell. + try testing.expectEqual(text.len + 2, byteAt(text, pads, 0, col + 2)); + // From a scrolled start, the pads before it are not counted. + try testing.expectEqual(@as(usize, 2), byteAt(text, pads, 2, 0)); + } +} + +test "tables: a markdown table is drawn aligned, its bytes in order, the caret and a click on each byte's cell" { + 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, sample, frame.allocator()); + const r = p.rects[0]; + const text_x = r.x + config.GUTTER + File.gutterWidth(pane); + const body_y = p.bodyTop(pane, r); + // The k-th pipe of every row on one screen column. + var pipe_x: [4]?u16 = @splat(null); + for (2..7) |line| { + const text = File.sourceLine(pane, @intCast(line)); + var k: usize = 0; + for (text, 0..) |b, i| { + const x = text_x + @as(u16, @intCast(File.displayOffset(pane, @intCast(line), 0, @intCast(i)))); + const cell = p.surface.at(x, body_y + @as(u16, @intCast(line))); + if (b == '|') { + if (pipe_x[k]) |want| try testing.expectEqual(want, x) else pipe_x[k] = x; + try testing.expect(!std.mem.eql(u8, cell.grapheme(), " ")); + k += 1; + } else if (line != 3) try testing.expectEqualStrings(text[i .. i + 1], cell.grapheme()); + } + } + try testing.expectEqual(@as(u16, 1 + 7 + 1 + 5 + 1 + 17), pipe_x[3].? - text_x); + // B1 on any cell of `| fig || |`: the caret on the byte the cell is + // (a pad's cells are the byte after them), drawn where that byte is. + const fig: i32 = 6; + const text = File.sourceLine(pane, fig); + const y = body_y + 6; + var col: u16 = 0; + while (col < pipe_x[3].? - text_x + 1) : (col += 1) { + p.update(.{ .mouse = .{ .button = config.select_button, .kind = .press, .col = text_x + col, .row = y } }); + p.update(.{ .mouse = .{ .button = config.select_button, .kind = .release, .col = text_x + col, .row = y } }); + const want = File.byteAtRowDisplay(pane, fig, 0, col); + try testing.expectEqual(fig, pane.body.cur_row); + try testing.expectEqual(want, pane.body.cur_col); + try testing.expect(want < text.len); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + const caret = p.surface.cursor orelse return error.NoCaret; + try testing.expectEqual(text_x + @as(u16, @intCast(File.displayOffset(pane, fig, 0, want))), caret.x); + // The caret is on the cell clicked, or on the glyph after its pad. + try testing.expect(caret.x >= text_x + col); + } + // A pad cell: the empty second cell's five, all the `|` after them. + const empty_x = pipe_x[1].? - text_x + 1; + for (0..5) |i| try testing.expectEqual(@as(i32, 7), File.byteAtRowDisplay(pane, fig, 0, @intCast(empty_x + i))); +} + +test "tables: typing in a cell re-aligns the table live, and j/k keep the column shown" { + 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, sample, frame.allocator()); + // On `3` in the apple row: insert before it. + pane.body.cur_row = 4; + pane.body.cur_col = 10; + p.update(.{ .key = .{ .cp = 'i', .text = "i" } }); + for ("123456789") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + try testing.expectEqualStrings("| apple | 1234567893 | red |", File.sourceLine(pane, 4)); + // The qty column is now 12 wide in every row. + for (2..7) |line| { + const text = File.sourceLine(pane, @intCast(line)); + var k: usize = 0; + for (text, 0..) |b, i| { + if (b != '|') continue; + k += 1; + if (k == 3) try testing.expectEqual(@as(i32, 1 + 7 + 1 + 12), File.displayOffset(pane, @intCast(line), 0, @intCast(i))); + } + } + // k from the kiwi row's `and` keeps its screen column: the apple + // row's centred `red` under it. + p.update(.{ .key = .{ .cp = Key.escape } }); + pane.body.cur_row = 5; + pane.body.cur_col = @intCast(std.mem.indexOf(u8, File.sourceLine(pane, 5), "and").?); + pane.body.vsel.active = false; + pane.body.sticky_col = -1; + const shown = File.displayOffset(pane, 5, 0, pane.body.cur_col); + p.update(.{ .key = .{ .cp = 'k', .text = "k" } }); + try testing.expectEqual(@as(i32, 4), pane.body.cur_row); + try testing.expectEqual(shown, File.displayOffset(pane, 4, 0, pane.body.cur_col)); +} -- cgit v1.3 From 8f5871340b87a0d5399967ccc7eac0d12dc10821 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 14:05:34 -0300 Subject: Tables 2/3: the look, a table's pipes drawn as rules, its delimiter row one rule with its colons kept, its header bold on the tag's ground (glyphs and colours on the cells there, nothing moved); the GUI draws light box-drawing rules edge to edge so they join across rows --- src/File.zig | 16 +++++--- src/Tables.zig | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/body_layer.zig | 1 + src/gui/gui.zig | 72 +++++++++++++++++++++++++++++++- src/panes.zig | 1 + 5 files changed, 199 insertions(+), 9 deletions(-) (limited to 'src/Tables.zig') diff --git a/src/File.zig b/src/File.zig index ddf91af3..5db04443 100644 --- a/src/File.zig +++ b/src/File.zig @@ -587,12 +587,18 @@ test "indexed source rows match uncached scans across content changes" { /// else none. Every mapping between a file row's bytes and its cells goes /// through here, so the drawn row, the caret and a click cannot disagree. pub fn rowPads(pane: *const Pane, row: i32, out: *[Tables.max_pads]Tables.Pad) []const Tables.Pad { - const f = if (pane.file) |*file| file else return &.{}; - if (row < 0 or f.tables.list.items.len == 0 or !f.tables.current(f)) return &.{}; - const table = f.tables.find(@intCast(row)) orelse return &.{}; + const table = rowTable(pane, row) orelse return &.{}; + return pane.file.?.tables.padsOf(table, sourceLine(pane, row), out); +} + +/// The table `row` is laid out in, in this pane (`rowPads`). +pub fn rowTable(pane: *const Pane, row: i32) ?*const Tables.Table { + const f = if (pane.file) |*file| file else return null; + if (row < 0 or f.tables.list.items.len == 0 or !f.tables.current(f)) return null; + const table = f.tables.find(@intCast(row)) orelse return null; const width = wrapWidth(pane, pane.body_wrapped); - if (width > 0 and table.width > width) return &.{}; - return f.tables.padsOf(table, sourceLine(pane, row), out); + if (width > 0 and table.width > width) return null; + return table; } pub fn displayOffset(pane: *const Pane, row: i32, from_raw: i32, to_raw: i32) i32 { diff --git a/src/Tables.zig b/src/Tables.zig index ca4a4b57..8038dd65 100644 --- a/src/Tables.zig +++ b/src/Tables.zig @@ -31,7 +31,10 @@ const std = @import("std"); const panes = @import("panes.zig"); const pardes = @import("pardes.zig"); const colors = @import("colors.zig"); +const modal = @import("modal.zig"); const Surface = @import("surface.zig").Surface; +const Cell = @import("surface.zig").Cell; +const Color = @import("surface.zig").Color; const Pardes = pardes.Pardes; const Pane = panes.Pane; const File = panes.File; @@ -448,7 +451,7 @@ pub fn between(pads: []const Pad, from: usize, to: usize) i32 { /// Past the end, a virtual column a cell (File.rawAtDisplay). pub fn byteAt(text: []const u8, pads: []const Pad, from_raw: usize, col: usize) usize { if (from_raw >= text.len) return from_raw +| col; - var at = @import("modal.zig").graphemeStart(text, from_raw); + var at = modal.graphemeStart(text, from_raw); var k: usize = 0; while (k < pads.len and pads[k].at <= at) k += 1; var used: usize = 0; @@ -458,7 +461,7 @@ pub fn byteAt(text: []const u8, pads: []const Pad, from_raw: usize, col: usize) k += 1; if (col < used) return at; } - const end = @import("modal.zig").nextGrapheme(text, at); + const end = modal.nextGrapheme(text, at); used += File.graphemeDisplayWidth(text[at..end]); if (col < used) return at; at = end; @@ -466,6 +469,101 @@ pub fn byteAt(text: []const u8, pads: []const Pad, from_raw: usize, col: usize) return text.len + (col - used); } +/// What a table's `|` and its delimiter row are drawn as: light box-drawing +/// rules, each one cell, as the glyph of the byte that is there. +const rule_v = "\u{2502}"; +const rule_h = "\u{2500}"; +const rule_cross = "\u{253c}"; +const rule_left = "\u{251c}"; +const rule_right = "\u{2524}"; + +fn glyph(cell: *Cell, g: []const u8) void { + cell.default = false; + @memcpy(cell.text[0..g.len], g); + cell.len = @intCast(g.len); +} + +/// A pane's table rows drawn as tables: every unescaped `|` a rule, the +/// delimiter row one rule across (its dashes, its blanks and its pads, its +/// `:` kept in the rule's colour, since they say the alignment), the header +/// row bold on the tag's ground, as compact context rows are. Only glyphs +/// and colours change, on the cells the row has: where each byte is and how +/// wide is fillBody's, so nothing is drawn over anything, B1 on a rule is +/// B1 on its `|`, and a selection's tint lies over the rules as over text. +pub fn paint(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16) void { + const f = if (pane.file) |*file| file else return; + if (f.tables.list.items.len == 0) return; + const th = p.theme(); + const tint = p.settings.colors; + const rule_fg: Color = .{ .rgb = th.border orelse th.lineno }; + const head_bg: Color = .{ .rgb = th.tag_bg }; + const prefix: usize = File.gutterWidth(pane); + const body_y = p.bodyTop(pane, r); + var vr: u16 = 0; + while (vr < body_h) : (vr += 1) { + const w = pane.wrapAt(vr); + const table = File.rowTable(pane, w.line) orelse continue; + const text = File.sourceLine(pane, w.line); + var buf: [max_pads]Pad = undefined; + const pads = f.tables.padsOf(table, text, &buf); + const pipes = Pipes.of(text); + const delimiter = w.line == table.first + 1; + const header = w.line == table.first; + const y = body_y + vr; + // The row's walk, as fillBody lays it out from its first shown byte. + var at = modal.graphemeStart(text, @min(@as(usize, @intCast(@max(0, w.at))), text.len)); + var col: usize = prefix + @as(usize, @intCast(w.pad)); + var k: usize = 0; + while (k < pads.len and pads[k].at <= at) k += 1; + var q: usize = 0; + while (q < pipes.n and pipes.at[q] < at) q += 1; + while (at < text.len and col < tw) { + if (k < pads.len and pads[k].at == at) { + for (0..pads[k].cells) |i| { + if (col + i >= tw) break; + const cell = s.at(tx + @as(u16, @intCast(col + i)), y); + if (delimiter) { + glyph(cell, rule_h); + if (tint) cell.style.fg = rule_fg; + } + if (header and tint) cell.style.bg = head_bg; + } + col += pads[k].cells; + k += 1; + if (col >= tw) break; + } + const end = modal.nextGrapheme(text, at); + const cells = File.graphemeDisplayWidth(text[at..end]); + const cell = s.at(tx + @as(u16, @intCast(col)), y); + const pipe = q < pipes.n and pipes.at[q] == at; + if (pipe) { + q += 1; + glyph(cell, if (!delimiter) + rule_v + else if (q == 1 and pipes.leading) + rule_left + else if (q == pipes.n and pipes.trailing) + rule_right + else + rule_cross); + if (tint) cell.style.fg = rule_fg; + cell.style.bold = false; + } else if (delimiter) { + if (text[at] == '-' or text[at] == ' ') glyph(cell, rule_h); + if (tint) cell.style.fg = rule_fg; + } else if (header) { + cell.default = false; + cell.style.bold = true; + } + if (header and tint) for (0..cells) |i| { + if (col + i < tw) s.at(tx + @as(u16, @intCast(col + i)), y).style.bg = head_bg; + }; + col += cells; + at = end; + } + } +} + // ---- tests ---- const testing = std.testing; @@ -578,6 +676,22 @@ test "tables: a markdown table is drawn aligned, its bytes in order, the caret a } } try testing.expectEqual(@as(u16, 1 + 7 + 1 + 5 + 1 + 17), pipe_x[3].? - text_x); + // The look: each `|` a rule; the delimiter row one rule across, its + // `:` kept; the header bold. The bytes under them are the source's. + const rule_y = body_y + 3; + var x = text_x; + while (x <= pipe_x[3].?) : (x += 1) { + const g = p.surface.at(x, rule_y).grapheme(); + try testing.expect(std.mem.eql(u8, g, rule_h) or std.mem.eql(u8, g, rule_cross) or + std.mem.eql(u8, g, rule_left) or std.mem.eql(u8, g, rule_right) or std.mem.eql(u8, g, ":")); + } + try testing.expectEqualStrings(rule_left, p.surface.at(pipe_x[0].?, rule_y).grapheme()); + try testing.expectEqualStrings(rule_cross, p.surface.at(pipe_x[1].?, rule_y).grapheme()); + try testing.expectEqualStrings(rule_right, p.surface.at(pipe_x[3].?, rule_y).grapheme()); + try testing.expectEqualStrings(rule_v, p.surface.at(pipe_x[2].?, body_y + 4).grapheme()); + try testing.expect(p.surface.at(text_x + 2, body_y + 2).style.bold); + try testing.expect(!p.surface.at(text_x + 2, body_y + 4).style.bold); + try testing.expectEqualStrings("| name | qty | note |", File.sourceLine(pane, 2)); // B1 on any cell of `| fig || |`: the caret on the byte the cell is // (a pad's cells are the byte after them), drawn where that byte is. const fig: i32 = 6; diff --git a/src/body_layer.zig b/src/body_layer.zig index 90622e52..3684605f 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -282,6 +282,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane panes.File.drawGutter(p, s, pane, r, tx, tw, body_h, active); if (p.settings.colors and pane.colorAlgo() != .none) panes.File.recolorSyntax(p, s, pane, f, r, tx, tw, body_h); + panes.Tables.paint(p, s, pane, r, tx, tw, body_h); panes.File.drawWrapMarkers(p, s, pane, r, tx, tw, body_h, pane_bg); pane.places.paint(p, s, pane, r, body_h); panes.Query.paint(p, s, pane, r, body_h); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 49a98f5b..d71f7047 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -105,9 +105,52 @@ const GlyphKey = packed struct(u64) { centered: bool = false, lead: u4 = 0, span: u4 = 1, - _: u2 = 0, + /// A light box-drawing rule (`ruleArms`), drawn edge to edge rather + /// than from the font; `glyph` is its codepoint. + rule: bool = false, + _: u1 = 0, }; +/// The light box-drawing rules drawn by hand, as Ghostty and kitty draw +/// them: a logical pixel thick through the cell's middle, out to its edges, +/// so a markdown table's `|` (Tables.zig) joins the rows above and below it +/// and its delimiter row is one line across, at any line height a font's +/// own glyph would leave gaps in. The arms each one has: left, right, up, down. +fn ruleArms(cp: u32) ?[4]bool { + return switch (cp) { + 0x2500 => .{ true, true, false, false }, // ─ + 0x2502 => .{ false, false, true, true }, // │ + 0x250c => .{ false, true, false, true }, // ┌ + 0x2510 => .{ true, false, false, true }, // ┐ + 0x2514 => .{ false, true, true, false }, // └ + 0x2518 => .{ true, false, true, false }, // ┘ + 0x251c => .{ false, true, true, true }, // ├ + 0x2524 => .{ true, false, true, true }, // ┤ + 0x252c => .{ true, true, false, true }, // ┬ + 0x2534 => .{ true, true, true, false }, // ┴ + 0x253c => .{ true, true, true, true }, // ┼ + else => null, + }; +} + +fn drawRule(out: [*]u8, stride: usize, width: usize, height: usize, arms: [4]bool) void { + if (width == 0 or height == 0) return; + const t: usize = @min(@max(1, @as(usize, @intFromFloat(dp(1)))), @min(width, height)); + const mx = (width - t) / 2; + const my = (height - t) / 2; + for (0..height) |y| @memset(out[y * stride ..][0..width], 0); + if (arms[0] or arms[1]) for (0..t) |k| { + const from: usize = if (arms[0]) 0 else mx; + const to: usize = if (arms[1]) width else mx + t; + @memset(out[(my + k) * stride + from ..][0 .. to - from], 255); + }; + if (arms[2] or arms[3]) { + const from: usize = if (arms[2]) 0 else my; + const to: usize = if (arms[3]) height else my + t; + for (from..to) |y| @memset(out[y * stride + mx ..][0..t], 255); + } +} + const GlyphDecoration = packed struct(u5) { bold: bool = false, underline: @FieldType(pardes.CellStyle, "ul") = .off, @@ -7314,6 +7357,8 @@ fn wordCell(cell: *const pardes.Cell) WordCell { /// cell tints its own slice. fn wordBreak(a: WordCell, b: WordCell, i: usize, cursor: ?usize) bool { if (a.cp == ' ' or b.cp == ' ') return true; + // A rule is drawn by hand (ruleArms), never a slice of a shaped word. + if (ruleArms(a.cp) != null or ruleArms(b.cp) != null) return true; if (cursor) |at| if (at == i or at == i + 1) return true; if (a.role != b.role or !std.meta.eql(a.decoration, b.decoration)) return true; return (a.cp == 'f' and (b.cp == 'i' or b.cp == 'l')) or (a.cp == 's' and b.cp == 't'); @@ -7334,6 +7379,8 @@ fn cellSlot(g: *Gui, line: []const pardes.Cell, at: usize, cursor: ?usize, role: var here = wordCell(&line[at]); // An underlined or struck-out space is a glyph of its own: it draws its line. if (here.cp == ' ' and !here.decoration.drawsBlank()) return g.space_slot; + if (ruleArms(here.cp) != null) + return cachedGlyph(g, .{ .face = 0, .glyph = here.cp, .role = role, .rule = true }) orelse g.space_slot; const gpa = g.glyphs.allocator; const cell_key: Words.CellKey = .{ .cp = @intCast(here.cp), .role = role, .decoration = here.decoration }; const known = words.cells.getOrPut(gpa, cell_key) catch return g.space_slot; @@ -7589,7 +7636,9 @@ fn cachedGlyph(g: *Gui, key: GlyphKey) ?Slot { const raster_w = if (key.centered) slot_w else cell_w * key.span; const raster_h = if (key.role == .tagline) g.tagline_height else g.cell_h; const bold = @intFromBool(key.decoration.bold); - if (key.centered) { + if (key.rule) { + drawRule(out, atlas_w, raster_w, raster_h, ruleArms(key.glyph).?); + } else if (key.centered) { _ = c.ui_font_raster_centered(face, px, key.glyph, bold, out, @intCast(atlas_w), @intCast(raster_w), @intCast(raster_h)); } else { _ = c.ui_font_raster(face, px, key.glyph, bold, out, @intCast(atlas_w), @intCast(raster_w), @intCast(raster_h), @intCast(cell_w * key.lead), @intCast(cell_w), baseline); @@ -7764,6 +7813,25 @@ fn decorateLine(out: [*]u8, stride: usize, width: usize, height: usize, baseline } } +test "a box-drawing rule reaches its cell's edges, and only its arms do" { + const stride = 12; + const width = 9; + const height = 15; + var mask: [stride * height]u8 = @splat(0xa5); + drawRule(&mask, stride, width, height, ruleArms(0x253c).?); + // ┼: ink on all four edges, none in the corners, nothing past the slot. + try std.testing.expectEqual(@as(u8, 255), mask[0 * stride + width / 2]); + try std.testing.expectEqual(@as(u8, 255), mask[(height - 1) * stride + width / 2]); + try std.testing.expectEqual(@as(u8, 255), mask[(height / 2) * stride]); + try std.testing.expectEqual(@as(u8, 255), mask[(height / 2) * stride + width - 1]); + try std.testing.expectEqual(@as(u8, 0), mask[0]); + for (0..height) |y| for (mask[y * stride + width ..][0 .. stride - width]) |guard| try std.testing.expectEqual(@as(u8, 0xa5), guard); + drawRule(&mask, stride, width, height, ruleArms(0x2502).?); + try std.testing.expectEqual(@as(u8, 0), mask[(height / 2) * stride]); + try std.testing.expectEqual(@as(u8, 255), mask[0 * stride + width / 2]); + try std.testing.expect(ruleArms('|') == null); +} + test "glyph decorations render spaces and stay inside their atlas slot" { const stride = 9; const width = 5; diff --git a/src/panes.zig b/src/panes.zig index 10e0d427..20618ccc 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -662,6 +662,7 @@ pub const File = @import("File.zig"); pub const Output = @import("Output.zig"); pub const Places = @import("Places.zig"); +pub const Tables = @import("Tables.zig"); pub const Query = @import("Query.zig"); -- cgit v1.3 From 349bc6fa736edcebd789c5a39885d08f251713a1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 14:31:04 -0300 Subject: Tables 3/3: what a table costs, an edit's tables shifted by its splice and only its block read again (a fence line in it reads all), a big text read from the top 4 ms a frame (nextWake while busy); Tables.zig's tests registered; an empty delimiter cell's alignment --- src/Tables.zig | 419 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/pardes.zig | 3 + 2 files changed, 406 insertions(+), 16 deletions(-) (limited to 'src/Tables.zig') diff --git a/src/Tables.zig b/src/Tables.zig index 8038dd65..1dd296f7 100644 --- a/src/Tables.zig +++ b/src/Tables.zig @@ -67,18 +67,42 @@ pub const max_cols = 64; /// A centred column pads both sides of a cell, and a leading `|` its indent. pub const max_pads = 2 * max_cols + 1; -/// The text these tables are of: its revision, where it is and how long. +/// The text these tables are of: its revision, where it is, how long, and +/// how many lines. revision: ?u32 = null, content: usize = 0, len: usize = 0, +lines: usize = 0, list: std.ArrayList(Table) = .empty, widths: std.ArrayList(u16) = .empty, aligns: std.ArrayList(Align) = .empty, +/// The fenced code blocks, opener and closer lines (an unclosed one closes +/// past the end): no table is in one, and an edit to one of these lines +/// changes what every line after it is, so it finds them all again. +fences: std.ArrayList([2]u32) = .empty, +/// Columns in `widths` that no table points at any more (an edit's tables +/// are added at the end); past a bound, everything is found again. +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, +/// 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. +pub const frame_ns: u64 = 4 * std.time.ns_per_ms; pub fn deinit(t: *Tables, gpa: std.mem.Allocator) void { t.list.deinit(gpa); t.widths.deinit(gpa); t.aligns.deinit(gpa); + t.fences.deinit(gpa); t.* = .{}; } @@ -102,19 +126,182 @@ pub fn find(t: *const Tables, line: usize) ?*const Table { /// Find the tables of `f`'s text again unless they are of it already. Only /// a markdown file has any; on no memory there are none (the text is shown -/// as it is). -pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *const File.State) void { - if (t.current(f)) return; - t.revision = f.revision; - t.content = @intFromPtr(f.content.ptr); - t.len = f.content.len; +/// as it is). After one edit (File.State.splice) the tables it did not +/// touch shift and only the block around it is read again (`reflow`): a +/// keystroke costs its table, not the file. +pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void { + if (t.current(f)) { + if (t.pending != null) t.go(gpa, f.content, frame_ns); + return; + } + defer { + t.revision = f.revision; + t.content = @intFromPtr(f.content.ptr); + t.len = f.content.len; + } + 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; + }; + 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(gpa, f.content, frame_ns); + return; + } + } else if (t.reflow(gpa, f)) |_| return else |_| {} + } + t.begin(starts.len); + t.go(gpa, f.content, frame_ns); +} + +/// 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(); - if (f.output != null or f.mini != null or !isMarkdown(f.path)) return; - scan(t, gpa, f.content) catch { + 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: to the end). On no memory there are +/// no tables (the text is shown as it is). +fn go(t: *Tables, gpa: std.mem.Allocator, content: []const u8, budget_ns: u64) void { + var at = t.pending orelse return; + const done = t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, budget_ns) catch { t.list.clearRetainingCapacity(); - t.widths.clearRetainingCapacity(); - t.aligns.clearRetainingCapacity(); + t.pending = null; + return; }; + t.pending = if (done) null else at; +} + +/// Line `n` of a text whose line index is `starts`. +fn lineOf(content: []const u8, starts: []const usize, n: usize) Line { + const start = starts[n]; + const end = if (n + 1 < starts.len) starts[n + 1] - 1 else content.len; + const text = content[start..end]; + return .{ .text = if (text.len > 0 and text[text.len - 1] == '\r') text[0 .. text.len - 1] else text, .start = start, .n = @intCast(n) }; +} + +/// The line byte `at` is on. +fn lineHolding(starts: []const usize, at: usize) usize { + var lo: usize = 0; + var hi: usize = starts.len; + while (lo < hi) { + const mid = (lo + hi) / 2; + if (starts[mid] <= at) lo = mid + 1 else hi = mid; + } + return lo -| 1; +} + +/// The tables after one edit: those it did not touch kept (shifted by the +/// lines it added), the block around it read again. error.Whole when the +/// edit could change what a line far from it is (a fence line in it), or +/// the block is unbounded. +fn reflow(t: *Tables, gpa: std.mem.Allocator, f: *File.State) !void { + const content = f.content; + const starts = try File.lineIndex(gpa, f); + const sp = f.splice; + const delta: i64 = @as(i64, @intCast(starts.len)) - @as(i64, @intCast(t.lines)); + // The edited lines: [a, b] now, [a, b_old] before it. + const a = lineHolding(starts, sp.head); + const b = lineHolding(starts, content.len - sp.tail); + const b_old_i = @as(i64, @intCast(b)) - delta; + if (b_old_i < @as(i64, @intCast(a))) return error.Whole; + const b_old: usize = @intCast(b_old_i); + for (a..b + 1) |n| if (fenceOf(lineOf(content, starts, n).text) != null) return error.Whole; + var inside = false; + for (t.fences.items) |fence| { + if ((fence[0] >= a and fence[0] <= b_old) or (fence[1] >= a and fence[1] <= b_old)) return error.Whole; + if (fence[0] < a and fence[1] > b_old) inside = true; + } + const shift = struct { + fn by(n: u32, d: i64) u32 { + return @intCast(@as(i64, n) + d); + } + }.by; + for (t.fences.items) |*fence| { + if (fence[0] > b_old) fence[0] = shift(fence[0], delta); + if (fence[1] > b_old and fence[1] != std.math.maxInt(u32)) fence[1] = shift(fence[1], delta); + } + // The old tables the edit touched or is next to: [lo_i, hi_i). + const items = t.list.items; + var lo_i: usize = 0; + while (lo_i < items.len and items[lo_i].end < a) lo_i += 1; + var hi_i = lo_i; + while (hi_i < items.len and items[hi_i].first <= b_old + 1) hi_i += 1; + for (items[hi_i..]) |*table| { + table.first = shift(table.first, delta); + table.end = shift(table.end, delta); + } + // Inside fenced code, no table changes. + if (inside) { + t.lines = starts.len; + return; + } + // The block: from the blank line before it to the one after, the old + // tables it touched taken in, never into a neighbouring table. + var r0 = a; + var r1 = b; + if (lo_i < hi_i) { + // Old lines: those past the edit moved by what it added. + const head: usize = items[lo_i].first; + const last: usize = items[hi_i - 1].end - 1; + r0 = @min(r0, if (head > b_old) @as(usize, shift(@intCast(head), delta)) else head); + r1 = @max(r1, if (last > b_old) @as(usize, shift(@intCast(last), delta)) else last); + } + const floor: usize = if (lo_i > 0) items[lo_i - 1].end else 0; + const ceiling: usize = if (hi_i < items.len) items[hi_i].first else starts.len; + var steps: usize = 0; + while (r0 > floor and !blank(lineOf(content, starts, r0 - 1).text)) : (steps += 1) { + if (steps > 512 or fenceOf(lineOf(content, starts, r0 - 1).text) != null) break; + r0 -= 1; + } + steps = 0; + while (r1 + 1 < ceiling and !blank(lineOf(content, starts, r1 + 1).text)) : (steps += 1) { + if (steps > 512) return error.Whole; + if (fenceOf(lineOf(content, starts, r1 + 1).text) != null) break; + r1 += 1; + } + for (items[lo_i..hi_i]) |table| t.garbage += table.cols; + // Its tables, found as the whole text's are, then put in the old ones' place. + const before = t.list.items.len; + const prev = if (r0 == 0) "" else lineOf(content, starts, r0 - 1).text; + const prev_blank = r0 == 0 or blank(prev) or closesAny(prev) 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(gpa, content, &at, @intCast(r1), false, 0); + const fresh = try gpa.dupe(Table, t.list.items[before..]); + defer gpa.free(fresh); + t.list.shrinkRetainingCapacity(before); + try t.list.replaceRange(gpa, lo_i, hi_i - lo_i, fresh); + t.lines = starts.len; +} + +/// Whether a line closes some fence (any fence line does, to the block +/// after it: a header may follow it). +fn closesAny(text: []const u8) bool { + return fenceOf(text) != null; } pub fn isMarkdown(path: []const u8) bool { @@ -267,6 +454,8 @@ fn delimiterCells(text: []const u8, out: *[max_cols]Align) ?usize { while (i < text.len and (text[i] == ' ' or text[i] == '\t')) i += 1; if (i >= text.len) break; if (text[i] == '|') { + // An empty cell (`||`): the scanner counts it, aligned as none. + if (cells < max_cols) out[cells] = .left; cells += 1; pipes += 1; i += 1; @@ -309,19 +498,41 @@ pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void { t.list.clearRetainingCapacity(); t.widths.clearRetainingCapacity(); t.aligns.clearRetainingCapacity(); - var fence: ?Fence = null; - var prev_blank = true; - var line = lineAt(content, 0, 0); - while (line) |l| { + t.begin(std.mem.count(u8, content, "\n") + 1); + var at = t.pending.?; + _ = try t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, 0); + t.pending = null; +} + +/// 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, gpa: std.mem.Allocator, content: []const u8, at: *Resume, stop: u32, fences: bool, budget_ns: u64) !bool { + var fence = at.fence; + var prev_blank = at.prev_blank; + var line = lineAt(content, at.at, at.n); + const t0 = if (budget_ns > 0) panes.Places.nowNs() else 0; + var lines: usize = 0; + while (line) |l| : (lines += 1) { + if (l.n > stop) break; + if (budget_ns > 0 and lines % 256 == 255 and panes.Places.nowNs() -| 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)) fence = null; + if (closes(l.text, open)) { + fence = null; + if (fences) t.fences.items[t.fences.items.len - 1][1] = l.n; + } prev_blank = fence == null; line = following; continue; } if (fenceOf(l.text)) |open| { fence = open; + if (fences) try t.fences.append(gpa, .{ l.n, std.math.maxInt(u32) }); prev_blank = false; line = following; continue; @@ -348,6 +559,7 @@ pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void { 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 @@ -753,3 +965,178 @@ test "tables: typing in a cell re-aligns the table live, and j/k keep the column try testing.expectEqual(@as(i32, 4), pane.body.cur_row); try testing.expectEqual(shown, File.displayOffset(pane, 4, 0, pane.body.cur_col)); } + +test "tables: an edit's tables, shifted and found again around it, are the whole text's" { + 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, sample ++ "\n```\n| a | b |\n|---|---|\n```\n\n" ++ sample ++ "para\n| x | y |\n|---|--|\n| 1 | 2 |\n", frame.allocator()); + const f = &pane.file.?; + var whole: Tables = .{}; + defer whole.deinit(testing.allocator); + var prng: std.Random.DefaultPrng = .init(11); + const random = prng.random(); + const alphabet = "|||--: \n\nab`"; + var reflowed: usize = 0; + for (0..1500) |round| { + const old = f.content; + const at = random.uintAtMost(usize, old.len); + const cut = if (random.boolean()) @min(old.len - at, random.uintAtMost(usize, 3)) else 0; + const grow = random.uintAtMost(usize, 2); + const new = try p.gpa.alloc(u8, old.len - cut + grow); + @memcpy(new[0..at], old[0..at]); + for (new[at..][0..grow]) |*c| c.* = alphabet[random.uintLessThan(usize, alphabet.len)]; + @memcpy(new[at + grow ..], old[at + cut ..]); + const scans = f.tables.scans; + File.setContent(p, f, new); + f.tables.ensure(p.gpa, f); + if (f.tables.scans == scans) reflowed += 1; + try whole.scan(testing.allocator, f.content); + testing.expectEqual(whole.list.items.len, f.tables.list.items.len) catch |e| { + std.debug.print("round {d}\n{s}\n", .{ round, f.content }); + return e; + }; + 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.cols, mine.cols); + try testing.expectEqual(w.lead, mine.lead); + try testing.expectEqual(w.width, mine.width); + try testing.expectEqualSlices(u16, whole.widths.items[w.col0..][0..w.cols], f.tables.widths.items[mine.col0..][0..mine.cols]); + try testing.expectEqualSlices(Align, whole.aligns.items[w.col0..][0..w.cols], f.tables.aligns.items[mine.col0..][0..mine.cols]); + } + try testing.expectEqual(whole.lines, f.tables.lines); + } + // Most edits took the short way. + 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; + } + _ = 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(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 "zz tables timings on a big markdown file (TABLES_TIMES=)" { + const path_z = std.c.getenv("TABLES_TIMES") orelse return error.SkipZigTest; + const path = std.mem.span(path_z); + const io = testing.io; + const bytes = try std.Io.Dir.cwd().readFileAlloc(io, path, testing.allocator, .limited(16 << 20)); + defer testing.allocator.free(bytes); + const now = panes.Places.nowNs; + var report: std.ArrayList(u8) = .empty; + defer report.deinit(testing.allocator); + { + var t: Tables = .{}; + defer t.deinit(testing.allocator); + var worst: u64 = 0; + var sum: u64 = 0; + for (0..5) |_| { + const t0 = now(); + try t.scan(testing.allocator, bytes); + const d = now() - t0; + sum += d; + worst = @max(worst, d); + } + try report.print(testing.allocator, "scan of {d} bytes: {d} tables, mean {d} us, worst {d} us\n", .{ bytes.len, t.list.items.len, sum / 5 / 1000, worst / 1000 }); + } + // The same text as markdown and as plain text: what the tables cost a + // frame and an edit. + for ([_][]const u8{ "/stress.md", "/stress.txt" }) |name| { + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 50 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const pane = try p.setTestFile(bytes); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, name); + { + var t0 = now(); + _ = try p.render(frame.allocator()); + const first = now() - t0; + var frames: usize = 1; + var worst: u64 = first; + t0 = now(); + while (pane.file.?.tables.busy()) : (frames += 1) { + const t1 = now(); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + worst = @max(worst, now() - t1); + } + try report.print(testing.allocator, "{s}: the first frame {d} us; the tables read in {d} frames, the worst {d} us\n", .{ name, first / 1000, frames, worst / 1000 }); + } + // Into the middle of a table half way down. + var line: usize = File.nlines(p.gpa, &pane.file.?) / 2; + while (!std.mem.startsWith(u8, File.sourceLine(pane, @intCast(line)), "| ") or + std.mem.indexOf(u8, File.sourceLine(pane, @intCast(line)), "col") != null) line += 1; + pane.body.cur_row = @intCast(line + 2); + pane.body.cur_col = 2; + pane.ensureCursorVisible(); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + const Clock = struct { + fn step(pp: *Pardes, fr: *std.heap.ArenaAllocator, ev: pardes.Event) !u64 { + const t0 = panes.Places.nowNs(); + pp.update(ev); + _ = fr.reset(.retain_capacity); + _ = try pp.render(fr.allocator()); + return panes.Places.nowNs() - t0; + } + }; + var sum: u64 = 0; + var worst: u64 = 0; + for (0..10) |_| { + const d = try Clock.step(p, &frame, .{ .key = .{ .cp = 'j', .text = "j" } }); + sum += d; + worst = @max(worst, d); + } + try report.print(testing.allocator, "{s}: j and its frame: mean {d} us, worst {d} us\n", .{ name, sum / 10 / 1000, worst / 1000 }); + _ = try Clock.step(p, &frame, .{ .key = .{ .cp = 'i', .text = "i" } }); + sum = 0; + worst = 0; + for (0..10) |_| { + const d = try Clock.step(p, &frame, .{ .key = .{ .cp = 'x', .text = "x" } }); + sum += d; + worst = @max(worst, d); + } + try report.print(testing.allocator, "{s}: a letter typed in a cell and its frame: mean {d} us, worst {d} us\n", .{ name, sum / 10 / 1000, worst / 1000 }); + } + std.debug.print("\n{s}", .{report.items}); +} diff --git a/src/pardes.zig b/src/pardes.zig index 7f79f094..79a2e5a5 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -494,6 +494,7 @@ test { _ = @import("Names.zig"); _ = @import("Stream.zig"); _ = @import("Finder.zig"); + _ = @import("Tables.zig"); } pub const Builtin = builtins.registry.Builtin(); @@ -7655,6 +7656,8 @@ pub const Pardes = struct { if (p.chrome_animation.isActive() or p.presentation.animating()) return next; // Places still to find resume on the next frame. for (p.panes) |slot| if (slot) |pane| if (pane.places.busy(pane)) return next; + // A markdown text's tables still being read from the top (Tables.zig). + for (p.panes) |slot| if (slot) |pane| if (pane.file) |f| if (f.tables.busy()) return next; if (panes.Query.busy(p)) return next; // A typed Grep waiting out its pause. const streaming = p.stream.wake(next); -- cgit v1.3 From f4e295a0b61ae34e8abfd339eb1b6f658f8bf329 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 14:39:23 -0300 Subject: Tables 4/4: a delimiter cell's colons kept at its edges; a wide table shown as source in a wrapping pane, tested; docs/next-tables.md, the design as built, the numbers and the open questions --- docs/next-tables.md | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/File.zig | 2 +- src/Tables.zig | 41 ++++++++++-- 3 files changed, 212 insertions(+), 7 deletions(-) create mode 100644 docs/next-tables.md (limited to 'src/Tables.zig') diff --git a/docs/next-tables.md b/docs/next-tables.md new file mode 100644 index 00000000..cc9e567c --- /dev/null +++ b/docs/next-tables.md @@ -0,0 +1,176 @@ +# Tables in markdown files, as built + +> Could we do special rendering for tables in markdown files? + +A GFM pipe table in a `.md` file is drawn as a table, in its own rows: every +column's `|` on one screen column whatever the source spacing, the `|` drawn +as rules, the delimiter row (`|:--|--:|`) drawn as one rule with its colons +kept, the header bold on the tag's ground. Every byte stays in its row as the +text it is: B1 selects it, B2/B3 take its words, the chords cut and paste it, +the caret sits on it, and nothing is drawn over anything. Editing a cell +re-aligns the table on the keystroke. + +## The one idea: a pad is a tab's extra cells + +The rows are not rewritten. A cell narrower than its column is given the +cells it lacks as **virtual width before one of its bytes** (a *pad*): before +the `|` that ends it (left), before its first byte (right), split (centred). +A pad is to its byte what a tab's extra cells are to the tab: drawn blank, +each of its cells maps back to that byte, and the caret before that byte is +drawn after the pad, against its glyph. So there is no parallel map: the +existing pane-aware column functions in `File.zig` add the pads, and so does +every walk that draws a file row. + +| where | what changed | +|---|---| +| `File.rowPads` / `rowTable` | the gate: a row of a table of the text as it is now, that fits the pane | +| `File.displayOffset`, `displayEndOffset`, `byteAtRowDisplay` | `+ Tables.between(pads)` / `Tables.byteAt` | +| `File.fillBody` | writes the pads as blanks into the body string (so `paneText`, `expandedSel`, the sweep text see the drawn row) | +| `File.recolorSyntax` | its screen walk steps over the pads | +| `body_layer` | pointer-selection tint and the view-edge checks through the pane-aware functions | +| `edit.capturePointerSelection` | a sweep's columns to bytes through `byteAtRowDisplay` | +| `Text.verticalMove` / `visualMove` | j/k keep the column the table shows (a body of a file pane only) | + +Everything else (mouse.zig, exec.zig's B2 cells, look.zig's cursor word, the +caret, multi-cursor tints, `gw` labels) already went through those three +functions and needed no change. That is the evidence the mapping is in one +place: the round-trip test clicks every cell of a row with pads of all three +kinds and checks the caret lands on the byte and is drawn where it is. + +## The look (`Tables.paint`) + +Glyphs and colours only, on the cells the row has, after the syntax colours: +`|` as `│`, the delimiter row as `├──┼──┤` (its `-`, blanks and pads as `─`, +its `:` in the rule colour), the header bold on `tag_bg` (the compact context +rows' ground: tables reuse a look the editor has). The rule colour is the +theme's `border`, else `lineno`. + +## Finding the tables + +tree-sitter-markdown here does expose `pipe_table` (header, delimiter row, +cells, alignments). I did not use the tree to find them: the highlighter +parses only the lines around the view (`refreshHighlights`, a fragment from +`scroll - slack`), which starts mid-table as soon as a long table is +scrolled, and a fence is only known from the top. So `Tables.zig` finds +tables by lines with the grammar's own rules (its scanner's +`PIPE_TABLE_START`: a header's cells counted by pipes, a `:?-+:?` delimiter +row of the same count with at least one pipe; rows until a blank line or a +block that interrupts a paragraph; escaped `\|`), and the grammar is the +**oracle in a test** (`tables are the pipe_tables tree-sitter finds`: five +tables including an indented one, a no-pipe continuation row and an escaped +pipe; the fenced one and the short-delimiter one not). + +## Cost (measured) + +`.scratch/stress-tables.md`: 2.7 MB, 54 508 lines, 2000 tables of 3-6 columns +and 10-30 rows, fences every 50 tables. Debug build, 160x50, each step +`update` + a full render, the same text as `.md` and as `.txt` +(`zz tables timings`, `TABLES_TIMES=`). + +| what | .md | .txt | +|---|---|---| +| a whole read of the tables (once, blocking, for comparison) | 127 ms | - | +| the first frame | 27 ms | 18 ms | +| frames until every table is read (4 ms a frame from the top) | 24 | - | +| `j` and its frame, in a table | 1.0 ms (worst 1.5) | 0.6 ms (0.9) | +| a letter typed in a cell and its frame | 3.2 ms (worst 8.5) | 2.0 ms (6.8) | + +The first version re-read the whole text on every edit: 119 ms a keystroke. +Two changes the numbers forced, both Places' patterns: + +- **An edit reflows**: the tables it did not touch shift by the lines it + added (`File.State.splice`), and only its block (blank line to blank line, + the touched tables taken in) is read again. A fence line in the edit, or + in what it replaced, reads everything (a fence changes every line after + it). A random 1500-edit test checks every result against a whole read; + more than 1000 took the short way. +- **Opening reads from the top a frame's budget at a time** (`Tables.frame_ns`, + `Pardes.nextWake` while `busy`): rows below the read point are drawn as + source until it gets there (a quarter second on this file). An edit past + the read point changes nothing read; one before it starts the read again. + +## GUI and tty + +Both draw the same grid: alignment, rules, header, the mapping are core. +What only the GUI does: the light box-drawing rules (`─│┌┐└┘├┤┬┴┼`) are drawn +by hand edge to edge (`gui.zig` `ruleArms`/`drawRule`, a `rule` bit in +`GlyphKey`, never a slice of a shaped word), so a table's `│` joins the rows +above and below at any line height and the delimiter row is one unbroken +line. In a terminal they are the terminal font's glyphs, which may leave gaps +between rows (the terminal's business). This applies to every box-drawing +cell in the GUI, terminal panes' TUIs too (as Ghostty and kitty do). + +Things only a GUI could do next, not done: pixel-thin rules between cells +rather than a cell-wide glyph (needs a cell decoration the surface does not +have); a half-cell inset so a rule does not take a whole column. + +## Rough + +- A table wider than a wrapping pane is shown as its source, wrapped (a + padded row must not wrap: wrapping is byte-fitted). `Wrap off` lays it out + again with hscroll. +- Inline markdown is not coloured inside cells: `syntax.inject` injects + `markdown_inline` into `inline` nodes, and `pipe_table_cell` is not one. +- A table in a blockquote or list item is not found (the line detector has + no container prefixes); tree-sitter finds it. +- Mixed leading pipes (`| a |` over `a |`) misalign by one cell; a leading + pipe's indentation is padded only when the pad is not at byte 0. +- Columns past 64 are not aligned. Zerox twins do not share the splice, so a + twin re-reads the whole text after an edit (sliced, so it still does not + hold a frame). +- The sweep-text fallback (`selectionText` without captured rows, a sweep + begun on the tag) reads the drawn row, so it would carry pad blanks; the + same is true of the callstack guide today. + +## Open questions, and my position on each + +1. **Hide redundant source spaces?** Position: no. Every byte stays visible; + only width is added, never taken. `| foo|` shows its two spaces. Hiding + bytes would make a click land on something you cannot see, which is the + constraint's spirit even if a space is "text". +2. **The glyph substitution: is drawing `|` as `│` drawing over text?** + Position: no; it is the byte's glyph, like a font ligature (which the GUI + already does), with the byte selectable, searchable, typed over. The + alternative, keeping `|` and only padding, is one constant away + (`rule_v`); I find the plain `|` reads as source, not as a table. +3. **Delimiter colons**: kept, in the rule colour, at the cell edges. The + alignment is visible anyway, but you edit them, so they must show. +4. **Rule colour**: `border` is faint on doom_one, especially the 1 px + horizontal rule. `lineno` or a mix toward `fg` would read better; a theme + field (`table_rule`) is the honest answer if you want it tunable. +5. **Wide tables**: source wrapped (now) versus clipped laid-out rows with + hscroll per table. Position: source; a clipped row hides text, and the + wrap is honest. A column could also be squeezed by wrapping a cell inside + it, but that is a virtual row, and virtual rows break the row = line + invariant everything here rests on. + +## Pushback + +- The NEXT brief says "check whether the grammar exposes pipe_table nodes and + use them if so". It does, and using them for detection would be worse here + (a view-local fragment parse starts mid-table; a whole-file parse is + ~seconds in Debug on the stress file, by converge's 4.8 s for 1.2 MB of + Zig). Using the grammar as the test oracle keeps its rules without its + cost. If a whole-file markdown tree ever exists (a worker parse, as + converge does for declarations), the detector could read it instead. +- Re-aligning live means a column widens under the caret as you type in its + widest cell, so the rest of the row moves right per keystroke. That is the + point of the feature, but it is motion in text you are not editing; a + `TableAlign off` setting may be wanted for heavy table editing. +- Making box-drawing rules procedural is a GUI-wide change (terminal TUIs + change look too). I think it is right (Ghostty, kitty, WezTerm all do it), + but it is a decision beyond tables. + +## Tests + +`zig build unit-test -Dplatform=gui -Dtest-filter="tables"`: +`tables: a pipe table's columns, alignments and pads, by line`; `...every +byte's cell maps back to it, and every pad cell to the byte after it`; `...is +drawn aligned, its bytes in order, the caret and a click on each byte's cell` +(also the look); `...typing in a cell re-aligns the table live, and j/k keep +the column shown`; `...an edit's tables, shifted and found again around it, +are the whole text's` (1500 random edits); `...a big text's tables are read +a frame's budget at a time`; `...wider than a wrapping pane is shown as its +source`; `tables are the pipe_tables tree-sitter finds` (syntax.zig); +`a box-drawing rule reaches its cell's edges` (gui.zig); `zz tables timings` +(skipped without `TABLES_TIMES`). No layout change, so no rects invariant. diff --git a/src/File.zig b/src/File.zig index 5db04443..cf44c69a 100644 --- a/src/File.zig +++ b/src/File.zig @@ -588,7 +588,7 @@ test "indexed source rows match uncached scans across content changes" { /// through here, so the drawn row, the caret and a click cannot disagree. pub fn rowPads(pane: *const Pane, row: i32, out: *[Tables.max_pads]Tables.Pad) []const Tables.Pad { const table = rowTable(pane, row) orelse return &.{}; - return pane.file.?.tables.padsOf(table, sourceLine(pane, row), out); + return pane.file.?.tables.padsOf(table, @intCast(row), sourceLine(pane, row), out); } /// The table `row` is laid out in, in this pane (`rowPads`). diff --git a/src/Tables.zig b/src/Tables.zig index 1dd296f7..2e65a716 100644 --- a/src/Tables.zig +++ b/src/Tables.zig @@ -594,7 +594,7 @@ fn add(t: *Tables, gpa: std.mem.Allocator, content: []const u8, header: Line, de var buf: [max_pads]Pad = undefined; while (row) |r| : (row = if (r.n == last.n) null else nextLine(content, r)) { var width = widthOf(r.text); - for (t.padsOf(&table, r.text, &buf)) |pad| width += pad.cells; + for (t.padsOf(&table, r.n, r.text, &buf)) |pad| width += pad.cells; table.width = @max(table.width, @as(u32, @intCast(@min(width, std.math.maxInt(u32))))); } try t.list.append(gpa, table); @@ -604,7 +604,7 @@ fn add(t: *Tables, gpa: std.mem.Allocator, content: []const u8, header: Line, de /// The pads of one of `table`'s rows, by byte, in order. No pad is ever at /// byte 0: a row is drawn from its first byte, so there is no cell before /// it to give. -pub fn padsOf(t: *const Tables, table: *const Table, text: []const u8, out: *[max_pads]Pad) []Pad { +pub fn padsOf(t: *const Tables, table: *const Table, line: usize, text: []const u8, out: *[max_pads]Pad) []Pad { const pipes = Pipes.of(text); var n: usize = 0; if (pipes.leading and pipes.at[0] > 0) { @@ -625,6 +625,17 @@ pub fn padsOf(t: *const Tables, table: *const Table, text: []const u8, out: *[ma // The first cell with no leading pipe starts at byte 0: it pads at // its end, whatever its alignment. const how: Align = if (from == 0) .left else t.aligns.items[table.col0 + k]; + // The delimiter row's cells keep their colons at their edges: the + // rule is drawn long between them (a pad before a closing `:`). + if (line == table.first + 1) { + var last = stop; + while (last > from and (text[last - 1] == ' ' or text[last - 1] == '\t')) last -= 1; + const at = if (last > from and text[last - 1] == ':') last - 1 else stop; + if (at == 0) continue; + out[n] = .{ .at = @intCast(at), .cells = missing }; + n += 1; + continue; + } const before: u16 = switch (how) { .left => 0, .right => missing, @@ -717,7 +728,7 @@ pub fn paint(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: const table = File.rowTable(pane, w.line) orelse continue; const text = File.sourceLine(pane, w.line); var buf: [max_pads]Pad = undefined; - const pads = f.tables.padsOf(table, text, &buf); + const pads = f.tables.padsOf(table, @intCast(w.line), text, &buf); const pipes = Pipes.of(text); const delimiter = w.line == table.first + 1; const header = w.line == table.first; @@ -818,11 +829,11 @@ test "tables: a pipe table's columns, alignments and pads, by line" { const fig = "| fig || |"; try testing.expectEqualSlices(Pad, &.{ .{ .at = 6, .cells = 2 }, .{ .at = 7, .cells = 5 }, .{ .at = 8, .cells = 8 }, .{ .at = 9, .cells = 8 }, - }, t.padsOf(&table, fig, &buf)); + }, t.padsOf(&table, 6, fig, &buf)); // `| apple | 3 | red |`: right-aligned 3 pads before its first byte. try testing.expectEqualSlices(Pad, &.{ .{ .at = 9, .cells = 2 }, .{ .at = 13, .cells = 6 }, .{ .at = 18, .cells = 6 }, - }, t.padsOf(&table, "| apple | 3 | red |", &buf)); + }, t.padsOf(&table, 4, "| apple | 3 | red |", &buf)); // Every row laid out is one width: the table's. try testing.expectEqual(@as(u32, 1 + 7 + 1 + 5 + 1 + 17 + 1), table.width); // Not tables: a fence's rows, counts that differ, a delimiter alone. @@ -847,7 +858,7 @@ test "tables: every byte's cell maps back to it, and every pad cell to the byte while (lines.next()) |text| : (n += 1) { if (n < table.first or n >= table.end) continue; var buf: [max_pads]Pad = undefined; - const pads = t.padsOf(table, text, &buf); + const pads = t.padsOf(table, n, text, &buf); var col: usize = 0; for (text, 0..) |_, b| { // The pad before b: each of its cells is b's. @@ -1054,6 +1065,24 @@ test "tables: a big text's tables are read a frame's budget at a time from the t } } +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(); + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const pane = try testPane(p, sample, frame.allocator()); + try testing.expect(pane.body_wrapped); + try testing.expect(File.wrapWidth(pane, true) < 34); + try testing.expect(File.rowTable(pane, 2) == null); + // Source: a pipe is a pipe, at its source column. + try testing.expectEqual(@as(i32, 7), File.displayOffset(pane, 5, 0, 7)); + try testing.expect(p.executeBuiltinLine(0, "Wrap off")); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + try testing.expect(!pane.body_wrapped); + try testing.expect(File.rowTable(pane, 2) != null); +} + test "zz tables timings on a big markdown file (TABLES_TIMES=)" { const path_z = std.c.getenv("TABLES_TIMES") orelse return error.SkipZigTest; const path = std.mem.span(path_z); -- cgit v1.3