From 56274a68fb7a418625f01d479ced9fbfc19383f2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 06:01:44 -0300 Subject: lib: the text pane as a library, `pardes-editor` (docs/next-editor.md, R5): src/lib_editor.zig is an editor a program keeps as plain data (`Editor(capacity)`: a gap buffer for the text, Text's cursor fields by name, two byte rings of whole-text snapshots for undo and redo, registers as records; no pointer, no allocator), thawed into a Text and Registers in the allocator a call is handed, run through normal.zig and edit.zig as a pane's keys run, and frozen back, a key that does not fit refused whole (`key`, `run`, `selectRegex`, `setText`, `setRegister`; `Outcome` done, pending, unbound, action, refused), and `Syntax`, a parse derived from a grammar handed in; the seam is `pardes_config.lib`, true in the library's build alone: Text.pane is null at compile time there, a body's characters and undo are its own (flatSurface, editText, setEditTextSpan, pushUndo, undoStep), bodyFile and filePane are none, Esc and `*` do the text's part only and `+` is a plain register, and config.zig's check of the builtins' leader paths is the editor's; normal.textKey is the mode dispatch keys.zig and the library share, handleNormal hands back the parse and runAction runs an action across the ranges, and applySelRegex's search over the ranges is selectByRegex, which the library calls with a pattern; build.zig: `lib` in the options, mvzr's patch and uucode as functions (patchedMvzr, uucodeModule) the editor's module takes too, editorModule, and `library` exports `pardes-editor` to a package that asks (`.editor = true`: it fetches uucode, mvzr and vaxis, of which it compiles the grapheme width), so a pardes-syntax consumer fetches what it did; test/lib: a value typed into, copied elsewhere and its memory gone, types on equal to one never moved, undo past the ring and redo back, a text that does not fit refused with the value unchanged, an action handed back and a regex done by the caller, a language handed in parsed and coloured; core-test 811 of 950, shell-test 990 of 993, hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, replicas pass, lib-test 8 of 8 --- test/lib/editor.zig | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 test/lib/editor.zig (limited to 'test/lib/editor.zig') diff --git a/test/lib/editor.zig b/test/lib/editor.zig new file mode 100644 index 00000000..412e5109 --- /dev/null +++ b/test/lib/editor.zig @@ -0,0 +1,144 @@ +//! The editor as a package that depends on pardes takes it +//! (`pardes-editor`): a value of plain data a program keeps, typed into a +//! key at a time with an arena each call, copied anywhere, refused whole at +//! its capacities (docs/next-editor.md). +const std = @import("std"); +const pe = @import("pardes-editor"); + +const Ed = pe.Editor(.{ .text = 4096, .undo = 8192, .registers = 1024, .selections = 16 }); + +/// Keys in helix's notation, the few these tests type: printables, and +/// ``, ``, ``. +fn typeKeys(ed: anytype, gpa: std.mem.Allocator, keys: []const u8) !void { + var i: usize = 0; + while (i < keys.len) { + var k: pe.Key = .{ .cp = keys[i], .text = keys[i .. i + 1] }; + inline for (.{ .{ "", pe.Key.escape }, .{ "", pe.Key.enter }, .{ "", pe.Key.backspace } }) |special| { + if (std.mem.startsWith(u8, keys[i..], special[0])) k = .{ .cp = special[1] }; + } + i += if (k.text.len == 0) std.mem.indexOfScalarPos(u8, keys, i, '>').? + 1 - i else 1; + // 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)) { + .refused => return error.Refused, + else => {}, + } + } +} + +fn text(ed: anytype, buf: []u8) []const u8 { + return ed.read(buf); +} + +const first = "ifn main() { let x = 1;}ggxyp" ++ "wwCcfoo"; +const second = "%dpuuUjjAbar" ++ "ggiqu"; + +test "a value copied elsewhere, its old memory gone, types on as one never moved" { + const gpa = std.testing.allocator; + const still = try gpa.create(Ed); + defer gpa.destroy(still); + still.* = .{}; + try typeKeys(still, gpa, first); + try typeKeys(still, gpa, second); + + const before = try gpa.create(Ed); + before.* = .{}; + try typeKeys(before, gpa, first); + const moved = try gpa.create(Ed); + defer gpa.destroy(moved); + @memcpy(std.mem.asBytes(moved), std.mem.asBytes(before)); + @memset(std.mem.asBytes(before), 0xAA); + gpa.destroy(before); + try typeKeys(moved, gpa, second); + + try std.testing.expectEqualSlices(u8, std.mem.asBytes(still), std.mem.asBytes(moved)); + var buf: [4096]u8 = undefined; + // `%d` then `p`, both undone, the delete redone: `A` types into nothing + try std.testing.expectEqualStrings("bar", text(moved, &buf)); +} + +test "undo goes back as far as the ring holds, and redo all the way again" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + const xs: [1000]u8 = @splat('x'); + try ed.load(&xs); + // thirty edits, each a snapshot of a thousand bytes: an 8 KiB ring + // keeps the last few + for (0..30) |_| try typeKeys(ed, gpa, "ia"); + var buf: [4096]u8 = undefined; + var last: [4096]u8 = undefined; + const final = text(ed, &last); + try std.testing.expectEqual(1030, final.len); + try std.testing.expect(ed.undo.count < 30); + const kept = ed.undo.count; + for (0..40) |_| try typeKeys(ed, gpa, "u"); + try std.testing.expectEqual(1030 - kept, text(ed, &buf).len); + for (0..40) |_| try typeKeys(ed, gpa, "U"); + try std.testing.expectEqualStrings(final, text(ed, &buf)); +} + +test "a key whose text would not fit is refused, and the value is as it was" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + const ys: [5000]u8 = @splat('y'); + try std.testing.expectError(error.TextFull, ed.load(&ys)); + try ed.load(ys[0..4094]); + try typeKeys(ed, gpa, "iab"); + const kept = try gpa.create(Ed); + defer gpa.destroy(kept); + 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.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed)); +} + +test "what is not the text's comes back, and the caller can do it" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + try ed.load("one two one\n"); + 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); + _ = 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" }); + try std.testing.expectEqual(pe.Role.goto_prefix, ed.pending().?); +} + +extern fn tree_sitter_c() *const pe.syntax.ts.Language; + +test "a language handed in at run time parses and colours the text" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + try ed.load("int f() { return 1; }\n"); + pe.syntax.start(gpa); + defer pe.syntax.stop(); + const lang: pe.Language = .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .highlights = "\"return\" @keyword (number_literal) @number" }; + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + var syn: pe.Syntax = .{}; + defer syn.forget(); + const tree = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; + try std.testing.expectEqualStrings("translation_unit", tree.rootNode().kind()); + const colors = try pe.Syntax.colors(arena.allocator(), ed, lang, 0, ed.len()); + try std.testing.expectEqual(pe.syntax.Syn.keyword, colors[10]); + try std.testing.expectEqual(pe.syntax.Syn.number, colors[17]); + try std.testing.expectEqual(pe.syntax.Syn.none, colors[0]); +} -- cgit v1.3 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/Editor.zig | 13 +++++++++ src/File.zig | 2 +- src/edit.zig | 9 ++---- src/lib_editor.zig | 79 +++++++++++++++++++++++++++++++++++++++-------------- src/normal.zig | 74 +++++++++++++++++++++++++++---------------------- test/hxlib.zig | 11 ++++++-- test/lib/bench.zig | 8 +++--- test/lib/editor.zig | 48 ++++++++++++++++++++++++++++---- 8 files changed, 171 insertions(+), 73 deletions(-) (limited to 'test/lib/editor.zig') 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 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]); +} -- cgit v1.3 From b954bf23cf3137bb7fa731ab3a9dbe388a485166 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:29:27 -0300 Subject: +66 -0 lib: `replace(a, from, to, with, join)` rewrites a byte range of the library's text (docs/next-editor.md): one step to undo, every range following the edit as a sam edit moves them (edit.replaceBytes: one sam.Op, the cursor and anchor cells mapped through it as normalAlign's are, then setEditCells), refused whole at capacity as a key is (keep); `join` makes it part of the step before it instead, so a drag that rewrites a number every frame passes false on its first frame and true on the rest and one undo takes the whole drag back; pardes calls none of it; test/lib: a number dragged three frames, the cursor after it following, one undo step for the drag, undone and redone, and a replace that would not fit refused with the value unchanged; hxlib 538 of 658 / --parity 618 of 738 with 0 mismatches, core-test 811 of 950, shell-test 990 of 993, hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, replicas pass, lib-test 10 of 10 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/edit.zig | 18 ++++++++++++++++++ src/lib_editor.zig | 12 ++++++++++++ test/lib/editor.zig | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) (limited to 'test/lib/editor.zig') diff --git a/src/edit.zig b/src/edit.zig index 8c59ffa1..32943c19 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -1563,6 +1563,24 @@ fn mapThroughChanges(chs: []const sam.Op, pos: usize) usize { return @intCast(@as(i64, @intCast(pos)) + delta); } +/// The bytes from `from` to `to` replaced with `bytes` (the library's +/// `replace`), every range following the edit as `normalAlign`'s do; a step +/// to undo of its own when `step`, else part of the one before it. +pub fn replaceBytes(e: Editor, t: *Text, from: usize, to: usize, bytes: []const u8, step: bool) Error!void { + const expl = t.vsel.active and t.vsel.explicit; + const text = try flatSurface(e, t); + var rs: [Text.max_selections]modal.Selection = undefined; + const got = t.ranges(text, 0, &rs); + const ch = [1]sam.Op{.{ .q0 = @intCast(from), .q1 = @intCast(to), .text = bytes }}; + var cells: [Text.max_selections]Text.Cells = undefined; + for (rs[0..got.n], cells[0..got.n]) |r, *cell| { + const c = Text.rangeCells(text, r); + cell.* = .{ .cur = mapThroughChanges(&ch, c.cur), .anc = mapThroughChanges(&ch, c.anc) }; + } + if (step) try pushUndo(e, t); + try setEditCells(e, t, try sam.apply(e.gpa, text, &ch), 0, cells[0..got.n], got.pri, expl); +} + /// `J` / `Alt-J`: join every range's lines with the next, in ONE edit /// (helix join_selections): the newline and the next line's leading blanks /// become one space, or nothing when that line is blank, and a line two diff --git a/src/lib_editor.zig b/src/lib_editor.zig index f9149ef0..9bfa5cff 100644 --- a/src/lib_editor.zig +++ b/src/lib_editor.zig @@ -384,6 +384,18 @@ pub fn Editor(comptime cap: Capacity) type { return ed.keep(a, live, .done); } + /// The bytes from `from` to `to` replaced with `with`, as one step + /// to undo, the ranges following as an edit moves them (one inside + /// the bytes goes to their start). `join`: part of the step before + /// it instead, as the frames of a drag after its first, so one undo + /// takes the whole drag back. + pub fn replace(ed: *Self, a: std.mem.Allocator, from: usize, to: usize, with: []const u8, join: bool) Outcome { + std.debug.assert(from <= to and to <= ed.len()); + const live = ed.thaw(a, null) catch return .{ .refused = .out_of_memory }; + edit.replaceBytes(live.context(a), &live.text, from, to, with, !join) catch return .{ .refused = .out_of_memory }; + return ed.keep(a, live, .done); + } + /// The register `name`'s values, one per range that wrote it. pub fn register(ed: *const Self, a: std.mem.Allocator, name: u21) error{OutOfMemory}!?[]const []const u8 { var r: Registers = .{}; diff --git a/test/lib/editor.zig b/test/lib/editor.zig index 2b234cd8..2784b176 100644 --- a/test/lib/editor.zig +++ b/test/lib/editor.zig @@ -178,3 +178,39 @@ test "with a parse handed in, the tree-sitter walks run in the library; without, _ = try ed.ranges(a, &out); try std.testing.expectEqual(pe.Selection{ .anchor = 0, .head = 25 }, out[0]); } + +test "a byte range replaced is one step to undo, the ranges following it; a drag joined is one step, refused whole when it does not fit" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + try ed.load("a = 12 + 3;\n"); + try typeKeys(ed, gpa, "9l"); + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var buf: [4096]u8 = undefined; + var out: [pe.max_selections]pe.Selection = undefined; + // a drag on the number: its first frame a step, the rest joined to it + try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 6, "130", false)); + _ = try ed.ranges(a, &out); + try std.testing.expectEqual(pe.Selection{ .anchor = 10, .head = 11 }, out[0]); // still on `3` + const steps = ed.undo.count; + try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "131", true)); + try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "14", true)); + try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf)); + try std.testing.expectEqual(steps, ed.undo.count); + _ = try ed.ranges(a, &out); + try std.testing.expectEqual(pe.Selection{ .anchor = 9, .head = 10 }, out[0]); + try typeKeys(ed, gpa, "u"); + try std.testing.expectEqualStrings("a = 12 + 3;\n", text(ed, &buf)); + try typeKeys(ed, gpa, "U"); + try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf)); + // what would not fit: refused, the value as it was + const kept = try gpa.create(Ed); + defer gpa.destroy(kept); + kept.* = ed.*; + const ys: [4096]u8 = @splat('y'); + try std.testing.expectEqual(pe.Outcome{ .refused = .text_full }, ed.replace(a, 0, 1, &ys, false)); + try std.testing.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed)); +} -- cgit v1.3