summaryrefslogtreecommitdiff
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
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
-rw-r--r--src/Editor.zig13
-rw-r--r--src/File.zig2
-rw-r--r--src/edit.zig9
-rw-r--r--src/lib_editor.zig79
-rw-r--r--src/normal.zig74
-rw-r--r--test/hxlib.zig11
-rw-r--r--test/lib/bench.zig8
-rw-r--r--test/lib/editor.zig48
8 files changed, 171 insertions, 73 deletions
diff --git a/src/Editor.zig b/src/Editor.zig
index 014d1923..b7c7057d 100644
--- a/src/Editor.zig
+++ b/src/Editor.zig
@@ -12,9 +12,18 @@ const Registers = @import("Registers.zig");
/// on this at compile time, so the library never compiles the core.
pub const lib = @import("pardes_config").lib;
const Pardes = if (lib) void else @import("Core.zig");
+const Syntax = if (lib) @import("lib_editor.zig").Syntax else void;
+const modal = @import("modal.zig");
const Editor = @This();
+/// A text's parse as the tree-sitter walks read it (normal.nodeTree): the
+/// tree, a path its grammar's queries are found by, and helix's
+/// object_selections, what Alt-o grew from for Alt-i to go back to, its
+/// ranges in `gpa`. Dropped with the tree when the text changes.
+pub const Tree = struct { root: *anyopaque, path: []const u8, history: *std.ArrayList(NodeStep), gpa: std.mem.Allocator };
+pub const NodeStep = struct { ranges: []modal.Selection, primary: usize };
+
gpa: std.mem.Allocator,
/// What an edit makes and drops: the update's scratch.
scratch: std.mem.Allocator,
@@ -39,6 +48,9 @@ p: if (lib) void else *Pardes,
/// The library's: an edit failed (its allocator ran out) and the key is
/// refused.
failed: if (lib) *bool else void,
+/// The library's: the parse its caller handed in, which the tree-sitter
+/// walks read; null, and they are the caller's (`textRole`).
+syntax: if (lib) ?*Syntax else void,
pub fn of(p: *Pardes) Editor {
return .{
@@ -53,6 +65,7 @@ pub fn of(p: *Pardes) Editor {
.retired_edit = &p.retired_edit,
.p = p,
.failed = {},
+ .syntax = {},
};
}
diff --git a/src/File.zig b/src/File.zig
index 0c3c3a2f..d049fc27 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -181,7 +181,7 @@ pub const State = struct {
follow: bool = false,
};
-pub const NodeStep = struct { ranges: []modal.Selection, primary: usize };
+pub const NodeStep = @import("Editor.zig").NodeStep;
pub fn graphemeDisplayWidth(grapheme: []const u8) usize {
if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now;
diff --git a/src/edit.zig b/src/edit.zig
index 105b9abf..8c59ffa1 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -1261,7 +1261,7 @@ fn insertEdit(e: Editor, t: *Text, key: Key) Error!void {
const pane = t.pane() orelse break :smart;
const f = pane.file orelse break :smart;
if (!syntax.supportsPath(f.path)) break :smart;
- return normal.nodeSelect(e.p, pane, t, .parent_node_end);
+ return normal.nodeSelect(e, t, f.content, .parent_node_end);
}
try insertTab(e, t);
},
@@ -1943,11 +1943,8 @@ pub fn textobjectSelect(e: Editor, t: *Text, text: []const u8, obj: u21, around:
else => null,
};
if (object) |o| {
- if (t.what != .body) return;
- const pane = t.pane() orelse return;
- const f = if (pane.file) |*file| file else return;
- const tree = normal.fileTree(e.p, f) orelse return;
- const s = syntax.objectAt(f.path, tree, text, o, around, modal.selectionCursor(text, range)) orelse return;
+ const tree = normal.nodeTree(e, t, text) orelse return;
+ const s = syntax.objectAt(tree.path, tree.root, text, o, around, modal.selectionCursor(text, range)) orelse return;
return t.setRange(text, 0, .{ .anchor = s.from, .head = s.to }, false);
}
switch (obj) {
diff --git a/src/lib_editor.zig b/src/lib_editor.zig
index 86e494e1..f9149ef0 100644
--- a/src/lib_editor.zig
+++ b/src/lib_editor.zig
@@ -221,6 +221,7 @@ const Live = struct {
multi_count: usize = 1,
retired_edit: ?[]u8 = null,
failed: bool = false,
+ syntax: ?*Syntax = null,
undo_thawed: usize = 0,
redo_thawed: usize = 0,
/// The ring records thawed, by their text's address: a snapshot still
@@ -241,6 +242,7 @@ const Live = struct {
.retired_edit = &l.retired_edit,
.p = {},
.failed = &l.failed,
+ .syntax = l.syntax,
};
}
};
@@ -318,21 +320,24 @@ pub fn Editor(comptime cap: Capacity) type {
/// The ranges in document order and which is the primary.
pub fn ranges(ed: *const Self, a: std.mem.Allocator, out: *[max_selections]Selection) error{OutOfMemory}!struct { n: usize, primary: usize } {
- const live = try ed.thaw(a);
+ const live = try ed.thaw(a, null);
const got = live.text.ranges(live.text.own.?, 0, out);
return .{ .n = got.n, .primary = got.pri };
}
- /// A key, as a text pane in normal or insert mode takes it.
- pub fn key(ed: *Self, a: std.mem.Allocator, k: Key) Outcome {
- const live = ed.thaw(a) catch return .{ .refused = .out_of_memory };
+ /// A key, as a text pane in normal or insert mode takes it. With
+ /// `syn`, the tree-sitter walks (Alt-o and kin, `]f`, `mif`) run on
+ /// its parse, made again if the text has changed; without, they
+ /// come back as actions (`mif` does nothing).
+ pub fn key(ed: *Self, a: std.mem.Allocator, syn: ?*Syntax, k: Key) Outcome {
+ const live = ed.thaw(a, syn) catch return .{ .refused = .out_of_memory };
const parsed = normal.textKey(live.context(a), &live.text, k);
switch (parsed) {
.unbound => return ed.keep(a, live, .{ .unbound = k }),
.pending => return ed.keep(a, live, .pending),
.ignored => return ed.keep(a, live, .done),
.action => |act| {
- if (!normal.textRole(act.role)) {
+ if (!normal.textRole(act.role, syn != null)) {
// the parse's state goes on; nothing ran
ed.cursor.normal = live.text.normal;
return .{ .action = act };
@@ -344,9 +349,9 @@ pub fn Editor(comptime cap: Capacity) type {
/// A normal-mode action, as its key would run it: across every
/// range unless it is the selection's as a whole.
- pub fn run(ed: *Self, a: std.mem.Allocator, act: Action) Outcome {
- if (!normal.textRole(act.role)) return .{ .action = act };
- const live = ed.thaw(a) catch return .{ .refused = .out_of_memory };
+ pub fn run(ed: *Self, a: std.mem.Allocator, syn: ?*Syntax, act: Action) Outcome {
+ if (!normal.textRole(act.role, syn != null)) return .{ .action = act };
+ const live = ed.thaw(a, syn) catch return .{ .refused = .out_of_memory };
normal.runAction(live.context(a), &live.text, act);
return ed.keep(a, live, .done);
}
@@ -354,7 +359,7 @@ pub fn Editor(comptime cap: Capacity) type {
/// helix's `s` (select), `S` (split), `K` (keep) and `Alt-K`
/// (remove) with `pattern` over the ranges there are.
pub fn selectRegex(ed: *Self, a: std.mem.Allocator, pattern: []const u8, how: RegexMode) Outcome {
- const live = ed.thaw(a) catch return .{ .refused = .out_of_memory };
+ const live = ed.thaw(a, null) catch return .{ .refused = .out_of_memory };
const t = &live.text;
// as pardes's prompt answers it (look.submitSearch): helix
// keeps the pattern in `/`
@@ -370,7 +375,7 @@ pub fn Editor(comptime cap: Capacity) type {
/// Replace the whole text with `text`, as one step to undo.
pub fn setText(ed: *Self, a: std.mem.Allocator, text: []const u8) Outcome {
- const live = ed.thaw(a) catch return .{ .refused = .out_of_memory };
+ const live = ed.thaw(a, null) catch return .{ .refused = .out_of_memory };
const e = live.context(a);
const t = &live.text;
edit.pushUndo(e, t) catch return .{ .refused = .out_of_memory };
@@ -390,14 +395,15 @@ pub fn Editor(comptime cap: Capacity) type {
/// Write register `name` (the system clipboard's `+`, a pattern's
/// `/`): one value.
pub fn setRegister(ed: *Self, a: std.mem.Allocator, name: u21, value: []const u8) Outcome {
- const live = ed.thaw(a) catch return .{ .refused = .out_of_memory };
+ const live = ed.thaw(a, null) catch return .{ .refused = .out_of_memory };
live.registers.put(a, name, value, 0, 1, true) catch return .{ .refused = .out_of_memory };
return ed.keep(a, live, .done);
}
- fn thaw(ed: *const Self, a: std.mem.Allocator) error{OutOfMemory}!*Live {
+ fn thaw(ed: *const Self, a: std.mem.Allocator, syn: ?*Syntax) error{OutOfMemory}!*Live {
const live = try a.create(Live);
- live.* = .{ .text = .{ .what = .body, .own = try ed.bytes(a), .gpa = a } };
+ live.* = .{ .text = .{ .what = .body, .own = try ed.bytes(a), .gpa = a }, .syntax = syn };
+ if (syn) |s| s.at(ed.revision);
const t = &live.text;
inline for (@typeInfo(Cursor).@"struct".field_names) |name| @field(t, name) = @field(ed.cursor, name);
if (ed.cursor.nsel > 0) {
@@ -523,19 +529,50 @@ pub fn Editor(comptime cap: Capacity) type {
pub const Syntax = struct {
tree: ?*syntax.ts.Tree = null,
revision: u32 = 0,
+ /// The grammar the tree is parsed in: the last `parse`'s, or set by a
+ /// caller that hands this to `key` before parsing. None, and a walk
+ /// does nothing, as in a text without a grammar.
+ lang: ?Language = null,
+ /// helix's object_selections, what Alt-o grew from for Alt-i to go back
+ /// to (normal.nodeSelect): the tree's, dropped with it. In the page
+ /// allocator, which holds nothing of the code that made it.
+ history: std.ArrayList(Context.NodeStep) = .empty,
+ path: [64]u8 = undefined,
/// The tree of `ed`'s text in `lang`, parsed again when the text has
/// changed since the last; null when the language cannot parse.
pub fn parse(s: *Syntax, a: std.mem.Allocator, ed: anytype, lang: Language) ?*syntax.ts.Tree {
- if (s.tree != null and s.revision == ed.revision) return s.tree;
+ s.lang = lang;
+ s.at(ed.revision);
+ if (s.tree == null) _ = s.walkTree(ed.bytes(a) catch return null);
+ return s.tree;
+ }
+
+ /// What is kept of another revision of the text dropped: the tree and
+ /// Alt-o's history.
+ fn at(s: *Syntax, revision: u32) void {
+ if (s.revision == revision) return;
+ s.drop();
+ s.revision = revision;
+ }
+
+ fn drop(s: *Syntax) void {
if (s.tree) |tree| tree.destroy();
s.tree = null;
- const text = ed.bytes(a) catch return null;
- if (!syntax_impl.addLanguage(lang)) return null;
- var buf: [64]u8 = undefined;
- s.tree = @ptrCast(@alignCast(syntax_impl.parseTree(lang.path(&buf), text, null) orelse return null));
- s.revision = ed.revision;
- return s.tree;
+ for (s.history.items) |step| std.heap.page_allocator.free(step.ranges);
+ s.history.clearAndFree(std.heap.page_allocator);
+ }
+
+ /// The parse of `text`, the text at `revision`, as a key's walks read
+ /// it (normal.nodeTree): made the first time one asks for it.
+ pub fn walkTree(s: *Syntax, text: []const u8) ?Context.Tree {
+ const lang = s.lang orelse return null;
+ const path = lang.path(&s.path);
+ if (s.tree == null) {
+ if (!syntax_impl.addLanguage(lang)) return null;
+ s.tree = @ptrCast(@alignCast(syntax_impl.parseTree(path, text, null) orelse return null));
+ }
+ return .{ .root = s.tree.?, .path = path, .history = &s.history, .gpa = std.heap.page_allocator };
}
/// One `Syn` a byte of `ed`'s text from `from` to `to`, in `a`.
@@ -549,7 +586,7 @@ pub const Syntax = struct {
/// The tree gone, and every language handed in (at a reload: their
/// code is about to go).
pub fn forget(s: *Syntax) void {
- if (s.tree) |tree| tree.destroy();
+ s.drop();
s.* = .{};
syntax_impl.forgetLanguages();
}
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),
diff --git a/test/hxlib.zig b/test/hxlib.zig
index 423851d7..b6c1bfd3 100644
--- a/test/hxlib.zig
+++ b/test/hxlib.zig
@@ -4,8 +4,10 @@
//! against helix's goldens or the file pane (`zig build hxlib`). The
//! library does what an app would with the actions it hands back that a
//! case needs: s, S, K and Alt-K take the keys up to <ret> as their
-//! pattern. A case that reaches any other action (a view, `:`, `/`, LSP, a
-//! macro) is the app's from there: printed as skipped, with the role.
+//! pattern, and the tree-sitter walks run on the app's parse (a case's text
+//! has no grammar). A case that reaches any other action (a view, `:`,
+//! `/`, LSP, a macro) is the app's from there: printed as skipped, with the
+//! role.
const std = @import("std");
const pe = @import("pardes-editor");
const hxkeys = @import("hxkeys.zig");
@@ -60,6 +62,9 @@ pub fn main(init: std.process.Init) !void {
fn runCase(a: std.mem.Allocator, ed: *Ed, case: Case) ![]u8 {
ed.* = .{};
try ed.load(case.text);
+ // the app's parse: a case's text has no grammar (no path), so the
+ // tree-sitter walks run and leave the ranges, as a file pane's do
+ var syn: pe.Syntax = .{};
var i: usize = 0;
while (try hxkeys.next(pe.Key, case.keys, &i)) |k| {
// a page key in insert mode moves a view, which the library has not
@@ -67,7 +72,7 @@ fn runCase(a: std.mem.Allocator, ed: *Ed, case: Case) ![]u8 {
return std.json.Stringify.valueAlloc(a, Skip{ .name = case.name, .skip = "insert page key" }, .{});
var call = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer call.deinit();
- switch (ed.key(call.allocator(), k)) {
+ switch (ed.key(call.allocator(), &syn, k)) {
.done, .pending, .unbound => {},
.refused => |why| return std.json.Stringify.valueAlloc(a, Skip{ .name = case.name, .skip = @tagName(why) }, .{}),
.action => |act| switch (act.role) {
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]);
+}