summaryrefslogtreecommitdiff
path: root/test/lib
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 07:24:58 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitf233d87376bbdfde3918f803e31b52a214ea9bdb (patch)
treef62cc8df1c9c4ad3c635b189631bbda08f9086ae /test/lib
parent946e96c0ec793d6b546da8ebef19555d639ccda6 (diff)
downloadpardes-f233d87376bbdfde3918f803e31b52a214ea9bdb.tar.gz
pardes-f233d87376bbdfde3918f803e31b52a214ea9bdb.zip
+171 -73 lib: the tree-sitter walks run in the library on a parse its caller hands in (docs/next-editor.md): `key(a, syn, k)` and `run(a, syn, act)` take a `?*Syntax`, which keeps the grammar it parses in, the tree, made on the first walk after the text changes, and helix's object_selections (Alt-o's history, for Alt-i to go back the way it came) in the page allocator; with one, Alt-o, Alt-i, Alt-n, Alt-p, Alt-a, Alt-I, Alt-e, Alt-b and `]f` and kin run in the library, and `mif` and kin find the grammar's textobjects; without, they come back as actions, as before; normal.nodeTree is the parse they read, a file's (fileTree) in pardes and the one handed in in the library, `Editor.Tree` its tree, path and history, so nodeSelect and objectJump take the editor's context and are textAction's, textRole saying the walks are the text's with a parse at hand, and edit.textobjectSelect reads it too; NodeStep moves to Editor.zig (File.zig names it); nothing a pane does changes; hxlib hands each case a parse with no grammar (a case's text has none): 538 of 658 compared with 0 mismatches (expand_node's one case moved from the caller's), --parity 618 of 738 with 0 mismatches; test/lib: Alt-o and Alt-i on C, back the way they came, `maf` from a textobjects query handed in, and Alt-o without a parse the caller's; core-test 811 of 950, shell-test 990 of 993, hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, replicas pass, lib-test pass
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'test/lib')
-rw-r--r--test/lib/bench.zig8
-rw-r--r--test/lib/editor.zig48
2 files changed, 46 insertions, 10 deletions
diff --git a/test/lib/bench.zig b/test/lib/bench.zig
index f2127dc4..7e945064 100644
--- a/test/lib/bench.zig
+++ b/test/lib/bench.zig
@@ -34,10 +34,10 @@ pub fn main(init: std.process.Init) !void {
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);
+ for ([_]pe.Key{ .{ .cp = '%', .text = "%" }, .{ .cp = ';', .text = ";" } }) |k| _ = ed.key(arena.allocator(), null, 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" });
+ for (std.fmt.bufPrint(&to_middle, "{d}G", .{text.items.len / 2 / 32}) catch unreachable) |c| _ = ed.key(arena.allocator(), null, .{ .cp = c, .text = &.{c} });
+ _ = ed.key(arena.allocator(), null, .{ .cp = 'i', .text = "i" });
_ = arena.reset(.free_all);
if (keys == 0) return;
@@ -48,7 +48,7 @@ pub fn main(init: std.process.Init) !void {
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))} });
+ const out = ed.key(arena.allocator(), null, .{ .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);
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]);
+}