@@ -5,6 +5,7 @@
55
66 const std = @import("std");
77 const ts = @import("tree-sitter");
8+const render = @import("render.zig");
89 const theme = @import("theme.zig");
910
1011 extern fn tree_sitter_javascript() callconv(.c) *const ts.Language;
@@ -12,11 +13,18 @@ extern fn tree_sitter_typescript() callconv(.c) *const ts.Language;
1213 extern fn tree_sitter_tsx() callconv(.c) *const ts.Language;
1314 extern fn tree_sitter_markdown() callconv(.c) *const ts.Language;
1415 extern fn tree_sitter_markdown_inline() callconv(.c) *const ts.Language;
16+extern fn tree_sitter_python() callconv(.c) *const ts.Language;
17+extern fn tree_sitter_go() callconv(.c) *const ts.Language;
18+extern fn tree_sitter_zig() callconv(.c) *const ts.Language;
1519
1620 const JS_QUERIES = @embedFile("js_highlights");
1721 const TS_QUERIES = @embedFile("ts_highlights");
1822 const MD_QUERIES = @embedFile("md_highlights");
23+const MD_TABLES = @embedFile("md_tables");
1924 const MD_INLINE_QUERIES = @embedFile("md_inline_highlights");
25+const PY_QUERIES = @embedFile("py_highlights");
26+const GO_QUERIES = @embedFile("go_highlights");
27+const ZIG_QUERIES = @embedFile("zig_highlights");
2028
2129 const Syntax = struct {
2230 parser: *ts.Parser,
@@ -50,6 +58,9 @@ const Grammars = struct {
5058 tsx: Syntax,
5159 markdown: Syntax,
5260 markdown_inline: Syntax,
61+ python: Syntax,
62+ go: Syntax,
63+ zig: Syntax,
5364
5465 fn init(a: std.mem.Allocator) Grammars {
5566 const js = tree_sitter_javascript();
@@ -57,12 +68,18 @@ const Grammars = struct {
5768 const tsx_lang = tree_sitter_tsx();
5869 const md = tree_sitter_markdown();
5970 const md_inline = tree_sitter_markdown_inline();
71+ const py = tree_sitter_python();
72+ const go_lang = tree_sitter_go();
73+ const zig_lang = tree_sitter_zig();
6074 return .{
6175 .javascript = syntax(a, js, &.{JS_QUERIES}) orelse unreachable,
6276 .typescript = syntax(a, ts_lang, &.{ JS_QUERIES, TS_QUERIES }) orelse unreachable,
6377 .tsx = syntax(a, tsx_lang, &.{ JS_QUERIES, TS_QUERIES }) orelse unreachable,
64- .markdown = syntax(a, md, &.{MD_QUERIES}) orelse unreachable,
78+ .markdown = syntax(a, md, &.{ MD_QUERIES, MD_TABLES }) orelse unreachable,
6579 .markdown_inline = syntax(a, md_inline, &.{MD_INLINE_QUERIES}) orelse unreachable,
80+ .python = syntax(a, py, &.{PY_QUERIES}) orelse unreachable,
81+ .go = syntax(a, go_lang, &.{GO_QUERIES}) orelse unreachable,
82+ .zig = syntax(a, zig_lang, &.{ZIG_QUERIES}) orelse unreachable,
6683 };
6784 }
6885 };
@@ -302,7 +319,9 @@ pub fn highlightMarkdown(a: std.mem.Allocator, text: []const u8) []const u8 {
302319 spans.appendSlice(a, spansOf(a, &g.markdown, root.rootNode(), u, text, 0)) catch {};
303320
304321 var inline_nodes: std.ArrayList(ts.Node) = .empty;
305- collectByKind(a, root.rootNode(), &.{"inline"}, &inline_nodes);
322+ // Table cells hold inline markup too, and the block grammar leaves it
323+ // unparsed: the inline grammar runs over the cell text the same way.
324+ collectByKind(a, root.rootNode(), &.{ "inline", "pipe_table_cell" }, &inline_nodes);
306325 for (inline_nodes.items) |node| {
307326 const start = byteOf(u, node.startByte() / 2);
308327 const inline_text = text[start..byteOf(u, node.endByte() / 2)];
@@ -344,6 +363,129 @@ pub fn highlightMarkdown(a: std.mem.Allocator, text: []const u8) []const u8 {
344363 return paint(a, text, spans.items);
345364 }
346365
366+const Align = enum { none, left, center, right };
367+
368+/// A delimiter cell's alignment, read off its own text: `:--`, `:-:`, `--:`.
369+fn alignOf(cell: []const u8) Align {
370+ const left = cell.len > 0 and cell[0] == ':';
371+ const right = cell.len > 1 and cell[cell.len - 1] == ':';
372+ if (left and right) return .center;
373+ if (left) return .left;
374+ if (right) return .right;
375+ return .none;
376+}
377+
378+const Row = struct { cells: []const []const u8, delimiter: bool };
379+
380+/// One table, printed the way prettier prints one: every column is as wide as
381+/// its widest cell, three characters at least, and each cell is padded to that
382+/// width under the alignment its delimiter cell declares. Cells come from the
383+/// grammar's own nodes, never from a split on `|`, so an escaped pipe and a
384+/// code span holding one each stay inside one cell. Null when the header names
385+/// no column.
386+fn formatTable(a: std.mem.Allocator, text: []const u8, u: Utf16, table: ts.Node) ?[]const u8 {
387+ var rows: std.ArrayList(Row) = .empty;
388+ // GFM reads the column count off the header row: a row with more cells than
389+ // the header has drops the extras, and the delimiter row is capped by it.
390+ var named: usize = 0;
391+ var i: u32 = 0;
392+ while (i < table.childCount()) : (i += 1) {
393+ const child = table.child(i) orelse continue;
394+ const kind = child.kind();
395+ const delimiter = std.mem.eql(u8, kind, "pipe_table_delimiter_row");
396+ const header = std.mem.eql(u8, kind, "pipe_table_header");
397+ if (!delimiter and !header and !std.mem.eql(u8, kind, "pipe_table_row")) continue;
398+ var cells: std.ArrayList([]const u8) = .empty;
399+ var j: u32 = 0;
400+ while (j < child.childCount()) : (j += 1) {
401+ const cell = child.child(j) orelse continue;
402+ const cell_kind = cell.kind();
403+ if (!std.mem.eql(u8, cell_kind, "pipe_table_cell") and !std.mem.eql(u8, cell_kind, "pipe_table_delimiter_cell")) continue;
404+ const span = text[byteOf(u, cell.startByte() / 2)..byteOf(u, cell.endByte() / 2)];
405+ cells.append(a, std.mem.trim(u8, span, " \t")) catch {};
406+ }
407+ if (header) named = cells.items.len;
408+ rows.append(a, .{ .cells = cells.items, .delimiter = delimiter }) catch {};
409+ }
410+ if (rows.items.len == 0 or named == 0) return null;
411+
412+ // The delimiter row is the only row whose alignment counts; a row that
413+ // leaves a column out pads it empty under the same alignment.
414+ const aligns = a.alloc(Align, named) catch return null;
415+ const widths = a.alloc(usize, named) catch return null;
416+ @memset(aligns, .none);
417+ @memset(widths, 3);
418+ for (rows.items) |row| {
419+ for (row.cells, 0..) |cell, k| {
420+ if (k >= named) break;
421+ if (row.delimiter) aligns[k] = alignOf(cell) else widths[k] = @max(widths[k], render.displayWidth(cell));
422+ }
423+ }
424+
425+ // The table node's range includes the newline that ends its last row, and
426+ // an element still streaming has none: write back exactly what was there.
427+ const end = byteOf(u, table.endByte() / 2);
428+ const trailing = end > 0 and end <= text.len and text[end - 1] == '\n';
429+
430+ var block: std.ArrayList(u8) = .empty;
431+ for (rows.items, 0..) |row, r| {
432+ if (r > 0) block.append(a, '\n') catch {};
433+ block.append(a, '|') catch {};
434+ for (0..named) |k| {
435+ const cell = if (k < row.cells.len) row.cells[k] else "";
436+ const alignment = aligns[k];
437+ block.append(a, ' ') catch {};
438+ if (row.delimiter) {
439+ block.append(a, if (alignment == .left or alignment == .center) ':' else '-') catch {};
440+ block.appendNTimes(a, '-', widths[k] - 2) catch {};
441+ block.append(a, if (alignment == .right or alignment == .center) ':' else '-') catch {};
442+ } else {
443+ const pad = widths[k] - render.displayWidth(cell);
444+ const before: usize = switch (alignment) {
445+ .right => pad,
446+ .center => pad / 2,
447+ else => 0,
448+ };
449+ block.appendNTimes(a, ' ', before) catch {};
450+ block.appendSlice(a, cell) catch {};
451+ block.appendNTimes(a, ' ', pad - before) catch {};
452+ }
453+ block.appendSlice(a, " |") catch {};
454+ }
455+ }
456+ if (trailing) block.append(a, '\n') catch {};
457+ return block.items;
458+}
459+
460+/// Prints every top-level table of a committed block under aligned columns.
461+/// A table inside a quote or a list is left alone: its rows carry a prefix that
462+/// is no part of any cell, so rebuilding a row from its cells would drop it.
463+pub fn formatTables(a: std.mem.Allocator, text: []const u8) []const u8 {
464+ const u = utf16(a, text) orelse return text;
465+ const root = parse(getGrammars(a).markdown.parser, u) orelse return text;
466+ defer root.destroy();
467+ var tables: std.ArrayList(ts.Node) = .empty;
468+ collectByKind(a, root.rootNode(), &.{"pipe_table"}, &tables);
469+
470+ var out: std.ArrayList(u8) = .empty;
471+ var at: u32 = 0;
472+ var found = false;
473+ for (tables.items) |table| {
474+ const parent = table.parent() orelse continue;
475+ if (!std.mem.eql(u8, parent.kind(), "section")) continue;
476+ const start = byteOf(u, table.startByte() / 2);
477+ const end = byteOf(u, table.endByte() / 2);
478+ const block = formatTable(a, text, u, table) orelse continue;
479+ out.appendSlice(a, text[at..start]) catch {};
480+ out.appendSlice(a, block) catch {};
481+ at = end;
482+ found = true;
483+ }
484+ if (!found) return text;
485+ out.appendSlice(a, text[at..]) catch {};
486+ return out.items;
487+}
488+
347489 /// The reference binding feeds every grammar UTF16LE and reports offsets in
348490 /// UTF16 code units; the grammars' scanners parse input without a final line
349491 /// ending differently per encoding, so the port feeds the same one.
@@ -392,14 +534,14 @@ pub fn highlightCode(a: std.mem.Allocator, info: []const u8, text: []const u8) [
392534 const lang = std.ascii.allocLowerString(a, trimmed[0..end]) catch trimmed[0..end];
393535 const g = getGrammars(a);
394536 const u = utf16(a, text) orelse return text;
395- const syn: ?*Syntax = if (std.mem.eql(u8, lang, "js") or std.mem.eql(u8, lang, "javascript") or std.mem.eql(u8, lang, "jsx"))
396- &g.javascript
397- else if (std.mem.eql(u8, lang, "ts") or std.mem.eql(u8, lang, "typescript"))
398- &g.typescript
399- else if (std.mem.eql(u8, lang, "tsx"))
400- &g.tsx
401- else
402- null;
537+ const syn: ?*Syntax =
538+ if (std.mem.eql(u8, lang, "js") or std.mem.eql(u8, lang, "javascript") or std.mem.eql(u8, lang, "jsx")) &g.javascript
539+ else if (std.mem.eql(u8, lang, "ts") or std.mem.eql(u8, lang, "typescript")) &g.typescript
540+ else if (std.mem.eql(u8, lang, "tsx")) &g.tsx
541+ else if (std.mem.eql(u8, lang, "py") or std.mem.eql(u8, lang, "python")) &g.python
542+ else if (std.mem.eql(u8, lang, "go") or std.mem.eql(u8, lang, "golang")) &g.go
543+ else if (std.mem.eql(u8, lang, "zig")) &g.zig
544+ else null;
403545 const s = syn orelse return text;
404546 const root = parse(s.parser, u) orelse return text;
405547 defer root.destroy();