From ed0103e9738e3cf5a7c7a91aec5adf0be2fc409a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 21 Nov 2026 05:30:58 -0300 Subject: library: a patch row's text is its line without its end, said and tested: hunks.format trims a diff line's `\r` before its `\n` (and diff.Walk.step, and each of a source's lines, so the two compare) on purpose, and nothing a consumer reads said so (toilet saw a CRLF file's rows without their `\r`); the Patch doc comment, lib_syntax's, abi/wasm.zig's record and docs/typ/building.typ (the patch view, the record's text) say it now; test/consumer: a CRLF diff stacks to the LF diff's rows with no file, and with a CRLF main.c handed in its lines still match the diff's (its declaration found above `main.c:3 *`); no code changes Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- abi/wasm.zig | 4 ++-- core/hunks.zig | 5 ++++- core/lib_syntax.zig | 3 ++- docs/typ/building.typ | 7 +++++-- test/consumer/lib.zig | 25 +++++++++++++++++++++++++ 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/abi/wasm.zig b/abi/wasm.zig index d2b3889a..0c67e0da 100644 --- a/abi/wasm.zig +++ b/abi/wasm.zig @@ -87,8 +87,8 @@ pub const Record = extern struct { /// Where its code starts, a byte of its text: after a label's /// `path:line` and stars. code_start: u32, - /// Its text, without its newline, and its section's path: offsets into - /// the header's `text`. + /// Its text, without its line end (`\n`, and a `\r` before it), and its + /// section's path: offsets into the header's `text`. text_offset: u32, text_len: u32, path_offset: u32, diff --git a/core/hunks.zig b/core/hunks.zig index 98584980..bee06cc2 100644 --- a/core/hunks.zig +++ b/core/hunks.zig @@ -80,7 +80,10 @@ pub const Ctx = struct { pub const Source = struct { path: []const u8, bytes: []const u8 }; /// A diff stacked: its text, and a row for each of its lines (row i is -/// line i), the arena's; a row's path may be the diff's own bytes. +/// line i), the arena's; a row's path may be the diff's own bytes. A row's +/// text is its line without its end, the `\n` and a `\r` before it: a CRLF +/// diff's rows have no `\r` (one that must show it reads it off the diff), +/// and a source's lines are compared with the diff's so too. pub const Patch = struct { text: []const u8, rows: []const Row }; pub const Row = struct { diff --git a/core/lib_syntax.zig b/core/lib_syntax.zig index 0bb3f510..759720ec 100644 --- a/core/lib_syntax.zig +++ b/core/lib_syntax.zig @@ -72,7 +72,8 @@ pub const ground_line = syntax.ground_line; pub const Grounds = colors.Grounds; /// A unified diff stacked as pardes's patch view (its +Diff) shows it: -/// `formatPatch` gives a `Patch`, its text and a `PatchRow` a line: a +/// `formatPatch` gives a `Patch`, its text (each line without its end: no +/// `\r` from a CRLF diff) and a `PatchRow` a line: a /// hunk's `path:line`, the declarations it stands in (or git's function /// line), its lines with both their numbers, each byte's style (a `Syn` /// when `colors` are given, the grounds, the words a changed line changed). diff --git a/docs/typ/building.typ b/docs/typ/building.typ index f2b3289f..69468042 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -184,7 +184,10 @@ nothing of the editor: each hunk under its `path:line`, what it stands in above it, its lines behind their markers on their grounds, and the words a changed line changed emphasised (delta's way). It returns a `Patch`, its text and a `PatchRow` for each of its lines (row _i_ is line _i_), all in -the arena it is handed: +the arena it is handed. A row's text is its line without its end, the +`\n` and a `\r` before it: a CRLF diff's rows have no `\r` (a reader that +must show one reads it off the diff), and a file handed in as `source` is +compared line by line the same way, so a CRLF file matches its diff. #pairs( [`kind`], [`other` (a mailed patch's headers, message, diffstat), `meta` (a section's own words: a mode, a rename, `Binary files ... differ`), `hunk` (its label, a star a declaration it stands in), `declaration` (what it stands in: a declaration's `path:line` and header lines from the file, or git's own words after the `@@`), `context`, `added`, `removed`], @@ -242,7 +245,7 @@ and a row's record: [0 `kind`], [0 other, 1 meta, 2 hunk, 3 declaration (git's words after the `@@`), 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 newline, in the header's `text`], + [16 `text_offset`, 20 `text_len`], [its text, without its line end (the `\n`, and a `\r` before it), in the header's `text`], [24 `path_offset`, 28 `path_len`], [its section's path, in the header's `text` too], [32 `flags`], [bit 0: the diff has `\ No newline at end of file` after it], ) diff --git a/test/consumer/lib.zig b/test/consumer/lib.zig index fa0f8aee..ea0d56fd 100644 --- a/test/consumer/lib.zig +++ b/test/consumer/lib.zig @@ -254,3 +254,28 @@ test "a diff stacked with its file handed in: the declaration it stands in, from 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); } + +test "a row's text is its line without its end: a CRLF diff's rows have no \\r, and a CRLF file handed in still matches" { + const gpa = std.testing.allocator; + pardes.start(gpa); + defer pardes.stop(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const patch_crlf = try std.mem.replaceOwned(u8, a, patch_c, "\n", "\r\n"); + const Given = struct { + var text: []const u8 = ""; + fn source(_: *const pardes.PatchCtx, _: []const u8, path: []const u8) ?pardes.PatchSource { + return .{ .path = path, .bytes = text }; + } + }; + Given.text = try std.mem.replaceOwned(u8, a, main_c, "\n", "\r\n"); + // no file: the rows as an LF diff's + const plain = try pardes.formatPatch(&.{ .arena = a }, "", patch_crlf); + 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", plain.text); + try std.testing.expect(plain.rows[6].no_newline); + // a CRLF file: its lines compare with the diff's, so its declaration is found + const given = try pardes.formatPatch(&.{ .arena = a, .source = Given.source, .declarations = pardes.PatchCtx.parsed }, "", patch_crlf); + 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", given.text); + try std.testing.expectEqual(pardes.PatchRow.Kind.declaration, given.rows[0].kind); +} -- cgit v1.3