//! pardes's library from outside pardes (build.zig here): a C and a Zig //! source coloured, outlined and walked, and a theme's colours for them. const std = @import("std"); const pardes = @import("pardes-syntax"); const c_source = \\#include \\ \\// say it \\int main(void) { \\ printf("hi %d\n", 42); \\ return 0; \\} \\ ; const zig_source = \\const std = @import("std"); \\ \\pub fn main() void { \\ // say it \\ std.debug.print("hi {d}\n", .{42}); \\} \\ ; /// Every byte of `text`'s first occurrence in `source` is drawn as `want`. fn expectSyn(styles: []const u8, source: []const u8, text: []const u8, want: pardes.Syn) !void { const at = std.mem.indexOf(u8, source, text).?; for (styles[at..][0..text.len]) |style| try std.testing.expectEqual(want, @as(pardes.Syn, @fromBackingInt(style))); } test "a C and a Zig source are coloured, outlined and walked as pardes has them, in a named theme's colours" { const gpa = std.testing.allocator; // under the testing allocator, so what tree-sitter keeps is counted too pardes.start(gpa); defer pardes.stop(); try std.testing.expect(pardes.supportsPath("main.c") and pardes.supportsPath("main.zig")); try std.testing.expect(!pardes.supportsPath("main.rs")); // minimal: c, c++, zig try std.testing.expectEqualStrings("a.c", pardes.pathFor("C").?); try std.testing.expectEqualStrings("a.cpp", pardes.pathFor("c++").?); try std.testing.expectEqual(null, pardes.pathFor("klingon")); var c = try pardes.analyzeSource(gpa, "main.c", c_source, true, true); defer c.deinit(gpa); try std.testing.expectEqual(c_source.len, c.colors.len); try expectSyn(c.colors, c_source, "#include", .keyword); try expectSyn(c.colors, c_source, "", .string); try expectSyn(c.colors, c_source, "// say it", .comment); try expectSyn(c.colors, c_source, "main", .none); try expectSyn(c.colors, c_source, "\"hi %d\\n\"", .string); try expectSyn(c.colors, c_source, "42", .number); try expectSyn(c.colors, c_source, "return", .keyword); try std.testing.expectEqual(1, c.declarations.len); const main = c.declarations[0]; try std.testing.expectEqualStrings("int main(void) {", c_source[main.start_byte..main.header_end_byte]); try std.testing.expectEqual('}', c_source[main.end_byte - 1]); try std.testing.expectEqual(3, main.start_line); try std.testing.expectEqual(6, main.end_line); try std.testing.expectEqualStrings("int main(void) {", pardes.lineSlice(c_source, main.start_line)); try std.testing.expectEqual(main.start_byte, pardes.lineStartOffset(c_source, main.start_line)); try std.testing.expectEqual(8, pardes.lineCount(c_source)); const zig = try pardes.highlightFileRange(gpa, pardes.pathFor("zig").?, zig_source, 0, zig_source.len); defer gpa.free(zig); try std.testing.expectEqual(zig_source.len, zig.len); try expectSyn(zig, zig_source, "fn", .keyword); try expectSyn(zig, zig_source, "\"std\"", .string); try expectSyn(zig, zig_source, "// say it", .comment); try expectSyn(zig, zig_source, "42", .number); try expectSyn(zig, zig_source, "main", .none); const outline = try pardes.analyzeSource(gpa, "main.zig", zig_source, true, false); defer gpa.free(outline.declarations); try std.testing.expectEqual(1, outline.declarations.len); try std.testing.expectEqualStrings("pub fn main() void {", zig_source[outline.declarations[0].start_byte..outline.declarations[0].header_end_byte]); // the tree: a statement from a byte in it, and the nodes themselves const tree = pardes.parse("main.c", c_source).?; defer tree.destroy(); try std.testing.expectEqualStrings("translation_unit", tree.rootNode().kind()); const at = std.mem.indexOf(u8, c_source, "return").?; var span: [1]pardes.Span = undefined; try std.testing.expectEqual(1, pardes.walkNodes(tree, .expand_node, at, at, at, &span)); try std.testing.expectEqualStrings("return", c_source[span[0].from..span[0].to]); try std.testing.expectEqual(1, pardes.walkNodes(tree, .expand_node, span[0].from, span[0].to, at, &span)); try std.testing.expectEqualStrings("return 0;", c_source[span[0].from..span[0].to]); try std.testing.expectEqual(null, pardes.parse("main.rs", "fn main() {}")); const orchard = pardes.theme("Orchard").?; try std.testing.expectEqual(orchard, &pardes.themes[0]); try std.testing.expectEqual([3]u8{ 0x0d, 0x14, 0x10 }, pardes.page(orchard)); try std.testing.expectEqual([3]u8{ 0xe5, 0xee, 0xe5 }, pardes.ink(orchard)); try std.testing.expectEqual([3]u8{ 0xc9, 0xa7, 0xed }, pardes.synInk(orchard, .keyword).?); try std.testing.expectEqual([3]u8{ 0xb8, 0xd8, 0x8a }, pardes.synInk(orchard, .string).?); try std.testing.expectEqual([3]u8{ 0xf8, 0xc0, 0x82 }, pardes.synInk(orchard, .number).?); try std.testing.expectEqual([3]u8{ 0xb9, 0xf4, 0x80 }, pardes.synInk(orchard, .comment).?); try std.testing.expectEqual(null, pardes.synInk(orchard, .none)); // `dark` has no page of its own, and an imported palette is in the ring try std.testing.expectEqual(pardes.theme("dark").?.tag_bg, pardes.page(pardes.theme("dark").?)); try std.testing.expect(pardes.theme("gruvbox") != null); try std.testing.expectEqual(null, pardes.theme("no such theme")); } test "a diff is a language: its hunks coloured as their file's, its added and removed rows on a theme's grounds" { const gpa = std.testing.allocator; pardes.start(gpa); defer pardes.stop(); const patch = \\diff --git a/main.c b/main.c \\--- a/main.c \\+++ b/main.c \\@@ -1,3 +1,3 @@ \\ int main(void) { \\- return 1; \\+ return 0; \\ } \\ ; const styles = try pardes.highlightDiff(gpa, patch, 0, patch.len); defer gpa.free(styles); try std.testing.expectEqual(patch.len, styles.len); const syn = struct { fn at(s: []const u8, text: []const u8, needle: []const u8) pardes.Syn { return @fromBackingInt(s[std.mem.indexOf(u8, text, needle).?] & pardes.syn_bits); } }.at; try std.testing.expectEqual(pardes.Syn.comment, syn(styles, patch, "--- a/main.c")); try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "@@ -1,3")); // each side's code as C, behind its marker try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "return 1")); try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "return 0")); try std.testing.expectEqual(pardes.Syn.number, syn(styles, patch, "0;")); const added = std.mem.indexOf(u8, patch, "+ return 0").?; const removed = std.mem.indexOf(u8, patch, "- return 1").?; try std.testing.expectEqual(pardes.ground_add | pardes.ground_emph, styles[added]); try std.testing.expect(styles[added + 5] & pardes.ground_add != 0 and styles[added + 5] & pardes.ground_emph == 0); try std.testing.expectEqual(pardes.ground_del | pardes.ground_emph, styles[removed]); try std.testing.expectEqual(0, styles[std.mem.indexOf(u8, patch, " int main").?] & (pardes.ground_add | pardes.ground_del)); // a row's hue in a theme: a ground between the page and the theme's green, a stronger and a fainter one const th = pardes.theme("Orchard").?; const g = pardes.Grounds.of(th); const ground = g.forStyle(styles[added]).?; try std.testing.expectEqual(g.add, ground); try std.testing.expectEqual(g.del, g.forStyle(styles[removed]).?); try std.testing.expectEqual(null, g.forStyle(@backingInt(pardes.Syn.keyword))); try std.testing.expect(!std.mem.eql(u8, &ground.row, &pardes.page(th)) and !std.mem.eql(u8, &ground.row, &ground.emph) and !std.mem.eql(u8, &ground.row, &ground.line)); // what a row points at: the new line a `+` row is var lines: std.ArrayList([]const u8) = .empty; defer lines.deinit(gpa); var it = std.mem.splitScalar(u8, patch, '\n'); while (it.next()) |line| try lines.append(gpa, line); var buf: [64]u8 = undefined; try std.testing.expectEqualStrings("main.c:2", pardes.diff.targetAt(lines.items, 6, false).?.address(&buf).?); } test "every name the library has compiles outside pardes" { std.testing.refAllDecls(pardes); }