diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 05:49:06 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 05:56:16 -0300 |
| commit | bf16b1c2b461f4d7db17efb6de912799a5bf78ac (patch) | |
| tree | 0df6f9681fd7a353c1689c0a67f054579197d19c | |
| parent | ed0103e9738e3cf5a7c7a91aec5adf0be2fc409a (diff) | |
| download | pardes-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
| -rw-r--r-- | abi/wasm.zig | 53 | ||||
| -rw-r--r-- | build.zig | 6 | ||||
| -rw-r--r-- | docs/typ/building.typ | 20 | ||||
| -rw-r--r-- | test/consumer/wasm.mjs | 38 |
4 files changed, 105 insertions, 12 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); @@ -415,7 +415,7 @@ pub fn build(b: *std.Build) void { /// pardes's highlighter; or, with no language, the patch view alone, with /// no tree-sitter in it. Its exports are abi/wasm.zig's (`memory`, /// `_initialize`, `reserve`, `release`, `patch`, and with a language -/// `highlight`), the same in every module; docs/typ/building.typ has their +/// `highlight` and `patchFile`), the same in every module; docs/typ/building.typ has their /// layouts and the WASI imports a page stubs. The package's dependency on /// pardes is named "pardes". A name the manifest does not have fails the /// configure, as `.languages` does. @@ -429,7 +429,7 @@ pub fn wasmModule(b: *std.Build, options: struct { else b.dependency("pardes", .{ .target = target, .optimize = options.optimize, .grammars = .none }); const exports = b.addOptions(); - exports.addOption(bool, "highlight", options.language != null); + exports.addOption(bool, "language", options.language != null); const module = b.addExecutable(.{ .name = b.fmt("pardes-{s}", .{options.language orelse "patch"}), .root_module = b.createModule(.{ @@ -445,7 +445,7 @@ pub fn wasmModule(b: *std.Build, options: struct { module.entry = .disabled; module.wasi_exec_model = .reactor; // Its own functions alone: tree-sitter's and libc's stay inside. - module.root_module.export_symbol_names = if (options.language != null) &.{ "reserve", "release", "patch", "highlight" } else &.{ "reserve", "release", "patch" }; + module.root_module.export_symbol_names = if (options.language != null) &.{ "reserve", "release", "patch", "highlight", "patchFile" } else &.{ "reserve", "release", "patch" }; return module; } diff --git a/docs/typ/building.typ b/docs/typ/building.typ index 69468042..9a0bab44 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -226,9 +226,25 @@ manifest does not have fails the configure. The module is [`memory`, `_initialize`], [its memory, and what a reactor runs once before any other call], [`reserve(len)`, `release(ptr, len)`], [`len` bytes for the page to write an argument into (at least one, so `reserve(0)` is a pointer too), and their return], [`highlight(path, path_len, text, len)`], [(a module with a language) a style byte for each byte of `text`: 0 none, 1 keyword, 2 string, 3 number, 4 comment, as pardes colours the file `path` names by its extension; all 0 for a path of another language. The module's until the next `highlight`], - [`patch(text, len)`], [the diff `text` stacked as the patch view shows it, with no file and no colours: a pointer to a header. The module's until the next `patch`], + [`patch(text, len)`], [the diff `text` stacked as the patch view shows it, with no file and no colours: a pointer to a header. The module's until the next `patch` or `patchFile`], + [`patchFile(text, len, file, file_len)`], [(a module with a language) `patch` with `file`, the text of the file the diff's first section names, the new one's or the old one's: each hunk is found in it and stacks under the declarations it sits in, from the file's parse (`declaration` rows), as the editor's +Diff does; a hunk the text does not hold, and the diff's other files, stack as with no file. The same header and records as `patch`], ) +xit writes its hunks' `@@` with nothing after it; with the file's text a +hunk still gets what it sits in. A page holding a commit's diff of +`src/lib.rs` and the file's new text calls (`put` copies bytes into a +`reserve`d buffer and gives the pointer and length): + +``` +const [d, dn] = put(diffBytes), [f, fn] = put(fileBytes); +const header = rust.exports.patchFile(d, dn, f, fn); // as patch's +``` + +and reads the rows as it reads `patch`'s. With the new text the labels and +declarations are numbered as the new file is; with the old text, as the +old one. Its cost is the file's parse: 18 ms against `patch`'s 4 for a +28-hunk diff of a 1,100-line Zig file, under node. + Each returns 0 when its memory runs out. Pointers and lengths are wasm32's: unsigned 32-bit; in memory they are little-endian. `patch`'s header is five u32, 20 bytes: @@ -242,7 +258,7 @@ header is five u32, 20 bytes: and a row's record: #pairs( - [0 `kind`], [0 other, 1 meta, 2 hunk, 3 declaration (git's words after the `@@`), 4 context, 5 added, 6 removed], + [0 `kind`], [0 other, 1 meta, 2 hunk, 3 declaration (git's words after the `@@`; with `patchFile`, a declaration's `path:line`, whose `code_start` is its whole length, and its header's lines), 4 context, 5 added, 6 removed], [4 `old`, 8 `new`], [its numbers in the old and the new file, from 1; 0 where it has none], [12 `code_start`], [where its code starts in its text: after a hunk label's `path:line` and stars], [16 `text_offset`, 20 `text_len`], [its text, without its line end (the `\n`, and a `\r` before it), in the header's `text`], diff --git a/test/consumer/wasm.mjs b/test/consumer/wasm.mjs index 9e76af0f..97b0ba6b 100644 --- a/test/consumer/wasm.mjs +++ b/test/consumer/wasm.mjs @@ -56,6 +56,20 @@ function patch(m, diff) { const [p, n] = put(m, diff); const h = m.ex.patch(p, n); m.ex.release(p, n); + return read(m, h); +} + +// patchFile: the same, with the text of the file the diff's first section names +function patchFile(m, diff, text) { + const [p, n] = put(m, diff); + const [t, tn] = put(m, text); + const h = m.ex.patchFile(p, n, t, tn); + m.ex.release(p, n); + m.ex.release(t, tn); + return read(m, h); +} + +function read(m, h) { const v = new DataView(m.mem()); const [text, textLen, styles, rows, count] = [0, 4, 8, 12, 16].map((o) => v.getUint32(h + o, true)); const bytes = new Uint8Array(m.mem(), text); @@ -109,5 +123,29 @@ for (const file of process.argv.slice(2)) { expect(`${file} highlight another language`, [...new Uint8Array(m.mem(), other, tn)].every((b) => b === 0), true); } } +// patchFile (a module with a language): xit's bare `@@`, and the file's +// text: the declaration the hunk sits in, from the file's own parse +const oldRs = "fn total(items: &[u32]) -> u32 {\n let mut sum = 0;\n let mut n = 0;\n for i in items { sum += i; }\n n += 1;\n sum\n}\n"; +const newRs = "// sums\n" + oldRs.replace("sum += i;", "sum += *i;"); +const bare = "diff --git a/src/lib.rs b/src/lib.rs\n--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -0,0 +1 @@\n+// sums\n@@ -3,3 +4,3 @@\n let mut n = 0;\n- for i in items { sum += i; }\n+ for i in items { sum += *i; }\n n += 1;\n"; +const more = bare + "diff --git a/src/b.rs b/src/b.rs\n--- a/src/b.rs\n+++ b/src/b.rs\n@@ -3,3 +4,3 @@\n let mut n = 0;\n- for i in items { sum += i; }\n+ for i in items { sum += *i; }\n n += 1;\n"; +const language = process.argv.slice(2).map(load).find((m) => m.ex.highlight); +if (language) { + const lines = (p) => p.rows.map((r) => [r.kind, r.text]); + const plain = lines(patch(language, bare)); + // the new file's text: its numbers + expect("patchFile new text", lines(patchFile(language, bare, newRs)), [ + ["hunk", "src/lib.rs:1"], ["added", "+// sums"], + ["declaration", "src/lib.rs:2"], ["declaration", " fn total(items: &[u32]) -> u32 {"], + ["hunk", "src/lib.rs:5 *"], ["context", " let mut n = 0;"], ["removed", "- for i in items { sum += i; }"], ["added", "+ for i in items { sum += *i; }"], ["context", " n += 1;"], + ]); + // the old file's: found on its side, and numbered so + expect("patchFile old text", lines(patchFile(language, bare, oldRs)).slice(2, 5), [["declaration", "src/lib.rs:1"], ["declaration", " fn total(items: &[u32]) -> u32 {"], ["hunk", "src/lib.rs:4 *"]]); + // a text the diff is not of: the rows as with none + expect("patchFile another text", lines(patchFile(language, bare, "nothing like it\n")), plain); + // a second file: as with no text + const two = lines(patchFile(language, more, newRs)); + expect("patchFile second file", two.slice(9), lines(patch(language, more)).slice(plain.length)); +} if (failed) process.exit(1); console.log("wasm: the modules run"); |
