summaryrefslogtreecommitdiff
path: root/src/lib_editor.zig
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 /src/lib_editor.zig
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 'src/lib_editor.zig')
-rw-r--r--src/lib_editor.zig79
1 files changed, 58 insertions, 21 deletions
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();
}