From 205b1d26b5be40424eace945a73867bdfefe90ce Mon Sep 17 00:00:00 2001 From: t Date: Wed, 25 Nov 2026 11:25:00 -0700 Subject: Anchor ghosts and alternatives to surrounding text, not byte offsets Ghosted text and alternatives are saved with context from the text around them, so an outside edit no longer sends the whole file to raw Markdown. A ghost with no clear place goes to Overflow. An alternatives group that cannot be matched is dropped; its text is not kept. HTML comments show dimmed in place, with their delimiters hidden until the caret enters them. Ctrl+/ on a comment uncomments it. Comments are left out of Lab and AI requests, except the one an alternatives target sits in. The metadata moves to format v3. Builds before this one show v3 files as raw Markdown, so install this build before saving. --- src/doc.zig | 481 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 426 insertions(+), 55 deletions(-) (limited to 'src/doc.zig') diff --git a/src/doc.zig b/src/doc.zig index 2320a6a..1964159 100644 --- a/src/doc.zig +++ b/src/doc.zig @@ -160,12 +160,19 @@ pub fn suggestion(line: []const u8) ?[]const u8 { // --- The file ------------------------------------------------------------------------------- // -// v2 puts optional Overflow and write_away comments before the visible Markdown. One LF +// v3 puts optional Overflow and write_away comments before the visible Markdown. One LF // separates the comments when both are present; two LFs separate the last comment from // the exact visible bytes. YAML frontmatter is those bytes' one exception: it stays on the // file's first line and the comments follow it. Legacy comments were trailers. Offsets -// always address the full editor body, so v2 ghosts must be restored before the -// alternatives are reanchored. +// always address the full editor body, so ghosts must be restored before the alternatives +// are reanchored. +// +// The file may have been edited elsewhere. A ghost's text is not among the visible bytes, so +// it is found by the text that was on either side of it: see `Near`. One with no clear +// place goes to Overflow and is a ghost no more. v2 had only the offsets, and a digest of the +// visible bytes to say whether they still held. A group's showing +// version is there to be found, but may be there twice, so its surroundings tell it apart in +// the same way. One with no clear place is dropped. const overflow_mark = ""; // legacy const overflow_open = " leaked)-->\n\nBody\n", "\n\n\nBody\n", "\n\n\nBody\n", - "\n\n\nBody\n", + "\n\n\nBody\n", "Body\n\n\n\n\n", "Text.\n\n\n", }) |bad| { @@ -603,7 +822,7 @@ test "v1 ghosts require an exact or unique anchor" { try t.expectEqual(4, exact.ghosts[0].at); } -test "v1 and v2 groups preserve all raw metadata if any record cannot be anchored" { +test "groups without surroundings need an exact or unique anchor, or are dropped" { const t = std.testing; var state: std.heap.ArenaAllocator = .init(t.allocator); defer state.deinit(); @@ -615,9 +834,20 @@ test "v1 and v2 groups preserve all raw metadata if any record cannot be anchore "{\"at\":0,\"variants\":[\"ana\",\"other\"]}", // Two overlapping matches. "{\"at\":5,\"variants\":[\"ana\"]}", // Not a persistent group. "{\"at\":5,\"variants\":[\"ana\",\"other\"],\"active\":2}", - }) |bad| { - try expectRaw(a, try mem.concat(a, u8, &.{ body, "\n\n\nNotes\n", meta_open, "{\"v\":1,", first_group, bad, "]}", meta_close })); - try expectRaw(a, try mem.concat(a, u8, &.{ "\n", meta_open, "{\"v\":2,", first_group, bad, "]}", meta_close, framing, body })); + }, [_]bool{ true, true, false, false }) |bad, sensible| { + for ([_][]const u8{ + try mem.concat(a, u8, &.{ body, "\n\n\nNotes\n", meta_open, "{\"v\":1,", first_group, bad, "]}", meta_close }), + try mem.concat(a, u8, &.{ "\n", meta_open, "{\"v\":2,", first_group, bad, "]}", meta_close, framing, body }), + }) |bytes| { + if (!sensible) { // not something an edit elsewhere could do + try expectRaw(a, bytes); + continue; + } + const f = try parse(a, bytes); + try t.expectEqualStrings(body, f.body); + try t.expectEqualStrings("Notes", f.overflow); + try t.expect(f.groups.len == 1 and !f.displaced); + } } const record = "{\"at\":5,\"variants\":[\"ana\",\"other\"]}]"; @@ -630,7 +860,7 @@ test "v1 and v2 groups preserve all raw metadata if any record cannot be anchore } } -test "v2 hides ghosts and rejects edits or invalid ghost positions without losing the source" { +test "ghosts are hidden, and found again after edits elsewhere or left in Overflow" { const t = std.testing; var arena_state: std.heap.ArenaAllocator = .init(t.allocator); defer arena_state.deinit(); @@ -646,7 +876,7 @@ test "v2 hides ghosts and rejects edits or invalid ghost positions without losin }; const bytes = try serialize(arena, file); const overflow = splitComment(bytes, overflow_open, overflow_close).?; - try t.expect(mem.startsWith(u8, overflow.rest, "\n" ++ meta_open ++ "{\"v\":2,")); + try t.expect(mem.startsWith(u8, overflow.rest, "\n" ++ meta_open ++ "{\"v\":3,")); const block = splitComment(overflow.rest[1..], meta_open, meta_close).?; try t.expectEqualStrings("\n\n# Title\n\nThe tension is real. ", block.rest); try t.expect(mem.find(u8, bytes, "a -->") == null); @@ -657,24 +887,117 @@ test "v2 hides ghosts and rejects edits or invalid ghost positions without losin try t.expectEqualStrings("a --> b", back.groups[0].variants[2]); try t.expectEqual(30, back.ghosts[0].at); - // Even a same-length edit with all offsets still valid cannot relocate a ghost. - for ([_][]const u8{ - try mem.replaceOwned(u8, arena, bytes, "# Title", "# A Longer Title"), - try mem.replaceOwned(u8, arena, bytes, "# Title", "# Other"), - try mem.concat(arena, u8, &.{ bytes, "\n" }), - try mem.replaceOwned(u8, arena, bytes, "\"at\":30", "\"at\":3000"), - "\n\nBody", - }) |edited| { - const f = try parse(arena, edited); - try t.expectEqualStrings(edited, f.body); - try t.expectEqual(0, f.ghosts.len); - try t.expectEqual(0, f.groups.len); - try t.expectEqualStrings("", f.overflow); - try t.expectEqualStrings(edited, try serialize(arena, f)); + try t.expect(!back.displaced); + try t.expectEqualStrings(bytes, try serialize(arena, back)); + // A ghost with no text around it on record, and nothing to vouch for its offset. + try expectRaw(arena, "\n\nBody"); + + // [was] is a ghost, and a group. What the visible text became, and how that opens. + const cases = [_][3][]const u8{ + // Edits before it, after it, and on one side right up against it. + .{ "# Title\n\nThe tension is real.[ Maybe cut this.]", "# A longer title\n\nThe tension is real.", "# A longer title\n\nThe tension is real.[ Maybe cut this.]" }, + .{ "# Title\n\nThe tension is real.[ Maybe cut this.]", "# Title\n\nThe tension is real. And more.", "# Title\n\nThe tension is real.[ Maybe cut this.] And more." }, + .{ "One thing. [Cut. ]The rest of it goes on from there.", "Changed! The rest of it goes on from there.", "Changed! [Cut. ]The rest of it goes on from there." }, + // New text at the very spot stands after it. + .{ "The tension is real.[ Cut.] The rest goes on.", "The tension is real. New. The rest goes on.", "The tension is real.[ Cut.] New. The rest goes on." }, + // The same text twice: only an offset that still holds can tell which. + .{ "Same line here.\nSame line here.[ Cut.]\nSame line here.\n", "Same line here.\nSame line here.\nSame line here.\n", "Same line here.\nSame line here.[ Cut.]\nSame line here.\n" }, + .{ "echo echo echo [Cut]echo echo echo echo", "echo echo echo echo echo echo echo echo", "echo echo echo [Cut]echo echo echo echo echo" }, + .{ "a\nA line.\nA line.[ Cut.]\nA line.\nA line.\nA line.\nA line.\nA line.\n", "abc\nA line.\nA line.\nA line.\nA line.\nA line.\nA line.\nA line.\n", "abc\nA line.\nA line.\nA line.\nA line.\nA line.\nA line.\nA line.\n| Cut." }, + // Both sides rewritten, or changed about. + .{ "The tension is real.[ Cut.] And so on.", "Nothing like it.", "Nothing like it.| Cut." }, + .{ "First this came.[ Cut.]Then that went.", "Then that went. First this came.", "Then that went. First this came.| Cut." }, + .{ "[All of it.]", "New.", "New.|All of it." }, + // Each goes its own way, and those side by side stay in order. UTF-8 is kept whole. + .{ "終終終終終終終終終終終終é[👻][ a]é終終終終終終終終終終終終. Then [gone]. The end is near now.", "New 終終終終終終終終終終終終éé終終終終終終終終終終終終.", "New 終終終終終終終終終終終終é[👻][ a]é終終終終終終終終終終終終.|gone" }, + // They follow their text past one another. + .{ "First paragraph is here.[ One.]\n\nSecond paragraph follows.[ Two.]\n\nThird.", "Second paragraph follows.\n\nFirst paragraph is here.\n\nThird.", "Second paragraph follows.[ Two.]\n\nFirst paragraph is here.[ One.]\n\nThird." }, + }; + for (cases) |c| { + const saved = try serialize(arena, try bracketed(arena, c[0])); + const visible = splitComment(saved, meta_open, meta_close).?.rest; + const f = try parse(arena, try mem.concat(arena, u8, &.{ saved[0 .. saved.len - visible.len], framing, c[1] })); + var want = mem.splitScalar(u8, c[2], '|'); + const opened = try bracketed(arena, want.first()); + try t.expectEqualStrings(opened.body, f.body); + try t.expectEqualStrings(want.next() orelse "", f.overflow); + try t.expectEqual(f.overflow.len != 0, f.displaced); + try t.expectEqual(opened.ghosts.len, f.ghosts.len); + for (opened.ghosts, f.ghosts) |a, b| { + try t.expectEqual(a.at, b.at); + try t.expectEqualStrings(a.text, b.text); + } + // What is saved next opens the same, with nothing left to place. + const again = try parse(arena, try serialize(arena, f)); + try t.expectEqualStrings(f.body, again.body); + try t.expectEqualStrings(f.overflow, again.overflow); + try t.expectEqual(f.ghosts.len, again.ghosts.len); + try t.expect(!again.displaced); } + + // Text already in Overflow stays first. Once there, a ghost does not come back with its place. + const gone = try parse(arena, try mem.replaceOwned(u8, arena, bytes, "\n\nThe tension is real. ", "\n\nNo tension at all.")); + try t.expectEqualStrings("Kept for later.\n\nMaybe cut this.", gone.overflow); + try t.expectEqual(1, gone.groups.len); + const stowed = try mem.replaceOwned(u8, arena, try serialize(arena, gone), "\n\nNo tension at all.", "\n\nThe tension is real. "); + const back_again = try parse(arena, stowed); + try t.expectEqualStrings("Kept for later.\n\nMaybe cut this.", back_again.overflow); + try t.expectEqual(0, back_again.ghosts.len); } -test "v2 restores adjacent UTF-8 ghosts and overlapping groups after an editor edit" { +/// A file whose ghosts are the bracketed parts of `text`. +fn bracketed(arena: Allocator, text: []const u8) !File { + var body: std.ArrayList(u8) = .empty; + var ghosts: std.ArrayList(GhostRec) = .empty; + var parts = mem.splitScalar(u8, text, '['); + try body.appendSlice(arena, parts.first()); + while (parts.next()) |part| { + const close = mem.findScalar(u8, part, ']').?; + try ghosts.append(arena, .{ .at = body.items.len, .text = part[0..close] }); + try body.appendSlice(arena, part[0..close]); + try body.appendSlice(arena, part[close + 1 ..]); + } + return .{ .body = body.items, .ghosts = ghosts.items }; +} + +test "alternatives go where their ghost goes" { + const t = std.testing; + var state: std.heap.ArenaAllocator = .init(t.allocator); + defer state.deinit(); + const a = state.allocator(); + const group: GroupRec = .{ .at = 5, .variants = &.{ "echo", "other" }, .ai = &.{"other"} }; + const saved = try serialize(a, .{ .body = "echo echo and then the end.", .groups = &.{group}, .ghosts = &.{.{ .at = 5, .text = "echo" }} }); + // The first "echo" reads the same, but it is not the one. + const moved = try parse(a, try mem.replaceOwned(u8, a, saved, "\n\necho and", "\n\nAn echo and")); + try t.expectEqualStrings("An echo echo and then the end.", moved.body); + try t.expectEqual(8, moved.groups[0].at); + const lost = try parse(a, try mem.replaceOwned(u8, a, saved, "\n\necho and then the end.", "\n\necho only")); + try t.expectEqualStrings("echo only", lost.body); + try t.expectEqual(0, lost.groups.len); + try t.expectEqualStrings("echo", lost.overflow); +} + +test "v2 offsets hold while the visible bytes do; after that its ghosts are in Overflow" { + const t = std.testing; + var state: std.heap.ArenaAllocator = .init(t.allocator); + defer state.deinit(); + const a = state.allocator(); + const visible = "# A draft\n\nThe tension is real.\n"; + for ([_]usize{ 31, 3000 }, [_][]const u8{ "# A draft\n\nThe tension is real. Cut this.\n", visible }, [_][]const u8{ "", " Cut this." }) |at, body, over| { + const old = try mem.concat(a, u8, &.{ try std.fmt.allocPrint(a, "{s}{{\"v\":2,\"groups\":[{{\"at\":15,\"variants\":[\"tension\",\"strain\"]}}],\"ghosts\":[{{\"at\":{d},\"text\":\" Cut this.\"}}],\"visible_sha256\":\"{s}\"}}{s}", .{ meta_open, at, &digest(visible), meta_close }), framing, visible }); + const f = try parse(a, old); + try t.expectEqualStrings(body, f.body); + try t.expectEqualStrings(over, f.overflow); + try t.expectEqual(1, f.groups.len); + const edited = try parse(a, try mem.replaceOwned(u8, a, old, "# A draft", "# My draft")); + try t.expectEqualStrings("# My draft\n\nThe tension is real.\n", edited.body); + try t.expectEqualStrings(" Cut this.", edited.overflow); + try t.expectEqual(16, edited.groups[0].at); + try t.expect(edited.displaced); + } +} + +test "restores adjacent UTF-8 ghosts and overlapping groups after an editor edit" { const t = std.testing; var state: std.heap.ArenaAllocator = .init(t.allocator); defer state.deinit(); @@ -803,8 +1126,56 @@ test "plain Markdown is byte-exact; v1 ghosts and ghost-free v2 groups still fol const moved = try parse(a, edited); try t.expectEqualStrings("Some tension is real.\r\n\n", moved.body); try t.expectEqual(5, moved.groups[0].at); - const gone = try mem.replaceOwned(u8, a, no_ghosts, "The tension is", "The strain is"); - try expectRaw(a, gone); + const gone = try parse(a, try mem.replaceOwned(u8, a, no_ghosts, "\n\nThe tension is", "\n\nThe strain is")); + try t.expectEqualStrings("The strain is real.\r\n\n", gone.body); + try t.expect(gone.groups.len == 0 and gone.overflow.len == 0 and !gone.displaced); + try t.expectEqualStrings(gone.body, try serialize(a, gone)); +} + +test "a group on a word that is there twice keeps its own, or is dropped" { + const t = std.testing; + var state: std.heap.ArenaAllocator = .init(t.allocator); + defer state.deinit(); + const a = state.allocator(); + // is a group with "other". What the text became, and how that opens. + const cases = [_][3][]const u8{ + .{ "The end of the story is the .", "So, the end of the story is the end.", "So, the end of the story is the ." }, + .{ "The of the story is the end.", "So, the end of the tale is the end.", "So, the of the tale is the end." }, + .{ "The of the story is the end.", "At the end of a story comes the end.", "At the of a story comes the end." }, + // Its own was rewritten, and the other is not it. + .{ "The of the story is the end.", "The close of the story is the end.", "The close of the story is the end." }, + .{ "One here.", "One more tension there, and a tension here.", "One more tension there, and a here." }, + .{ "", "More. All of it.", "More. " }, + .{ "", "None.", "None." }, + }; + for (cases) |c| { + const saved = try serialize(a, try angled(a, c[0])); + const was = splitComment(saved, meta_open, meta_close).?.rest; + const f = try parse(a, try mem.concat(a, u8, &.{ saved[0 .. saved.len - was.len], framing, c[1] })); + const want = try angled(a, c[2]); + try t.expectEqualStrings(want.body, f.body); + try t.expectEqual(want.groups.len, f.groups.len); + for (want.groups, f.groups) |x, y| try t.expectEqual(x.at, y.at); + try t.expect(f.overflow.len == 0 and !f.displaced); + } + + // Two on the same word, one of which is gone, do not share the other. + const two = try serialize(a, .{ .body = "echo echo", .groups = &.{ + .{ .at = 0, .variants = &.{ "echo", "first" } }, + .{ .at = 5, .variants = &.{ "echo", "second" } }, + } }); + const f = try parse(a, try mem.replaceOwned(u8, a, two, "\n\necho echo", "\n\necho")); + try t.expect(f.groups.len < 2); +} + +/// A file in which the part of `text` in angle brackets is a group. +fn angled(arena: Allocator, text: []const u8) !File { + const lo = mem.findScalar(u8, text, '<') orelse return .{ .body = text }; + const hi = mem.findScalar(u8, text, '>').?; + const groups = try arena.alloc(GroupRec, 1); + const variants = try arena.dupe([]const u8, &.{ text[lo + 1 .. hi], "other" }); + groups[0] = .{ .at = lo, .variants = variants }; + return .{ .body = try mem.concat(arena, u8, &.{ text[0..lo], variants[0], text[hi + 1 ..] }), .groups = groups }; } test "variants and text helpers" { -- cgit v1.3