summaryrefslogtreecommitdiff
path: root/src/syntax.zig
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 /src/syntax.zig
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
Diffstat (limited to 'src/syntax.zig')
-rw-r--r--src/syntax.zig69
1 files changed, 69 insertions, 0 deletions
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;
+ };
+}