From ed185f305731309cbddb981166678a552f2fda84 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:42:48 -0300 Subject: examples/live: pardes's editor in State, typed with helix's keys The plain buffer of part 1 gives way to pardes's text pane as a library (`pardes-editor`, src/lib_editor.zig there): `Editor(.{ .text = 4096, .undo = 16 KiB, .registers = 1024, .selections = 64 })`, a value of plain data, 39,264 bytes of a State of 43,480. The package takes pardes by path (`../../../02-pardes-code`, the checkout beside sketchbook's) with `.editor = true` and its smallest tree-sitter set; sketchbook is spelled `../../../sketchbook` as pardes spells it, since a package is told by its path's spelling and sketchbook's build.zig cannot be two modules. `update` turns events into keys: printables from `text` events only, specials (Escape, Return, Backspace, Tab, arrows, Home, End, pages, Delete) and ctrl or alt chords (helix's `A-I` an upper-case letter) from `key_down`, a chord's letter dropped if it also comes as text. Each call into the editor thaws the value into a static 128 KiB buffer reset for the call (`update` may not allocate from the host; a key at 4 KiB of text took 29 KiB of it). The text is compiled when the editor's `revision` moves. Measured (a scratch bench through `update`, the survey's program, 646 bytes, a key typed into its middle, 400 times): a typed key, edit and compile, 59.5 us best and 78.8 mean in Debug, 7.4 and 7.8 in ReleaseFast; a motion 6.4 and 0.3. The package's cold `zig build lib` went from 3.1 s to 6.6 s, its first build under `--watch -fincremental` from 4.0 to 5.9 s, and an edit of main.zig rebuilt under it from 0.062-0.067 s to 0.067-0.092 s. Test: test/live.script types through helix now (`i`, the text, Escape, `gl`, `h`, `rx` for the edit, `l` `d` to break the line, `u` to mend it) and still replays to equal text, tapes and screenshots, its seeks moved to the marks' new times. zig build test: 81 of 81 steps, 6 of 6 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/src/main.zig | 122 +++++++++++++++++++++++++++++++++------------ 1 file changed, 91 insertions(+), 31 deletions(-) (limited to 'examples/live/src/main.zig') diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index 965152a..e62522f 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -18,6 +18,7 @@ const gpu = sk.abi.gpu; const Lang = @import("Lang"); const shaders = @import("shaders"); const scope = @import("scope"); +const pe = @import("pardes-editor"); pub const name = "live"; pub const width = 1280; @@ -31,11 +32,15 @@ const stream = 1; /// The most samples synthesised in one frame. const max_block = 8192; +/// The text: pardes's editor, helix's keys over a value of plain data +/// (src/lib_editor.zig in pardes), its capacities sized to the language's +/// 4 KiB of text. The undo ring keeps whole texts, the oldest dropped: four +/// at the most text, hundreds of a short program. 39,264 bytes in all, of a +/// State of 43,480. +pub const Editor = pe.Editor(.{ .text = 4096, .undo = 16 * 1024, .registers = 1024, .selections = 64 }); + pub const State = struct { - // ponytail: a plain buffer, typed characters appended and backspace - // deleting the last; part 2 puts pardes's editor here. - text: [4096]u8 = @splat(0), - len: u32 = 0, + ed: Editor = .{}, /// The last program that parsed. out: Lang.Tape = .{}, color: Lang.Tape = .{}, @@ -72,35 +77,45 @@ pub fn command(ss: *Session, c: *sk.Console, line: []const u8) !bool { return true; } +/// What the editor thaws its value into for each call, reset after it: +/// `update` may not allocate from the host, and a fixed buffer keeps a +/// call deterministic and bounded. A key at the most text took 29 KiB of it. +var scratch: [128 * 1024]u8 = undefined; + pub fn update(s: *State, in: *const sk.Input) void { - var edited = false; - for (in.events) |e| switch (e.kind) { - .tick => s.time += e.x, - .text => { - var utf8: [4]u8 = undefined; - const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; - if (s.len + n > s.text.len) continue; - @memcpy(s.text[s.len..][0..n], utf8[0..n]); - s.len += n; - edited = true; - }, - .key_down => switch (e.key()) { - .backspace => if (s.len > 0) { - s.len -= 1; - while (s.len > 0 and s.text[s.len] & 0xc0 == 0x80) s.len -= 1; - edited = true; + const revision = s.ed.revision; + // a chord's key_down may come with its letter as text too: not typed + var chord = false; + for (in.events) |e| { + var utf8: [4]u8 = undefined; + const k: pe.Key = switch (e.kind) { + .tick => { + s.time += e.x; + continue; }, - .@"return", .keypad_enter => if (s.len < s.text.len) { - s.text[s.len] = '\n'; - s.len += 1; - edited = true; + .text => text: { + const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; + if (chord) { + chord = false; + continue; + } + break :text .{ .cp = @intCast(@min(e.value, 0x10ffff)), .text = utf8[0..n] }; }, - else => {}, - }, - else => {}, - }; - if (!edited) return; - switch (Lang.compile(s.text[0..s.len])) { + .key_down => chord: { + const k = key(e) orelse continue; + chord = (k.ctrl or k.alt) and k.cp >= ' ' and k.cp < 0x7f; + break :chord k; + }, + else => continue, + }; + var fba: std.heap.FixedBufferAllocator = .init(&scratch); + // ponytail: a key the text has no room for is dropped, as part 1's + // buffer dropped it; the editor leaves the value as it was. + _ = s.ed.key(fba.allocator(), null, k); + } + if (s.ed.revision == revision) return; + var text: [Editor.capacity.text]u8 = undefined; + switch (Lang.compile(s.ed.read(&text))) { .program => |p| { s.out = p.out; s.color = p.color; @@ -110,6 +125,51 @@ pub fn update(s: *State, in: *const sk.Input) void { } } +/// A key_down as the editor takes it: the specials, and chords (ctrl or alt +/// with a letter, digit or sign), helix's `A-I` an upper-case letter with +/// alt. A printable without a chord comes as `text`, so it is not one here. +fn key(e: sk.Event) ?pe.Key { + const mods = e.value; + const ctrl = mods & sk.Key.Mod.ctrl != 0; + const alt = mods & sk.Key.Mod.alt != 0; + const shift = mods & sk.Key.Mod.shift != 0; + const special: u21 = switch (e.key()) { + .escape => pe.Key.escape, + .@"return", .keypad_enter, .return2 => pe.Key.enter, + .backspace => pe.Key.backspace, + .tab => pe.Key.tab, + .delete => pe.Key.delete, + .left => pe.Key.left, + .right => pe.Key.right, + .up => pe.Key.up, + .down => pe.Key.down, + .home => pe.Key.home, + .end => pe.Key.end, + .page_up => pe.Key.page_up, + .page_down => pe.Key.page_down, + else => 0, + }; + if (special != 0) return .{ .cp = special, .ctrl = ctrl, .alt = alt, .shift = shift }; + if (!ctrl and !alt) return null; + const scan = @backingInt(e.key()); + const cp: u21 = switch (e.key()) { + .a, .b, .c, .d, .e, .f, .g, .h, .i, .j, .k, .l, .m, .n, .o, .p, .q, .r, .s, .t, .u, .v, .w, .x, .y, .z => @as(u21, if (shift) 'A' else 'a') + scan - @backingInt(sk.Key.a), + .@"1", .@"2", .@"3", .@"4", .@"5", .@"6", .@"7", .@"8", .@"9" => '1' + @as(u21, scan - @backingInt(sk.Key.@"1")), + .@"0" => '0', + .space => ' ', + .minus => '-', + .equals => '=', + .left_bracket => '[', + .right_bracket => ']', + .semicolon => ';', + .comma => ',', + .period => '.', + .slash => '/', + else => return null, + }; + return .{ .cp = cp, .ctrl = ctrl, .alt = alt }; +} + const synth: gpu.Kernel = .{ .code = shaders.synth_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1 }; const picture_pipeline: gpu.Pipeline = .{ .vertex = .{ .code = shaders.fullscreen_vert }, @@ -220,7 +280,7 @@ const red = 0xff5a50; // ponytail: one face at one size, so a column is a fixed advance; part 2 // draws the editor's text with sketchbook's new text instead. fn code(s: *const State, f: *sk.Frame) void { - const text = s.text[0..s.len]; + const text = s.ed.bytes(f.scratch) catch return; const cell = f.host.textSizeIn(.code, scale, "M"); const lines = std.mem.count(u8, text, "\n") + 1; var widest: f32 = 0; -- cgit v1.3