diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 01:59:49 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 02:00:21 -0300 |
| commit | ee5b6aa3ef0bf398c5381498af40fa93d20cf0b3 (patch) | |
| tree | 808bb75b174c0835e1bfedff9d01f15ba273c629 /test/consumer/lib.zig | |
| parent | 44b4d81cd1ba49a035e66699416639b46723c872 (diff) | |
| download | pardes-ee5b6aa3ef0bf398c5381498af40fa93d20cf0b3.tar.gz pardes-ee5b6aa3ef0bf398c5381498af40fa93d20cf0b3.zip | |
library: the patch view without the editor: pardes-syntax exports hunks.format as `formatPatch` with its `PatchCtx` (an arena; optional `source`, `declarations` (`PatchCtx.parsed`), `colors` (`PatchCtx.languages`: highlightDiff's), `width`; their defaults no file, no declaration, no colour, code points) and its rows, `Patch`, `PatchRow` (kind other, meta, hunk, declaration, context, added, removed; the path; the line it points at; the old and the new number, each null where that side has none; location_end, code_start; a style byte a byte; no_newline, the `\\ No newline` the editor folds into the line above); format no longer makes locations.Row (it needs no editor type: the file's first half imports nothing of root.zig's) and the editor's openDiff maps its rows onto a pane's (`paneResult`, one pass, the rows as before, so the +Diff is unchanged), handing in its own source, declarations cache, colours and display width (File.displayWidth, the one use of vaxis: `width` replaces Ctx.tab); with no `colors` the formatter calls nothing of tree-sitter (the grounds and the words a changed line changed are text alone); inferEdits' 8 MiB scratch is made once a format call, not once a run of changed lines (a browser's wasm memory took 8 MiB a run, never given back in the call); test/consumer: a C diff with no file (numbered by the diff, under git's `@@` words, `foo`/`bar` emphasised, coloured as C) and with main.c handed in (its declaration from the parse above `main.c:3 *`), and in a build with no grammar (none.zig: rows, grounds and the changed `*`, no colour; a file handed in places the hunk and makes no declaration up)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'test/consumer/lib.zig')
| -rw-r--r-- | test/consumer/lib.zig | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/test/consumer/lib.zig b/test/consumer/lib.zig index 7e297920..fa0f8aee 100644 --- a/test/consumer/lib.zig +++ b/test/consumer/lib.zig @@ -159,3 +159,98 @@ test "a diff is a language: its hunks coloured as their file's, its added and re test "every name the library has compiles outside pardes" { std.testing.refAllDecls(pardes); } + +// ---- the patch view (+Diff), without the editor ---- + +const patch_c = + \\diff --git a/main.c b/main.c + \\--- a/main.c + \\+++ b/main.c + \\@@ -2,4 +2,4 @@ int main(void) + \\ { + \\- int x = foo(1); + \\+ int x = bar(1); + \\ return x; + \\ } + \\\ No newline at end of file + \\ +; +/// main.c as the patch leaves it: what a reader hands in as its source. +const main_c = "int main(void)\n{\n int x = bar(1);\n return x;\n}"; + +/// Each byte of `text` that has `bit`, the rest as dots. +fn marked(buf: []u8, text: []const u8, styles: []const u8, bit: u8) []const u8 { + for (buf[0..text.len], text, styles) |*o, c, s| o.* = if (s & bit != 0) c else '.'; + return buf[0..text.len]; +} + +test "a diff stacked with no file to read: numbered by the diff, under git's function line, its changed words emphasised" { + const gpa = std.testing.allocator; + pardes.start(gpa); + defer pardes.stop(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const ctx: pardes.PatchCtx = .{ .arena = arena.allocator(), .colors = pardes.PatchCtx.languages }; + const p = try pardes.formatPatch(&ctx, "", patch_c); + try std.testing.expectEqualStrings("main.c:3\n int main(void)\n {\n- int x = foo(1);\n+ int x = bar(1);\n return x;\n }\n", p.text); + const Kind = pardes.PatchRow.Kind; + const want = [_]struct { Kind, ?usize, ?usize }{ + .{ .hunk, null, null }, + // the `@@`'s own words: the hunk's context + .{ .declaration, null, null }, + .{ .context, 2, 2 }, + .{ .removed, 3, null }, + .{ .added, null, 3 }, + .{ .context, 4, 4 }, + .{ .context, 5, 5 }, + }; + try std.testing.expectEqual(want.len, p.rows.len); + for (want, p.rows) |w, row| { + try std.testing.expectEqual(w[0], row.kind); + try std.testing.expectEqual(w[1], row.old); + try std.testing.expectEqual(w[2], row.new); + try std.testing.expectEqualStrings("main.c", row.path); + } + try std.testing.expectEqual(3, p.rows[0].line); + try std.testing.expectEqual("main.c:3".len, p.rows[0].location_end); + try std.testing.expect(p.rows[6].no_newline and !p.rows[5].no_newline); + var lines = std.mem.splitScalar(u8, p.text, '\n'); + var buf: [64]u8 = undefined; + for (p.rows) |row| { + const line = lines.next().?; + if (row.styles.len > 0) try std.testing.expectEqual(line.len, row.styles.len); + switch (row.kind) { + // the words a changed line changed, on both lines; each on its + // ground, its marker not on the line's own (it was paired) + .removed => try std.testing.expectEqualStrings(".............foo....", marked(&buf, line, row.styles, pardes.ground_emph)), + .added => try std.testing.expectEqualStrings(".............bar....", marked(&buf, line, row.styles, pardes.ground_emph)), + else => {}, + } + if (row.kind == .removed) try std.testing.expectEqual(pardes.ground_del, row.styles[0]); + if (row.kind == .added) try std.testing.expectEqual(pardes.ground_add, row.styles[0]); + } + // coloured as C: `return` a keyword + const ret = p.rows[5].styles[std.mem.indexOf(u8, " return x;", "return").?]; + try std.testing.expectEqual(pardes.Syn.keyword, @as(pardes.Syn, @fromBackingInt(ret & pardes.syn_bits))); +} + +test "a diff stacked with its file handed in: the declaration it stands in, from the file's own parse" { + const gpa = std.testing.allocator; + pardes.start(gpa); + defer pardes.stop(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const Given = struct { + fn source(_: *const pardes.PatchCtx, _: []const u8, path: []const u8) ?pardes.PatchSource { + return if (std.mem.eql(u8, path, "main.c")) .{ .path = path, .bytes = main_c } else null; + } + }; + const ctx: pardes.PatchCtx = .{ .arena = arena.allocator(), .source = Given.source, .declarations = pardes.PatchCtx.parsed, .colors = pardes.PatchCtx.languages }; + const p = try pardes.formatPatch(&ctx, "", patch_c); + try std.testing.expectEqualStrings("main.c:1\n int main(void)\nmain.c:3 *\n {\n- int x = foo(1);\n+ int x = bar(1);\n return x;\n }\n", p.text); + try std.testing.expectEqual(pardes.PatchRow.Kind.declaration, p.rows[0].kind); + try std.testing.expectEqual(1, p.rows[0].line); + try std.testing.expectEqual(pardes.PatchRow.Kind.declaration, p.rows[1].kind); + try std.testing.expectEqual(pardes.PatchRow.Kind.hunk, p.rows[2].kind); + try std.testing.expectEqual("main.c:3".len, p.rows[2].location_end); +} |
