From f233d87376bbdfde3918f803e31b52a214ea9bdb Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:24:58 -0300 Subject: +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 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/normal.zig | 74 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 32 deletions(-) (limited to 'src/normal.zig') diff --git a/src/normal.zig b/src/normal.zig index c4c00824..70933607 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -225,16 +225,25 @@ pub fn fileTree(p: *Pardes, f: *panes.File.State) ?*anyopaque { return f.node_tree; } +/// The parse a body's tree-sitter walks read: its file's (`fileTree`), or +/// in the library the one its caller handed in (lib_editor.Syntax); null +/// without a grammar, and the walk leaves the ranges as they are. +pub fn nodeTree(e: Editor, t: *Text, text: []const u8) ?Editor.Tree { + if (t.what != .body) return null; + if (comptime Editor.lib) return (e.syntax orelse return null).walkTree(text); + const pane = t.pane() orelse return null; + const f = if (pane.file) |*file| file else return null; + return .{ .root = fileTree(e.p, f) orelse return null, .path = f.path, .history = &f.node_history, .gpa = e.gpa }; +} + /// `]f`/`[f` and kin (helix goto_ts_object): to the next object starting /// after the cursor or the previous one ending before it, count times; select /// mode stretches the range to it. -fn objectJump(p: *Pardes, pane: *Pane, t: *Text, text: []const u8, range: modal.Selection, object: modal.Normal.Object, forward: bool, count: u32) void { - if (t.what != .body) return; - const f = if (pane.file) |*file| file else return; - const tree = fileTree(p, f) orelse return; +fn objectJump(e: Editor, t: *Text, text: []const u8, range: modal.Selection, object: modal.Normal.Object, forward: bool, count: u32) void { + const tree = nodeTree(e, t, text) orelse return; var last = range; for (0..count) |_| { - const s = syntax.objectNext(f.path, tree, text, object, forward, modal.selectionCursor(text, last)) orelse break; + const s = syntax.objectNext(tree.path, tree.root, text, object, forward, modal.selectionCursor(text, last)) orelse break; const next: modal.Selection = .{ .anchor = s.from, .head = s.to }; if (std.meta.eql(next, last)) break; last = next; @@ -248,21 +257,19 @@ fn objectJump(p: *Pardes, pane: *Pane, t: *Text, text: []const u8, range: modal. t.setRange(text, 0, out, false); } -/// helix's tree-sitter selection keys: every range walks the file's parse, -/// which is made on the first of these keys after an edit and kept, never -/// made again for a key that changed nothing. A range the tree has nothing -/// to say about stays as it was; a text without a grammar, all of them. -pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk) edit.Error!void { - if (t.what != .body) return; - const f = if (pane.file) |*file| file else return; - const tree = fileTree(p, f) orelse return; - const text = f.content; +/// helix's tree-sitter selection keys: every range walks the text's parse +/// (`nodeTree`), which is made on the first of these keys after an edit and +/// kept, never made again for a key that changed nothing. A range the tree +/// has nothing to say about stays as it was; a text without a grammar, all +/// of them. +pub fn nodeSelect(e: Editor, t: *Text, text: []const u8, walk: modal.Normal.NodeWalk) edit.Error!void { + const tree = nodeTree(e, t, text) orelse return; const expl = t.vsel.active and t.vsel.explicit; - const arena = p.scratch.allocator(); + const arena = e.scratch; const rs = try arena.alloc(modal.Selection, Text.max_selections); const got = t.ranges(text, 0, rs[0..Text.max_selections]); - if (walk == .shrink_node) if (f.node_history.pop()) |prev| { - defer p.gpa.free(prev.ranges); + if (walk == .shrink_node) if (tree.history.pop()) |prev| { + defer tree.gpa.free(prev.ranges); // back to what Alt-o grew from, while the selection still holds it const holds = for (prev.ranges) |pr| { const pf = @min(pr.anchor, pr.head); @@ -273,8 +280,8 @@ pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk break false; } else true; if (holds) return t.setRanges(text, prev.ranges, &.{}, prev.primary, expl); - for (f.node_history.items) |step| p.gpa.free(step.ranges); - f.node_history.clearRetainingCapacity(); + for (tree.history.items) |step| tree.gpa.free(step.ranges); + tree.history.clearRetainingCapacity(); }; const out = try arena.alloc(modal.Selection, Text.max_selections); const spans = try arena.alloc(syntax.Span, Text.max_selections); @@ -284,7 +291,7 @@ pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk const from = @min(r.anchor, r.head); const to = @max(r.anchor, r.head); const back = r.head < r.anchor; - const n = syntax.walkNodes(tree, walk, from, to, modal.selectionCursor(text, r), spans[0 .. out.len - m]); + const n = syntax.walkNodes(tree.root, walk, from, to, modal.selectionCursor(text, r), spans[0 .. out.len - m]); if (n == 0) { out[m] = r; m += 1; @@ -312,8 +319,8 @@ pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk } const same = m == got.n and std.mem.eql(u8, std.mem.sliceAsBytes(out[0..m]), std.mem.sliceAsBytes(rs[0..m])); if (walk == .expand_node and !same) remember: { - const ranges = p.gpa.dupe(modal.Selection, rs[0..got.n]) catch break :remember; - f.node_history.append(p.gpa, .{ .ranges = ranges, .primary = got.pri }) catch p.gpa.free(ranges); + const ranges = tree.gpa.dupe(modal.Selection, rs[0..got.n]) catch break :remember; + tree.history.append(tree.gpa, .{ .ranges = ranges, .primary = got.pri }) catch tree.gpa.free(ranges); } t.setRanges(text, out[0..m], &.{}, @min(got.pri, m - 1), expl); } @@ -552,7 +559,7 @@ pub fn handleNormal(e: Editor, t: *Text, key: Key) modal.Normal.Result { }; t.normal.register = 0; // the core's roles are the library's caller's - if (comptime Editor.lib) if (!textRole(action.role)) return parsed; + if (comptime Editor.lib) if (!textRole(action.role, e.syntax != null)) return parsed; e.register.* = named; defer e.register.* = 0; runAction(e, t, action); @@ -984,10 +991,13 @@ fn shrinkSelToLineBounds(t: *Text, text: []const u8, range: modal.Selection) voi /// Whether a normal-mode role is the text's alone (`textAction`), or /// reaches past it: a view, a builtin, a prompt, a language server, the -/// look ring, the jump labels, a macro. -pub fn textRole(role: modal.Normal.Role) bool { +/// look ring, the jump labels, a macro. The tree-sitter walks are the +/// text's with a parse at hand (`tree`, `nodeTree`): a pane's file has +/// one, the library's caller may hand one in. +pub fn textRole(role: modal.Normal.Role, tree: bool) bool { return switch (role) { - .all_children, .all_siblings, .append_output, .command_line, .expand_node, .format, .goto_callstack, .goto_class, .goto_comment, .goto_declaration, .goto_definition, .goto_diagnostic, .goto_diagnostic_end, .goto_entry, .goto_function, .goto_implementation, .goto_parameter, .goto_references, .goto_test, .goto_type_definition, .goto_view_bottom, .goto_view_top, .goto_word, .goto_xml_element, .half_page_down, .half_page_up, .insert_output, .keep_pipe, .keep_regex, .leader, .macro_record, .macro_replay, .next_sibling, .page_down, .page_up, .parent_node_end, .parent_node_start, .pipe_selection, .pipe_selection_to, .prev_sibling, .remove_regex, .repeat_insert, .search, .search_next, .search_prev, .select_regex, .shrink_node, .split_regex, .view_bottom, .view_center, .view_middle, .view_scroll_down, .view_scroll_up, .view_top => false, + .all_children, .all_siblings, .expand_node, .goto_class, .goto_comment, .goto_entry, .goto_function, .goto_parameter, .goto_test, .goto_xml_element, .next_sibling, .parent_node_end, .parent_node_start, .prev_sibling, .shrink_node => tree, + .append_output, .command_line, .format, .goto_callstack, .goto_declaration, .goto_definition, .goto_diagnostic, .goto_diagnostic_end, .goto_implementation, .goto_references, .goto_type_definition, .goto_view_bottom, .goto_view_top, .goto_word, .half_page_down, .half_page_up, .insert_output, .keep_pipe, .keep_regex, .leader, .macro_record, .macro_replay, .page_down, .page_up, .pipe_selection, .pipe_selection_to, .remove_regex, .repeat_insert, .search, .search_next, .search_prev, .select_regex, .split_regex, .view_bottom, .view_center, .view_middle, .view_scroll_down, .view_scroll_up, .view_top => false, else => true, }; } @@ -1059,6 +1069,11 @@ pub fn textAction(e: Editor, t: *Text, a: modal.Normal.Action) edit.Error!void { return pointMove(t, text, range, modal.offsetAt(text, mc)); }, .match_inside, .match_around => return edit.textobjectSelect(e, t, text, a.char, a.role == .match_around), + inline .goto_function, .goto_class, .goto_parameter, .goto_comment, .goto_test, .goto_entry, .goto_xml_element => |role| { + const object = @field(modal.Normal.Object, @tagName(role)["goto_".len..]); + return objectJump(e, t, text, range, object, a.forward, a.count); + }, + inline .expand_node, .shrink_node, .next_sibling, .prev_sibling, .all_siblings, .all_children, .parent_node_end, .parent_node_start => |role| return nodeSelect(e, t, text, @field(modal.Normal.NodeWalk, @tagName(role))), .surround_add => return edit.surroundAdd(e, t, a.char), .surround_delete => return edit.surroundDelete(e, t, a.char), .surround_replace => return edit.surroundReplace(e, t, a.from, a.char), @@ -1175,7 +1190,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! pane.sel[sel_slot].state = .none; }; if (raw_selection and a.role == .yank) return edit.normalYank(p.editor(), t); - if (textRole(a.role)) return textAction(p.editor(), t, a); + if (textRole(a.role, true)) return textAction(p.editor(), t, a); edit.pinPaneCursor(p.editor(), t); const text = try edit.flatSurface(p.editor(), t); const range = t.primaryRange(text, 0); @@ -1222,10 +1237,6 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! if (set.hits.items.len == 0) return; set.goTo(pane, if (a.forward) set.hits.items.len - 1 else 0, false); }, - inline .goto_function, .goto_class, .goto_parameter, .goto_comment, .goto_test, .goto_entry, .goto_xml_element => |role| { - const object = @field(modal.Normal.Object, @tagName(role)["goto_".len..]); - return objectJump(p, pane, t, text, range, object, a.forward, a.count); - }, // These four move exactly one viewport unit, whatever the count. .half_page_down => return halfPageMove(t, text, range, true), .half_page_up => return halfPageMove(t, text, range, false), @@ -1234,7 +1245,6 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! // s, S and | answer for the text they are pressed in; `/` searches // the body wherever it is pressed, as acme's Look from a tag does. inline .select_regex, .split_regex, .keep_regex, .remove_regex => |role| return startSelRegex(p, pane, t, @field(modal.Normal.RegexMode, @tagName(role)[0 .. @tagName(role).len - "_regex".len])), - inline .expand_node, .shrink_node, .next_sibling, .prev_sibling, .all_siblings, .all_children, .parent_node_end, .parent_node_start => |role| return nodeSelect(p, pane, t, @field(modal.Normal.NodeWalk, @tagName(role))), // Language shortcuts run the same builtins as tags and leader commands. .goto_definition => return exec.runBuiltin(p, .Def, v.active, "", null), .goto_references => return exec.runBuiltin(p, .Refs, v.active, "", null), -- cgit v1.3