diff options
Diffstat (limited to 'src/syntax.zig')
| -rw-r--r-- | src/syntax.zig | 69 |
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; + }; +} |
