diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 07:24:58 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | f233d87376bbdfde3918f803e31b52a214ea9bdb (patch) | |
| tree | f62cc8df1c9c4ad3c635b189631bbda08f9086ae /test/lib/editor.zig | |
| parent | 946e96c0ec793d6b546da8ebef19555d639ccda6 (diff) | |
| download | pardes-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/editor.zig')
| -rw-r--r-- | test/lib/editor.zig | 48 |
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]); +} |
