summaryrefslogtreecommitdiff
path: root/test/lib/editor.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 06:01:44 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit56274a68fb7a418625f01d479ced9fbfc19383f2 (patch)
tree09caef7ced4ce0c39da9c464f65289ae5e35b3d6 /test/lib/editor.zig
parent82d998af568caebd7fe28c4f6fb7feb1bfb4572f (diff)
downloadpardes-56274a68fb7a418625f01d479ced9fbfc19383f2.tar.gz
pardes-56274a68fb7a418625f01d479ced9fbfc19383f2.zip
lib: the text pane as a library, `pardes-editor` (docs/next-editor.md, R5): src/lib_editor.zig is an editor a program keeps as plain data (`Editor(capacity)`: a gap buffer for the text, Text's cursor fields by name, two byte rings of whole-text snapshots for undo and redo, registers as records; no pointer, no allocator), thawed into a Text and Registers in the allocator a call is handed, run through normal.zig and edit.zig as a pane's keys run, and frozen back, a key that does not fit refused whole (`key`, `run`, `selectRegex`, `setText`, `setRegister`; `Outcome` done, pending, unbound, action, refused), and `Syntax`, a parse derived from a grammar handed in; the seam is `pardes_config.lib`, true in the library's build alone: Text.pane is null at compile time there, a body's characters and undo are its own (flatSurface, editText, setEditTextSpan, pushUndo, undoStep), bodyFile and filePane are none, Esc and `*` do the text's part only and `+` is a plain register, and config.zig's check of the builtins' leader paths is the editor's; normal.textKey is the mode dispatch keys.zig and the library share, handleNormal hands back the parse and runAction runs an action across the ranges, and applySelRegex's search over the ranges is selectByRegex, which the library calls with a pattern; build.zig: `lib` in the options, mvzr's patch and uucode as functions (patchedMvzr, uucodeModule) the editor's module takes too, editorModule, and `library` exports `pardes-editor` to a package that asks (`.editor = true`: it fetches uucode, mvzr and vaxis, of which it compiles the grapheme width), so a pardes-syntax consumer fetches what it did; test/lib: a value typed into, copied elsewhere and its memory gone, types on equal to one never moved, undo past the ring and redo back, a text that does not fit refused with the value unchanged, an action handed back and a regex done by the caller, a language handed in parsed and coloured; core-test 811 of 950, shell-test 990 of 993, hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, replicas pass, lib-test 8 of 8
Diffstat (limited to 'test/lib/editor.zig')
-rw-r--r--test/lib/editor.zig144
1 files changed, 144 insertions, 0 deletions
diff --git a/test/lib/editor.zig b/test/lib/editor.zig
new file mode 100644
index 00000000..412e5109
--- /dev/null
+++ b/test/lib/editor.zig
@@ -0,0 +1,144 @@
+//! The editor as a package that depends on pardes takes it
+//! (`pardes-editor`): a value of plain data a program keeps, typed into a
+//! key at a time with an arena each call, copied anywhere, refused whole at
+//! its capacities (docs/next-editor.md).
+const std = @import("std");
+const pe = @import("pardes-editor");
+
+const Ed = pe.Editor(.{ .text = 4096, .undo = 8192, .registers = 1024, .selections = 16 });
+
+/// Keys in helix's notation, the few these tests type: printables, and
+/// `<esc>`, `<ret>`, `<bs>`.
+fn typeKeys(ed: anytype, gpa: std.mem.Allocator, keys: []const u8) !void {
+ var i: usize = 0;
+ while (i < keys.len) {
+ var k: pe.Key = .{ .cp = keys[i], .text = keys[i .. i + 1] };
+ inline for (.{ .{ "<esc>", pe.Key.escape }, .{ "<ret>", pe.Key.enter }, .{ "<bs>", pe.Key.backspace } }) |special| {
+ if (std.mem.startsWith(u8, keys[i..], special[0])) k = .{ .cp = special[1] };
+ }
+ i += if (k.text.len == 0) std.mem.indexOfScalarPos(u8, keys, i, '>').? + 1 - i else 1;
+ // every call its own arena, gone after it: nothing of a key outlives it
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ switch (ed.key(arena.allocator(), k)) {
+ .refused => return error.Refused,
+ else => {},
+ }
+ }
+}
+
+fn text(ed: anytype, buf: []u8) []const u8 {
+ return ed.read(buf);
+}
+
+const first = "ifn main() {<ret> let x = 1;<ret>}<esc>ggxyp" ++ "wwCcfoo<esc>";
+const second = "%dpuuUjjAbar<esc>" ++ "ggiq<esc>u";
+
+test "a value copied elsewhere, its old memory gone, types on as one never moved" {
+ const gpa = std.testing.allocator;
+ const still = try gpa.create(Ed);
+ defer gpa.destroy(still);
+ still.* = .{};
+ try typeKeys(still, gpa, first);
+ try typeKeys(still, gpa, second);
+
+ const before = try gpa.create(Ed);
+ before.* = .{};
+ try typeKeys(before, gpa, first);
+ const moved = try gpa.create(Ed);
+ defer gpa.destroy(moved);
+ @memcpy(std.mem.asBytes(moved), std.mem.asBytes(before));
+ @memset(std.mem.asBytes(before), 0xAA);
+ gpa.destroy(before);
+ try typeKeys(moved, gpa, second);
+
+ try std.testing.expectEqualSlices(u8, std.mem.asBytes(still), std.mem.asBytes(moved));
+ var buf: [4096]u8 = undefined;
+ // `%d` then `p`, both undone, the delete redone: `A` types into nothing
+ try std.testing.expectEqualStrings("bar", text(moved, &buf));
+}
+
+test "undo goes back as far as the ring holds, and redo all the way again" {
+ const gpa = std.testing.allocator;
+ const ed = try gpa.create(Ed);
+ defer gpa.destroy(ed);
+ ed.* = .{};
+ const xs: [1000]u8 = @splat('x');
+ try ed.load(&xs);
+ // thirty edits, each a snapshot of a thousand bytes: an 8 KiB ring
+ // keeps the last few
+ for (0..30) |_| try typeKeys(ed, gpa, "ia<esc>");
+ var buf: [4096]u8 = undefined;
+ var last: [4096]u8 = undefined;
+ const final = text(ed, &last);
+ try std.testing.expectEqual(1030, final.len);
+ try std.testing.expect(ed.undo.count < 30);
+ const kept = ed.undo.count;
+ for (0..40) |_| try typeKeys(ed, gpa, "u");
+ try std.testing.expectEqual(1030 - kept, text(ed, &buf).len);
+ for (0..40) |_| try typeKeys(ed, gpa, "U");
+ try std.testing.expectEqualStrings(final, text(ed, &buf));
+}
+
+test "a key whose text would not fit is refused, and the value is as it was" {
+ const gpa = std.testing.allocator;
+ const ed = try gpa.create(Ed);
+ defer gpa.destroy(ed);
+ ed.* = .{};
+ const ys: [5000]u8 = @splat('y');
+ try std.testing.expectError(error.TextFull, ed.load(&ys));
+ try ed.load(ys[0..4094]);
+ try typeKeys(ed, gpa, "iab");
+ const kept = try gpa.create(Ed);
+ defer gpa.destroy(kept);
+ kept.* = ed.*;
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ try std.testing.expectEqual(pe.Outcome{ .refused = .text_full }, ed.key(arena.allocator(), .{ .cp = 'c', .text = "c" }));
+ try std.testing.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed));
+}
+
+test "what is not the text's comes back, and the caller can do it" {
+ const gpa = std.testing.allocator;
+ const ed = try gpa.create(Ed);
+ defer gpa.destroy(ed);
+ ed.* = .{};
+ try ed.load("one two one\n");
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ try std.testing.expectEqual(pe.Role.command_line, ed.key(a, .{ .cp = ':', .text = ":" }).action.role);
+ _ = ed.key(a, .{ .cp = '%', .text = "%" });
+ try std.testing.expectEqual(pe.Role.select_regex, ed.key(a, .{ .cp = 's', .text = "s" }).action.role);
+ _ = ed.selectRegex(a, "one", .select);
+ var out: [pe.max_selections]pe.Selection = undefined;
+ const got = try ed.ranges(a, &out);
+ try std.testing.expectEqual(2, got.n);
+ try std.testing.expectEqual(pe.Selection{ .anchor = 8, .head = 11 }, out[1]);
+ try std.testing.expectEqual(pe.Mode.normal, ed.mode());
+ _ = ed.key(a, .{ .cp = 'g', .text = "g" });
+ try std.testing.expectEqual(pe.Role.goto_prefix, ed.pending().?);
+}
+
+extern fn tree_sitter_c() *const pe.syntax.ts.Language;
+
+test "a language handed in at run time parses and colours the text" {
+ const gpa = std.testing.allocator;
+ const ed = try gpa.create(Ed);
+ defer gpa.destroy(ed);
+ ed.* = .{};
+ try ed.load("int f() { return 1; }\n");
+ pe.syntax.start(gpa);
+ defer pe.syntax.stop();
+ const lang: pe.Language = .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .highlights = "\"return\" @keyword (number_literal) @number" };
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ var syn: pe.Syntax = .{};
+ defer syn.forget();
+ const tree = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree;
+ try std.testing.expectEqualStrings("translation_unit", tree.rootNode().kind());
+ const colors = try pe.Syntax.colors(arena.allocator(), ed, lang, 0, ed.len());
+ try std.testing.expectEqual(pe.syntax.Syn.keyword, colors[10]);
+ try std.testing.expectEqual(pe.syntax.Syn.number, colors[17]);
+ try std.testing.expectEqual(pe.syntax.Syn.none, colors[0]);
+}