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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
//! 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(), null, 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(), null, .{ .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, null, .{ .cp = ':', .text = ":" }).action.role);
_ = ed.key(a, null, .{ .cp = '%', .text = "%" });
try std.testing.expectEqual(pe.Role.select_regex, ed.key(a, null, .{ .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, null, .{ .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]);
}
test "with a parse handed in, the tree-sitter walks run in the library; without, they are the caller's" {
const gpa = std.testing.allocator;
const ed = try gpa.create(Ed);
defer gpa.destroy(ed);
ed.* = .{};
try ed.load("int f() { return 1 + 2; }\n");
pe.syntax.start(gpa);
defer pe.syntax.stop();
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const a = arena.allocator();
const alt_o: pe.Key = .{ .cp = 'o', .alt = true };
const alt_i: pe.Key = .{ .cp = 'i', .alt = true };
try std.testing.expectEqual(pe.Role.expand_node, ed.key(a, null, alt_o).action.role);
var syn: pe.Syntax = .{ .lang = .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .textobjects = "(function_definition body: (_) @function.inside) @function.around" } };
defer syn.forget();
for ("21l") |c| _ = ed.key(a, &syn, .{ .cp = c, .text = &.{c} });
var out: [pe.max_selections]pe.Selection = undefined;
// `2`, then `1 + 2`, then `return 1 + 2;`; Alt-i goes back the way it
// came (helix's object_selections: not the first child, `1`)
for ([_]struct { pe.Key, pe.Selection }{
.{ alt_o, .{ .anchor = 17, .head = 22 } },
.{ alt_o, .{ .anchor = 10, .head = 23 } },
.{ alt_i, .{ .anchor = 17, .head = 22 } },
.{ alt_i, .{ .anchor = 21, .head = 22 } },
}) |step| {
try std.testing.expectEqual(pe.Outcome.done, ed.key(a, &syn, step[0]));
_ = try ed.ranges(a, &out);
try std.testing.expectEqual(step[1], out[0]);
}
// the grammar's textobjects handed in: `maf`, the function around
for ("maf") |c| _ = ed.key(a, &syn, .{ .cp = c, .text = &.{c} });
_ = try ed.ranges(a, &out);
try std.testing.expectEqual(pe.Selection{ .anchor = 0, .head = 25 }, out[0]);
}
test "a byte range replaced is one step to undo, the ranges following it; a drag joined is one step, refused whole when it does not fit" {
const gpa = std.testing.allocator;
const ed = try gpa.create(Ed);
defer gpa.destroy(ed);
ed.* = .{};
try ed.load("a = 12 + 3;\n");
try typeKeys(ed, gpa, "9l");
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const a = arena.allocator();
var buf: [4096]u8 = undefined;
var out: [pe.max_selections]pe.Selection = undefined;
// a drag on the number: its first frame a step, the rest joined to it
try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 6, "130", false));
_ = try ed.ranges(a, &out);
try std.testing.expectEqual(pe.Selection{ .anchor = 10, .head = 11 }, out[0]); // still on `3`
const steps = ed.undo.count;
try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "131", true));
try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "14", true));
try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf));
try std.testing.expectEqual(steps, ed.undo.count);
_ = try ed.ranges(a, &out);
try std.testing.expectEqual(pe.Selection{ .anchor = 9, .head = 10 }, out[0]);
try typeKeys(ed, gpa, "u");
try std.testing.expectEqualStrings("a = 12 + 3;\n", text(ed, &buf));
try typeKeys(ed, gpa, "U");
try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf));
// what would not fit: refused, the value as it was
const kept = try gpa.create(Ed);
defer gpa.destroy(kept);
kept.* = ed.*;
const ys: [4096]u8 = @splat('y');
try std.testing.expectEqual(pe.Outcome{ .refused = .text_full }, ed.replace(a, 0, 1, &ys, false));
try std.testing.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed));
}
|