From 6d9272b83d122585ad7bbe7214c078f65351c3c0 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:25:19 -0300 Subject: lib: the diff is a language of pardes-syntax: `diff` (Walk, targetAt), `highlightDiff`, the style byte's `syn_bits` and grounds and colors.Grounds, a theme's colours for them; test/lib colours a C patch through it (each side's code as C, the rows' grounds, Orchard's, and the place a `+` row names) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/lib_syntax.zig | 19 +++++++++++++++++++ test/lib/lib.zig | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/lib_syntax.zig b/src/lib_syntax.zig index 395a02f0..a25af884 100644 --- a/src/lib_syntax.zig +++ b/src/lib_syntax.zig @@ -51,6 +51,25 @@ pub const SourceAnalysis = syntax.SourceAnalysis; pub const ContextDeclaration = syntax.ContextDeclaration; pub const analyzeSource = syntax.analyzeSource; +/// A unified diff as a language: `diff` walks one a line at a time +/// (`Walk`: a line's kind, the file its section names, the hunk's line +/// numbers) and says what a row of it points at (`targetAt`); +/// `highlightDiff` is a style byte for each byte of `content[start..end]`, +/// the caller's to free: headers as comments, `@@` lines as keywords, each +/// hunk's code in the language of the file its section names (old and new +/// side each parsed whole), and over that an added or removed line's +/// grounds (`ground_add`, `ground_del`; its first byte `ground_emph`, the +/// strong row). A byte's low bits (`syn_bits`) are a `Syn`; `Grounds` are +/// a theme's colours for the rest. +pub const diff = @import("diff.zig"); +pub const highlightDiff = syntax.highlightDiff; +pub const syn_bits = syntax.syn_bits; +pub const ground_add = syntax.ground_add; +pub const ground_del = syntax.ground_del; +pub const ground_emph = syntax.ground_emph; +pub const ground_line = syntax.ground_line; +pub const Grounds = colors.Grounds; + /// The tree-sitter bindings a tree is read through: `tree.rootNode()`, and /// a node's `kind()`, `startByte()`, `endByte()`, `child(i)`, `parent()`. pub const ts = syntax.ts; diff --git a/test/lib/lib.zig b/test/lib/lib.zig index a3f99b56..7e297920 100644 --- a/test/lib/lib.zig +++ b/test/lib/lib.zig @@ -102,6 +102,60 @@ test "a C and a Zig source are coloured, outlined and walked as pardes has them, 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); } -- cgit v1.3