summaryrefslogtreecommitdiff
path: root/test/lib/editor.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/lib/editor.zig')
-rw-r--r--test/lib/editor.zig48
1 files changed, 42 insertions, 6 deletions
diff --git a/test/lib/editor.zig b/test/lib/editor.zig
index 412e5109..2b234cd8 100644
--- a/test/lib/editor.zig
+++ b/test/lib/editor.zig
@@ -20,7 +20,7 @@ fn typeKeys(ed: anytype, gpa: std.mem.Allocator, keys: []const u8) !void {
// 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)) {
+ switch (ed.key(arena.allocator(), null, k)) {
.refused => return error.Refused,
else => {},
}
@@ -94,7 +94,7 @@ test "a key whose text would not fit is refused, and the value is as it was" {
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.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));
}
@@ -107,16 +107,16 @@ test "what is not the text's comes back, and the caller can do it" {
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);
+ 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, .{ .cp = 'g', .text = "g" });
+ _ = ed.key(a, null, .{ .cp = 'g', .text = "g" });
try std.testing.expectEqual(pe.Role.goto_prefix, ed.pending().?);
}
@@ -142,3 +142,39 @@ test "a language handed in at run time parses and colours the text" {
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]);
+}