summaryrefslogtreecommitdiff
path: root/src/lab.zig
diff options
context:
space:
mode:
authort <t@tjp.lol>2026-11-25 11:25:00 -0700
committert <t@tjp.lol>2026-11-25 11:25:13 -0700
commit205b1d26b5be40424eace945a73867bdfefe90ce (patch)
tree74f51369662d6d065b218f8ba32bf32760eb4596 /src/lab.zig
parentd85d5e98868a4ee0dda6d3faf666996cdb18f998 (diff)
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.
Diffstat (limited to 'src/lab.zig')
-rw-r--r--src/lab.zig30
1 files changed, 23 insertions, 7 deletions
diff --git a/src/lab.zig b/src/lab.zig
index 3750d80..84ab9d1 100644
--- a/src/lab.zig
+++ b/src/lab.zig
@@ -2,6 +2,7 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const mem = std.mem;
+const doc = @import("doc.zig");
const Json = std.json.Value;
pub const Pass = enum {
@@ -159,9 +160,11 @@ const Sentences = struct {
};
/// Pair with pass.prompt(). The same UTF-8 snapshot must be used for parse().
-/// Input is plain text with paragraphs on separate lines and Markdown # headings.
-pub fn question(arena: Allocator, pass: Pass, body: []const u8) ![]const u8 {
- if (!std.unicode.utf8ValidateSlice(body)) return error.InvalidUtf8;
+/// Input is plain text with paragraphs on separate lines and Markdown # headings. Its
+/// comments are nobody else's to read.
+pub fn question(arena: Allocator, pass: Pass, draft: []const u8) ![]const u8 {
+ if (!std.unicode.utf8ValidateSlice(draft)) return error.InvalidUtf8;
+ const body = try doc.withoutComments(arena, draft);
var buf: std.ArrayList(u8) = .empty;
var blocks: Blocks = .{ .lines = .{ .text = body } };
var n: usize = 0;
@@ -183,12 +186,14 @@ pub fn question(arena: Allocator, pass: Pass, body: []const u8) ![]const u8 {
return buf.toOwnedSlice(arena);
}
-/// `text` borrows body. The array and replacements live in arena, independent of answer.
-/// Invalid JSON is an error; invalid individual entries are ignored.
-pub fn parse(arena: Allocator, pass: Pass, body: []const u8, answer: []const u8) ![]Finding {
- if (!std.unicode.utf8ValidateSlice(body) or !std.unicode.utf8ValidateSlice(answer)) return error.InvalidUtf8;
+/// Everything returned lives in arena, independent of answer. A finding can run over a
+/// comment, which is blank in its text. Invalid JSON is an error; invalid individual entries
+/// are ignored.
+pub fn parse(arena: Allocator, pass: Pass, draft: []const u8, answer: []const u8) ![]Finding {
+ if (!std.unicode.utf8ValidateSlice(draft) or !std.unicode.utf8ValidateSlice(answer)) return error.InvalidUtf8;
const root = try std.json.parseFromSliceLeaky(Json, arena, answer, .{ .allocate = .alloc_always });
if (root != .array) return error.InvalidAnswer;
+ const body = try doc.withoutComments(arena, draft);
return if (pass.isTrim()) trims(arena, pass, body, root.array.items) else parseMarks(arena, pass, body, root.array.items);
}
@@ -581,6 +586,17 @@ test "Lab numbers the snapshot consistently and anchors only unique exact quotes
try std.testing.expect(findings[0].replacement == null and findings[1].replacement == null);
}
+test "Lab reads around comments, and its offsets still address the draft" {
+ var a = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer a.deinit();
+ const arena = a.allocator();
+ const body = "One <!-- sécret --> thing.\n\n<!--\nPrivate.\n-->\nLast fuzzy.";
+ try std.testing.expectEqualStrings("[1] One" ++ " " ** 18 ++ "thing.\n[2] Last fuzzy.\n", try question(arena, .hedges, body));
+ const found = try parse(arena, .hedges, body, "[{\"paragraph\":2,\"text\":\"fuzzy\"},{\"paragraph\":1,\"text\":\"cret\"}]");
+ try std.testing.expectEqual(@as(usize, 1), found.len);
+ try std.testing.expectEqual(mem.indexOf(u8, body, "fuzzy").?, found[0].at);
+}
+
test "Lab typo fixes reject rewrites, conflicts and line separators" {
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
defer a.deinit();