From 779ff8cc27014b8c2f2f078985ab2ca9cf285740 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 05:45:38 -0300 Subject: Editor: what an edit of a text runs with, out of Pardes (docs/next-editor.md, R1): src/Editor.zig is a context of the allocators and pointers to the registers, the register the running command names, the replay's multi_* and the retired text, and the core (`p`), gathered per call by `p.editor()` from where they are, so no field of Pardes or its view moves and a dump crosses builds as before (an embedded `ed: Editor` would have renamed `registers` in the dump, matched by name, and lost them at an Upgrade); setYank, registerValue, retireEditText and normal.multiOnce take it, their callers hand `p.editor()`; core-test 810 of 949, shell-test 989 of 992, hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, replicas pass --- src/Editor.zig | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/Editor.zig (limited to 'src/Editor.zig') diff --git a/src/Editor.zig b/src/Editor.zig new file mode 100644 index 00000000..559ea196 --- /dev/null +++ b/src/Editor.zig @@ -0,0 +1,54 @@ +//! What an edit of a text runs with (normal.zig, edit.zig): the allocators, +//! the registers, the register the running command names, the replay of a +//! key across the ranges, the text an edit made that nothing kept, and the +//! core the text's pane is in. Gathered for each call from where pardes +//! keeps them (`Pardes.editor`); the library gathers it from the value a +//! sketch stores (src/lib_editor.zig). Nothing here is kept between calls. +const std = @import("std"); +const Registers = @import("Registers.zig"); +const Pardes = @import("Core.zig"); + +const Editor = @This(); + +gpa: std.mem.Allocator, +/// What an edit makes and drops: the update's scratch. +scratch: std.mem.Allocator, +registers: *Registers, +/// The register the running normal command names (`"`); 0 for the +/// default one. +register: *u21, +/// While a key is replayed across the ranges (normal.replaySels): whether +/// this is the first pass, whether a pass stopped the replay, which range +/// this pass is, in document order, and how many there are; outside a +/// replay, the one range and its first pass. A register value belongs to +/// its range by `multi_index`. +multi_first: *bool, +multi_stop: *bool, +multi_index: *usize, +multi_count: *usize, +/// An edited text nothing kept (a tag's shown text): see +/// edit.retireEditText. +retired_edit: *?[]u8, +/// The core the text's pane is in: what an edit reaches past its text with. +p: *Pardes, + +pub fn of(p: *Pardes) Editor { + return .{ + .gpa = p.gpa, + .scratch = p.scratch.allocator(), + .registers = &p.registers, + .register = &p.view().register, + .multi_first = &p.multi_first, + .multi_stop = &p.multi_stop, + .multi_index = &p.multi_index, + .multi_count = &p.multi_count, + .retired_edit = &p.retired_edit, + .p = p, + }; +} + +/// An edit that failed, said on the message row of the pane the keyboard +/// is in. +pub fn reportError(e: Editor, err: anyerror) void { + e.p.reportError(e.p.view().active, "edit", err); +} -- cgit v1.3 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 --- build.zig | 278 ++++++++++++++++---------- src/Editor.zig | 17 +- src/Text.zig | 5 +- src/config.zig | 6 +- src/edit.zig | 43 ++-- src/keys.zig | 6 +- src/lib_editor.zig | 550 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/normal.zig | 104 ++++++---- test/lib/build.zig | 24 ++- test/lib/editor.zig | 144 ++++++++++++++ 10 files changed, 1010 insertions(+), 167 deletions(-) create mode 100644 src/lib_editor.zig create mode 100644 test/lib/editor.zig (limited to 'src/Editor.zig') diff --git a/build.zig b/build.zig index afc4e4f3..6a9d4d9f 100644 --- a/build.zig +++ b/build.zig @@ -338,94 +338,7 @@ pub fn build(b: *std.Build) void { // each entry to matchPattern, which its backtracking recurses through, // spends a step, and with none left the match fails and says so // (src/regexp.zig). A bump that moves the anchor stops the build. - const mvzr_mod = mvzr: { - const dep = b.dependency("mvzr", .{ .target = target, .optimize = optimize }); - const src = configureRead(b, io, dep.builder.root, "src/mvzr.zig", 1 << 20) catch @panic("read mvzr"); - const anchor = "fn matchPattern(patt: []const RegOp, sets: []const CharSet, haystack: []const u8, i_in: usize) ?OpMatch {\n"; - const at = (std.mem.indexOf(u8, src, anchor) orelse @panic("mvzr's matchPattern changed: redo its step budget in build.zig")) + anchor.len; - const spend = - \\ if (steps_left == 0) { - \\ exhausted = true; - \\ return null; - \\ } - \\ steps_left -= 1; - \\ - ; - const budget = - \\ - \\/// pardes's patch (its build.zig): matchPattern entries left before a - \\/// match gives up, and whether one did. Set by the caller per search. - \\pub threadlocal var steps_left: u64 = std.math.maxInt(u64); - \\pub threadlocal var exhausted: bool = false; - \\ - ; - const budgeted = b.fmt("{s}{s}{s}{s}", .{ src[0..at], spend, src[at..], budget }); - // A group's greedy match never gave back to what follows the group: - // on `aab`, `(a+)ab` missed where `a+ab` matched. hasAlt counts the - // group's own `)` as an alternative, so every group with pattern - // after it takes matchGroup's alternatives branch, which tries each - // alternative's one (greedy) match and gives up. When the rest fails - // after it, try that alternative's shorter matches, longest first - // (docs/mvzr-group-backtrack.md is the report for upstream). - const group_anchor = - \\ const next_match = matchPattern(next_patt, sets, haystack, m1.i); - \\ if (next_match) |m2| { - \\ // Whole group matches, and we can use m2.j - \\ return m2; - \\ } else { // Try our next pattern, if any. - \\ - ; - const group_backoff = - \\ const next_match = matchPattern(next_patt, sets, haystack, m1.i); - \\ if (next_match) |m2| { - \\ // Whole group matches, and we can use m2.j - \\ return m2; - \\ } else { // Try our next pattern, if any. - \\ // pardes's patch (its build.zig): back the group off. - \\ var e = m1.i; - \\ while (e > i) { - \\ e -= 1; - \\ const shorter = matchAlt(remaining_patt, sets, haystack[0..e], i) orelse continue; - \\ if (shorter.i != e) continue; - \\ if (matchPattern(next_patt, sets, haystack, e)) |whole| return whole; - \\ } - \\ - ; - if (std.mem.count(u8, budgeted, group_anchor) != 1) @panic("mvzr's matchGroup changed: redo its group backtracking in build.zig"); - const backed = std.mem.replaceOwned(u8, b.allocator, budgeted, group_anchor, group_backoff) catch @panic("OOM"); - // `$` is refused anywhere but last, so `h$|zz` was a bad pattern - // while `zz|h$` was fine. An alternative or a group ends at `|` and - // `)`, and the matcher's `.end` ends the alternative it is in, so - // `$` may stand before either (docs/mvzr-group-backtrack.md). - const end_anchor = - \\ '$' => { - \\ if (i + 1 < in.len) { - \\ - ; - const end_before_alt = - \\ '$' => { - \\ // pardes's patch (its build.zig): `$` may end an alternative. - \\ if (i + 1 < in.len and in[i + 1] != '|' and in[i + 1] != ')') { - \\ - ; - if (std.mem.count(u8, backed, end_anchor) != 1) @panic("mvzr's parser changed: redo its `$` before `|` in build.zig"); - var patched = std.mem.replaceOwned(u8, b.allocator, backed, end_anchor, end_before_alt) catch @panic("OOM"); - // Zig 0.17 removed `**`; mvzr still has it (its PR #16 replaces it upstream). - // A class's range ending at `\xff` counted to 256 in a u8, an - // overflow (a panic in a safe build, a runaway loop in a fast one): - // `[\x80-\xff]` in any search took the editor down. - for ([_][3][]const u8{ - .{ "for (c1..c2 + 1) |c_range|", "for (c1..@as(usize, c2) + 1) |c_range|", "1" }, - .{ "[1]RegOp{.{ .unused = {} }} ** ops", "@splat(.{ .unused = {} })", "1" }, - .{ "[1]CharSet{.{ .low = 0, .hi = 0 }} ** char_sets", "@splat(.{ .low = 0, .hi = 0 })", "1" }, - .{ "\"a\" ** 2048", "&@as([2048]u8, @splat('a'))", "2" }, - .{ "\"12345,\" ** 255", "std.mem.asBytes(&@as([255][6]u8, @splat(\"12345,\".*)))", "1" }, - }) |edit| { - if (std.mem.count(u8, patched, edit[0]) != std.fmt.parseInt(usize, edit[2], 10) catch unreachable) std.debug.panic("mvzr changed at `{s}`: redo its `**` patch in build.zig", .{edit[0]}); - patched = std.mem.replaceOwned(u8, b.allocator, patched, edit[0], edit[1]) catch @panic("OOM"); - } - break :mvzr b.createModule(.{ .root_source_file = b.addWriteFiles().add("mvzr.zig", patched), .target = target, .optimize = optimize }); - }; + const mvzr_mod = patchedMvzr(b, io, b.dependency("mvzr", .{ .target = target, .optimize = optimize }), target, optimize); // The C heap every C library with an allocator hook is handed: the core's // tree-sitter and fonts, and pdf.zig's MuPDF, which is a module of its own. @@ -540,18 +453,7 @@ pub fn build(b: *std.Build) void { const no_pdf: ?*std.Build.Step = if (pdf_addon == null) &b.addFail("this step needs the pdf addon, over MuPDF: add -Dpdf (it is fetched once, and on from then)").step else null; if (no_pdf) |s| for ([_]*std.Build.Step{ mupdf_check, pdf_pointer_bench_step, pdf_scroll_bench_step }) |step| step.dependOn(s); - const uucode_config = b.path("uucode_config.zig"); - const uucode_tables = b.dependency("uucode", .{ - .target = target, - .optimize = optimize, - .build_config_path = uucode_config, - }).namedLazyPath("tables.zig"); - const uucode_mod = b.dependency("uucode", .{ - .target = target, - .optimize = optimize, - .build_config_path = uucode_config, - .tables_path = uucode_tables, - }).module("uucode"); + const uucode_mod = uucodeModule(b, target, optimize).?; const vaxis_dep = b.dependency("vaxis", .{ .target = target, @@ -2028,6 +1930,9 @@ const ShellConfig = struct { /// `Build` word's, stamped with the commit (see `stamp_commit`). change: ?[]const u8, built: ?[]const u8, + /// The library's editor (src/lib_editor.zig): a text with no pane, so + /// the core is not compiled (src/Editor.zig). + lib: bool = false, }; fn shellOptions(b: *std.Build, cfg: ShellConfig, platform: Platform) *std.Build.Step.Options { @@ -2047,6 +1952,7 @@ fn shellOptions(b: *std.Build, cfg: ShellConfig, platform: Platform) *std.Build. o.addOption(?[]const u8, "change", cfg.change); o.addOption(?[]const u8, "built", cfg.built); o.addOption([]const u8, "deps", deps_text); + o.addOption(bool, "lib", cfg.lib); return o; } @@ -2214,13 +2120,183 @@ fn library(b: *std.Build) void { .built = null, }, .tui)); const ts = treeSitter(b, io, target, if (optimize == .debug) .fast else optimize, tree_sitter_grammars); + const themes = generatedThemes(b, io, target, optimize); wireCore(mod, .{ - .themes = generatedThemes(b, io, target, optimize), + .themes = themes, .tree_sitter = ts.mod, .ts_libs = ts.libs.items, .ts_queries = ts.queries, .c_heap = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/c_heap.zig") }), }); + // The editor, `pardes-editor`, to a package that asks for it: uucode, + // mvzr and vaxis (with what its build names) are fetched then and only + // then. Of vaxis it takes the grapheme width, which uucode alone feeds. + if (!(b.option(bool, "editor", "also export `pardes-editor`, the text pane as a library (fetches uucode, mvzr and vaxis)") orelse false)) return; + const uucode = uucodeModule(b, target, optimize) orelse return; + const mvzr = patchedMvzr(b, io, b.lazyDependency("mvzr", .{ .target = target, .optimize = optimize }) orelse return, target, optimize); + const vaxis_dep = b.lazyDependency("vaxis", .{ .target = target, .optimize = optimize, .external_uucode = true }) orelse return; + const width = b.addWriteFiles(); + _ = width.addCopyFile(vaxis_dep.path("src/gwidth.zig"), "gwidth.zig"); + const vaxis = b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = width.add("vaxis.zig", "pub const gwidth = @import(\"gwidth.zig\");\n"), + .imports = &.{.{ .name = "uucode", .module = uucode }}, + }); + _ = editorModule(b, "pardes-editor", target, optimize, .{ + .tree_sitter_grammars = tree_sitter_grammars, + .tracy = false, + .workspace_tag = null, + .prebuilt_shaders = false, + .embed_sources = false, + .commit = null, + .change = null, + .built = null, + }, .{ .themes = themes, .tree_sitter = ts.mod, .ts_libs = ts.libs.items, .ts_queries = ts.queries, .uucode = uucode, .mvzr = mvzr, .vaxis = vaxis }); +} + +fn patchedMvzr(b: *std.Build, io: std.Io, dep: *std.Build.Dependency, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Module { + const src = configureRead(b, io, dep.builder.root, "src/mvzr.zig", 1 << 20) catch @panic("read mvzr"); + const anchor = "fn matchPattern(patt: []const RegOp, sets: []const CharSet, haystack: []const u8, i_in: usize) ?OpMatch {\n"; + const at = (std.mem.indexOf(u8, src, anchor) orelse @panic("mvzr's matchPattern changed: redo its step budget in build.zig")) + anchor.len; + const spend = + \\ if (steps_left == 0) { + \\ exhausted = true; + \\ return null; + \\ } + \\ steps_left -= 1; + \\ + ; + const budget = + \\ + \\/// pardes's patch (its build.zig): matchPattern entries left before a + \\/// match gives up, and whether one did. Set by the caller per search. + \\pub threadlocal var steps_left: u64 = std.math.maxInt(u64); + \\pub threadlocal var exhausted: bool = false; + \\ + ; + const budgeted = b.fmt("{s}{s}{s}{s}", .{ src[0..at], spend, src[at..], budget }); + // A group's greedy match never gave back to what follows the group: + // on `aab`, `(a+)ab` missed where `a+ab` matched. hasAlt counts the + // group's own `)` as an alternative, so every group with pattern + // after it takes matchGroup's alternatives branch, which tries each + // alternative's one (greedy) match and gives up. When the rest fails + // after it, try that alternative's shorter matches, longest first + // (docs/mvzr-group-backtrack.md is the report for upstream). + const group_anchor = + \\ const next_match = matchPattern(next_patt, sets, haystack, m1.i); + \\ if (next_match) |m2| { + \\ // Whole group matches, and we can use m2.j + \\ return m2; + \\ } else { // Try our next pattern, if any. + \\ + ; + const group_backoff = + \\ const next_match = matchPattern(next_patt, sets, haystack, m1.i); + \\ if (next_match) |m2| { + \\ // Whole group matches, and we can use m2.j + \\ return m2; + \\ } else { // Try our next pattern, if any. + \\ // pardes's patch (its build.zig): back the group off. + \\ var e = m1.i; + \\ while (e > i) { + \\ e -= 1; + \\ const shorter = matchAlt(remaining_patt, sets, haystack[0..e], i) orelse continue; + \\ if (shorter.i != e) continue; + \\ if (matchPattern(next_patt, sets, haystack, e)) |whole| return whole; + \\ } + \\ + ; + if (std.mem.count(u8, budgeted, group_anchor) != 1) @panic("mvzr's matchGroup changed: redo its group backtracking in build.zig"); + const backed = std.mem.replaceOwned(u8, b.allocator, budgeted, group_anchor, group_backoff) catch @panic("OOM"); + // `$` is refused anywhere but last, so `h$|zz` was a bad pattern + // while `zz|h$` was fine. An alternative or a group ends at `|` and + // `)`, and the matcher's `.end` ends the alternative it is in, so + // `$` may stand before either (docs/mvzr-group-backtrack.md). + const end_anchor = + \\ '$' => { + \\ if (i + 1 < in.len) { + \\ + ; + const end_before_alt = + \\ '$' => { + \\ // pardes's patch (its build.zig): `$` may end an alternative. + \\ if (i + 1 < in.len and in[i + 1] != '|' and in[i + 1] != ')') { + \\ + ; + if (std.mem.count(u8, backed, end_anchor) != 1) @panic("mvzr's parser changed: redo its `$` before `|` in build.zig"); + var patched = std.mem.replaceOwned(u8, b.allocator, backed, end_anchor, end_before_alt) catch @panic("OOM"); + // Zig 0.17 removed `**`; mvzr still has it (its PR #16 replaces it upstream). + // A class's range ending at `\xff` counted to 256 in a u8, an + // overflow (a panic in a safe build, a runaway loop in a fast one): + // `[\x80-\xff]` in any search took the editor down. + for ([_][3][]const u8{ + .{ "for (c1..c2 + 1) |c_range|", "for (c1..@as(usize, c2) + 1) |c_range|", "1" }, + .{ "[1]RegOp{.{ .unused = {} }} ** ops", "@splat(.{ .unused = {} })", "1" }, + .{ "[1]CharSet{.{ .low = 0, .hi = 0 }} ** char_sets", "@splat(.{ .low = 0, .hi = 0 })", "1" }, + .{ "\"a\" ** 2048", "&@as([2048]u8, @splat('a'))", "2" }, + .{ "\"12345,\" ** 255", "std.mem.asBytes(&@as([255][6]u8, @splat(\"12345,\".*)))", "1" }, + }) |edit| { + if (std.mem.count(u8, patched, edit[0]) != std.fmt.parseInt(usize, edit[2], 10) catch unreachable) std.debug.panic("mvzr changed at `{s}`: redo its `**` patch in build.zig", .{edit[0]}); + patched = std.mem.replaceOwned(u8, b.allocator, patched, edit[0], edit[1]) catch @panic("OOM"); + } + return b.createModule(.{ .root_source_file = b.addWriteFiles().add("mvzr.zig", patched), .target = target, .optimize = optimize }); +} + +/// uucode with pardes's configuration (uucode_config.zig), which ghostty +/// and vaxis share; null while it is still to be fetched. +fn uucodeModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) ?*std.Build.Module { + const uucode_config = b.path("uucode_config.zig"); + const uucode_tables = (b.lazyDependency("uucode", .{ + .target = target, + .optimize = optimize, + .build_config_path = uucode_config, + }) orelse return null).namedLazyPath("tables.zig"); + return (b.lazyDependency("uucode", .{ + .target = target, + .optimize = optimize, + .build_config_path = uucode_config, + .tables_path = uucode_tables, + }) orelse return null).module("uucode"); +} + +/// The editor as a library (src/lib_editor.zig, docs/next-editor.md): the +/// text pane's own code built with `lib`, so no pane and no core, over +/// tree-sitter, uucode and mvzr, with pardes-syntax in it as `.syntax`. +/// `library` exports it to a package that asks; hxdiff runs its cases on it. +fn editorModule(b: *std.Build, name: ?[]const u8, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, cfg: ShellConfig, d: struct { + themes: *std.Build.Module, + tree_sitter: ?*std.Build.Module, + ts_libs: []const *std.Build.Step.Compile, + ts_queries: ?*std.Build.Step.Options, + uucode: *std.Build.Module, + mvzr: *std.Build.Module, + /// vaxis's, or a module of its grapheme width alone (`library`): a + /// cell's width for j and k's goal column. + vaxis: *std.Build.Module, +}) *std.Build.Module { + const options: std.Build.Module.CreateOptions = .{ + .target = target, + .optimize = optimize, + .root_source_file = b.path("src/lib_editor.zig"), + .link_libc = true, + }; + const mod = if (name) |n| b.addModule(n, options) else b.createModule(options); + var lib_cfg = cfg; + lib_cfg.lib = true; + mod.addOptions("pardes_config", shellOptions(b, lib_cfg, .tui)); + wireCore(mod, .{ + .plain = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/plain/plain.zig") }), + .themes = d.themes, + .tree_sitter = d.tree_sitter, + .ts_libs = d.ts_libs, + .ts_queries = d.ts_queries, + .mvzr = d.mvzr, + .uucode = d.uucode, + .vaxis = d.vaxis, + .c_heap = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/c_heap.zig") }), + }); + return mod; } fn wireCore(m: *std.Build.Module, d: struct { diff --git a/src/Editor.zig b/src/Editor.zig index 559ea196..014d1923 100644 --- a/src/Editor.zig +++ b/src/Editor.zig @@ -6,7 +6,12 @@ //! sketch stores (src/lib_editor.zig). Nothing here is kept between calls. const std = @import("std"); const Registers = @import("Registers.zig"); -const Pardes = @import("Core.zig"); + +/// The library's build (build.zig `library`): a text with no pane and no +/// core, its characters its own. Where an edit reaches a pane it branches +/// 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 Editor = @This(); @@ -30,7 +35,10 @@ multi_count: *usize, /// edit.retireEditText. retired_edit: *?[]u8, /// The core the text's pane is in: what an edit reaches past its text with. -p: *Pardes, +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, pub fn of(p: *Pardes) Editor { return .{ @@ -44,11 +52,16 @@ pub fn of(p: *Pardes) Editor { .multi_count = &p.multi_count, .retired_edit = &p.retired_edit, .p = p, + .failed = {}, }; } /// An edit that failed, said on the message row of the pane the keyboard /// is in. pub fn reportError(e: Editor, err: anyerror) void { + if (comptime lib) { + e.failed.* = true; + return; + } e.p.reportError(e.p.view().active, "edit", err); } diff --git a/src/Text.zig b/src/Text.zig index 271269ab..fd08bb71 100644 --- a/src/Text.zig +++ b/src/Text.zig @@ -110,7 +110,10 @@ refused: bool = false, /// The window this text belongs to (acme's t->w). A column's or the /// workspace's tag belongs to none, and the optional makes every caller /// say what it does then. -pub fn pane(t: *Text) ?*Pane { +pub inline fn pane(t: *Text) ?*Pane { + // the library's text has none (lib_editor.zig): known at compile time, + // so what an edit does with a pane is not compiled there + if (comptime @import("pardes_config").lib) return null; return switch (t.what) { .body => @alignCast(@fieldParentPtr("body", t)), .tag => @alignCast(@fieldParentPtr("tag", t)), diff --git a/src/config.zig b/src/config.zig index c7078d8e..43a68dd3 100644 --- a/src/config.zig +++ b/src/config.zig @@ -4,7 +4,7 @@ const layout = @import("layout.zig"); const limits = @import("memory.zig").limits; const pardes = @import("pardes.zig"); const builtins = @import("builtins.zig"); -const Key = pardes.Key; +const Key = @import("plain").Key; const Mouse = pardes.Mouse; const Builtin = builtins.registry.Builtin(); @@ -38,8 +38,8 @@ pub const leader_args = [_]struct { path: []const u8, cmd: Builtin, arg: []const comptime { @setEvalBranchQuota(20_000); // A path here is a leaf: no builtin's path is it or runs past it, and - // none of it is a builtin's whole path. - for (leader_args) |la| for (std.enums.values(Builtin)) |b| if (leader_path.get(b)) |path| { + // none of it is a builtin's whole path. The library has no builtins. + if (!@import("pardes_config").lib) for (leader_args) |la| for (std.enums.values(Builtin)) |b| if (leader_path.get(b)) |path| { if (std.mem.startsWith(u8, path, la.path) or std.mem.startsWith(u8, la.path, path)) @compileError("leader_args path collides with " ++ @tagName(b) ++ ": " ++ la.path); }; diff --git a/src/edit.zig b/src/edit.zig index 4b0e59fe..4a48cfd4 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -17,19 +17,19 @@ const syntax = @import("syntax.zig"); const indents = @import("indent.zig"); const platform = pardes.platform; const Pane = panes.Pane; -const Text = panes.Text; +const Text = @import("Text.zig"); const Layer = @import("Layer.zig"); const TagHit = pardes.TagHit; const BOX_H = pardes.BOX_H; const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET; const Mouse = pardes.Mouse; -const Key = pardes.Key; -const hit = pardes.hit; +const Key = @import("plain").Key; +const hit = @import("Event.zig").hit; const sel_slot = pardes.sel_slot; const Effect = pardes.Effect; const Pardes = pardes.Pardes; -const Registers = pardes.Registers; -const Editor = pardes.Editor; +const Registers = @import("Registers.zig"); +const Editor = @import("Editor.zig"); /// An edit's failure, memory or a terminal's rows not read, said by its key. pub const Error = error{ OutOfMemory, WriteFailed, UnknownPoint }; @@ -45,7 +45,8 @@ pub fn setYank(e: Editor, text: []const u8) error{OutOfMemory}!void { else => {}, } try e.registers.put(e.gpa, name, text, e.multi_index.*, e.multi_count.*, e.multi_first.*); - if (name == '+' and e.multi_first.*) e.p.emit(.{ .set_clipboard = {} }); + // the library's `+` is a register its caller fills and reads + if (comptime !Editor.lib) if (name == '+' and e.multi_first.*) e.p.emit(.{ .set_clipboard = {} }); } /// Value `i` of register `name` as the command at range `i` reads it, the @@ -526,11 +527,12 @@ pub fn terminalLines(p: *Pardes, pane: *Pane) ![]const []const u8 { pub fn flatSurface(e: Editor, t: *Text) Error![]const u8 { switch (t.what) { - .body => {}, + // the library's text has no pane: its characters are its own + .body => if (comptime Editor.lib) return t.own orelse "", // the tag as it is shown: the keyboard reaches its prefix too .tag => return if (t.pane()) |pane| try tagline.tagText(e.p, e.scratch, pane) else "", .input => return t.own orelse "", - .column, .workspace => return t.own orelse tagline.headerDefault(e.p, t.what), + .column, .workspace => return t.own orelse if (comptime Editor.lib) "" else tagline.headerDefault(e.p, t.what), } const pane = t.pane() orelse return ""; if (pane.file) |f| return f.content; @@ -574,7 +576,7 @@ fn editText(e: Editor, t: *Text, lo: i32, hi: i32, col: i32) Error!?panes.EditTe } if (t.what != .body and t.what != .input and t.own == null) t.own = try e.gpa.dupe(u8, try flatSurface(e, t)); - if (t.what != .body) return .{ .text = t.own orelse &.{}, .row0 = 0 }; + if (t.what != .body or comptime Editor.lib) return .{ .text = t.own orelse &.{}, .row0 = 0 }; const pane = t.pane() orelse return null; if (pane.file) |f| return .{ .text = f.content, .row0 = 0 }; if (pane.addon != null) return null; @@ -692,6 +694,16 @@ fn setEditTextSpan(e: Editor, t: *Text, new: []u8, known_end: ?modal.Cursor, spa t.own = new; return; } + if (comptime Editor.lib) { + // the library's text, its own, where the edit ended found as a + // file's is (below) + const found = span orelse panes.File.spanOf(t.own orelse "", new); + const end = known_end orelse modal.positionAt(new, new.len - found.tail); + t.last_edit = .{ .row = @intCast(end.row), .col = @intCast(end.col) }; + if (t.own) |old| e.gpa.free(old); + t.own = new; + return; + } const pane = t.pane() orelse return retireEditText(e, new); // A live query pane's rows are its line 1's (Query.zig): an edit there // is refused, as a tag's prefix refuses one, and one from line 1 into @@ -733,7 +745,8 @@ fn setEditTextSpan(e: Editor, t: *Text, new: []u8, known_end: ?modal.Cursor, spa /// The file whose text `t` edits in place (File.replace), the bytes it puts /// in and no copy of the text: a file's body, not a live query's (whose /// edits `setEditTextSpan` cuts to its line 1). -fn bodyFile(t: *Text) ?*panes.File.State { +inline fn bodyFile(t: *Text) ?*panes.File.State { + if (comptime Editor.lib) return null; if (t.what != .body) return null; const pane = t.pane() orelse return null; const f = if (pane.file) |*file| file else return null; @@ -1814,7 +1827,7 @@ pub fn normalToggleComment(e: Editor, t: *Text) Error!void { } next = @max(next, end); } - const file = if (t.what != .body) null else if (t.pane()) |pane| pane.file else null; + const file = if (Editor.lib or t.what != .body) null else if (t.pane()) |pane| pane.file else null; const ext = if (file) |f| std.Io.Dir.path.extension(f.path) else ""; var token: []const u8 = config.comment_token_default; lang: for (config.comment_tokens) |row| { @@ -2049,8 +2062,11 @@ pub fn pushUndo(e: Editor, t: *Text) error{OutOfMemory}!void { // one keystroke, one undo step — even when it edited at ten cursors. if (!e.multi_first.*) return; switch (t.what) { - // A B1 chord took its one undo point when it began (mouse.zig). - .body => if (e.p.view().chord_hold) return, + // A B1 chord took its one undo point when it began (mouse.zig); + // the library's text keeps its own undo, as a tag does. + .body => if (comptime Editor.lib) + return t.remember(e.gpa, if (t.own) |own| .{ .text = own } else null) + else if (e.p.view().chord_hold) return, .tag, .column, .workspace => return t.remember(e.gpa, if (t.own) |own| .{ .text = own } else null), // a prompt's answer is typed once and gone; it keeps no history .input => return, @@ -2063,6 +2079,7 @@ pub fn pushUndo(e: Editor, t: *Text) error{OutOfMemory}!void { /// Undo (back) or redo one step of the text's history: its file's, its /// terminal overlay's, or a tag's or header's own. pub fn undoStep(e: Editor, t: *Text, back: bool) error{OutOfMemory}!void { + if (comptime Editor.lib) return stepOwn(e, t, back); if (t.what == .tag or t.what == .column or t.what == .workspace) return stepOwn(e, t, back); if (t.what != .body) return; const pane = t.pane() orelse return; diff --git a/src/keys.zig b/src/keys.zig index 0277128b..05148fed 100644 --- a/src/keys.zig +++ b/src/keys.zig @@ -442,11 +442,7 @@ pub fn handleKey(p: *Pardes, key: Key) void { }; const was = t.mode; switch (t.mode) { - .normal => normal.handleNormal(p.editor(), t, key), - .insert => if (hit(key, config.escape) and !t.reg_wait) - edit.exitInsert(p.editor(), t) - else - edit.handleInsert(p.editor(), t, key), + .normal, .insert => _ = normal.textKey(p.editor(), t, key), .tty => forwardKey(p, p.view().active, key), } // `.`'s log: the keys of the normal command that entered insert diff --git a/src/lib_editor.zig b/src/lib_editor.zig new file mode 100644 index 00000000..5430285a --- /dev/null +++ b/src/lib_editor.zig @@ -0,0 +1,550 @@ +//! pardes's text pane as a library: the `pardes-editor` module a package +//! that depends on pardes imports (build.zig's `library` with `.editor = +//! true`, docs/next-editor.md). In-memory editing with helix's keys: no +//! files, commands, builtins, prompts or views, which come back to the +//! caller as an `action`. Nothing is copied here: a key runs the editor's +//! own normal.zig and edit.zig, built with `pardes_config.lib`, where a +//! text has no pane and its characters are its own. +//! +//! The editor is a value of plain data (`Editor(capacity)`): no pointer, +//! no allocator, every capacity in its type, so it can be kept where a +//! program keeps its state, copied anywhere, and outlive the code that +//! made it. Each call is handed an allocator it may use and drop (a +//! frame's arena): the value is thawed into the structures pardes edits, +//! the key runs, and they are frozen back. +const std = @import("std"); +const modal = @import("modal.zig"); +const Text = @import("Text.zig"); +const Registers = @import("Registers.zig"); +const Context = @import("Editor.zig"); +const normal = @import("normal.zig"); +const edit = @import("edit.zig"); +const syntax_impl = @import("syntax.zig"); + +/// pardes's syntax (src/lib_syntax.zig): its grammars, highlighting, +/// themes and line arithmetic. A file belongs to one module, so a package +/// imports this module or `pardes-syntax`, not both. +pub const syntax = @import("lib_syntax.zig"); +/// A key read: its codepoint or one of the specials (`Key.enter`, +/// `Key.left`...), the text it types, its modifiers. +pub const Key = @import("plain").Key; +/// A range as helix has it: byte offsets between characters. +pub const Selection = modal.Selection; +pub const Role = modal.Normal.Role; +/// What a normal-mode key means: a role, its count and the character it +/// takes. Built by the caller too, for `run`. +pub const Action = modal.Normal.Action; +pub const RegexMode = modal.Normal.RegexMode; +pub const max_selections = Text.max_selections; +/// A grammar handed in at run time (syntax.zig): its tree-sitter +/// language and its queries' sources, told by `ext`. +pub const Language = syntax_impl.Language; + +pub const Capacity = struct { + /// Bytes of text. + text: u32 = 64 * 1024, + /// Bytes of undo, and as many again of redo: whole-text snapshots, the + /// oldest dropped to make room. At least `text`, so every text that + /// fits is a snapshot that fits. + undo: u32 = 256 * 1024, + /// Bytes of every register's values together. + registers: u32 = 16 * 1024, + /// Ranges, the primary among them. + selections: u16 = 256, +}; + +/// Why a key was not taken: what it made does not fit the value's +/// capacities, or the allocator handed in ran out. The value is as it was +/// before the key. +pub const Refusal = enum { text_full, registers_full, selections_full, out_of_memory }; + +pub const Outcome = union(enum) { + /// The key did what helix does with it (a motion, an edit, a mode). + done, + /// A count, a prefix (`g`, `m`, `"x`) or a pair's character is awaited: + /// `pending()` says which. + pending, + /// Helix binds nothing to it: the caller's to give a meaning. + unbound: Key, + /// Bound, but not the text's to do (a view, `:`, `/`, `s`, LSP, a + /// macro): the caller's. The text and its ranges are as they were. + action: Action, + refused: Refusal, +}; + +pub const Mode = enum { normal, insert, select }; + +/// Text's fields that say where the cursor and the ranges are and what is +/// pending, kept here under their names: thaw and freeze copy each. +pub const Cursor = struct { + mode: Text.Mode = .normal, + cur_pinned: bool = false, + cur_row: i32 = 0, + cur_col: i32 = 0, + vsel: Text.CharSel = .{}, + nsel: u16 = 0, + select: bool = false, + sticky_col: i32 = -1, + restore_cursor: bool = false, + last_edit: @FieldType(Text, "last_edit") = null, + reg_wait: bool = false, + normal: modal.Normal.State = .{}, + find_op: ?modal.Normal.Find = null, + find_ch: u21 = 0, +}; + +/// Records of whole bytes in a ring, each `[len][payload][len]`, the newest +/// at `head`: pushing over a full ring drops the oldest. A record keeps its +/// place while it is held, so a push changes the pages it is written to and +/// no others. +pub fn Ring(comptime size: u32) type { + return struct { + const R = @This(); + bytes: [size]u8 = @splat(0), + head: u32 = 0, + used: u32 = 0, + count: u32 = 0, + + fn copyOut(r: *const R, at: u32, out: []u8) void { + const first = @min(out.len, size - at); + @memcpy(out[0..first], r.bytes[at..][0..first]); + @memcpy(out[first..], r.bytes[0 .. out.len - first]); + } + + fn copyIn(r: *R, at: u32, in: []const u8) void { + const first: u32 = @intCast(@min(in.len, size - at)); + @memcpy(r.bytes[at..][0..first], in[0..first]); + @memcpy(r.bytes[0 .. in.len - first], in[first..]); + } + + fn lenAt(r: *const R, at: u32) u32 { + var b: [4]u8 = undefined; + r.copyOut(at, &b); + return std.mem.readInt(u32, &b, .little); + } + + /// Where the record `skip` records below the newest ends. + fn endOf(r: *const R, skip: u32) u32 { + var at = r.head; + for (0..skip) |_| at = (at + size - (r.lenAt((at + size - 4) % size) + 8)) % size; + return at; + } + + /// The payload of the record `skip` below the newest, copied out. + pub fn get(r: *const R, a: std.mem.Allocator, skip: u32) ![]u8 { + const end = r.endOf(skip); + const len = r.lenAt((end + size - 4) % size); + const out = try a.alloc(u8, len); + r.copyOut((end + size - 4 - len) % size, out); + return out; + } + + pub fn pop(r: *R) void { + if (r.count == 0) return; + const len = r.lenAt((r.head + size - 4) % size); + r.head = (r.head + size - (len + 8)) % size; + r.used -= len + 8; + r.count -= 1; + } + + /// False when the payload alone is more than the ring holds. + pub fn fits(len: usize) bool { + return len + 8 <= size; + } + + pub fn push(r: *R, payload: []const u8) void { + std.debug.assert(fits(payload.len)); + const len: u32 = @intCast(payload.len); + while (r.used + len + 8 > size) { + const tail = (r.head + size - r.used) % size; + r.used -= r.lenAt(tail) + 8; + r.count -= 1; + } + var n: [4]u8 = undefined; + std.mem.writeInt(u32, &n, len, .little); + r.copyIn(r.head, &n); + r.copyIn((r.head + 4) % size, payload); + r.copyIn((r.head + 4 + len) % size, &n); + r.head = (r.head + len + 8) % size; + r.used += len + 8; + r.count += 1; + } + + pub fn clear(r: *R) void { + r.used = 0; + r.count = 0; + } + }; +} + +/// A snapshot's place in a ring record, before its text. +const SnapMeta = extern struct { cur_row: i32, cur_col: i32, vrow: i32, vcol: i32, active: u8, explicit: u8, has_text: u8, _: u8 = 0 }; + +fn snapBytes(a: std.mem.Allocator, s: Text.Snapshot) ![]u8 { + const text = if (s.ovl) |o| o.text else ""; + const out = try a.alloc(u8, @sizeOf(SnapMeta) + text.len); + const meta: SnapMeta = .{ + .cur_row = s.cur_row, + .cur_col = s.cur_col, + .vrow = s.vsel.row, + .vcol = s.vsel.col, + .active = @intFromBool(s.vsel.active), + .explicit = @intFromBool(s.vsel.explicit), + .has_text = @intFromBool(s.ovl != null), + }; + @memcpy(out[0..@sizeOf(SnapMeta)], std.mem.asBytes(&meta)); + @memcpy(out[@sizeOf(SnapMeta)..], text); + return out; +} + +fn snapOf(bytes: []u8) Text.Snapshot { + var meta: SnapMeta = undefined; + @memcpy(std.mem.asBytes(&meta), bytes[0..@sizeOf(SnapMeta)]); + return .{ + .ovl = if (meta.has_text != 0) .{ .text = bytes[@sizeOf(SnapMeta)..] } else null, + .cur_row = meta.cur_row, + .cur_col = meta.cur_col, + .vsel = .{ .active = meta.active != 0, .row = meta.vrow, .col = meta.vcol, .explicit = meta.explicit != 0 }, + }; +} + +/// What a key runs on: the value thawed (`Text`, `Registers`), the replay's +/// state, and what was thawed from the rings, to tell what the key did to +/// them. +const Live = struct { + text: Text, + registers: Registers = .{}, + register: u21 = 0, + multi_first: bool = true, + multi_stop: bool = false, + multi_index: usize = 0, + multi_count: usize = 1, + retired_edit: ?[]u8 = null, + failed: bool = false, + undo_thawed: usize = 0, + redo_thawed: usize = 0, + /// The ring records thawed, by their text's address: a snapshot still + /// in a history after the key is one of them when its text is. + undo_from: [1]?[*]const u8 = @splat(null), + redo_from: [2]?[*]const u8 = @splat(null), + + fn context(l: *Live, a: std.mem.Allocator) Context { + return .{ + .gpa = a, + .scratch = a, + .registers = &l.registers, + .register = &l.register, + .multi_first = &l.multi_first, + .multi_stop = &l.multi_stop, + .multi_index = &l.multi_index, + .multi_count = &l.multi_count, + .retired_edit = &l.retired_edit, + .p = {}, + .failed = &l.failed, + }; + } +}; + +fn textPtr(s: Text.Snapshot) ?[*]const u8 { + return if (s.ovl) |o| o.text.ptr else null; +} + +pub fn Editor(comptime cap: Capacity) type { + if (cap.undo < cap.text + @sizeOf(SnapMeta) + 8) @compileError("pardes-editor: an undo ring holds at least one whole text"); + if (cap.selections < 1 or cap.selections > Text.max_selections) @compileError("pardes-editor: 1 to max_selections ranges"); + return struct { + const Self = @This(); + pub const capacity = cap; + + /// The text as a gap buffer: `front` bytes at its start and `back` + /// at its end, the gap where the last edit was, so that typing + /// changes the page it types into and not every page after it. + text: [cap.text]u8 = @splat(0), + front: u32 = 0, + back: u32 = 0, + /// One more for each key that changed the text: what a parse of it + /// (`Syntax`) is kept against. + revision: u32 = 0, + cursor: Cursor = .{}, + /// The ranges beside the primary, `cursor.nsel` of them. + sels: [cap.selections - 1]Text.SelRange = @splat(.{ .row = 0, .col = 0, .arow = 0, .acol = 0 }), + undo: Ring(cap.undo) = .{}, + redo: Ring(cap.undo) = .{}, + /// Every register, `[name][count]` then each value `[len][bytes]`. + registers: [cap.registers]u8 = @splat(0), + registers_len: u32 = 0, + last_search: u21 = '/', + + pub fn len(ed: *const Self) usize { + return ed.front + ed.back; + } + + /// The text, copied whole into `out` (at least `len()` bytes). + pub fn read(ed: *const Self, out: []u8) []u8 { + @memcpy(out[0..ed.front], ed.text[0..ed.front]); + @memcpy(out[ed.front..][0..ed.back], ed.text[cap.text - ed.back ..]); + return out[0..ed.len()]; + } + + /// The text in `a`'s memory. + pub fn bytes(ed: *const Self, a: std.mem.Allocator) error{OutOfMemory}![]u8 { + return ed.read(try a.alloc(u8, ed.len())); + } + + /// A new text, nothing to undo, the cursor at its start, the + /// registers kept. + pub fn load(ed: *Self, text: []const u8) error{TextFull}!void { + if (text.len > cap.text) return error.TextFull; + @memcpy(ed.text[0..text.len], text); + ed.front = @intCast(text.len); + ed.back = 0; + ed.revision +%= 1; + ed.cursor = .{}; + ed.undo.clear(); + ed.redo.clear(); + } + + pub fn mode(ed: *const Self) Mode { + return switch (ed.cursor.mode) { + .insert => .insert, + else => if (ed.cursor.select) .select else .normal, + }; + } + + /// What the next key completes, null when nothing is awaited. + pub fn pending(ed: *const Self) ?Role { + return ed.cursor.normal.match_sub orelse ed.cursor.normal.prefix; + } + + /// 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 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 }; + 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)) { + // the parse's state goes on; nothing ran + ed.cursor.normal = live.text.normal; + return .{ .action = act }; + } + return ed.keep(a, live, .done); + }, + } + } + + /// 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 }; + normal.runAction(live.context(a), &live.text, act); + return ed.keep(a, live, .done); + } + + /// 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 t = &live.text; + var snap: [max_selections]Selection = undefined; + const got = t.ranges(t.own.?, 0, &snap); + normal.selectByRegex(live.context(a), t, snap[0..got.n], got.pri, t.vsel.active and t.vsel.explicit, pattern, how); + return ed.keep(a, live, .done); + } + + /// 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 e = live.context(a); + const t = &live.text; + edit.pushUndo(e, t) catch return .{ .refused = .out_of_memory }; + edit.setEditText(e, t, a.dupe(u8, text) catch return .{ .refused = .out_of_memory }) catch return .{ .refused = .out_of_memory }; + t.clampCursor(t.own.?, 0); + 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 = .{}; + try ed.readRegisters(a, &r); + const reg = r.get(name) orelse return null; + return @ptrCast(reg.values); + } + + /// 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 }; + 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 { + const live = try a.create(Live); + live.* = .{ .text = .{ .what = .body, .own = try ed.bytes(a), .gpa = a } }; + const t = &live.text; + inline for (@typeInfo(Cursor).@"struct".field_names) |name| @field(t, name) = @field(ed.cursor, name); + if (ed.cursor.nsel > 0) { + t.sels = try a.alloc(Text.SelRange, Text.max_selections - 1); + @memcpy(t.sels[0..ed.cursor.nsel], ed.sels[0..ed.cursor.nsel]); + } + // Of the histories, what one key can reach: the snapshot an + // undo goes back to (or a new one is compared with), and the + // two newest a redo has, which tell a redo (one gone) from + // an edit forgetting them (both). + if (ed.undo.count > 0) { + t.ed_undo[0] = snapOf(try ed.undo.get(a, 0)); + t.ed_undo_len = 1; + live.undo_thawed = 1; + live.undo_from[0] = textPtr(t.ed_undo[0]); + } + const nr = @min(ed.redo.count, 2); + for (0..nr) |i| { + t.ed_redo[i] = snapOf(try ed.redo.get(a, @intCast(nr - 1 - i))); + live.redo_from[i] = textPtr(t.ed_redo[i]); + } + t.ed_redo_len = nr; + live.redo_thawed = nr; + try ed.readRegisters(a, &live.registers); + return live; + } + + fn readRegisters(ed: *const Self, a: std.mem.Allocator, r: *Registers) error{OutOfMemory}!void { + r.last_search = ed.last_search; + var at: usize = 0; + const b = ed.registers[0..ed.registers_len]; + while (at < b.len) { + const name: u21 = @intCast(std.mem.readInt(u32, b[at..][0..4], .little)); + const n = std.mem.readInt(u32, b[at + 4 ..][0..4], .little); + at += 8; + const values = try a.alloc([]u8, n); + for (values) |*v| { + const l = std.mem.readInt(u32, b[at..][0..4], .little); + v.* = try a.dupe(u8, b[at + 4 ..][0..l]); + at += 4 + l; + } + try r.list.append(a, .{ .name = name, .values = values }); + } + } + + /// How many of `thawed` snapshots are still at the bottom of + /// `now`, in order. + fn kept(from: []const ?[*]const u8, now: []const Text.Snapshot) usize { + var k: usize = 0; + while (k < from.len and k < now.len and textPtr(now[k]) == from[k]) k += 1; + return k; + } + + /// Freeze what the key made back into `ed`: all of it fits, or + /// none of it is written and the key is refused. + fn keep(ed: *Self, a: std.mem.Allocator, live: *Live, out: Outcome) Outcome { + const t = &live.text; + if (live.failed) return .{ .refused = .out_of_memory }; + const text = t.own orelse ""; + if (text.len > cap.text) return .{ .refused = .text_full }; + if (t.nsel > cap.selections - 1) return .{ .refused = .selections_full }; + var regs: std.ArrayList(u8) = .empty; + for (live.registers.list.items) |reg| { + var w: [4]u8 = undefined; + std.mem.writeInt(u32, &w, reg.name, .little); + regs.appendSlice(a, &w) catch return .{ .refused = .out_of_memory }; + std.mem.writeInt(u32, &w, @intCast(reg.values.len), .little); + regs.appendSlice(a, &w) catch return .{ .refused = .out_of_memory }; + for (reg.values) |v| { + std.mem.writeInt(u32, &w, @intCast(v.len), .little); + regs.appendSlice(a, &w) catch return .{ .refused = .out_of_memory }; + regs.appendSlice(a, v) catch return .{ .refused = .out_of_memory }; + } + } + if (regs.items.len > cap.registers) return .{ .refused = .registers_full }; + const undo_kept = kept(live.undo_from[0..live.undo_thawed], t.ed_undo[0..t.ed_undo_len]); + const redo_kept = kept(live.redo_from[0..live.redo_thawed], t.ed_redo[0..t.ed_redo_len]); + var undo_new: [Text.history_max][]u8 = undefined; + var redo_new: [Text.history_max][]u8 = undefined; + for (t.ed_undo[undo_kept..t.ed_undo_len], 0..) |s, i| undo_new[i] = snapBytes(a, s) catch return .{ .refused = .out_of_memory }; + for (t.ed_redo[redo_kept..t.ed_redo_len], 0..) |s, i| redo_new[i] = snapBytes(a, s) catch return .{ .refused = .out_of_memory }; + const nu = t.ed_undo_len - undo_kept; + const nr = t.ed_redo_len - redo_kept; + + // Everything fits: write it. + const old = ed.bytes(a) catch return .{ .refused = .out_of_memory }; + if (!std.mem.eql(u8, old, text)) { + // ponytail: O(text) a key, the text compared and placed + // whole; a DSL of a few KiB does not notice. The way up is + // one Buffer under File and the library (the survey's R3): + // the splice journal and line index pardes has for files. + var head: usize = 0; + while (head < old.len and head < text.len and old[head] == text[head]) head += 1; + var tail: usize = 0; + while (tail < old.len - head and tail < text.len - head and old[old.len - 1 - tail] == text[text.len - 1 - tail]) tail += 1; + // the gap goes where the edit ended + const front = text.len - tail; + @memcpy(ed.text[0..front], text[0..front]); + @memcpy(ed.text[cap.text - tail ..], text[front..]); + ed.front = @intCast(front); + ed.back = @intCast(tail); + ed.revision +%= 1; + } + inline for (@typeInfo(Cursor).@"struct".field_names) |name| @field(ed.cursor, name) = @field(t, name); + @memcpy(ed.sels[0..t.nsel], t.sels[0..t.nsel]); + if (live.undo_thawed > undo_kept) ed.undo.pop(); + for (undo_new[0..nu]) |s| ed.undo.push(s); + // both thawed redos gone: an edit forgot them all + if (live.redo_thawed == 2 and redo_kept == 0) ed.redo.clear() else for (redo_kept..live.redo_thawed) |_| ed.redo.pop(); + for (redo_new[0..nr]) |s| ed.redo.push(s); + @memcpy(ed.registers[0..regs.items.len], regs.items); + ed.registers_len = @intCast(regs.items.len); + ed.last_search = live.registers.last_search; + return out; + } + }; +} + +/// A text's parse, made again from its bytes and a `Language` when the +/// text has changed: derived, never stored (a tree holds pointers into +/// the grammar's code, which a reload unloads). Drop it (`forget`) at a +/// reload. +pub const Syntax = struct { + tree: ?*syntax.ts.Tree = null, + revision: u32 = 0, + + /// 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; + 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; + } + + /// One `Syn` a byte of `ed`'s text from `from` to `to`, in `a`. + pub fn colors(a: std.mem.Allocator, ed: anytype, lang: Language, from: usize, to: usize) ![]syntax.Syn { + const text = try ed.bytes(a); + if (!syntax_impl.addLanguage(lang)) return error.TooManyLanguages; + var buf: [64]u8 = undefined; + return @ptrCast(try syntax_impl.highlightFileRange(a, lang.path(&buf), text, from, to)); + } + + /// 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.* = .{}; + syntax_impl.forgetLanguages(); + } +}; diff --git a/src/normal.zig b/src/normal.zig index 77f0c24c..c4c00824 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -14,14 +14,14 @@ const edit = @import("edit.zig"); const config = @import("config.zig"); const keyboard = @import("keys.zig"); const Pane = panes.Pane; -const Text = panes.Text; -const File = panes.File; -const Key = pardes.Key; -const hit = pardes.hit; +const Text = @import("Text.zig"); +const File = @import("File.zig"); +const Key = @import("plain").Key; +const hit = @import("Event.zig").hit; const sel_slot = pardes.sel_slot; const Pardes = pardes.Pardes; const Macro = pardes.Macro; -const Editor = pardes.Editor; +const Editor = @import("Editor.zig"); fn isPrefix(key: Key, cp: u21) bool { return key.cp == cp and !key.ctrl and !key.alt; @@ -55,8 +55,13 @@ pub fn selRegexArmed(pane: *Pane) ?struct { pat: []const u8, mode: modal.Normal. /// had when s/S/K/Alt-K was pressed; the prompt's snapshot of them lives on /// `pane`. pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: modal.Normal.RegexMode) void { - const text = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(p.view().active, "edit", err); - const snap = pane.sel_snap[0..pane.nsel_snap]; + selectByRegex(p.editor(), t, pane.sel_snap[0..pane.nsel_snap], pane.sel_snap_pri, pane.sel_snap_expl, pat, mode); +} + +/// Select (or split on, keep or remove by) `pat` inside `snap`, the +/// ranges there were (primary `pri`) when s/S/K/Alt-K was pressed. +pub fn selectByRegex(e: Editor, t: *Text, snap: []modal.Selection, pri: usize, expl: bool, pat: []const u8, mode: modal.Normal.RegexMode) void { + const text = edit.flatSurface(e, t) catch |err| return e.reportError(err); var out: [Text.max_selections]modal.Selection = undefined; var m: usize = 0; if (regexp.Regex.compile(pat)) |compiled| { @@ -64,7 +69,7 @@ pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: m var hay_all = text; if (for (pat) |c| { if (std.ascii.isUpper(c)) break false; - } else true) hay_all = std.ascii.allocLowerString(p.scratch.allocator(), text) catch |err| return p.reportError(p.view().active, "edit", err); + } else true) hay_all = std.ascii.allocLowerString(e.scratch, text) catch |err| return e.reportError(err); for (snap) |r| { const from = @min(r.anchor, r.head); const to = @min(@max(r.anchor, r.head), text.len); @@ -111,7 +116,7 @@ pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: m r.anchor = @min(r.anchor, text.len); r.head = @min(r.head, text.len); } - return t.setRanges(text, snap, &.{}, pane.sel_snap_pri, pane.sel_snap_expl); + return t.setRanges(text, snap, &.{}, pri, expl); } t.setRanges(text, out[0..m], &.{}, 0, true); // helix keeps primary 0 (its own TODO) } @@ -396,7 +401,7 @@ fn selStart(s: Text.SelRange) struct { row: i32, col: i32 } { } pub fn replaySels(e: Editor, t: *Text, what: Replay) void { - const id = e.p.view().active; + const id = if (comptime Editor.lib) 0 else e.p.view().active; // a header belongs to no pane and cannot go away under the replay const owner = t.pane(); const serial = if (owner) |pane| pane.serial else 0; @@ -459,7 +464,7 @@ pub fn replaySels(e: Editor, t: *Text, what: Replay) void { }; t.sticky_col = list[i].sticky; (switch (what) { - .normal => |normal_action| executeNormalAction(e.p, t, normal_action), + .normal => |normal_action| actionPass(e, t, normal_action), .insert => |insert_key| edit.insertKey(e, t, insert_key), }) catch |err| e.reportError(err); if (e.multi_stop.*) break; @@ -522,22 +527,53 @@ fn normalInput(key: Key) modal.Normal.Input { return out; } -pub fn handleNormal(e: Editor, t: *Text, key: Key) void { +/// A key in a text's normal or insert mode, as keys.zig hands it on after +/// its own intercepts, and as the library takes it (lib_editor.zig): what +/// the parse made of it, `.ignored` for a key typed in insert mode. +pub fn textKey(e: Editor, t: *Text, key: Key) modal.Normal.Result { + switch (t.mode) { + .normal => return handleNormal(e, t, key), + .insert => if (hit(key, config.escape) and !t.reg_wait) + edit.exitInsert(e, t) + else + edit.handleInsert(e, t, key), + .tty => unreachable, // the terminal's (keys.zig) + } + return .ignored; +} + +pub fn handleNormal(e: Editor, t: *Text, key: Key) modal.Normal.Result { // `"` is held by the parser until the command it names arrives const named = t.normal.register; const parsed = modal.Normal.parse(&t.normal, normalInput(key)); const action = switch (parsed) { - .pending, .ignored, .unbound => return, + .pending, .ignored, .unbound => return parsed, .action => |value| value, }; t.normal.register = 0; + // the core's roles are the library's caller's + if (comptime Editor.lib) if (!textRole(action.role)) return parsed; e.register.* = named; defer e.register.* = 0; + runAction(e, t, action); + return parsed; +} + +/// A normal-mode action run across every range, or once for the +/// selection as a whole. +pub fn runAction(e: Editor, t: *Text, action: modal.Normal.Action) void { if (t.nsel == 0 or action.once()) - return executeNormalAction(e.p, t, action) catch |err| e.reportError(err); + return actionPass(e, t, action) catch |err| e.reportError(err); replaySels(e, t, .{ .normal = action }); } +/// One pass of an action: the core's, which hands the text its own roles; +/// the library has the text's alone. +fn actionPass(e: Editor, t: *Text, action: modal.Normal.Action) edit.Error!void { + if (comptime Editor.lib) return textAction(e, t, action); + return executeNormalAction(e.p, t, action); +} + fn startSelRegex(p: *Pardes, pane: *Pane, t: *Text, mode: modal.Normal.RegexMode) edit.Error!void { const text = try edit.flatSurface(p.editor(), t); const got = t.ranges(text, 0, &pane.sel_snap); @@ -805,7 +841,8 @@ fn findMove(t: *Text, text: []const u8, range: modal.Selection, ch: u21, kind: m /// The file pane whose text `text` is, when `t` is its body: its rows are /// measured through their table pads (File.rowPads), so j and k keep the /// column a table shows, not the one its source has. -fn filePane(t: *Text, text: []const u8) ?*const Pane { +inline fn filePane(t: *Text, text: []const u8) ?*const Pane { + if (comptime Editor.lib) return null; if (t.what != .body) return null; const fp = t.pane() orelse return null; const f = if (fp.file) |*file| file else return null; @@ -966,6 +1003,7 @@ pub fn textAction(e: Editor, t: *Text, a: modal.Normal.Action) edit.Error!void { switch (a.role) { .escape => { t.select = false; + if (comptime Editor.lib) return; // typed again by a macro, Esc is only helix's if (e.p.macro.replaying > 0) return; tagline.exitHeader(e.p); // out of a header too, as Shift-Esc @@ -1359,12 +1397,12 @@ test "flat text movement and selection replay need no scratch rows" { p.scratch = .init(denied.allocator()); pane.body.cur_row = 4094; - handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); - handleNormal(p.editor(), &pane.body, .{ .cp = 'j' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'j' }); try std.testing.expectEqual(@as(i32, 4095), pane.body.cur_row); try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); - handleNormal(p.editor(), &pane.body, .{ .cp = 'h' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'h' }); try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col); try executeNormalAction(p, &pane.body, .{ .role = .match_inside, .char = 'w' }); try std.testing.expectEqual(@as(i32, 2), pane.body.cur_col); @@ -1376,8 +1414,8 @@ test "flat text movement and selection replay need no scratch rows" { pane.body.nsel = 1; _ = pane.body.reserveSels(); pane.body.sels[0] = .{ .row = 3072, .col = 0, .arow = 3072, .acol = 0 }; - handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); - handleNormal(p.editor(), &pane.body, .{ .cp = 'j' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'j' }); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); try std.testing.expectEqual(@as(i32, 1025), pane.body.cur_row); try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); @@ -1480,19 +1518,19 @@ test "one cursor holds no room for other ranges; a second range makes it and one const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 }); defer p.deinit(); const pane = try p.setTestFile("alpha beta\nalpha beta\n"); - for ("wxe%;gg") |cp| handleNormal(p.editor(), &pane.body, .{ .cp = cp }); + for ("wxe%;gg") |cp| _ = handleNormal(p.editor(), &pane.body, .{ .cp = cp }); try std.testing.expectEqual(@as(usize, 0), pane.body.sels.len); - handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); try std.testing.expect(pane.body.sels.len > 0); - handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); // replayed at both, still two + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); // replayed at both, still two try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); - handleNormal(p.editor(), &pane.body, .{ .cp = ',' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = ',' }); try std.testing.expectEqual(@as(usize, 0), pane.body.sels.len); - handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); - handleNormal(p.editor(), &pane.body, .{ .cp = ';' }); - handleNormal(p.editor(), &pane.body, .{ .cp = 'x' }); // x at both: still two lines - handleNormal(p.editor(), &pane.body, .{ .cp = '_', .alt = true }); // merged to one + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = ';' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'x' }); // x at both: still two lines + _ = handleNormal(p.editor(), &pane.body, .{ .cp = '_', .alt = true }); // merged to one try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); try std.testing.expectEqual(@as(usize, 0), pane.body.sels.len); } @@ -1503,9 +1541,9 @@ test "cursors that land on one place are one: a key typed there is typed once, a defer p.deinit(); const pane = try p.setTestFile("ab\ncd\n"); // A cursor below, then both up: the second lands on the first. - handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'C' }); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); - handleNormal(p.editor(), &pane.body, .{ .cp = 'k' }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = 'k' }); try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); p.update(.{ .key = .{ .cp = 'i', .text = "i" } }); p.update(.{ .key = .{ .cp = 'x', .text = "x" } }); @@ -1518,10 +1556,10 @@ test "r and Delete take whole graphemes" { defer p.deinit(); const pane = try p.setTestFile("A" ++ "e\u{301}" ++ "👩🏽\u{200d}🚀" ++ "🇧🇷" ++ "界" ++ "Z\n"); // é, the astronaut and the flag: three graphemes, three 界 - for ("lv2lr") |c| handleNormal(p.editor(), &pane.body, .{ .cp = c }); - handleNormal(p.editor(), &pane.body, .{ .cp = '界' }); + for ("lv2lr") |c| _ = handleNormal(p.editor(), &pane.body, .{ .cp = c }); + _ = handleNormal(p.editor(), &pane.body, .{ .cp = '界' }); try std.testing.expectEqualStrings("A界界界界Z\n", pane.file.?.content); - for ("u0li") |c| handleNormal(p.editor(), &pane.body, .{ .cp = c }); + for ("u0li") |c| _ = handleNormal(p.editor(), &pane.body, .{ .cp = c }); p.update(.{ .key = .{ .cp = Key.delete } }); try std.testing.expectEqualStrings("A" ++ "👩🏽\u{200d}🚀" ++ "🇧🇷" ++ "界" ++ "Z\n", pane.file.?.content); } diff --git a/test/lib/build.zig b/test/lib/build.zig index 6cd8d6e5..6f590eda 100644 --- a/test/lib/build.zig +++ b/test/lib/build.zig @@ -1,18 +1,24 @@ //! A package that depends on pardes for its library, as a program that draws //! source text itself would: pardes's `lib-test` step runs `zig build test` //! here, so the `pardes-syntax` module is built the way such a package -//! builds it (build.zig's `library`), with the minimal grammars. +//! builds it (build.zig's `library`), with the minimal grammars, and +//! `pardes-editor`, the text pane as a library, which it asks for. const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .@"tree-sitter" = .minimal }); - const tests = b.addTest(.{ .root_module = b.createModule(.{ - .target = target, - .optimize = optimize, - .root_source_file = b.path("lib.zig"), - .imports = &.{.{ .name = "pardes-syntax", .module = pardes.module("pardes-syntax") }}, - }) }); - b.step("test", "colour, outline and walk a C and a Zig source through pardes's library").dependOn(&b.addRunArtifact(tests).step); + const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .@"tree-sitter" = .minimal, .editor = true }); + const step = b.step("test", "colour, outline and walk a C and a Zig source through pardes's library, and edit a text with its editor"); + // A file belongs to one module: the syntax alone, and the editor (which + // has it as `.syntax`), each a test of its own. + for ([_][2][]const u8{ .{ "lib.zig", "pardes-syntax" }, .{ "editor.zig", "pardes-editor" } }) |t| { + const tests = b.addTest(.{ .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = b.path(t[0]), + .imports = &.{.{ .name = t[1], .module = pardes.module(t[1]) }}, + }) }); + step.dependOn(&b.addRunArtifact(tests).step); + } } 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 'src/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