summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-24 13:59:21 -0300
committerGabriel Schneider <[email protected]>2026-10-24 15:56:58 -0300
commitce6ba3d8a9e1f5c1e31892639049317f6a27b1bc (patch)
tree939446ce2794e49ba950fa29acfa69292485288f
parent05d03191a8bfdb7c76176a91fddd97f389ab6dc3 (diff)
downloadpardes-ce6ba3d8a9e1f5c1e31892639049317f6a27b1bc.tar.gz
pardes-ce6ba3d8a9e1f5c1e31892639049317f6a27b1bc.zip
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
-rw-r--r--src/File.zig55
-rw-r--r--src/Tables.zig641
-rw-r--r--src/Text.zig26
-rw-r--r--src/body_layer.zig22
-rw-r--r--src/edit.zig11
-rw-r--r--src/syntax.zig69
6 files changed, 813 insertions, 11 deletions
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;
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));
+}
diff --git a/src/Text.zig b/src/Text.zig
index 88630f83..b57c953a 100644
--- a/src/Text.zig
+++ b/src/Text.zig
@@ -514,17 +514,33 @@ pub fn findMove(t: *Text, text: []const u8, range: modal.Selection, ch: u21, fwd
setRange(t, text, 0, res, false);
}
+/// The file pane whose text `text` is, when `t` is its body: its rows are
+/// measured through their table pads (File.rowPads), so j and k keep the
+/// column a table shows, not the one its source has.
+fn filePane(t: *Text, text: []const u8) ?*const Pane {
+ if (t.what != .body) return null;
+ const fp = t.pane() orelse return null;
+ const f = if (fp.file) |*file| file else return null;
+ return if (f.content.ptr == text.ptr and f.content.len == text.len) fp else null;
+}
+
/// j/k and friends: sticky goal column, clamped onto short lines' newline
pub fn verticalMove(t: *Text, text: []const u8, range: modal.Selection, down: bool, cnt: usize) void {
const cur = modal.selectionCursor(text, range);
const pos = File.textPosition(t, text, cur);
+ const shown = filePane(t, text);
const goal: usize = if (t.sticky_col >= 0)
@intCast(t.sticky_col)
+ else if (shown) |fp|
+ @intCast(@max(0, File.displayOffset(fp, @intCast(pos.row), 0, @intCast(pos.col))))
else
File.rawDisplayCol(File.textLine(t, text, pos.row), pos.col);
const last_row = File.textLineCount(t, text) - 1;
const nline = if (down) @min(pos.row + @max(1, cnt), last_row) else pos.row -| @max(1, cnt);
- const target_col = File.rawAtDisplay(File.textLine(t, text, nline), goal);
+ const target_col: usize = if (shown) |fp|
+ @intCast(File.byteAtRowDisplay(fp, @intCast(nline), 0, @intCast(@min(goal, std.math.maxInt(i32)))))
+ else
+ File.rawAtDisplay(File.textLine(t, text, nline), goal);
const target = File.textOffset(t, text, .{ .row = nline, .col = target_col });
// extend mode never walks onto the empty trailing line (helix)
if (t.select and target == text.len and text.len > 0 and text[text.len - 1] == '\n') return;
@@ -546,8 +562,11 @@ pub fn visualMove(
var row = pos.row;
var line = modal.lineSlice(text, row);
var vrow = File.visualRow(line, pos.col, width);
+ const shown = filePane(t, text);
const goal: usize = if (t.sticky_col >= 0)
@intCast(t.sticky_col)
+ else if (shown) |fp|
+ @intCast(@max(0, File.displayOffset(fp, @intCast(row), @intCast(vrow.start), @intCast(pos.col))))
else
File.rawDisplayCol(line[vrow.start..vrow.end], pos.col -| vrow.start);
var steps = @max(1, cnt);
@@ -574,7 +593,10 @@ pub fn visualMove(
}
// The newline slot is a real cursor position, but the first byte of the
// NEXT visual row is not: landing there would read as two rows moved.
- var target_col = vrow.start + File.rawAtDisplay(line[vrow.start..vrow.end], goal);
+ var target_col: usize = if (shown) |fp|
+ @intCast(File.byteAtRowDisplay(fp, @intCast(row), @intCast(vrow.start), @intCast(@min(goal, std.math.maxInt(i32)))))
+ else
+ vrow.start + File.rawAtDisplay(line[vrow.start..vrow.end], goal);
if (vrow.end < line.len and target_col >= vrow.end)
target_col = modal.graphemeStart(line, vrow.end - 1);
const target = File.textOffset(t, text, .{ .row = row, .col = target_col });
diff --git a/src/body_layer.zig b/src/body_layer.zig
index e50b7e3e..90622e52 100644
--- a/src/body_layer.zig
+++ b/src/body_layer.zig
@@ -73,7 +73,9 @@ fn pastView(pane: *Pane, body: []const u8, line: i32, row: i32, at: i32, col: i3
const from: usize = @intCast(@max(0, at));
const to: usize = @intCast(@max(0, col));
if (from >= text.len) return to >= from +| tw;
- return to >= from + panes.File.byteAtDisplay(text[from..], tw) and to < text.len;
+ // A file row's last shown byte counts its table pads (File.rowPads).
+ const shown: usize = if (pane.file != null) @intCast(panes.File.byteAtRowDisplay(pane, line, at, tw)) else from + panes.File.byteAtDisplay(text[from..], tw);
+ return to >= shown and to < text.len;
}
fn paintSourceSelection(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, rows: []const Pane.PointerRow, bg: [3]u8, fg: ?[3]u8) void {
@@ -95,8 +97,15 @@ fn paintSourceSelection(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, rows: []c
const lo = @max(start, row.lo);
const hi = @min(end, row.hi);
if (lo >= hi) continue;
- const col = prefix + source.pad + @as(i32, @intCast(panes.File.rawDisplayCol(line[start..], lo - start)));
- const last = @min(width, if (row.to_edge and hi == end) @as(i32, width) else prefix + source.pad + @as(i32, @intCast(panes.File.rawDisplayCol(line[start..], hi - start))));
+ // A file row through its pads (File.rowPads), as it is drawn.
+ const col = prefix + source.pad + if (pane.file != null)
+ panes.File.displayOffset(pane, source.line, @intCast(start), @intCast(lo))
+ else
+ @as(i32, @intCast(panes.File.rawDisplayCol(line[start..], lo - start)));
+ const last = @min(width, if (row.to_edge and hi == end) @as(i32, width) else prefix + source.pad + if (pane.file != null)
+ panes.File.displayOffset(pane, source.line, @intCast(start), @intCast(hi))
+ else
+ @as(i32, @intCast(panes.File.rawDisplayCol(line[start..], hi - start))));
if (col < last) s.tint(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(visible)), @intCast(last - col), bg, fg);
}
}
@@ -361,7 +370,12 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane
// The byte past the row's last column, walked from its first:
// bounded by the view, not the line.
// A line that ends inside the view has no column past it.
- const shown_to = if (row_at >= row_text.len) row_text.len else row_at + panes.File.byteAtDisplay(row_text[row_at..], tw);
+ const shown_to = if (row_at >= row_text.len)
+ row_text.len
+ else if (pane.file != null)
+ @as(usize, @intCast(panes.File.byteAtRowDisplay(pane, w.line, w.at, tw)))
+ else
+ row_at + panes.File.byteAtDisplay(row_text[row_at..], tw);
const past_view = if (shown_to >= row_text.len) std.math.maxInt(usize) else shown_to;
// A row that ends before the selection starts has none of it.
if (w.line == bnd.lo_row and @as(usize, @intCast(@max(0, bnd.lo_col))) >= past_view) continue;
diff --git a/src/edit.zig b/src/edit.zig
index a06f5f2d..74dd0d1d 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -250,8 +250,15 @@ pub fn capturePointerSelection(p: *Pardes, pane: *Pane, slot: usize) !void {
if (raw) next = .{ .line = source.line + 1, .at = 0 };
const end = if (next.line == source.line) @min(line.len, @as(usize, @intCast(@max(0, next.at)))) else line.len;
const start: usize = @intCast(@max(0, source.at));
- const lo = @min(end, panes.File.byteAtDisplayFrom(line, start, @intCast(@max(0, left - prefix - source.pad))));
- const at = @min(end, panes.File.byteAtDisplayFrom(line, start, @intCast(@max(0, right - prefix - source.pad))));
+ // A file row through its table pads (File.rowPads), as it is drawn.
+ const lo: usize = @min(end, if (pane.file != null and !raw)
+ @as(usize, @intCast(panes.File.byteAtRowDisplay(pane, source.line, source.at, @max(0, left - prefix - source.pad))))
+ else
+ panes.File.byteAtDisplayFrom(line, start, @intCast(@max(0, left - prefix - source.pad))));
+ const at: usize = @min(end, if (pane.file != null and !raw)
+ @as(usize, @intCast(panes.File.byteAtRowDisplay(pane, source.line, source.at, @max(0, right - prefix - source.pad))))
+ else
+ panes.File.byteAtDisplayFrom(line, start, @intCast(@max(0, right - prefix - source.pad))));
row.* = .{
.row = source.line,
.lo = lo,
diff --git a/src/syntax.zig b/src/syntax.zig
index 81722ce5..aca2c3ff 100644
--- a/src/syntax.zig
+++ b/src/syntax.zig
@@ -2003,3 +2003,72 @@ test "syntax source analysis preserves declarations and injected colors" {
try std.testing.expectEqual(@as(usize, 0), unsupported.declarations.len);
try std.testing.expectEqual(@as(usize, 0), unsupported.colors.len);
}
+
+test "tables are the pipe_tables tree-sitter finds" {
+ if (!enabled or !full_grammars) return;
+ start(std.testing.allocator);
+ defer stop();
+ const Tables = @import("Tables.zig");
+ const source =
+ "# Tables\n" ++
+ "\n" ++
+ "| a | b |\n" ++
+ "|---|:-:|\n" ++
+ "| 1 | 2 |\n" ++
+ "no pipe row\n" ++
+ "\n" ++
+ "a | b\n" ++
+ "--|--\n" ++
+ "c | d\n" ++
+ "# heading ends it\n" ++
+ "| x |\n" ++
+ "|---|\n" ++
+ "\n" ++
+ "```\n" ++
+ "| in | fence |\n" ++
+ "|----|-------|\n" ++
+ "```\n" ++
+ "\n" ++
+ "| one | two |\n" ++
+ "|-----|\n" ++
+ "\n" ++
+ " | indented | table |\n" ++
+ " | -------- | ----- |\n" ++
+ " | row | here |\n" ++
+ "> quote ends\n" ++
+ "\n" ++
+ "| esc \\| aped | two |\n" ++
+ "| --- | --- |\n" ++
+ "- list ends\n";
+ const selected = (try forLang("markdown")).?;
+ const tree = selected.parser.parseString(source, null) orelse return error.ParseFailed;
+ defer tree.destroy();
+ // Every pipe_table node's lines, wherever it is in the tree.
+ var found: std.ArrayList([2]u32) = .empty;
+ defer found.deinit(std.testing.allocator);
+ var stack: std.ArrayList(ts.Node) = .empty;
+ defer stack.deinit(std.testing.allocator);
+ try stack.append(std.testing.allocator, tree.rootNode());
+ while (stack.pop()) |node| {
+ if (std.mem.eql(u8, node.kind(), "pipe_table")) {
+ const end = node.endPoint();
+ try found.append(std.testing.allocator, .{ node.startPoint().row, end.row + @intFromBool(end.column > 0) });
+ continue;
+ }
+ var i = node.childCount();
+ while (i > 0) : (i -= 1) if (node.child(i - 1)) |child| try stack.append(std.testing.allocator, child);
+ }
+ // The tables, the indented one and the escaped pipe's; not the fence's
+ // or the one whose delimiter has a cell too few.
+ try std.testing.expectEqual(@as(usize, 5), found.items.len);
+ var t: Tables = .{};
+ defer t.deinit(std.testing.allocator);
+ try t.scan(std.testing.allocator, source);
+ var mine: std.ArrayList([2]u32) = .empty;
+ defer mine.deinit(std.testing.allocator);
+ for (t.list.items) |table| try mine.append(std.testing.allocator, .{ table.first, table.end });
+ std.testing.expectEqualSlices([2]u32, found.items, mine.items) catch |e| {
+ std.debug.print("tree-sitter {any}\nTables {any}\n", .{ found.items, mine.items });
+ return e;
+ };
+}