summaryrefslogtreecommitdiff
path: root/abi
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-21 05:49:06 -0300
committerGabriel Schneider <[email protected]>2026-11-21 05:56:16 -0300
commitbf16b1c2b461f4d7db17efb6de912799a5bf78ac (patch)
tree0df6f9681fd7a353c1689c0a67f054579197d19c /abi
parented0103e9738e3cf5a7c7a91aec5adf0be2fc409a (diff)
downloadpardes-bf16b1c2b461f4d7db17efb6de912799a5bf78ac.tar.gz
pardes-bf16b1c2b461f4d7db17efb6de912799a5bf78ac.zip
library: a language's browser module formats one file's diff with that file's text: `patchFile(text, len, file, file_len)` beside `highlight` (xit writes its `@@` with nothing after it, so a hunk had no context at all; toilet has the file's blob), the same header and records as `patch`, with the declaration rows formatPatch stacks when it has a source (kind 3, as git's words are: a declaration's `path:line` row, its code_start its whole length, then its header's lines); the text is the first section's file, new or old (formatPatch finds each hunk on either side and numbers the labels as that side; tested both), a hunk the text does not hold and the diff's other files stack as with no text (tested: the same rows as `patch`); only abi/wasm.zig (the export, a `Given` source answering for the first path asked, `start` shared with highlight), build.zig (the export's name, the option named `language`) and the docs: formatPatch is unchanged; +7,031 bytes on rust's module (1,290,182 -> 1,297,213; about 7 KB on each language's), 18 ms against patch's 4 for a 28-hunk diff of a 1,100-line Zig file under node (the file's parse); test/consumer's node run calls it on a bare-`@@` rust diff with the new text, the old, another text and a second file
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'abi')
-rw-r--r--abi/wasm.zig53
1 files changed, 46 insertions, 7 deletions
diff --git a/abi/wasm.zig b/abi/wasm.zig
index 0c67e0da..88dc8d90 100644
--- a/abi/wasm.zig
+++ b/abi/wasm.zig
@@ -14,7 +14,11 @@
//! - `patch(text, len)`: the diff `text` stacked as pardes's patch view
//! shows it (`formatPatch`, with no file and no colours): a `Header`, its
//! rows' text, a style byte for each byte of it, and a `Record` a row.
-//! The module's until the next `patch`.
+//! The module's until the next `patch` or `patchFile`.
+//! - `patchFile(text, len, file, file_len)` (a module with a language):
+//! `patch` with the text of the file the diff's first section names (its
+//! new text or its old), so a hunk gets the declarations it sits in from
+//! the file's own parse; the diff's other files stack as with none.
//!
//! Each returns null (0) when its memory runs out.
const std = @import("std");
@@ -35,8 +39,12 @@ pub const panic = std.debug.FullPanic(struct {
}.write);
comptime {
- // A module with no language has no highlighter, and so no tree-sitter.
- if (options.highlight) @export(&highlight, .{ .name = "highlight" });
+ // A module with no language has no highlighter and no parse of a file,
+ // and so no tree-sitter.
+ if (options.language) {
+ @export(&highlight, .{ .name = "highlight" });
+ @export(&patchFile, .{ .name = "patchFile" });
+ }
}
export fn reserve(len: usize) ?[*]u8 {
@@ -50,9 +58,13 @@ export fn release(ptr: [*]u8, len: usize) void {
var started = false;
var styles: []u8 = &.{};
-fn highlight(path: [*]const u8, path_len: usize, text: [*]const u8, len: usize) callconv(.c) ?[*]u8 {
+fn start() void {
if (!started) ps.start(gpa);
started = true;
+}
+
+fn highlight(path: [*]const u8, path_len: usize, text: [*]const u8, len: usize) callconv(.c) ?[*]u8 {
+ start();
gpa.free(styles);
styles = ps.highlightFileRange(gpa, path[0..path_len], text[0..len], 0, len) catch &.{};
// A path in no language this module has, or an empty text: all plain.
@@ -101,11 +113,38 @@ var arena_state: std.heap.ArenaAllocator = .init(gpa);
export fn patch(text: [*]const u8, len: usize) ?*const Header {
_ = arena_state.reset(.retain_capacity);
- return stack(arena_state.allocator(), text[0..len]) catch null;
+ return stack(&.{ .arena = arena_state.allocator() }, text[0..len]) catch null;
+}
+
+fn patchFile(text: [*]const u8, len: usize, file: [*]const u8, file_len: usize) callconv(.c) ?*const Header {
+ start();
+ _ = arena_state.reset(.retain_capacity);
+ var given: Given = .{ .bytes = file[0..file_len] };
+ return stack(&.{
+ .arena = arena_state.allocator(),
+ .source = Given.source,
+ .declarations = ps.PatchCtx.parsed,
+ .host = &given,
+ }, text[0..len]) catch null;
}
-fn stack(arena: std.mem.Allocator, diff: []const u8) !*const Header {
- const p = try ps.formatPatch(&.{ .arena = arena }, "", diff);
+/// The one file `patchFile` is handed: the first section's. A text the
+/// hunk is not in is no source (`formatPatch` looks for the hunk's lines).
+const Given = struct {
+ bytes: []const u8,
+ path: ?[]const u8 = null,
+
+ fn source(ctx: *const ps.PatchCtx, _: []const u8, path: []const u8) ?ps.PatchSource {
+ const given: *Given = @ptrCast(@alignCast(ctx.host));
+ const first = given.path orelse path;
+ given.path = first;
+ return if (std.mem.eql(u8, path, first)) .{ .path = path, .bytes = given.bytes } else null;
+ }
+};
+
+fn stack(ctx: *const ps.PatchCtx, diff: []const u8) !*const Header {
+ const arena = ctx.arena;
+ const p = try ps.formatPatch(ctx, "", diff);
// The text, then each row's path where it changes from the row before.
var text: std.ArrayList(u8) = .empty;
try text.appendSlice(arena, p.text);