diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 07:44:55 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 0db7861d9da09e059fa1b9e40fff5cd52add27f2 (patch) | |
| tree | 6e95dfa36e07b3234b1d5cdb990b11f95c184a3f /test/hxlib.zig | |
| parent | d3742ddf2b3a85eb3935cb35e958fa27f1293a6b (diff) | |
| parent | dee94c740bdaea61ff35b4e0a7f1d2815c9ee441 (diff) | |
| download | pardes-0db7861d9da09e059fa1b9e40fff5cd52add27f2.tar.gz pardes-0db7861d9da09e059fa1b9e40fff5cd52add27f2.zip | |
integration: the text pane a library (pardes-editor), with the GPU move, the terminal, the plumber and the diff work
A merge of pardes-next (wmrtztwl) and editor-lib (rlumrxuq), no conflicts. Gated against the baseline of the integration line (.scratch/sketch/gpu/mbase): the merge with the library before its follow-up, in full: frame corpus 77/77 identical in Debug and in ReleaseFast, the gated window -0.019% instructions (threshold 0.5%), unit-test on gui 1584/1592 with no failure the baseline has not; this merge: corpus 77/77 in Debug, unit-test on gui 1584/1592, on tui core-test 811/950 (139 skipped), shell-test 990/993, lib-test, hxdiff 658, parity 738, hxgolf 124, hxlib 538 and its parity 618, all 0 mismatches, replicas pass.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'test/hxlib.zig')
| -rw-r--r-- | test/hxlib.zig | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/test/hxlib.zig b/test/hxlib.zig new file mode 100644 index 00000000..b6c1bfd3 --- /dev/null +++ b/test/hxlib.zig @@ -0,0 +1,136 @@ +//! 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, and the tree-sitter walks run on the app's parse (a case's text +//! has no grammar). 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); + // the app's parse: a case's text has no grammar (no path), so the + // tree-sitter walks run and leave the ranges, as a file pane's do + var syn: pe.Syntax = .{}; + 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(), &syn, 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 }); +} |
