1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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 });
}
|