summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 06:08:57 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitf923d0342f7fbbc965867c19e03cb6c94fcb7fd3 (patch)
tree5893aafa1253d4a49c61aab97fdacb9020cd8dbb
parent56274a68fb7a418625f01d479ced9fbfc19383f2 (diff)
downloadpardes-f923d0342f7fbbc965867c19e03cb6c94fcb7fd3.tar.gz
pardes-f923d0342f7fbbc965867c19e03cb6c94fcb7fd3.zip
hxlib: hxdiff's cases on the library, against helix's goldens and against the file pane (docs/next-editor.md, R6): test/hxlib.zig types each case into a `pardes-editor` value and prints its result as hxdiff prints a pane's, doing what an app does with the s, S, K and Alt-K it is handed back (the keys to <ret> its pattern, <esc> no answer) and printing a case that reaches any other action (a view, n and N, a macro, gw) as the caller's; hxdiff takes another driver's results (`--actual=`), and `zig build hxlib` (`-- --parity`) holds them to the goldens or the file pane; the key notation is test/hxkeys.zig, which both harnesses read; what the cases found, fixed: paneCursorLines gave the library's body no lines (mm, mi, ma, md, mr, insert-mode arrows), and the library's selectRegex keeps its pattern in `/` as pardes's prompt does (look.submitSearch); a page key in insert mode moves a view and is the caller's; test/lib's `zig build bench -- <KiB> <keys>`: a key's cost, the pages it changes, a parse; hxlib 537 of 658 compared with 0 mismatches (5 waived as for pardes, 121 the caller's), --parity 617 of 738 with 0 mismatches and 0 waived; hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, core-test 811 of 950, shell-test 990 of 993, replicas pass, lib-test pass
-rw-r--r--build.zig33
-rw-r--r--src/edit.zig2
-rw-r--r--src/lib_editor.zig6
-rw-r--r--test/hxdiff.zig109
-rw-r--r--test/hxkeys.zig80
-rw-r--r--test/hxlib.zig131
-rw-r--r--test/lib/bench.zig80
-rw-r--r--test/lib/build.zig10
8 files changed, 375 insertions, 76 deletions
diff --git a/build.zig b/build.zig
index 6a9d4d9f..063e364e 100644
--- a/build.zig
+++ b/build.zig
@@ -1269,6 +1269,39 @@ pub fn build(b: *std.Build) void {
const hxdiff_step = b.step("hxdiff", "compare Helix results (-- --strict cases reference [waivers]; cases alone emits; --parity: a pty pane against a file pane)");
hxdiff_step.dependOn(&run_hxdiff.step);
+ // The same cases on the library (src/lib_editor.zig): test/hxlib.zig
+ // types them into an editor value and prints its results, which
+ // hxdiff holds against helix's goldens, or with `-- --parity`
+ // against the file pane, so the library cannot drift from either.
+ const hxlib = b.addExecutable(.{
+ .name = "pardes-hxlib",
+ .root_module = b.createModule(.{
+ .target = target,
+ .optimize = optimize,
+ .root_source_file = b.path("test/hxlib.zig"),
+ .link_libc = true,
+ }),
+ });
+ hxlib.root_module.addImport("pardes-editor", editorModule(b, null, target, optimize, shell_cfg, .{
+ .themes = themes_mod,
+ .tree_sitter = ts_mod,
+ .ts_libs = ts_libs.items,
+ .ts_queries = ts_queries_opts,
+ .uucode = uucode_mod,
+ .mvzr = mvzr_mod,
+ .vaxis = vaxis_mod,
+ }));
+ const run_hxlib = b.addRunArtifact(hxlib);
+ run_hxlib.addArgs(&.{ "test/hxcases/cases.jsonl", "test/hxcases/parity.jsonl" });
+ run_hxlib.setCwd(b.path("."));
+ run_hxlib.has_side_effects = true;
+ const run_hxdiff_lib = b.addRunArtifact(hxdiff);
+ run_hxdiff_lib.has_side_effects = true;
+ run_hxdiff_lib.addPrefixedFileArg("--actual=", run_hxlib.captureStdOut(.{}));
+ run_hxdiff_lib.addPassthruArgs();
+ run_hxdiff_lib.setCwd(b.path("."));
+ b.step("hxlib", "hxdiff's cases on the library, against helix's goldens (-- --parity: against the file pane)").dependOn(&run_hxdiff_lib.step);
+
const helix_harness = b.option([]const u8, "helix-harness", "path to the reference hx-harness executable") orelse
b.graph.environ_map.get("HX_HARNESS") orelse "hx-harness";
const reference = b.addSystemCommand(&.{helix_harness});
diff --git a/src/edit.zig b/src/edit.zig
index 4a48cfd4..105b9abf 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -378,7 +378,7 @@ pub fn exitInsert(e: Editor, t: *Text) void {
pub fn paneCursorLines(e: Editor, t: *Text) Error![]const []const u8 {
const arena = e.scratch;
- if (t.what != .body) {
+ if (t.what != .body or comptime Editor.lib) {
const text = try flatSurface(e, t);
const lines = try arena.alloc([]const u8, std.mem.count(u8, text, "\n") + 1);
var it = std.mem.splitScalar(u8, text, '\n');
diff --git a/src/lib_editor.zig b/src/lib_editor.zig
index 5430285a..86e494e1 100644
--- a/src/lib_editor.zig
+++ b/src/lib_editor.zig
@@ -356,6 +356,12 @@ pub fn Editor(comptime cap: Capacity) type {
pub fn selectRegex(ed: *Self, a: std.mem.Allocator, pattern: []const u8, how: RegexMode) Outcome {
const live = ed.thaw(a) catch return .{ .refused = .out_of_memory };
const t = &live.text;
+ // as pardes's prompt answers it (look.submitSearch): helix
+ // keeps the pattern in `/`
+ if (pattern.len > 0) {
+ live.registers.put(a, '/', pattern, 0, 1, true) catch return .{ .refused = .out_of_memory };
+ live.registers.last_search = '/';
+ }
var snap: [max_selections]Selection = undefined;
const got = t.ranges(t.own.?, 0, &snap);
normal.selectByRegex(live.context(a), t, snap[0..got.n], got.pri, t.vsel.active and t.vsel.explicit, pattern, how);
diff --git a/test/hxdiff.zig b/test/hxdiff.zig
index ae73863e..f18f498b 100644
--- a/test/hxdiff.zig
+++ b/test/hxdiff.zig
@@ -3,6 +3,7 @@
// --strict checks complete corpus coverage. Waivers pin both sides of a known difference.
const std = @import("std");
const pardes = @import("pardes");
+const hxkeys = @import("hxkeys.zig");
// Its own root, so src/main.zig's logFn does not apply here: the core it
// drives feeds every case's text through ghostty-vt, which narrates each
@@ -36,6 +37,9 @@ pub fn main(init: std.process.Init) !void {
var verbose = false;
var strict = false;
var pane_override: ?[]const u8 = null;
+ // Another driver's results, a line a case (test/hxlib.zig, the
+ // library's): held against the goldens, or with --parity the file pane.
+ var actual_path: ?[]const u8 = null;
var goldens_path: ?[]const u8 = null;
var waivers_path: ?[]const u8 = null;
const paths = try arena.alloc([]const u8, @max(args.len - 1, 3)); // room for the defaults below
@@ -56,6 +60,8 @@ pub fn main(init: std.process.Init) !void {
const kind = a["--pane=".len..];
if (!std.mem.eql(u8, kind, "file") and !std.mem.eql(u8, kind, "tty")) return error.UnknownPane;
pane_override = kind;
+ } else if (std.mem.startsWith(u8, a, "--actual=")) {
+ actual_path = a["--actual=".len..];
} else if (std.mem.eql(u8, a, "--waivers")) {
if (waivers_path != null) return error.DuplicateWaiverPath;
want_waivers = true;
@@ -74,7 +80,8 @@ pub fn main(init: std.process.Init) !void {
&.{ "test/hxcases/cases.jsonl", "test/hxcases/parity.jsonl" }
else
&.{ "test/hxcases/cases.jsonl", "test/hxcases/goldens.jsonl", "test/hxcases/waivers.jsonl" };
- if (parity) waivers_path = waivers_path orelse "test/hxcases/parity-waivers.jsonl";
+ // the pty pane's differences from the file pane; the library has none
+ if (parity and actual_path == null) waivers_path = waivers_path orelse "test/hxcases/parity-waivers.jsonl";
@memcpy(paths[0..defaults.len], defaults);
n_paths = defaults.len;
}
@@ -135,6 +142,17 @@ pub fn main(init: std.process.Init) !void {
var n_cases: usize = 0;
var n_bad: usize = 0;
var n_waived: usize = 0;
+ var n_skipped: usize = 0;
+ var actuals: std.StringHashMap([]const u8) = .init(arena);
+ if (actual_path) |ap| {
+ const asrc = std.Io.Dir.cwd().readFileAlloc(init.io, ap, arena, .limited(256 * 1024 * 1024)) catch |e| fatal("read {s}: {s}", .{ ap, @errorName(e) });
+ var alines = std.mem.splitScalar(u8, asrc, '\n');
+ while (alines.next()) |line| {
+ if (line.len == 0) continue;
+ const named = std.json.parseFromSliceLeaky(struct { name: []const u8 }, arena, line, .{ .ignore_unknown_fields = true }) catch |e| fatal("{s}: bad result line: {s}", .{ ap, @errorName(e) });
+ try actuals.put(named.name, line);
+ }
+ }
var names: std.StringHashMap(void) = .init(arena);
for (case_paths) |path| {
const src = std.Io.Dir.cwd().readFileAlloc(init.io, path, arena, .limited(64 * 1024 * 1024)) catch |e| fatal("read {s}: {s}", .{ path, @errorName(e) });
@@ -155,7 +173,17 @@ pub fn main(init: std.process.Init) !void {
goldens.get(case.name) orelse fatal("no golden for case {s}", .{case.name})
else
null;
- const actual = try runCase(arena, case, if (parity) "tty" else pane_override orelse case.pane);
+ const actual = if (actual_path) |ap| other: {
+ const result = actuals.get(case.name) orelse fatal("{s}: no result for case {s}", .{ ap, case.name });
+ // a case the driver stopped in: where the app's part began
+ const skip = std.json.parseFromSliceLeaky(struct { skip: ?[]const u8 = null }, arena, result, .{ .ignore_unknown_fields = true }) catch fatal("{s}: bad result for {s}", .{ ap, case.name });
+ if (skip.skip) |why| {
+ n_skipped += 1;
+ if (verbose) std.debug.print("SKIPPED {s}: {s}\n", .{ case.name, why });
+ continue;
+ }
+ break :other std.json.parseFromSliceLeaky(Result, arena, result, .{}) catch fatal("{s}: bad result for {s}", .{ ap, case.name });
+ } else try runCase(arena, case, if (parity) "tty" else pane_override orelse case.pane);
try validateResult(actual);
const want = reference orelse {
const row = try std.json.Stringify.valueAlloc(arena, actual, .{ .emit_null_optional_fields = false });
@@ -165,7 +193,7 @@ pub fn main(init: std.process.Init) !void {
};
try validateResult(want);
const reference_name = if (parity) "file" else "helix";
- const actual_name = if (parity) "pty" else "pardes";
+ const actual_name = if (actual_path != null) "lib" else if (parity) "pty" else "pardes";
const difference = try diffResult(arena, want, actual, reference_name, actual_name);
if (difference.len == 0) {
if (strict and waivers.contains(case.name)) fatal("stale waiver: {s}", .{case.name});
@@ -194,7 +222,8 @@ pub fn main(init: std.process.Init) !void {
}
}
if (!parity and goldens_path == null) return;
- std.debug.print("{s}: {d} cases, {d} mismatches, {d} waived\n", .{ if (parity) "hxparity" else "hxdiff", n_cases, n_bad, n_waived });
+ std.debug.print("{s}{s}: {d} cases, {d} mismatches, {d} waived\n", .{ if (parity) "hxparity" else "hxdiff", if (actual_path != null) "-lib" else "", n_cases - n_skipped, n_bad, n_waived });
+ if (actual_path != null) std.debug.print("{d} cases the library hands its caller (an action not the text's), not compared\n", .{n_skipped});
if (n_bad > 0) std.process.exit(1);
}
@@ -347,77 +376,7 @@ fn runCase(arena: std.mem.Allocator, case: Case, pane_kind: []const u8) !Result
// keys: helix notation — printables verbatim, <...> specials
var ki: usize = 0;
- while (ki < case.keys.len) {
- var key: pardes.Key = .{ .cp = 0 };
- if (case.keys[ki] == '<') {
- const close = std.mem.indexOfScalarPos(u8, case.keys, ki, '>') orelse
- fatal("case {s}: unterminated < in keys", .{case.name});
- var name = case.keys[ki + 1 .. close];
- ki = close + 1;
- while (true) {
- if (std.mem.startsWith(u8, name, "C-")) {
- key.ctrl = true;
- name = name[2..];
- } else if (std.mem.startsWith(u8, name, "A-")) {
- key.alt = true;
- name = name[2..];
- } else if (std.mem.startsWith(u8, name, "S-")) {
- key.shift = true;
- name = name[2..];
- } else break;
- }
- if (name.len == 1 or (name.len > 1 and name.len <= 4 and name[0] >= 0x80)) {
- key.cp = std.unicode.utf8Decode(name) catch return error.InvalidKey;
- if (!key.ctrl and !key.alt) key.text = name;
- } else if (std.mem.eql(u8, name, "esc")) {
- key.cp = pardes.Key.escape;
- } else if (std.mem.eql(u8, name, "ret")) {
- key.cp = pardes.Key.enter;
- } else if (std.mem.eql(u8, name, "tab")) {
- key.cp = pardes.Key.tab;
- } else if (std.mem.eql(u8, name, "space")) {
- key.cp = ' ';
- if (!key.ctrl and !key.alt) key.text = " ";
- } else if (std.mem.eql(u8, name, "backspace")) {
- key.cp = pardes.Key.backspace;
- } else if (std.mem.eql(u8, name, "del")) {
- key.cp = pardes.Key.delete;
- } else if (std.mem.eql(u8, name, "lt")) {
- key.cp = '<';
- if (!key.ctrl and !key.alt) key.text = "<";
- } else if (std.mem.eql(u8, name, "gt")) {
- key.cp = '>';
- if (!key.ctrl and !key.alt) key.text = ">";
- } else if (std.mem.eql(u8, name, "minus")) {
- // helix spells Alt-'-' this way: a bare `-` after a modifier
- // is a parse error in its own macro notation
- key.cp = '-';
- if (!key.ctrl and !key.alt) key.text = "-";
- } else if (std.mem.eql(u8, name, "up")) {
- key.cp = pardes.Key.up;
- } else if (std.mem.eql(u8, name, "down")) {
- key.cp = pardes.Key.down;
- } else if (std.mem.eql(u8, name, "left")) {
- key.cp = pardes.Key.left;
- } else if (std.mem.eql(u8, name, "right")) {
- key.cp = pardes.Key.right;
- } else if (std.mem.eql(u8, name, "home")) {
- key.cp = pardes.Key.home;
- } else if (std.mem.eql(u8, name, "end")) {
- key.cp = pardes.Key.end;
- } else if (std.mem.eql(u8, name, "pageup")) {
- key.cp = pardes.Key.page_up;
- } else if (std.mem.eql(u8, name, "pagedown")) {
- key.cp = pardes.Key.page_down;
- } else fatal("case {s}: unknown key <{s}>", .{ case.name, name });
- } else {
- const len = std.unicode.utf8ByteSequenceLength(case.keys[ki]) catch return error.InvalidKey;
- if (len > case.keys.len - ki) return error.InvalidKey;
- const text = case.keys[ki..][0..len];
- key.cp = std.unicode.utf8Decode(text) catch return error.InvalidKey;
- key.text = text;
- ki += len;
- }
+ while (hxkeys.next(pardes.Key, case.keys, &ki) catch |err| fatal("case {s}: {s}", .{ case.name, @errorName(err) })) |key| {
core.update(.{ .key = key });
pump(core);
}
diff --git a/test/hxkeys.zig b/test/hxkeys.zig
new file mode 100644
index 00000000..c94e5900
--- /dev/null
+++ b/test/hxkeys.zig
@@ -0,0 +1,80 @@
+//! helix's key notation as hxdiff's cases spell it (test/hxcases): the
+//! next key of `keys` from `i.*`, null at the end; printables verbatim,
+//! `<...>` specials and modifiers. The core's harness (hxdiff.zig) and the
+//! library's (hxlib.zig) read the cases through it.
+const std = @import("std");
+
+pub fn next(comptime Key: type, keys: []const u8, i: *usize) !?Key {
+ if (i.* >= keys.len) return null;
+ var key: Key = .{ .cp = 0 };
+ if (keys[i.*] == '<') {
+ const close = std.mem.indexOfScalarPos(u8, keys, i.*, '>') orelse
+ return error.UnterminatedKey;
+ var name = keys[i.* + 1 .. close];
+ i.* = close + 1;
+ while (true) {
+ if (std.mem.startsWith(u8, name, "C-")) {
+ key.ctrl = true;
+ name = name[2..];
+ } else if (std.mem.startsWith(u8, name, "A-")) {
+ key.alt = true;
+ name = name[2..];
+ } else if (std.mem.startsWith(u8, name, "S-")) {
+ key.shift = true;
+ name = name[2..];
+ } else break;
+ }
+ if (name.len == 1 or (name.len > 1 and name.len <= 4 and name[0] >= 0x80)) {
+ key.cp = std.unicode.utf8Decode(name) catch return error.InvalidKey;
+ if (!key.ctrl and !key.alt) key.text = name;
+ } else if (std.mem.eql(u8, name, "esc")) {
+ key.cp = Key.escape;
+ } else if (std.mem.eql(u8, name, "ret")) {
+ key.cp = Key.enter;
+ } else if (std.mem.eql(u8, name, "tab")) {
+ key.cp = Key.tab;
+ } else if (std.mem.eql(u8, name, "space")) {
+ key.cp = ' ';
+ if (!key.ctrl and !key.alt) key.text = " ";
+ } else if (std.mem.eql(u8, name, "backspace")) {
+ key.cp = Key.backspace;
+ } else if (std.mem.eql(u8, name, "del")) {
+ key.cp = Key.delete;
+ } else if (std.mem.eql(u8, name, "lt")) {
+ key.cp = '<';
+ if (!key.ctrl and !key.alt) key.text = "<";
+ } else if (std.mem.eql(u8, name, "gt")) {
+ key.cp = '>';
+ if (!key.ctrl and !key.alt) key.text = ">";
+ } else if (std.mem.eql(u8, name, "minus")) {
+ // helix spells Alt-'-' this way: a bare `-` after a modifier
+ // is a parse error in its own macro notation
+ key.cp = '-';
+ if (!key.ctrl and !key.alt) key.text = "-";
+ } else if (std.mem.eql(u8, name, "up")) {
+ key.cp = Key.up;
+ } else if (std.mem.eql(u8, name, "down")) {
+ key.cp = Key.down;
+ } else if (std.mem.eql(u8, name, "left")) {
+ key.cp = Key.left;
+ } else if (std.mem.eql(u8, name, "right")) {
+ key.cp = Key.right;
+ } else if (std.mem.eql(u8, name, "home")) {
+ key.cp = Key.home;
+ } else if (std.mem.eql(u8, name, "end")) {
+ key.cp = Key.end;
+ } else if (std.mem.eql(u8, name, "pageup")) {
+ key.cp = Key.page_up;
+ } else if (std.mem.eql(u8, name, "pagedown")) {
+ key.cp = Key.page_down;
+ } else return error.UnknownKey;
+ } else {
+ const len = std.unicode.utf8ByteSequenceLength(keys[i.*]) catch return error.InvalidKey;
+ if (len > keys.len - i.*) return error.InvalidKey;
+ const text = keys[i.*..][0..len];
+ key.cp = std.unicode.utf8Decode(text) catch return error.InvalidKey;
+ key.text = text;
+ i.* += len;
+ }
+ return key;
+}
diff --git a/test/hxlib.zig b/test/hxlib.zig
new file mode 100644
index 00000000..423851d7
--- /dev/null
+++ b/test/hxlib.zig
@@ -0,0 +1,131 @@
+//! hxdiff's cases on the library (`pardes-editor`, src/lib_editor.zig):
+//! each case's keys typed into an editor value, its result printed as
+//! hxdiff prints a pane's, one line a case, for `hxdiff --actual` to hold
+//! against helix's goldens or the file pane (`zig build hxlib`). The
+//! library does what an app would with the actions it hands back that a
+//! case needs: s, S, K and Alt-K take the keys up to <ret> as their
+//! pattern. A case that reaches any other action (a view, `:`, `/`, LSP, a
+//! macro) is the app's from there: printed as skipped, with the role.
+const std = @import("std");
+const pe = @import("pardes-editor");
+const hxkeys = @import("hxkeys.zig");
+
+pub const std_options: std.Options = .{ .log_level = .err, .logFn = logFn };
+
+fn logFn(comptime level: std.log.Level, comptime scope: @EnumLiteral(), comptime format: []const u8, args: anytype) void {
+ if (scope == .mvzr) return; // a pattern typed a key at a time, as hxdiff.zig says
+ std.log.defaultLog(level, scope, format, args);
+}
+
+const Ed = pe.Editor(.{ .text = 1 << 20, .undo = 4 << 20, .registers = 64 << 10, .selections = 1024 });
+
+const Case = struct { name: []const u8, pane: []const u8 = "file", path: ?[]const u8 = null, text: []const u8, keys: []const u8 };
+const Pos = struct { row: i32, col: i32 };
+const Sel = struct { cursor: Pos, anchor: Pos };
+const Result = struct {
+ name: []const u8,
+ text: []const u8,
+ mode: []const u8,
+ cursor: Pos,
+ anchor: Pos,
+ sels: ?[]const Sel = null,
+ primary: ?usize = null,
+};
+const Skip = struct { name: []const u8, skip: []const u8 };
+
+pub fn main(init: std.process.Init) !void {
+ const gpa = std.heap.page_allocator;
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
+ const ed = try gpa.create(Ed);
+ const stdout = std.Io.File.stdout();
+ for (args[1..]) |path| {
+ var file_arena = std.heap.ArenaAllocator.init(gpa);
+ defer file_arena.deinit();
+ const src = try std.Io.Dir.cwd().readFileAlloc(init.io, path, file_arena.allocator(), .limited(64 << 20));
+ var lines = std.mem.splitScalar(u8, src, '\n');
+ while (lines.next()) |raw| {
+ const line = std.mem.trim(u8, raw, " \t\r");
+ if (line.len == 0) continue;
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const case = try std.json.parseFromSliceLeaky(Case, a, line, .{ .ignore_unknown_fields = true });
+ const out = try runCase(a, ed, case);
+ try stdout.writeStreamingAll(init.io, out);
+ try stdout.writeStreamingAll(init.io, "\n");
+ }
+ }
+}
+
+fn runCase(a: std.mem.Allocator, ed: *Ed, case: Case) ![]u8 {
+ ed.* = .{};
+ try ed.load(case.text);
+ var i: usize = 0;
+ while (try hxkeys.next(pe.Key, case.keys, &i)) |k| {
+ // a page key in insert mode moves a view, which the library has not
+ if (ed.mode() == .insert and (k.cp == pe.Key.page_up or k.cp == pe.Key.page_down))
+ return std.json.Stringify.valueAlloc(a, Skip{ .name = case.name, .skip = "insert page key" }, .{});
+ var call = std.heap.ArenaAllocator.init(std.heap.page_allocator);
+ defer call.deinit();
+ switch (ed.key(call.allocator(), k)) {
+ .done, .pending, .unbound => {},
+ .refused => |why| return std.json.Stringify.valueAlloc(a, Skip{ .name = case.name, .skip = @tagName(why) }, .{}),
+ .action => |act| switch (act.role) {
+ inline .select_regex, .split_regex, .keep_regex, .remove_regex => |role| {
+ // the app's prompt: the keys to <ret> are the pattern
+ var pattern: std.ArrayList(u8) = .empty;
+ const answered = while (try hxkeys.next(pe.Key, case.keys, &i)) |p| {
+ if (p.cp == pe.Key.enter) break true;
+ if (p.cp == pe.Key.escape) break false;
+ if (p.cp == pe.Key.backspace) {
+ if (pattern.items.len > 0) pattern.items.len -= 1;
+ continue;
+ }
+ try pattern.appendSlice(a, p.text);
+ } else false;
+ if (!answered) continue;
+ const how = @field(pe.RegexMode, @tagName(role)[0 .. @tagName(role).len - "_regex".len]);
+ _ = ed.selectRegex(call.allocator(), pattern.items, how);
+ },
+ else => return std.json.Stringify.valueAlloc(a, Skip{ .name = case.name, .skip = @tagName(act.role) }, .{}),
+ },
+ }
+ }
+ const c = ed.cursor;
+ const anchor: Pos = if (c.vsel.active) .{ .row = c.vsel.row, .col = c.vsel.col } else .{ .row = c.cur_row, .col = c.cur_col };
+ // as hxdiff.zig's: the primary slotted among the others in document order
+ var sels: ?[]const Sel = null;
+ var primary: ?usize = null;
+ if (c.nsel > 0) {
+ const out = try a.alloc(Sel, @as(usize, c.nsel) + 1);
+ var n: usize = 0;
+ const prim: Sel = .{ .cursor = .{ .row = c.cur_row, .col = c.cur_col }, .anchor = anchor };
+ const pr_row = @min(prim.cursor.row, prim.anchor.row);
+ const pr_col = if (prim.cursor.row == prim.anchor.row) @min(prim.cursor.col, prim.anchor.col) else if (prim.cursor.row < prim.anchor.row) prim.cursor.col else prim.anchor.col;
+ for (ed.sels[0..c.nsel]) |s| {
+ const s_row = @min(s.row, s.arow);
+ const s_col = if (s.row == s.arow) @min(s.col, s.acol) else if (s.row < s.arow) s.col else s.acol;
+ if (primary == null and (pr_row < s_row or (pr_row == s_row and pr_col <= s_col))) {
+ primary = n;
+ out[n] = prim;
+ n += 1;
+ }
+ out[n] = .{ .cursor = .{ .row = s.row, .col = s.col }, .anchor = .{ .row = s.arow, .col = s.acol } };
+ n += 1;
+ }
+ if (primary == null) {
+ primary = n;
+ out[n] = prim;
+ }
+ sels = out;
+ }
+ return std.json.Stringify.valueAlloc(a, Result{
+ .name = case.name,
+ .text = try ed.bytes(a),
+ .mode = if (c.mode == .normal and c.select) "select" else @tagName(c.mode),
+ .cursor = .{ .row = c.cur_row, .col = c.cur_col },
+ .anchor = anchor,
+ .sels = sels,
+ .primary = primary,
+ }, .{ .emit_null_optional_fields = false });
+}
diff --git a/test/lib/bench.zig b/test/lib/bench.zig
new file mode 100644
index 00000000..f2127dc4
--- /dev/null
+++ b/test/lib/bench.zig
@@ -0,0 +1,80 @@
+//! What a key costs on the library (`zig build bench -- <KiB> <keys>`, Debug
+//! or -Doptimize): a text of that size in the C grammar's shape, a key
+//! typed into its middle `keys` times, each with an arena of its own; the
+//! 4 KiB pages of the value a typed character changes; a full parse of the
+//! text. `keys` 0 makes the text alone, to count what the keys add; a
+//! third argument, `pages`, counts the pages (a copy and a compare a key,
+//! which an instruction count of the keys leaves out).
+const std = @import("std");
+const pe = @import("pardes-editor");
+
+const Ed = pe.Editor(.{ .text = 128 * 1024, .undo = 512 * 1024 });
+
+extern fn tree_sitter_c() *const pe.syntax.ts.Language;
+
+fn now() u64 {
+ var ts: std.c.timespec = undefined;
+ _ = std.c.clock_gettime(.MONOTONIC, &ts);
+ return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec));
+}
+
+pub fn main(init: std.process.Init) !void {
+ const gpa = std.heap.page_allocator;
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
+ const kib = try std.fmt.parseInt(usize, args[1], 10);
+ const keys = try std.fmt.parseInt(usize, args[2], 10);
+ const count_pages = args.len > 3;
+ var text: std.ArrayList(u8) = .empty;
+ var i: usize = 0;
+ while (text.items.len < kib * 1024) : (i += 1) try text.print(gpa, "int f{d}(int x) {{ return x + {d}; }}\n", .{ i, i });
+ text.items.len = kib * 1024;
+ const ed = try gpa.create(Ed);
+ ed.* = .{};
+ try ed.load(text.items);
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ // to the middle, into insert mode
+ for ([_]pe.Key{ .{ .cp = '%', .text = "%" }, .{ .cp = ';', .text = ";" } }) |k| _ = ed.key(arena.allocator(), k);
+ var to_middle: [8]u8 = undefined;
+ for (std.fmt.bufPrint(&to_middle, "{d}G", .{text.items.len / 2 / 32}) catch unreachable) |c| _ = ed.key(arena.allocator(), .{ .cp = c, .text = &.{c} });
+ _ = ed.key(arena.allocator(), .{ .cp = 'i', .text = "i" });
+ _ = arena.reset(.free_all);
+ if (keys == 0) return;
+
+ const before = try gpa.create(Ed);
+ var pages_max: usize = 0;
+ var pages_sum: usize = 0;
+ var key_ns: u64 = 0;
+ for (0..keys) |n| {
+ if (count_pages) before.* = ed.*;
+ const t0 = now();
+ const out = ed.key(arena.allocator(), .{ .cp = 'a' + @as(u21, @intCast(n % 26)), .text = &.{'a' + @as(u8, @intCast(n % 26))} });
+ key_ns += now() - t0;
+ if (out != .done) return error.NotTyped;
+ _ = arena.reset(.retain_capacity);
+ if (!count_pages) continue;
+ const a = std.mem.asBytes(before);
+ const b = std.mem.asBytes(ed);
+ var pages: usize = 0;
+ var at: usize = 0;
+ while (at < a.len) : (at += 4096) {
+ const end = @min(at + 4096, a.len);
+ if (!std.mem.eql(u8, a[at..end], b[at..end])) pages += 1;
+ }
+ pages_max = @max(pages_max, pages);
+ pages_sum += pages;
+ }
+ const per_key = key_ns / keys;
+
+ pe.syntax.start(std.heap.smp_allocator); // tree-sitter mallocs: not a page each
+ const lang: pe.Language = .{ .name = "c-as-dsl", .ext = ".cdsl", .ts = tree_sitter_c() };
+ var syn: pe.Syntax = .{};
+ // the parser and its queries made once, then the parse alone timed
+ _ = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree;
+ syn.tree.?.destroy();
+ syn.tree = null;
+ const p0 = now();
+ _ = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree;
+ const parse = now() - p0;
+ std.debug.print("{d} KiB: {d} keys, {d} ns a key, pages changed a key {d} max {d:.2} mean, value {d} bytes, full parse {d} ns\n", .{ kib, keys, per_key, pages_max, @as(f64, @floatFromInt(pages_sum)) / @as(f64, @floatFromInt(keys)), @sizeOf(Ed), parse });
+}
diff --git a/test/lib/build.zig b/test/lib/build.zig
index 6f590eda..436517d4 100644
--- a/test/lib/build.zig
+++ b/test/lib/build.zig
@@ -21,4 +21,14 @@ pub fn build(b: *std.Build) void {
}) });
step.dependOn(&b.addRunArtifact(tests).step);
}
+ // What a key costs, a page count and a parse: `zig build bench -- <KiB> <keys>`.
+ const bench = b.addExecutable(.{ .name = "bench", .root_module = b.createModule(.{
+ .target = target,
+ .optimize = optimize,
+ .root_source_file = b.path("bench.zig"),
+ .imports = &.{.{ .name = "pardes-editor", .module = pardes.module("pardes-editor") }},
+ }) });
+ const run_bench = b.addRunArtifact(bench);
+ run_bench.addPassthruArgs();
+ b.step("bench", "a key typed into the library's editor at a size, its pages and a parse").dependOn(&run_bench.step);
}