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/File.zig | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) (limited to 'src/File.zig') diff --git a/src/File.zig b/src/File.zig index 24dc9318..ddf91af3 100644 --- a/src/File.zig +++ b/src/File.zig @@ -17,6 +17,7 @@ const dump = @import("dump.zig"); const limits = @import("memory.zig").limits; const locations = @import("locations.zig"); const Colors = @import("colors.zig"); +const Tables = @import("Tables.zig"); const Pane = panes.Pane; const Output = panes.Output; const Mini = panes.mini; @@ -201,6 +202,9 @@ pub const State = struct { /// helix's object_selections: what Alt-o grew from, for Alt-i to go back /// to; gpa-owned ranges and the primary's index. node_history: std.ArrayList(NodeStep) = .empty, + /// A markdown text's pipe tables, laid out in their rows (Tables.zig): + /// found again on the first body drawn after an edit. + tables: Tables = .{}, history: *History, /// Zerox's group: the panes on one buffer share `content`, `history` /// and (while it is the one index) `line_starts`, each with its own @@ -577,20 +581,41 @@ test "indexed source rows match uncached scans across content changes" { } } +/// The pads `row` is laid out with in this pane: a row of a markdown +/// table (Tables.zig) of its text as it is now, when the table fits the +/// width a wrapping body has (a wider one is shown as its source, wrapped); +/// 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 width = wrapWidth(pane, pane.body_wrapped); + if (width > 0 and table.width > width) return &.{}; + return f.tables.padsOf(table, sourceLine(pane, row), out); +} + pub fn displayOffset(pane: *const Pane, row: i32, from_raw: i32, to_raw: i32) i32 { const line_text = sourceLine(pane, row); const from: usize = @intCast(@max(0, from_raw)); const to: usize = @intCast(@max(0, to_raw)); - return lineDisplayOffset(line_text, from, to); + var buf: [Tables.max_pads]Tables.Pad = undefined; + return lineDisplayOffset(line_text, from, to) + Tables.between(rowPads(pane, row, &buf), from, to); } pub fn displayEndOffset(pane: *const Pane, row: i32, from_raw: i32, at_raw: i32) i32 { const line_text = sourceLine(pane, row); - return lineDisplayEndOffset(line_text, @intCast(@max(0, from_raw)), @intCast(@max(0, at_raw))); + const from: usize = @intCast(@max(0, from_raw)); + const at: usize = @intCast(@max(0, at_raw)); + var buf: [Tables.max_pads]Tables.Pad = undefined; + return lineDisplayEndOffset(line_text, from, at) + Tables.between(rowPads(pane, row, &buf), from, at); } pub fn byteAtRowDisplay(pane: *const Pane, row: i32, from_raw: i32, display_col: i32) i32 { const line_text = sourceLine(pane, row); + var buf: [Tables.max_pads]Tables.Pad = undefined; + const pads = rowPads(pane, row, &buf); + if (pads.len > 0) return @intCast(Tables.byteAt(line_text, pads, @intCast(@max(0, from_raw)), @intCast(@max(0, display_col)))); return @intCast(byteAtDisplayFrom(line_text, @intCast(@max(0, from_raw)), @intCast(@max(0, display_col)))); } @@ -1130,6 +1155,7 @@ pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { if (file.node_tree) |tree| syntax.freeTree(tree); for (file.node_history.items) |step| p.gpa.free(step.ranges); file.node_history.deinit(p.gpa); + file.tables.deinit(p.gpa); if (ntwins > 0) return; file.history.destroy(p.gpa); } @@ -1768,6 +1794,7 @@ pub fn visualRow(line: []const u8, col: usize, width: usize) VisualRow { pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State, wrap: bool) ![]const u8 { const width = wrapWidth(pane, wrap); pane.body_wrapped = width > 0; + f.tables.ensure(pane.gpa, f); prepareContextRows(pane, f); pane.wrap_n = 0; @@ -1834,7 +1861,20 @@ fn fillBody(dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) written += prefix.len; if (dst) |out| @memcpy(out[written..][0..guide.len], guide); written += guide.len; - const shown = text[cut..end]; + // A table row's pads are blanks before their bytes: the row + // drawn as the column functions above measure it. + var pad_buf: [Tables.max_pads]Tables.Pad = undefined; + var from = cut; + for (rowPads(pane, @intCast(row), &pad_buf)) |table_pad| { + if (table_pad.at <= cut) continue; + if (table_pad.at > end) break; + if (dst) |out| @memcpy(out[written..][0 .. table_pad.at - from], text[from..table_pad.at]); + written += table_pad.at - from; + if (dst) |out| @memset(out[written..][0..table_pad.cells], ' '); + written += table_pad.cells; + from = table_pad.at; + } + const shown = text[from..end]; if (dst) |out| @memcpy(out[written..][0..shown.len], shown); written += shown.len; } @@ -2029,7 +2069,16 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. s.at(tx + @as(u16, @intCast(prefix_width + col)), body_y + vr).style.fg = .{ .rgb = p.bodyChrome().diagnostic_info }; } var screen_c: usize = pad; + var table_buf: [Tables.max_pads]Tables.Pad = undefined; + const table_pads = rowPads(pane, @intCast(row_index), &table_buf); + var table_at: usize = 0; while (hs + c < limit and prefix_width + screen_c < tw) { + while (table_at < table_pads.len and table_pads[table_at].at < hs + c) table_at += 1; + if (table_at < table_pads.len and table_pads[table_at].at == hs + c and hs + c > hs) { + screen_c += table_pads[table_at].cells; + table_at += 1; + if (prefix_width + screen_c >= tw) break; + } const grapheme_end = @min(limit, modal.nextGrapheme(line, hs + c)); const cells = graphemeDisplayWidth(line[hs + c .. grapheme_end]); const idx = base + hs + c; -- cgit v1.3