From 49b24196de1afe33023f0129a3d64bdc9775f1af Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 27 Sep 2026 21:55:14 -0300 Subject: Move the pipe's editor side out of pardes.zig into selection_pipe.zig Pure move, no behaviour change: submitPipe, pipeRequest, pipeFailed, pipeCut, pipeOutput and pipeResponse, the PendingPipe they share, pipeMarker, and the eight pipe tests with their nextPipeEffect helper go verbatim to the end of selection_pipe.zig, so the whole of `|` (runner, boundary values, prompt, request and atomic edit) is one file. Inside that file the `selection_pipe.` prefix drops; the file doc now says it holds both halves. The methods become free functions taking `p: *Pardes`: the four shells' `core.pipeRequest(id)` become `selection_pipe.pipeRequest(core, id)`, and pardes.zig's two calls change the same way. pushUndo becomes pub because the pipe's edit calls it; PendingPipe.deinit becomes pub for Pardes.deinit. Co-Authored-By: Claude Opus 5.5 --- src/selection_pipe.zig | 541 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 536 insertions(+), 5 deletions(-) (limited to 'src/selection_pipe.zig') diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index e1a42962..ea4a4d5b 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -1,10 +1,18 @@ -//! Native runner and boundary-owned values for `|`: pipe every editor -//! selection through one shell command. The core imports only the plain value -//! types below; native shells call `Job.copy` before starting a worker, then -//! `runOne` on that worker. No subprocess or borrowed core memory reaches the -//! editor/event-loop thread. +//! `|`: pipe every editor selection through one shell command. First the +//! native runner and its boundary-owned values: native shells call `Job.copy` +//! before starting a worker, then `runOne` on that worker, so no subprocess or +//! borrowed core memory reaches the editor/event-loop thread. Then the editor +//! side: the prompt, the request the core emits, and the atomic edit its +//! answer makes. const std = @import("std"); const filesystem = @import("fs.zig"); +const pardes = @import("pardes.zig"); +const modal = @import("modal.zig"); +const panes = @import("panes.zig"); +const config = @import("config.zig"); +const Pane = panes.Pane; +const Key = pardes.Key; +const Pardes = pardes.Pardes; /// A deliberately finite answer. One selection cannot retain more than 1 MiB /// and a multi-selection command cannot retain more than 4 MiB in total. @@ -392,3 +400,526 @@ test "native pipe runner preserves stdin/stdout bytes and reports how it failed" .failed => return error.EarlyStdinCloseShouldNotFail, } } + +// ---- the pipe inside the editor: the prompt, the request, the atomic edit ---- + +fn nextPipeEffect(p: *Pardes) ?u32 { + while (p.nextEffect()) |effect| switch (effect) { + .pipe => |request| return request.id, + else => {}, + }; + return null; +} + +/// The tag-tail marker each behaviour arms with. One function so the prompt +/// that is DRAWN and the command that is PARSED can never disagree. +pub fn pipeMarker(how: modal.Normal.PipeBehavior) []const u8 { + return switch (how) { + .replace => config.pipe_marker, + .ignore => config.pipe_marker_to, + .insert => config.pipe_marker_insert, + .append => config.pipe_marker_append, + }; +} + +pub const PendingPipe = struct { + id: u32, + pane: usize, + serial: u32, + revision: u32, + command: []u8, + cwd: []u8, + inputs: []Input, + ranges: [Pane.max_selections]modal.Selection, + primary: u8, + explicit: bool, + how: modal.Normal.PipeBehavior, + nranges: u8, + + pub fn deinit(wait: *PendingPipe, gpa: std.mem.Allocator) void { + gpa.free(wait.command); + gpa.free(wait.cwd); + for (wait.inputs) |input| gpa.free(@constCast(input.bytes)); + gpa.free(wait.inputs); + wait.* = undefined; + } + + fn request(wait: *const PendingPipe) Request { + return .{ + .id = wait.id, + .command = wait.command, + .cwd = wait.cwd, + .inputs = wait.inputs, + }; + } +}; + +pub fn submitPipe(p: *Pardes, id: usize) void { + const pane = p.panes[id] orelse return; + const f = pane.file orelse return; + if (!panes.Output.fileTraits(f.output).saves) return; + const prompt = switch (pane.prompt) { + .pipe => |pipe| pipe, + else => return, + }; + const tail = pane.tagSlice(); + const armed = tail[@min(prompt.at, tail.len)..]; + const marker = pipeMarker(prompt.how); + if (!std.mem.startsWith(u8, armed, marker)) return; + const command = armed[marker.len..]; + if (command.len == 0) return; + + var ranges: [Pane.max_selections]modal.Selection = undefined; + const got = pane.ranges(f.content, 0, &ranges); + // `!`/`A-!` take no stdin and run ONCE — see `PendingPipe.nranges`. + const ninputs = if (prompt.how.pipes()) got.n else 1; + const inputs = p.gpa.alloc(Input, ninputs) catch return; + var made: usize = 0; + for (ranges[0..ninputs], 0..) |range, i| { + const bytes = if (prompt.how.pipes()) bytes: { + const lo = @min(range.anchor, range.head); + const hi = @max(range.anchor, range.head); + if (hi > f.content.len) break; + break :bytes f.content[lo..hi]; + } else ""; + const copy = p.gpa.dupe(u8, bytes) catch break; + inputs[i] = .{ .bytes = copy }; + made += 1; + } + if (made != ninputs) { + for (inputs[0..made]) |input| p.gpa.free(@constCast(input.bytes)); + p.gpa.free(inputs); + return; + } + const command_copy = p.gpa.dupe(u8, command) catch { + for (inputs) |input| p.gpa.free(@constCast(input.bytes)); + p.gpa.free(inputs); + return; + }; + const cwd_copy = p.gpa.dupe(u8, Pardes.paneDir(pane)) catch { + p.gpa.free(command_copy); + for (inputs) |input| p.gpa.free(@constCast(input.bytes)); + p.gpa.free(inputs); + return; + }; + + p.pipe_seq +%= 1; + if (p.pipe_wait) |*old| old.deinit(p.gpa); + p.pipe_wait = .{ + .id = p.pipe_seq, + .pane = id, + .serial = pane.serial, + .revision = f.revision, + .command = command_copy, + .cwd = cwd_copy, + .inputs = inputs, + .ranges = ranges, + .primary = @intCast(got.pri), + .explicit = (pane.vsel.active and pane.vsel.explicit) or pane.msel.active, + .how = prompt.how, + .nranges = @intCast(got.n), + }; + p.emit(.{ .pipe = .{ .id = p.pipe_seq } }); +} + +/// Frontends call this while draining a .pipe effect, then immediately +/// copy the borrowed view into a worker-owned selection_pipe.Job. +pub fn pipeRequest(p: *const Pardes, id: u32) ?Request { + if (p.pipe_wait) |*wait| { + if (wait.id == id) return wait.request(); + } + return null; +} + +fn pipeFailed(p: *Pardes, wait: *const PendingPipe, failure: ?Failure) void { + var out: std.Io.Writer.Allocating = .init(p.gpa); + defer out.deinit(); + const w = &out.writer; + w.print("| {s}\n\n", .{wait.command}) catch return; + if (failure) |fail| { + if (wait.inputs.len > 1) + w.print("selection {d} of {d}: ", .{ fail.index + 1, wait.inputs.len }) catch return; + switch (fail.kind) { + .exit => w.print("exit status {d}\n", .{fail.code}) catch return, + .signal => w.writeAll("killed by a signal\n") catch return, + .timeout => w.print( + "still running after {d} seconds, and stopped\n", + .{command_timeout_seconds}, + ) catch return, + .too_large => w.writeAll("produced more output than a filter may return\n") catch return, + .spawn => w.writeAll("could not be started\n") catch return, + .io => w.writeAll("could not be read\n") catch return, + } + if (fail.stderr.len > 0) w.print("\n{s}", .{fail.stderr}) catch return; + } else { + // No diagnosis at all: nobody ran it. The detached daemon, the + // browser and the board all leave `pipe` null on purpose. + w.writeAll("this session cannot run filters\n") catch return; + } + const content = out.toOwnedSlice() catch return; + const was = p.active; + panes.Output.openErrors(p, wait.pane, content) catch |err| { + p.gpa.free(content); + // Nowhere to put the report is itself worth one line. + p.reportError(wait.pane, "pipe", err); + return; + }; + if (p.panes[was] != null) p.active = was; +} + +fn pipeCut(wait: *const PendingPipe, content: []const u8, i: usize) ?struct { lo: usize, hi: usize } { + const range = wait.ranges[i]; + const lo = @min(range.anchor, range.head); + const hi = @max(range.anchor, range.head); + if (hi > content.len) return null; + return switch (wait.how) { + .replace, .ignore => .{ .lo = lo, .hi = hi }, + .insert => .{ .lo = lo, .hi = lo }, + .append => .{ .lo = hi, .hi = hi }, + }; +} + +fn pipeOutput( + wait: *const PendingPipe, + outputs: []const []const u8, + content: []const u8, + i: usize, +) []const u8 { + var out = if (wait.how.pipes()) outputs[i] else outputs[0]; + const range = wait.ranges[i]; + const lo = @min(range.anchor, range.head); + const hi = @max(range.anchor, range.head); + const selected = if (hi <= content.len) content[lo..hi] else ""; + if (!std.mem.endsWith(u8, selected, "\n") and std.mem.endsWith(u8, out, "\n")) { + out = out[0 .. out.len - 1]; + if (std.mem.endsWith(u8, out, "\r")) out = out[0 .. out.len - 1]; + } + return out; +} + +pub fn pipeResponse( + p: *Pardes, + id: u32, + success: bool, + outputs: []const []const u8, + failure: ?Failure, +) void { + // A SUPERSEDED OR UNKNOWN id is the one silence worth keeping: it is + // the answer to a question nobody is still asking. + if (p.pipe_wait == null or p.pipe_wait.?.id != id) return; + var wait = p.pipe_wait.?; + p.pipe_wait = null; + defer wait.deinit(p.gpa); + + if (!success or outputs.len != wait.inputs.len) return pipeFailed(p, &wait, failure); + + const pane = p.panes[wait.pane] orelse return; + if (pane.serial != wait.serial) return; + const f = if (pane.file) |*file| file else return; + if (!panes.Output.fileTraits(f.output).saves) return; + if (f.revision != wait.revision) + return p.reportError(wait.pane, "pipe", error.FileChangedWhileFiltering); + + if (wait.how == .ignore) return; + + const n = wait.nranges; + var total_output: usize = 0; + var removed: usize = 0; + var previous_end: usize = 0; + for (0..n) |i| { + const cut = pipeCut(&wait, f.content, i) orelse return; + const output = pipeOutput(&wait, outputs, f.content, i); + if (output.len > max_stdout_bytes) return; + total_output = std.math.add(usize, total_output, output.len) catch return; + if (total_output > max_total_stdout_bytes) return; + if (cut.lo < previous_end) return; + removed = std.math.add(usize, removed, cut.hi - cut.lo) catch return; + previous_end = cut.hi; + } + const kept = f.content.len - removed; + const final_len = std.math.add(usize, kept, total_output) catch return; + const replacement = p.gpa.alloc(u8, final_len) catch return; + + var new_ranges: [Pane.max_selections]modal.Selection = undefined; + var read_at: usize = 0; + var write_at: usize = 0; + for (0..n) |i| { + const range = wait.ranges[i]; + const cut = pipeCut(&wait, f.content, i).?; + const output = pipeOutput(&wait, outputs, f.content, i); + @memcpy(replacement[write_at .. write_at + (cut.lo - read_at)], f.content[read_at..cut.lo]); + write_at += cut.lo - read_at; + const out_start = write_at; + @memcpy(replacement[write_at .. write_at + output.len], output); + write_at += output.len; + const out_end = write_at; + new_ranges[i] = if (range.anchor > range.head) + .{ .anchor = out_end, .head = out_start } + else + .{ .anchor = out_start, .head = out_end }; + read_at = cut.hi; + } + @memcpy(replacement[write_at..], f.content[read_at..]); + + // One async request is one history transaction, even at 64 cursors. + p.pushUndo(pane); + panes.File.setContent(p, f, replacement); + pane.setRanges(f.content, new_ranges[0..n], &.{}, wait.primary, wait.explicit); + pane.select = false; + pane.ensureCursorVisible(); +} + +test "selection pipe prompt submits exact request and Escape cancels" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("one\ntwo\n"); + pane.cur_row = 0; + pane.cur_col = 2; + pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + + p.update(.{ .key = .{ .cp = '|' } }); + try std.testing.expect(pane.tag_edit and pane.prompt == .pipe); + try std.testing.expect(std.mem.endsWith(u8, pane.tagSlice(), config.pipe_marker)); + try std.testing.expect(nextPipeEffect(p) == null); + p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(!pane.tag_edit and pane.prompt != .pipe); + const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + const request = pipeRequest(p, id) orelse return error.MissingPipeRequest; + try std.testing.expectEqualStrings("tr a-z A-Z", request.command); + try std.testing.expectEqualStrings("/", request.cwd); + try std.testing.expectEqual(@as(usize, 1), request.inputs.len); + try std.testing.expectEqualSlices(u8, "one", request.inputs[0].bytes); + + const before = pane.file.?.content; + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(!pane.tag_edit and pane.prompt != .pipe); + try std.testing.expectEqualSlices(u8, before, pane.file.?.content); + try std.testing.expect(nextPipeEffect(p) == null); +} + +test "selection pipe replaces all ranges atomically and undo restores them" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("aa bb cc\n"); + const ranges = [_]modal.Selection{ + .{ .anchor = 0, .head = 2 }, + .{ .anchor = 6, .head = 8 }, + }; + pane.setRanges(pane.file.?.content, &ranges, &.{}, 0, true); + + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + const request = pipeRequest(p, id) orelse return error.MissingPipeRequest; + try std.testing.expectEqual(@as(usize, 2), request.inputs.len); + try std.testing.expectEqualSlices(u8, "aa", request.inputs[0].bytes); + try std.testing.expectEqualSlices(u8, "cc", request.inputs[1].bytes); + + var denied = std.testing.FailingAllocator.init(gpa, .{ .fail_index = 0 }); + p.scratch.deinit(); + p.scratch = .init(denied.allocator()); + const outputs: []const []const u8 = &.{ "AA\n", "" }; + p.update(.{ .pipe_resp = .{ .id = id, .success = true, .outputs = outputs } }); + try std.testing.expectEqualSlices(u8, "AA bb \n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 1), pane.file.?.history.undo_len); + try std.testing.expectEqual(@as(u8, 1), pane.nsel); + try std.testing.expectEqual(@as(i32, 1), pane.cur_col); + try std.testing.expectEqual(@as(i32, 6), pane.sels[0].col); + try std.testing.expect(!denied.has_induced_failure); + + p.update(.{ .key = .{ .cp = 'u' } }); + try std.testing.expectEqualSlices(u8, "aa bb cc\n", pane.file.?.content); +} + +test "the four shell behaviours put their output where helix puts it" { + const gpa = std.testing.allocator; + const Case = struct { + key: Key, + outputs: []const []const u8, + want: []const u8, + want_effect: bool = true, + }; + // One selection, `bb`, in `aa bb cc\n` — bytes 3..5. + for ([_]Case{ + // `|` replaces the selection with what the command wrote. + .{ .key = .{ .cp = '|' }, .outputs = &.{"XX"}, .want = "aa XX cc\n" }, + // `!` inserts BEFORE it, and the selection survives to its right. + .{ .key = .{ .cp = '!' }, .outputs = &.{"XX"}, .want = "aa XXbb cc\n" }, + // `A-!` appends AFTER it. + .{ .key = .{ .cp = '!', .alt = true }, .outputs = &.{"XX"}, .want = "aa bbXX cc\n" }, + // `A-|` runs the command and leaves the text completely alone: the + // point is the side effect, not the output. + .{ .key = .{ .cp = '|', .alt = true }, .outputs = &.{"XX"}, .want = "aa bb cc\n" }, + }) |case| { + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("aa bb cc\n"); + pane.setRanges(pane.file.?.content, &.{.{ .anchor = 3, .head = 5 }}, &.{}, 0, true); + + p.update(.{ .key = case.key }); + for ("cmd") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + p.update(.{ .pipe_resp = .{ .id = id, .success = true, .outputs = case.outputs } }); + try std.testing.expectEqualSlices(u8, case.want, pane.file.?.content); + } +} + +test "a command with no stdin runs once and every cursor gets that one answer" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("aa bb cc\n"); + const ranges = [_]modal.Selection{ + .{ .anchor = 0, .head = 2 }, + .{ .anchor = 6, .head = 8 }, + }; + pane.setRanges(pane.file.?.content, &ranges, &.{}, 0, true); + + p.update(.{ .key = .{ .cp = '!' } }); // insert-output: no stdin + for ("date") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + const request = pipeRequest(p, id) orelse return error.MissingPipeRequest; + + try std.testing.expectEqual(@as(usize, 1), request.inputs.len); + try std.testing.expectEqualSlices(u8, "", request.inputs[0].bytes); + + p.update(.{ .pipe_resp = .{ .id = id, .success = true, .outputs = &.{"T"} } }); + try std.testing.expectEqualSlices(u8, "Taa bb Tcc\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 1), pane.file.?.history.undo_len); +} + +test "selection pipe failure and stale completion never mutate the file" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("abc\n"); + pane.cur_col = 2; + pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const failed_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + p.update(.{ .pipe_resp = .{ .id = failed_id, .success = false, .outputs = &.{} } }); + try std.testing.expectEqualSlices(u8, "abc\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); + + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const stale_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + panes.File.setContent(p, &pane.file.?, try gpa.dupe(u8, "changed\n")); + p.update(.{ .pipe_resp = .{ .id = stale_id, .success = true, .outputs = &.{"ABC"} } }); + try std.testing.expectEqualSlices(u8, "changed\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); +} + +test "a failed filter opens an errors buffer carrying the command's own words" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("abc\n"); + pane.cur_col = 2; + pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + + p.update(.{ .key = .{ .cp = '|' } }); + for ("trr") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + + // The shape a real runner brings home for `| trr`: nonzero exit, and the + // shell's own sentence about it. + var stderr = "sh: line 1: trr: command not found\n".*; + p.update(.{ .pipe_resp = .{ + .id = id, + .success = false, + .outputs = &.{}, + .failure = .{ .kind = .exit, .code = 127, .stderr = &stderr }, + } }); + + // The text is untouched — a failed filter is not an edit... + try std.testing.expectEqualSlices(u8, "abc\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); + // ...and the cursor did not go anywhere, so `|` again edits the same file. + try std.testing.expectEqual(@as(usize, 0), p.active); + + var found: ?[]const u8 = null; + for (p.panes) |slot| { + const q = slot orelse continue; + const qf = q.file orelse continue; + const o = qf.output orelse continue; + if (std.meta.activeTag(o.from) == .errors) found = qf.content; + } + const report = found orelse return error.NoErrorsBuffer; + try std.testing.expect(std.mem.indexOf(u8, report, "| trr") != null); + try std.testing.expect(std.mem.indexOf(u8, report, "exit status 127") != null); + try std.testing.expect(std.mem.indexOf(u8, report, "command not found") != null); +} + +test "selection pipe rejects a reused pane slot and a superseded request" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + var pane = try p.setTestFile("old\n"); + pane.cur_col = 2; + pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const replaced_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + pane = try p.setTestFile("new\n"); // same slot, different serial + p.update(.{ .pipe_resp = .{ .id = replaced_id, .success = true, .outputs = &.{"OLD"} } }); + try std.testing.expectEqualSlices(u8, "new\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); + + pane.cur_col = 2; + pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const first_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "tr a-z A-Z" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const latest_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + try std.testing.expect(first_id != latest_id); + + p.update(.{ .pipe_resp = .{ .id = first_id, .success = true, .outputs = &.{"BAD"} } }); + try std.testing.expectEqualSlices(u8, "new\n", pane.file.?.content); + p.update(.{ .pipe_resp = .{ .id = latest_id, .success = true, .outputs = &.{"NEW"} } }); + try std.testing.expectEqualSlices(u8, "NEW\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 1), pane.file.?.history.undo_len); +} + +test "selection pipe binding is file-normal-only" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("x"); + + pane.file.?.output = .{ .from = .search }; + p.update(.{ .key = .{ .cp = '|' } }); + try std.testing.expect(pane.prompt != .pipe); + pane.file.?.output = null; + pane.mode = .insert; + p.update(.{ .key = .{ .cp = '|', .text = "|" } }); + try std.testing.expect(pane.prompt != .pipe); + try std.testing.expectEqualSlices(u8, "|x", pane.file.?.content); +} -- cgit v1.3 From ff953a6236ebdf97c73deda86fd9005906a2071b Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 27 Sep 2026 22:09:27 -0300 Subject: Move text editing out of pardes.zig into edit.zig Pure move, no behaviour change (acme keeps this in text.c): which text a pane edits and how it maps to the screen (editText, editTextEol, setEditText, paneCursorLines, paneByteAtDisplay, pinPaneCursor, flatSurface, paneWrapWidth), insert mode (enterInsert, handleInsert, insertKey, insertTab, exitInsert, clampFileCursor), the d/c/y/p edit operations with replace, case, join, indent, comment, number, textobjects and surround, undo and redo, yank/clipboard/paste (setYank, setClipboard, ClipRequest, clipRequest, typeToTty, applyPaste, clipYank), and the pointer selections as text (PointerTextSelection, pointerTextSelection, capturePointerSelection, paneText, pointerSourceLine, selectionText, spanHas, currentSelText), with four tests, go verbatim to edit.zig. The methods become free functions taking `p: *Pardes`; calls change from `p.insertKey(..)` to `edit.insertKey(p, ..)` (pardes.zig, body_layer.zig, selection_pipe.zig, builtins.zig, test/hxdiff.zig, test/perf.zig). In executeNormalAction the `.edit => |edit|` capture becomes `|op|`, since it would now shadow the edit import. test/lspbench.zig's first anchor follows its needle into src/edit.zig. Co-Authored-By: Claude Opus 5.5 --- src/body_layer.zig | 5 +- src/builtins.zig | 11 +- src/edit.zig | 1692 +++++++++++++++++++++++++++++++++++++++++++ src/fs.zig | 1 + src/pardes.zig | 1874 +++--------------------------------------------- src/selection_pipe.zig | 3 +- test/hxdiff.zig | 2 +- test/lspbench.zig | 2 +- test/perf.zig | 2 +- 9 files changed, 1812 insertions(+), 1780 deletions(-) create mode 100644 src/edit.zig (limited to 'src/selection_pipe.zig') diff --git a/src/body_layer.zig b/src/body_layer.zig index d7ddbfb5..8c345b97 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -5,6 +5,7 @@ //! and cursor. const std = @import("std"); const pardes = @import("pardes.zig"); +const edit = @import("edit.zig"); const config = @import("config.zig"); const limits = @import("memory.zig").limits; const layout = @import("layout.zig"); @@ -180,7 +181,7 @@ fn paintSourceSelection(p: *Pardes, pane: *Pane, r: Rect, rows: []const Pane.Poi var visible: i32 = 0; while (visible < @as(i32, r.h -| BOX_H)) : (visible += 1) { const source = pane.wrapAt(visible); - const line = Pardes.pointerSourceLine(pane, terminal_lines, source.line); + const line = edit.pointerSourceLine(pane, terminal_lines, source.line); const start = @min(line.len, @as(usize, @intCast(@max(0, source.at)))); const next = pane.wrapAt(visible + 1); const end = if (next.line == source.line) @min(line.len, @as(usize, @intCast(@max(0, next.at)))) else line.len; @@ -401,7 +402,7 @@ pub fn renderBody(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, r: Rect, id .arow = if (pane.vsel.active) pane.vsel.row else pane.cur_row, .acol = if (pane.vsel.active) pane.vsel.col else pane.cur_col, } else pane.sels[si]; - const bnd = Pardes.cellBounds(sr); + const bnd = edit.cellBounds(sr); const hover_only = primary and modal_hover and pane.mode != .normal and !preview; const bg = if (hover_only) colors.mix(page_rgb, colors.mix(page_rgb, th.sel_bg)) diff --git a/src/builtins.zig b/src/builtins.zig index ad2c5f6a..87679f64 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1,6 +1,7 @@ //! Command structs and runtime settings form the builtin registry; bindings live in config.zig. const std = @import("std"); const pardes = @import("pardes.zig"); +const edit = @import("edit.zig"); const colors = @import("colors.zig"); const Pardes = pardes.Pardes; const Pane = pardes.Pane; @@ -431,7 +432,7 @@ pub const Ascii = struct { pub const ClipYank = struct { pub fn run(c: Ctx) void { - c.p.clipYank(c.pane, false); + edit.clipYank(c.p, c.pane, false); } }; @@ -439,25 +440,25 @@ pub const ClipYank = struct { /// cursor's. One cursor makes them the same word. pub const ClipYankMain = struct { pub fn run(c: Ctx) void { - c.p.clipYank(c.pane, true); + edit.clipYank(c.p, c.pane, true); } }; pub const ClipPaste = struct { pub fn run(c: Ctx) void { - c.p.clipRequest(c.id, .after); + edit.clipRequest(c.p, c.id, .after); } }; pub const ClipPasteBefore = struct { pub fn run(c: Ctx) void { - c.p.clipRequest(c.id, .before); + edit.clipRequest(c.p, c.id, .before); } }; pub const ClipReplace = struct { pub fn run(c: Ctx) void { - c.p.clipRequest(c.id, .replace); + edit.clipRequest(c.p, c.id, .replace); } }; diff --git a/src/edit.zig b/src/edit.zig new file mode 100644 index 00000000..e44a3364 --- /dev/null +++ b/src/edit.zig @@ -0,0 +1,1692 @@ +//! Editing a pane's text (acme's text.c): which text a pane edits and how +//! its lines and cursor map to the screen, insert mode, the d/c/y/p edit +//! operations, replace, case, join, indent, comments, numbers, textobjects +//! and surround, undo, yank and paste, and the pointer selections as text. +const pardes = @import("pardes.zig"); +const std = @import("std"); +const modal = @import("modal.zig"); +const panes = @import("panes.zig"); +const body_layer = @import("body_layer.zig"); +const config = @import("config.zig"); +const pdf_enabled = panes.Pdf.enabled; +const lsp = @import("lsp/lsp.zig"); +const platform = pardes.platform; +const Pane = panes.Pane; +const TagHit = @import("tag_layer.zig").Hit; +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 sel_slot = pardes.sel_slot; +const Effect = pardes.Effect; +const Pardes = pardes.Pardes; + +pub fn setYank(p: *Pardes, text: []const u8) void { + if (p.multi_on and !p.multi_first) { + const old = p.yank orelse ""; + const sep: []const u8 = if (text.len > 0 and text[text.len - 1] == '\n') "" else "\n"; + const joined = std.fmt.allocPrint(p.gpa, "{s}{s}{s}", .{ text, sep, old }) catch return; + if (p.yank) |y| p.gpa.free(y); + p.yank = joined; + return; + } + if (p.yank) |y| p.gpa.free(y); + p.yank = p.gpa.dupe(u8, text) catch null; +} + +pub fn setClipboard(p: *Pardes, text: []const u8) void { + setYank(p, text); + if (!p.multi_on or p.multi_first) p.emit(.{ .set_clipboard = {} }); +} + +/// Where a `SPC p` / `SPC P` / `SPC R` goes once the shell answers. +pub const ClipRequest = struct { + pane: usize, + serial: u32, + mode: enum { after, before, replace }, +}; + +pub fn clipRequest(p: *Pardes, id: usize, mode: @FieldType(ClipRequest, "mode")) void { + const pane = p.panes[id] orelse return; + p.clip_pending = .{ .pane = id, .serial = pane.serial, .mode = mode }; + p.emit(.read_clipboard); +} + +pub fn typeToTty(p: *Pardes, id: usize, pane: *const Pane, text: []const u8) void { + if (text.len == 0) return; + if (panes.Terminal.bracketedPaste(pane)) { + p.emitWrite(id, "\x1b[200~"); + p.emitWrite(id, text); + p.emitWrite(id, "\x1b[201~"); + return; + } + const cp = p.scratch.allocator().dupe(u8, text) catch return; + for (cp) |*ch| { + if (ch.* == '\n') ch.* = '\r'; + } + p.emitWrite(id, cp); +} + +pub fn applyPaste(p: *Pardes, bytes: []const u8) void { + if (p.topbar_col != null) { + p.clip_pending = null; + p.headerReplace(bytes); + return; + } + const req = p.clip_pending; + p.clip_pending = null; + if (bytes.len == 0) return; + const id = if (req) |r| r.pane else p.active; + const pane = p.panes[id] orelse return; + if (req) |r| if (pane.serial != r.serial) return; + p.active = id; + if (pane.tag_edit) { + const text = p.tagText(p.scratch.allocator(), pane) catch return; + const range: @TypeOf(Pardes.tagSelBounds(pane)) = if (pane.tag_sel) Pardes.tagSelBounds(pane) else .{ .lo = pane.tag_col, .hi = pane.tag_col }; + const hi = if (pane.tag_sel) modal.nextGrapheme(text, range.hi) else range.hi; + _ = p.replaceTagText(pane, range.lo, hi, bytes); + if (Pardes.selRegexArmed(pane)) |a| p.applySelRegex(pane, a.pat, a.split); + return; + } + if (pane.isTerminal() and pane.mode == .tty) return typeToTty(p, id, pane, bytes); + switch (if (req) |r| r.mode else .after) { + .after => pasteText(p, pane, bytes, false, 1), + .before => pasteText(p, pane, bytes, true, 1), + .replace => replaceWithText(p, pane, bytes), + } +} + +pub fn clipYank(p: *Pardes, pane: *Pane, main_only: bool) void { + if (comptime pdf_enabled) if (pane.pdf) |pv| { + if (pv.selection_text.len > 0) setClipboard(p, pv.selection_text); + return; + }; + if (pane.nsel > 0 and !main_only) { + p.replaySels(pane, .{ .normal = .{ .edit = .{ .kind = .yank, .count = 1 } } }); + } else { + const others = pane.nsel; + pane.nsel = 0; + normalYank(p, pane); + pane.nsel = others; + } + p.emit(.{ .set_clipboard = {} }); +} + +/// tag + '\n' + body: the full selectable pane text (row 0 = the tag) +pub fn paneText(p: *Pardes, pane: *Pane) ![]u8 { + const arena = p.scratch.allocator(); + return std.fmt.allocPrint(arena, "{s}\n{s}", .{ try p.tagText(arena, pane), try body_layer.bodyText(p, arena, pane) }); +} + +pub fn pointerSourceLine(pane: *Pane, terminal_lines: []const []const u8, row: i32) []const u8 { + if (row < 0) return ""; + if (pane.file != null) return panes.File.sourceLine(pane, row); + const index: usize = @intCast(row); + return if (index < terminal_lines.len) terminal_lines[index] else ""; +} + +pub fn capturePointerSelection(p: *Pardes, pane: *Pane, slot: usize) !void { + var gesture = pane.sel[slot]; + if (pane.hasPdf()) return; + if (gesture.state != .done or @min(gesture.r0, gesture.r1) < BOX_H) return; + if (pane.pointerSelection(gesture) != null) return; + pane.clearPointerSelection(slot); + // Refresh the source map before translating the rendered rectangle. + const raw = pane.isTerminal() and pane.mode == .tty; + const body = try body_layer.bodyText(p, p.scratch.allocator(), pane); + const terminal_lines = if (pane.file == null and !raw) try panes.Terminal.cursorLines(p, pane) else &.{}; + const first = @max(0, @min(gesture.r0, gesture.r1) - BOX_H); + const logical_rows = if (pane.body_rows > 0) pane.body_rows else pane.rows; + const last = @min(@as(i32, logical_rows) - 1, @max(gesture.r0, gesture.r1) - BOX_H); + if (first > last) return; + const rows = try p.gpa.alloc(Pane.PointerRow, @intCast(last - first + 1)); + errdefer p.gpa.free(rows); + const raw_text = if (raw) try p.gpa.dupe(u8, try selectionText(p, pane, gesture)) else null; + const prefix: i32 = if (pane.file != null) panes.File.gutterWidth(pane) else 0; + const left = @max(0, @min(gesture.c0, gesture.c1)); + const right = @max(0, @max(gesture.c0, gesture.c1)); + for (rows, 0..) |*row, index| { + const visible = first + @as(i32, @intCast(index)); + var source = pane.wrapAt(visible); + if (raw) source = .{ .line = panes.Terminal.gridOffset(pane) + visible, .at = 0 }; + const line = if (raw) modal.lineSlice(body, @intCast(visible)) else pointerSourceLine(pane, terminal_lines, source.line); + var next = pane.wrapAt(visible + 1); + if (raw) next = .{ .line = source.line + 1, .at = 0 }; + const end = if (next.line == source.line) @min(line.len, @as(usize, @intCast(@max(0, next.at)))) else line.len; + const start: usize = @intCast(@max(0, source.at)); + const lo = @min(end, panes.File.byteAtDisplayFrom(line, start, @intCast(@max(0, left - prefix)))); + const at = @min(end, panes.File.byteAtDisplayFrom(line, start, @intCast(@max(0, right - prefix)))); + row.* = .{ + .row = source.line, + .lo = lo, + .hi = if (right < prefix) lo else @min(end, modal.nextGrapheme(line, at)), + .raw_terminal = raw, + .prompt_bytes = if (raw) panes.Terminal.promptPrefixBytes(pane, source.line, line) else 0, + }; + const rendered = modal.lineSlice(body, @intCast(visible)); + const prefix_end = @min(rendered.len, @as(usize, @intCast(@min(prefix, right + 1)))); + const prefix_start = @min(prefix_end, @as(usize, @intCast(left))); + row.prefix_len = @intCast(prefix_end - prefix_start); + @memcpy(row.prefix[0..row.prefix_len], rendered[prefix_start..prefix_end]); + } + pane.next_pointer_selection += 1; + gesture.source_id = pane.next_pointer_selection; + pane.sel[slot] = gesture; + pane.pointer_selections[slot] = .{ .gesture = gesture, .rows = rows, .raw_text = raw_text }; +} + +/// block-selected text, newline-joined per row; reads the rendered screen +/// so typed text and shell output select identically. Scratch-owned. +pub fn selectionText(p: *Pardes, pane: *Pane, sl: Pane.Sel) ![]const u8 { + const arena = p.scratch.allocator(); + if (pane.rawPointerText(sl)) |text| return arena.dupe(u8, text); + if (pane.pointerSelection(sl)) |rows| { + const terminal_lines = if (pane.file == null) try panes.Terminal.cursorLines(p, pane) else &.{}; + var total: usize = rows.len -| 1; + for (rows) |row| { + const line = pointerSourceLine(pane, terminal_lines, row.row); + total += row.prefix_len + @min(row.hi, line.len) - @min(row.lo, line.len); + } + const result = try arena.alloc(u8, total); + var at: usize = 0; + for (rows, 0..) |row, index| { + if (index > 0) { + result[at] = '\n'; + at += 1; + } + @memcpy(result[at..][0..row.prefix_len], row.prefix[0..row.prefix_len]); + at += row.prefix_len; + const line = pointerSourceLine(pane, terminal_lines, row.row); + const bytes = line[@min(row.lo, line.len)..@min(row.hi, line.len)]; + @memcpy(result[at..][0..bytes.len], bytes); + at += bytes.len; + } + return result; + } + const r0 = @min(sl.r0, sl.r1); + const r1 = @max(sl.r0, sl.r1); + const c0: usize = @intCast(@max(0, @min(sl.c0, sl.c1))); + const c1: usize = @intCast(@max(0, @max(sl.c0, sl.c1))); + const text = try paneText(p, pane); + var total: usize = 0; + var selected: usize = 0; + var count_it = std.mem.splitAny(u8, text, "\n"); + var count_row: i32 = 0; + while (count_it.next()) |line| : (count_row += 1) { + if (count_row < r0 or count_row > r1) continue; + const b0 = @min(panes.File.renderedLineByteCol(pane, count_row, line, c0), line.len); + const b1 = modal.nextGrapheme(line, @min(panes.File.renderedLineByteCol(pane, count_row, line, c1), line.len)); + total += b1 - b0 + @intFromBool(selected > 0); + selected += 1; + } + const out = try arena.alloc(u8, total); + var at: usize = 0; + var it = std.mem.splitAny(u8, text, "\n"); + var v: i32 = 0; + var first = true; + while (it.next()) |line| : (v += 1) { + if (v < r0 or v > r1) continue; + if (!first) { + out[at] = '\n'; + at += 1; + } + first = false; + const b0 = @min(panes.File.renderedLineByteCol(pane, v, line, c0), line.len); + const b1 = modal.nextGrapheme(line, @min(panes.File.renderedLineByteCol(pane, v, line, c1), line.len)); + @memcpy(out[at..][0 .. b1 - b0], line[b0..b1]); + at += b1 - b0; + } + return out; +} + +pub fn spanHas(r: i32, c: i32, ar: i32, ac: i32, br: i32, bc: i32) bool { + const fwd = ar < br or (ar == br and ac <= bc); + const sr, const sc = if (fwd) .{ ar, ac } else .{ br, bc }; + const er, const ec = if (fwd) .{ br, bc } else .{ ar, ac }; + return (r > sr or (r == sr and c >= sc)) and (r < er or (r == er and c <= ec)); +} + +/// the active modal selection as text (v range or x lines), if any +pub fn currentSelText(p: *Pardes, pane: *Pane) ?[]const u8 { + if (comptime pdf_enabled) if (p.native_images) if (pane.pdf) |pv| + if (pv.selection != null and pv.selection_text.len > 0) return pv.selection_text; + if (pane.vsel.active) return vselText(p, pane); + if (!pane.msel.active) return null; + return yankRows(p, pane, @min(pane.msel.r0, pane.msel.r1), @max(pane.msel.r0, pane.msel.r1)); +} + +pub fn exitInsert(p: *Pardes, pane: *Pane) void { + pane.mode = .normal; + pane.msel.active = false; + pane.normal.clear(); + if (pane.append_at) |aa| { + pane.append_at = null; + const text = flatSurface(p, pane) catch return; + const gap = modal.offsetAt(text, .{ .row = @intCast(@max(0, pane.cur_row)), .col = @intCast(@max(0, pane.cur_col)) }); + const a_off = modal.offsetAt(text, .{ .row = @intCast(@max(0, aa.row)), .col = @intCast(@max(0, aa.col)) }); + const back = @max(a_off, modal.prevGrapheme(text, gap)); + const bc = modal.positionAt(text, back); + pane.cur_row = @intCast(bc.row); + pane.cur_col = @intCast(bc.col); + pane.vsel = .{ .active = a_off != back, .row = aa.row, .col = aa.col, .explicit = false }; + pane.cur_pinned = true; + pane.ensureCursorVisible(); + for (pane.sels[0..pane.nsel]) |*s| { + const sgap = modal.offsetAt(text, .{ .row = @intCast(@max(0, s.row)), .col = @intCast(@max(0, s.col)) }); + const b2 = if (back == gap) sgap else modal.prevGrapheme(text, sgap); + const bp = modal.positionAt(text, b2); + s.row = @intCast(bp.row); + s.col = @intCast(bp.col); + s.arow = s.row; + s.acol = s.col; + } + } +} + +pub fn paneCursorLines(p: *Pardes, pane: *Pane) ![]const []const u8 { + const arena = p.scratch.allocator(); + if (pane.file) |*f| return panes.File.cursorLines(arena, pane, f); + if (pane.hasPdf()) { + if (comptime pdf_enabled) return panes.Pdf.textLines(&pane.pdf.?, p.pdf_gpa, arena); + unreachable; + } + return panes.Terminal.cursorLines(p, pane); +} + +pub fn paneByteAtDisplay(p: *Pardes, pane: *Pane, row: i32, from_raw: i32, display_col: i32) i32 { + if (pane.file != null) + return panes.File.byteAtRowDisplay(pane, row, from_raw, display_col); + const lines = paneCursorLines(p, pane) catch return @max(0, from_raw + display_col); + const local = row; + if (local < 0 or @as(usize, @intCast(local)) >= lines.len) + return @max(0, from_raw + display_col); + return @intCast(panes.File.byteAtDisplayFrom( + lines[@intCast(local)], + @intCast(@max(0, from_raw)), + @intCast(@max(0, display_col)), + )); +} + +/// Freeze the live terminal cursor into the modal coordinate space. The +/// emulator reports screen cells; editing state stores UTF-8 byte offsets. +pub fn pinPaneCursor(p: *Pardes, pane: *Pane) void { + if (pane.cur_pinned) return; + pane.pinCursor(); + if (pane.file == null and !pane.hasPdf()) + pane.cur_col = paneByteAtDisplay(p, pane, pane.cur_row, 0, pane.cur_col); +} + +pub fn flatSurface(p: *Pardes, pane: *Pane) ![]const u8 { + if (pane.file) |f| return f.content; + if (pane.hasPdf()) { + if (comptime pdf_enabled) return pane.pdf.?.ensureText(p.pdf_gpa); + unreachable; + } + const lines = try panes.Terminal.cursorLines(p, pane); + return panes.Terminal.flatSurface(p, pane, lines); +} + +pub fn paneWrapWidth(p: *const Pardes, pane: *const Pane) usize { + if (pane.file == null) return 0; + return panes.File.wrapWidth(pane, p.settings.wrap); +} + +/// Route shared edit operations to a file's content or a terminal overlay. +fn editText(p: *Pardes, pane: *Pane, lo: i32, hi: i32, col: i32) ?panes.EditText { + if (pane.file) |f| return .{ .text = f.content, .row0 = 0 }; + if (pane.image != null or pane.hasPdf()) return null; + return panes.Terminal.editText(p, pane, lo, hi, col); +} + +fn editTextEol(p: *Pardes, pane: *Pane, b: Bounds) ?panes.EditText { + const eb = editText(p, pane, b.lo_row, b.hi_row, -1) orelse return null; + const r: usize = @intCast(@max(0, b.hi_row - eb.row0)); + if (r + 1 < modal.lineCount(eb.text)) return eb; + if (b.hi_col < @as(i32, @intCast(modal.lineSlice(eb.text, r).len))) return eb; + return editText(p, pane, b.lo_row, b.hi_row + 1, -1); +} + +/// install a rewritten editable text (frees the old one) +fn setEditText(p: *Pardes, pane: *Pane, new: []u8) void { + if (pane.file) |*f| return panes.File.setContent(p, f, new); + panes.Terminal.setEditText(p, pane, new); +} + +pub const InsertAt = enum { at, append, line_start, line_end, open_below, open_above }; + +/// Files insert only inside existing rows and at grapheme boundaries. +/// Terminal overlays can materialize virtual rows/columns, so keep those. +pub fn clampFileCursor(pane: *Pane) bool { + const file = if (pane.file) |*file| file else return false; + const old_row = pane.cur_row; + const old_col = pane.cur_col; + const last = panes.File.nlines(pane.gpa, file) -| 1; + pane.cur_row = @intCast(@min(last, @as(usize, @intCast(@max(0, pane.cur_row))))); + const line = panes.File.sourceLine(pane, pane.cur_row); + pane.cur_col = @intCast(modal.graphemeStart(line, @min(line.len, @as(usize, @intCast(@max(0, pane.cur_col)))))); + return old_row != pane.cur_row or old_col != pane.cur_col; +} + +pub fn enterInsert(p: *Pardes, pane: *Pane, where: InsertAt, cnt: usize) void { + if (pane.hasPdf()) return; + pinPaneCursor(p, pane); + _ = clampFileCursor(pane); + // snapshot once per insert session (WITH the pre-insert selection) so + // `u` undoes the whole session and restores what was selected + pushUndo(p, pane); + pane.select = false; + pane.append_at = null; + pane.sticky_col = -1; + const text = flatSurface(p, pane) catch { + pane.mode = .insert; + pane.msel.active = false; + pane.vsel.active = false; + pane.normal.clear(); + return; + }; + const cur = pane.toModalCursor(); + const line = panes.File.textLine(pane, text, cur.row); + const b: ?Bounds = if (pane.vsel.active) vselBounds(pane) else null; + switch (where) { + .at => { + if (b) |bb| { + pane.cur_row = bb.lo_row; + pane.cur_col = bb.lo_col; + pane.vsel = .{ .active = true, .row = bb.hi_row, .col = bb.hi_col, .explicit = false }; + pane.cur_pinned = true; + } + }, + .append => { + const lo_row = if (b) |bb| bb.lo_row else pane.cur_row; + const lo_col = if (b) |bb| bb.lo_col else pane.cur_col; + const hi_row = if (b) |bb| bb.hi_row else pane.cur_row; + const hi_col = if (b) |bb| bb.hi_col else pane.cur_col; + pane.append_at = .{ .row = lo_row, .col = lo_col }; + const gap = modal.nextGrapheme(text, modal.offsetAt(text, .{ .row = @intCast(@max(0, hi_row)), .col = @intCast(@max(0, hi_col)) })); + const gc = modal.positionAt(text, gap); + pane.cur_row = @intCast(gc.row); + pane.cur_col = @intCast(gc.col); + pane.vsel = .{ .active = b != null, .row = lo_row, .col = lo_col, .explicit = false }; + pane.cur_pinned = true; + }, + .line_start => { + pane.fromModalCursor(.{ .row = cur.row, .col = modal.firstNonWs(line) }); + pane.vsel.active = false; + }, + .line_end => { + pane.cur_col = @intCast(line.len); + pane.cur_pinned = true; + pane.vsel.active = false; + }, + .open_below, .open_above => { + pane.vsel.active = false; + const below = where == .open_below; + const abs: i32 = if (b) |bb| (if (below) bb.hi_row else bb.lo_row) else pane.cur_row; + const eb = editText(p, pane, abs, abs, -1) orelse return; + const row: usize = @intCast(@max(0, abs - eb.row0)); + const ind = modal.indentText(modal.lineSlice(eb.text, row)); + const arena = p.scratch.allocator(); + const block_len = std.math.mul(usize, cnt, ind.len + 1) catch return; + const block = arena.alloc(u8, block_len) catch return; + var block_at: usize = 0; + for (0..cnt) |_| { + if (below) { + block[block_at] = '\n'; + block_at += 1; + } + @memcpy(block[block_at..][0..ind.len], ind); + block_at += ind.len; + if (!below) { + block[block_at] = '\n'; + block_at += 1; + } + } + const at: modal.Cursor = if (below) + .{ .row = row, .col = modal.lineSlice(eb.text, row).len } + else + .{ .row = row, .col = 0 }; + const new = modal.insertAt(p.gpa, eb.text, at, block) catch return; + setEditText(p, pane, new); + pane.cur_row = @as(i32, @intCast(if (below) row + 1 else row)) + eb.row0; + pane.cur_col = @intCast(ind.len); + pane.cur_pinned = true; + if (cnt > 1 and !p.multi_on) opened: { + const t2 = flatSurface(p, pane) catch break :opened; + var rs: [Pane.max_selections]modal.Selection = undefined; + const m = @min(cnt, Pane.max_selections); + for (0..m) |k| { + const o = modal.offsetAt(t2, .{ .row = @intCast(@max(0, pane.cur_row) + @as(i32, @intCast(k))), .col = ind.len }); + rs[k] = .{ .anchor = o, .head = o }; + } + pane.setRanges(t2, rs[0..m], &.{}, 0, false); + } + }, + } + pane.mode = .insert; + pane.msel.active = false; + pane.normal.clear(); + pane.ensureCursorVisible(); +} + +pub fn handleInsert(p: *Pardes, pane: *Pane, key: Key) void { + if (pane.nsel == 0) return insertKey(p, pane, key); + p.replaySels(pane, .{ .insert = key }); +} + +pub fn insertKey(p: *Pardes, pane: *Pane, key: Key) void { + // helix aliases: normalize to the base key and re-dispatch + if (hit(key, config.insert_backspace_alias)) return insertKey(p, pane, .{ .cp = Key.backspace }); + if (hit(key, config.insert_enter_alias)) return insertKey(p, pane, .{ .cp = Key.enter }); + if (hit(key, config.insert_delete_alias)) return insertKey(p, pane, .{ .cp = Key.delete }); + pane.vsel.active = false; + if (!pane.cur_pinned) pinPaneCursor(p, pane); + _ = clampFileCursor(pane); + switch (key.cp) { + Key.left, Key.right, Key.up, Key.down => { + const lines = paneCursorLines(p, pane) catch return; + const cur0 = pane.toModalCursor(); + const nc = switch (key.cp) { + Key.left => modal.charLeft(lines, cur0), + Key.right => modal.charRight(lines, cur0), + Key.up => Pane.insertVerticalCursor(lines, cur0, false), + Key.down => Pane.insertVerticalCursor(lines, cur0, true), + else => cur0, + }; + pane.fromModalCursor(nc); + pane.ensureCursorVisible(); + return; + }, + Key.page_up, Key.page_down => { + // helix binds insert pageup/pagedown to the same view + // scroll + cursor snap as normal mode + const flat = flatSurface(p, pane) catch return; + const range = pane.primaryRange(flat, 0); + const step: i32 = @intCast(@max(1, pane.rows)); + pane.scrollViewMove(flat, range, if (key.cp == Key.page_down) step else -step); + return; + }, + Key.home => { + pane.cur_col = 0; + pane.cur_pinned = true; + pane.ensureCursorVisible(); + return; + }, + Key.end => { + // helix insert End: past the last char (goto_line_end_newline) + const text = flatSurface(p, pane) catch return; + const cur0 = pane.toModalCursor(); + pane.cur_col = @intCast(panes.File.textLine(pane, text, cur0.row).len); + pane.cur_pinned = true; + pane.ensureCursorVisible(); + return; + }, + else => {}, + } + const eb = editText(p, pane, pane.cur_row, pane.cur_row, pane.cur_col) orelse return; + const text = eb.text; + const c = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - eb.row0)), .col = @intCast(@max(0, pane.cur_col)) }; + if (key.text.len > 0 and !key.ctrl and !key.alt) { + const new = modal.insertAt(p.gpa, text, c, key.text) catch return; + setEditText(p, pane, new); + for (key.text) |ch| { + if (ch == '\n') { + pane.cur_row += 1; + pane.cur_col = 0; + } else pane.cur_col += 1; + } + pane.cur_pinned = true; + pane.ensureCursorVisible(); + return; + } + // helix insert-mode kills (word/line; deleteSpan is exclusive) + if (hit(key, config.delete_word_backward)) { + const e2 = if (c.row == 0 and c.col == 0) + (editText(p, pane, pane.cur_row - 1, pane.cur_row, 0) orelse return) + else + eb; + const c2 = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - e2.row0)), .col = c.col }; + const g = modal.offsetAt(e2.text, c2); + if (g == 0) return; + const wr = modal.moveWord(e2.text, .{ .anchor = g, .head = g }, 1, .prev_word_start); + const from = @min(wr.anchor, wr.head); + const fc = modal.positionAt(e2.text, from); + const new = modal.deleteSpan(p.gpa, e2.text, fc, c2) catch return; + setEditText(p, pane, new); + pane.cur_row = @as(i32, @intCast(fc.row)) + e2.row0; + pane.cur_col = @intCast(fc.col); + pane.cur_pinned = true; + pane.ensureCursorVisible(); + return; + } + if (hit(key, config.delete_word_forward)) { + const e2 = if (c.col >= modal.lineSlice(text, c.row).len and c.row + 1 >= modal.lineCount(text)) + (editText(p, pane, pane.cur_row, pane.cur_row + 1, pane.cur_col) orelse return) + else + eb; + const c2 = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - e2.row0)), .col = c.col }; + const g = modal.offsetAt(e2.text, c2); + const wr = modal.moveWord(e2.text, .{ .anchor = g, .head = g }, 1, .next_word_end); + const to = @max(wr.anchor, wr.head); + if (to <= g) return; + const new = modal.deleteSpan(p.gpa, e2.text, c2, modal.positionAt(e2.text, to)) catch return; + setEditText(p, pane, new); + pane.cur_pinned = true; + return; + } + if (hit(key, config.kill_to_line_start)) { + const new = modal.deleteSpan(p.gpa, text, .{ .row = c.row, .col = 0 }, c) catch return; + setEditText(p, pane, new); + pane.cur_col = 0; + pane.cur_pinned = true; + pane.ensureCursorVisible(); + return; + } + if (hit(key, config.kill_to_line_end)) { + const llen = modal.lineSlice(text, c.row).len; + const new = modal.deleteSpan(p.gpa, text, c, .{ .row = c.row, .col = llen }) catch return; + setEditText(p, pane, new); + pane.cur_pinned = true; + return; + } + switch (key.cp) { + Key.enter => { + const line = modal.lineSlice(text, c.row); + const indent = modal.newlineIndentWidth(line, c.col); + const arena = p.scratch.allocator(); + const block = arena.alloc(u8, 1 + indent) catch return; + block[0] = '\n'; + @memset(block[1..], ' '); + const new = modal.insertAt(p.gpa, text, c, block) catch return; + setEditText(p, pane, new); + pane.cur_row += 1; + pane.cur_col = @intCast(indent); + pane.cur_pinned = true; + pane.ensureCursorVisible(); + }, + Key.backspace => { + if (pane.cur_col > 0) { + const line = modal.lineSlice(text, c.row); + const prev = modal.prevGrapheme(line, c.col); + const new = modal.deleteChar(p.gpa, text, .{ .row = c.row, .col = prev }) catch return; + setEditText(p, pane, new); + pane.cur_col = @intCast(prev); + pane.cur_pinned = true; + pane.ensureCursorVisible(); + return; + } + // at col 0: eat the newline above (join up) — a terminal grows + // its buffer over the row it is joining with + if (pane.cur_row <= 0) return; + const e2 = editText(p, pane, pane.cur_row - 1, pane.cur_row, 0) orelse return; + const row: usize = @intCast(@max(0, pane.cur_row - e2.row0)); + if (row == 0) return; + const prevlen = modal.lineSlice(e2.text, row - 1).len; + const new = modal.deleteSpan(p.gpa, e2.text, .{ .row = row - 1, .col = prevlen }, .{ .row = row, .col = 0 }) catch return; + setEditText(p, pane, new); + pane.cur_row -= 1; + pane.cur_col = @intCast(prevlen); + pane.cur_pinned = true; + pane.ensureCursorVisible(); + }, + Key.delete => { + // delete forward; at line end eat the newline (join), which on + // a terminal means the buffer swallowing the row below + const line = modal.lineSlice(text, c.row); + if (c.col < line.len) { + const new = modal.deleteChar(p.gpa, text, c) catch return; + setEditText(p, pane, new); + } else { + const e2 = editText(p, pane, pane.cur_row, pane.cur_row + 1, pane.cur_col) orelse return; + const row: usize = @intCast(@max(0, pane.cur_row - e2.row0)); + if (row + 1 >= modal.lineCount(e2.text)) return; + const new = modal.deleteSpan(p.gpa, e2.text, .{ .row = row, .col = c.col }, .{ .row = row + 1, .col = 0 }) catch return; + setEditText(p, pane, new); + } + pane.cur_pinned = true; + }, + Key.tab => { + const ln = modal.lineSlice(text, c.row); + if (!p.multi_on and c.col > 0 and c.col <= ln.len and ln[c.col - 1] == '.') dot: { + const f = pane.file orelse break :dot; + if (f.output != null or !lsp.speaks(f.path)) break :dot; + const seq = p.lsp_seq; + p.lspRequest(p.active, .completion, ""); + if (p.lsp_seq != seq) return; + } + insertTab(p, pane); + }, + else => {}, + } +} + +pub fn insertTab(p: *Pardes, pane: *Pane) void { + const eb = editText(p, pane, pane.cur_row, pane.cur_row, pane.cur_col) orelse return; + const c: modal.Cursor = .{ + .row = @intCast(@max(0, pane.cur_row - eb.row0)), + .col = @intCast(@max(0, pane.cur_col)), + }; + const pad = modal.INDENT_W - (c.col % modal.INDENT_W); + const new = modal.insertAt(p.gpa, eb.text, c, " "[0..pad]) catch return; + setEditText(p, pane, new); + pane.cur_col += @intCast(pad); + pane.cur_pinned = true; + pane.ensureCursorVisible(); +} + +const Bounds = struct { lo_row: i32, lo_col: i32, hi_row: i32, hi_col: i32 }; + +/// a range's two cells, normalized to document order +pub fn cellBounds(s: Pane.SelRange) Bounds { + if (s.row < s.arow or (s.row == s.arow and s.col < s.acol)) + return .{ .lo_row = s.row, .lo_col = s.col, .hi_row = s.arow, .hi_col = s.acol }; + return .{ .lo_row = s.arow, .lo_col = s.acol, .hi_row = s.row, .hi_col = s.col }; +} + +/// the char selection [anchor, cursor] normalized to document order +fn vselBounds(pane: *Pane) Bounds { + return cellBounds(.{ .row = pane.cur_row, .col = pane.cur_col, .arow = pane.vsel.row, .acol = pane.vsel.col }); +} + +fn vselText(p: *Pardes, pane: *Pane) []const u8 { + const arena = p.scratch.allocator(); + const b = vselBounds(pane); + const text = flatSurface(p, pane) catch return ""; + return modal.rangeText(arena, text, .{ .row = @intCast(@max(0, b.lo_row)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row)), .col = @intCast(@max(0, b.hi_col)) }) catch ""; +} + +/// join surface rows [r0, r1] (absolute) with '\n'; scratch-owned +fn yankRows(p: *Pardes, pane: *Pane, r0: i32, r1: i32) []const u8 { + const arena = p.scratch.allocator(); + const lines = paneCursorLines(p, pane) catch return ""; + const rows_count: usize = @intCast(@max(0, r1 - r0 + 1)); + var total: usize = rows_count -| 1; + var i = r0; + while (i <= r1) : (i += 1) { + if (i >= 0 and @as(usize, @intCast(i)) < lines.len) + total += lines[@intCast(i)].len; + } + const out = arena.alloc(u8, total) catch return ""; + var at: usize = 0; + i = r0; + while (i <= r1) : (i += 1) { + if (i > r0) { + out[at] = '\n'; + at += 1; + } + if (i >= 0 and @as(usize, @intCast(i)) < lines.len) { + const line = lines[@intCast(i)]; + @memcpy(out[at..][0..line.len], line); + at += line.len; + } + } + return out; +} + +pub fn normalDelete(p: *Pardes, pane: *Pane, yank: bool) void { + pane.select = false; // helix delete exits select mode + pane.sticky_col = -1; + if (pane.vsel.active) { + const b = vselBounds(pane); + if (yank) setYank(p, vselText(p, pane)); // read the range before the move + pushUndo(p, pane); + const eb = editTextEol(p, pane, b) orelse { + pane.vsel.active = false; + return; + }; + const d = modal.deleteRange(p.gpa, eb.text, .{ .row = @intCast(@max(0, b.lo_row - eb.row0)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row - eb.row0)), .col = @intCast(@max(0, b.hi_col)) }) catch { + pane.vsel.active = false; + return; + }; + setEditText(p, pane, d.content); + p.gpa.free(d.deleted); + pane.cur_row = b.lo_row; + pane.cur_col = b.lo_col; + pane.clampCursor(d.content, eb.row0); + return; + } + if (pane.msel.active) { + const r0 = @min(pane.msel.r0, pane.msel.r1); + const r1 = @max(pane.msel.r0, pane.msel.r1); + pushUndo(p, pane); + // linewise: a line's newline lives on the row below it + const eb = editText(p, pane, r0, r1 + 1, -1) orelse { + pane.msel.active = false; + return; + }; + const d = modal.deleteLines(p.gpa, eb.text, @intCast(@max(0, r0 - eb.row0)), @intCast(@max(0, r1 - eb.row0))) catch { + pane.msel.active = false; + return; + }; + setEditText(p, pane, d.content); + if (yank) setYank(p, d.deleted); + p.gpa.free(d.deleted); + const n = modal.lineCount(d.content); + pane.cur_row = @as(i32, @intCast(@min(@as(usize, @intCast(@max(0, r0 - eb.row0))), if (n == 0) 0 else n - 1))) + eb.row0; + pane.cur_col = 0; + pane.msel.active = false; + pane.cur_pinned = true; + pane.ensureCursorVisible(); + } else { + pushUndo(p, pane); + const eb = editTextEol(p, pane, .{ .lo_row = pane.cur_row, .lo_col = pane.cur_col, .hi_row = pane.cur_row, .hi_col = pane.cur_col }) orelse return; + const c = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - eb.row0)), .col = @intCast(@max(0, pane.cur_col)) }; + if (yank) { + const arena = p.scratch.allocator(); + setYank(p, modal.rangeText(arena, eb.text, c, c) catch return); + } + // deleteRange (not deleteChar): the cursor may sit ON a '\n' + // cell, where a delete joins the lines (helix 1-wide selection) + const d = modal.deleteRange(p.gpa, eb.text, c, c) catch return; + setEditText(p, pane, d.content); + p.gpa.free(d.deleted); + const llen = modal.lineSlice(d.content, c.row).len; + pane.cur_col = @min(pane.cur_col, @as(i32, @intCast(llen))); + } +} + +pub fn normalYank(p: *Pardes, pane: *Pane) void { + pane.select = false; + if (pane.rawPointerText(pane.sel[sel_slot])) |text| { + setYank(p, text); + return; + } + if (pane.vsel.active) { + setYank(p, vselText(p, pane)); + return; + } + if (pane.msel.active) { + setYank(p, yankRows(p, pane, @min(pane.msel.r0, pane.msel.r1), @max(pane.msel.r0, pane.msel.r1))); + } else { + const save = pane.vsel; + pane.vsel = .{ .active = true, .row = pane.cur_row, .col = pane.cur_col, .explicit = false }; + setYank(p, vselText(p, pane)); + pane.vsel = save; + } +} + +pub fn pasteText(p: *Pardes, pane: *Pane, y0: []const u8, before: bool, count: usize) void { + if (y0.len == 0) return; + pushUndo(p, pane); + pane.select = false; + pane.sticky_col = -1; + const arena = p.scratch.allocator(); + var y: []const u8 = y0; + if (count > 1) { + const total = std.math.mul(usize, count, y0.len) catch return; + const buf = arena.alloc(u8, total) catch return; + for (0..count) |i| @memcpy(buf[i * y0.len ..][0..y0.len], y0); + y = buf; + } + const b: Bounds = if (pane.vsel.active) + vselBounds(pane) + else + .{ .lo_row = pane.cur_row, .lo_col = pane.cur_col, .hi_row = pane.cur_row, .hi_col = pane.cur_col }; + const eb = editText(p, pane, b.lo_row, b.hi_row, -1) orelse return; + const row0 = eb.row0; + if (y[y.len - 1] == '\n') { + const block_text = y[0 .. y.len - 1]; + const n = @max(1, modal.lineCount(block_text)); + var out: []u8 = undefined; + if (before) { + const row: usize = @intCast(@max(0, b.lo_row - row0)); + const block = std.fmt.allocPrint(arena, "{s}\n", .{block_text}) catch return; + out = modal.insertAt(p.gpa, eb.text, .{ .row = row, .col = 0 }, block) catch return; + pane.cur_row = @as(i32, @intCast(row + n - 1)) + row0; + } else { + const row: usize = @intCast(@max(0, b.hi_row - row0)); + out = modal.pasteLineBelow(p.gpa, eb.text, row, block_text) catch return; + pane.cur_row = @as(i32, @intCast(row + n)) + row0; + } + setEditText(p, pane, out); + pane.vsel = .{ .active = true, .row = pane.cur_row - @as(i32, @intCast(n - 1)), .col = 0, .explicit = false }; + // cursor ON the last pasted line's '\n' (helix) + const llen = modal.lineSlice(out, @intCast(@max(0, pane.cur_row - row0))).len; + pane.cur_col = @intCast(llen); + } else { + const at: modal.Cursor = if (before) + .{ .row = @intCast(@max(0, b.lo_row - row0)), .col = @intCast(@max(0, b.lo_col)) } + else blk: { + const hrow: usize = @intCast(@max(0, b.hi_row - row0)); + const hi_col: usize = @intCast(@max(0, b.hi_col)); + const high_line = modal.lineSlice(eb.text, hrow); + if (hi_col >= high_line.len) break :blk .{ .row = hrow + 1, .col = 0 }; + const gcol = modal.nextGrapheme(high_line, hi_col); + break :blk .{ .row = hrow, .col = gcol }; + }; + const out = modal.insertAt(p.gpa, eb.text, at, y) catch return; + setEditText(p, pane, out); + pane.vsel = .{ .active = modal.nextGrapheme(y, 0) < y.len, .row = @as(i32, @intCast(at.row)) + row0, .col = @intCast(at.col), .explicit = false }; + const end = modal.advanceBy(at, y); + if (end.col > 0) { + pane.cur_row = @as(i32, @intCast(end.row)) + row0; + pane.cur_col = @intCast(modal.prevGrapheme(modal.lineSlice(out, end.row), end.col)); + } else { + pane.cur_row = @as(i32, @intCast(end.row -| 1)) + row0; + pane.cur_col = @intCast(modal.lineSlice(out, end.row -| 1).len); + } + } + pane.msel.active = false; + pane.cur_pinned = true; + pane.ensureCursorVisible(); +} + +pub fn normalChange(p: *Pardes, pane: *Pane) void { + pane.select = false; + pane.sticky_col = -1; + if (pane.vsel.active) { + const b = vselBounds(pane); + const eb = editTextEol(p, pane, b) orelse return; + const hi_len = modal.lineSlice(eb.text, @intCast(@max(0, b.hi_row - eb.row0))).len; + if (b.lo_col == 0 and @as(usize, @intCast(@max(0, b.hi_col))) >= hi_len) { + setYank(p, vselText(p, pane)); + pushUndo(p, pane); + const d = modal.deleteLines(p.gpa, eb.text, @intCast(@max(0, b.lo_row - eb.row0)), @intCast(@max(0, b.hi_row - eb.row0))) catch return; + setEditText(p, pane, d.content); + p.gpa.free(d.deleted); + const n = modal.lineCount(d.content); + const row: usize = @min(@as(usize, @intCast(@max(0, b.lo_row - eb.row0))), if (n == 0) 0 else n - 1); + const ind = modal.indentText(modal.lineSlice(d.content, row)); + const arena = p.scratch.allocator(); + const block = std.fmt.allocPrint(arena, "{s}\n", .{ind}) catch return; + const new = modal.insertAt(p.gpa, d.content, .{ .row = row, .col = 0 }, block) catch return; + setEditText(p, pane, new); + pane.cur_row = @as(i32, @intCast(row)) + eb.row0; + pane.cur_col = @intCast(ind.len); + pane.vsel.active = false; + pane.msel.active = false; + pane.cur_pinned = true; + pane.mode = .insert; + pane.normal.clear(); + pane.ensureCursorVisible(); + return; + } + setYank(p, vselText(p, pane)); + pushUndo(p, pane); + const d = modal.deleteRange(p.gpa, eb.text, .{ .row = @intCast(@max(0, b.lo_row - eb.row0)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row - eb.row0)), .col = @intCast(@max(0, b.hi_col)) }) catch { + pane.vsel.active = false; + return; + }; + setEditText(p, pane, d.content); + p.gpa.free(d.deleted); + pane.cur_row = b.lo_row; + pane.cur_col = b.lo_col; + pane.clampCursor(d.content, eb.row0); + pane.mode = .insert; + pane.normal.clear(); + return; + } + if (pane.msel.active) { + const r0 = @min(pane.msel.r0, pane.msel.r1); + const r1 = @max(pane.msel.r0, pane.msel.r1); + pushUndo(p, pane); + const eb = editText(p, pane, r0, r1 + 1, -1) orelse return; + const arena = p.scratch.allocator(); + const lo: usize = @intCast(@max(0, r0 - eb.row0)); + const hi: usize = @intCast(@max(0, r1 - eb.row0)); + var total: usize = hi - lo; + for (lo..hi + 1) |r| total += modal.lineSlice(eb.text, r).len; + const buf = arena.alloc(u8, total) catch return; + var at: usize = 0; + for (lo..hi + 1) |r| { + if (r > lo) { + buf[at] = '\n'; + at += 1; + } + const line = modal.lineSlice(eb.text, r); + @memcpy(buf[at..][0..line.len], line); + at += line.len; + } + setYank(p, buf); + var body: []u8 = eb.text; + if (hi > lo) { + const d = modal.deleteLines(p.gpa, body, lo + 1, hi) catch { + pane.msel.active = false; + return; + }; + setEditText(p, pane, d.content); + p.gpa.free(d.deleted); + body = d.content; + } + const cl = modal.clearLine(p.gpa, body, lo) catch { + pane.msel.active = false; + return; + }; + setEditText(p, pane, cl); + pane.cur_row = r0; + pane.cur_col = 0; + pane.msel.active = false; + pane.cur_pinned = true; + pane.mode = .insert; + pane.normal.clear(); + pane.ensureCursorVisible(); + } else { + pushUndo(p, pane); + const eb = editTextEol(p, pane, .{ .lo_row = pane.cur_row, .lo_col = pane.cur_col, .hi_row = pane.cur_row, .hi_col = pane.cur_col }) orelse return; + const c = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - eb.row0)), .col = @intCast(@max(0, pane.cur_col)) }; + const arena = p.scratch.allocator(); + setYank(p, modal.rangeText(arena, eb.text, c, c) catch return); + const d = modal.deleteRange(p.gpa, eb.text, c, c) catch return; + setEditText(p, pane, d.content); + p.gpa.free(d.deleted); + const llen = modal.lineSlice(d.content, c.row).len; + pane.cur_col = @min(pane.cur_col, @as(i32, @intCast(llen))); + pane.mode = .insert; + pane.normal.clear(); + } +} + +fn selRange(pane: *Pane, content: []const u8, row0: i32) modal.Range { + if (pane.vsel.active) { + const b = vselBounds(pane); + return .{ + .a = .{ .row = @intCast(@max(0, b.lo_row - row0)), .col = @intCast(@max(0, b.lo_col)) }, + .b = .{ .row = @intCast(@max(0, b.hi_row - row0)), .col = @intCast(@max(0, b.hi_col)) }, + }; + } + if (pane.msel.active) { + const r0: usize = @intCast(@max(0, @min(pane.msel.r0, pane.msel.r1) - row0)); + const r1: usize = @intCast(@max(0, @max(pane.msel.r0, pane.msel.r1) - row0)); + const llen = modal.lineSlice(content, r1).len; + return .{ .a = .{ .row = r0, .col = 0 }, .b = .{ .row = r1, .col = if (llen == 0) 0 else modal.prevGrapheme(modal.lineSlice(content, r1), llen) } }; + } + const c = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - row0)), .col = @intCast(@max(0, pane.cur_col)) }; + return .{ .a = c, .b = c }; +} + +/// the rows an op's selection spans, absolute (what editText must cover) +fn selRows(pane: *Pane) Bounds { + if (pane.vsel.active) return vselBounds(pane); + if (pane.msel.active) return .{ + .lo_row = @min(pane.msel.r0, pane.msel.r1), + .lo_col = 0, + .hi_row = @max(pane.msel.r0, pane.msel.r1), + .hi_col = 0, + }; + return .{ .lo_row = pane.cur_row, .lo_col = pane.cur_col, .hi_row = pane.cur_row, .hi_col = pane.cur_col }; +} + +/// `r`: overwrite the selection (or the cursor char) with ch — +/// newlines included (helix), so `xrz` joins the selected lines +pub fn normalReplaceChar(p: *Pardes, pane: *Pane, ch: u21) void { + pane.select = false; + const eb = editTextEol(p, pane, selRows(pane)) orelse return; + const before = pane.primaryRange(eb.text, eb.row0); + const lo = @min(before.anchor, before.head); + const hi = @max(before.anchor, before.head); + var graphemes: usize = 0; + var at = lo; + while (at < hi) : (graphemes += 1) at = modal.nextGrapheme(eb.text, at); + var encoded: [4]u8 = undefined; + const encoded_len = std.unicode.utf8Encode(ch, &encoded) catch return; + const r = selRange(pane, eb.text, eb.row0); + pushUndo(p, pane); + const new = modal.replaceChars(p.gpa, eb.text, r.a, r.b, ch) catch return; + setEditText(p, pane, new); + const end = lo + graphemes * encoded_len; + const mapped: modal.Selection = if (before.head < before.anchor) + .{ .anchor = end, .head = lo } + else + .{ .anchor = lo, .head = end }; + pane.setRange(new, eb.row0, mapped, pane.vsel.explicit); +} + +/// `R`: replace the selection (or the cursor char) with the DEFAULT +/// register. `SPC R` is the same verb over the system clipboard. +pub fn normalReplaceYank(p: *Pardes, pane: *Pane) void { + replaceWithText(p, pane, p.yank orelse return); +} + +/// The pasted text becomes the selection, head on its last char (which for +/// a trailing-newline `y` is the '\n' cell of the last full line). +fn replaceWithText(p: *Pardes, pane: *Pane, y: []const u8) void { + if (y.len == 0) return; + pane.select = false; + const eb = editTextEol(p, pane, selRows(pane)) orelse return; + const r = selRange(pane, eb.text, eb.row0); + pushUndo(p, pane); + const new = modal.replaceRange(p.gpa, eb.text, r.a, r.b, y) catch return; + setEditText(p, pane, new); + pane.msel.active = false; + pane.vsel = .{ .active = modal.nextGrapheme(y, 0) < y.len, .row = @as(i32, @intCast(r.a.row)) + eb.row0, .col = @intCast(r.a.col), .explicit = false }; + const end = modal.advanceBy(r.a, y); + if (end.col > 0) { + pane.cur_row = @as(i32, @intCast(end.row)) + eb.row0; + pane.cur_col = @intCast(modal.prevGrapheme(modal.lineSlice(new, end.row), end.col)); + } else { + // the yank ended in '\n': the cursor lands ON that newline + pane.cur_row = @as(i32, @intCast(end.row -| 1)) + eb.row0; + pane.cur_col = @intCast(modal.lineSlice(new, end.row -| 1).len); + } + pane.cur_pinned = true; + pane.sticky_col = -1; + pane.ensureCursorVisible(); +} + +/// `~` / `` ` `` / ``Alt-` ``: case-map the selection (or the cursor char), +/// keeping the selection (helix) +pub fn normalCase(p: *Pardes, pane: *Pane, op: modal.CaseOp) void { + pane.select = false; + const eb = editTextEol(p, pane, selRows(pane)) orelse return; + const r = selRange(pane, eb.text, eb.row0); + pushUndo(p, pane); + const new = modal.changeCase(p.gpa, eb.text, r.a, r.b, op) catch return; + setEditText(p, pane, new); +} + +/// a change to apply to file content: delete [from, to), insert `ins` at +/// `from`. Ascending and disjoint. +const TextChange = struct { from: usize, to: usize, ins: []const u8 }; + +fn mapThroughChanges(chs: []const TextChange, pos: usize) usize { + var delta: i64 = 0; + for (chs) |ch| { + if (pos < ch.from) break; + if (ch.from == ch.to) { + delta += @intCast(ch.ins.len); + continue; + } + if (pos == ch.from) break; + if (pos < ch.to) return @intCast(@as(i64, @intCast(ch.from)) + delta); + delta += @as(i64, @intCast(ch.ins.len)) - @as(i64, @intCast(ch.to - ch.from)); + } + return @intCast(@as(i64, @intCast(pos)) + delta); +} + +/// apply a change list. gpa-owned result. +fn applyChanges(p: *Pardes, text: []const u8, chs: []const TextChange) ![]u8 { + var total = text.len; + for (chs) |ch| total = total - (ch.to - ch.from) + ch.ins.len; + const out = try p.gpa.alloc(u8, total); + errdefer p.gpa.free(out); + var copied: usize = 0; + var at: usize = 0; + for (chs) |ch| { + const unchanged = text[copied..ch.from]; + @memcpy(out[at..][0..unchanged.len], unchanged); + at += unchanged.len; + @memcpy(out[at..][0..ch.ins.len], ch.ins); + at += ch.ins.len; + copied = ch.to; + } + @memcpy(out[at..], text[copied..]); + return out; +} + +pub fn normalJoin(p: *Pardes, pane: *Pane) void { + // a join always eats the newline of its last line, so the buffer has + // to reach one row PAST the selection + const sr = selRows(pane); + const eb = editText(p, pane, sr.lo_row, sr.hi_row + 1, -1) orelse return; + const text = eb.text; + const range = pane.primaryRange(text, eb.row0); + const span = Pane.rangeLineSpan(text, range); + const nlines = modal.cursorLineCount(text); + var end = span.end; + if (span.start == end) end = @min(end + 1, nlines - 1); + if (end <= span.start) return; + const arena = p.scratch.allocator(); + const chs = arena.alloc(TextChange, end - span.start) catch return; + var chs_len: usize = 0; + var l = span.start; + while (l < end) : (l += 1) { + const from = modal.lineEndOffset(text, l); + var to = if (l + 1 >= nlines) text.len else modal.lineStartOffset(text, l + 1); + while (to < text.len and (text[to] == ' ' or text[to] == '\t')) to += 1; + const sep: []const u8 = if (to == modal.lineEndOffset(text, @min(l + 1, nlines - 1))) "" else " "; + chs[chs_len] = .{ .from = from, .to = to, .ins = sep }; + chs_len += 1; + } + if (chs_len == 0) return; + pushUndo(p, pane); + const cur_off = modal.offsetAt(text, .{ .row = @intCast(@max(0, pane.cur_row - eb.row0)), .col = @intCast(@max(0, pane.cur_col)) }); + const anc_off = if (pane.vsel.active) + modal.offsetAt(text, .{ .row = @intCast(@max(0, pane.vsel.row - eb.row0)), .col = @intCast(@max(0, pane.vsel.col)) }) + else + cur_off; + const new = applyChanges(p, text, chs[0..chs_len]) catch return; + const nc = mapThroughChanges(chs[0..chs_len], cur_off); + const na = mapThroughChanges(chs[0..chs_len], anc_off); + setEditText(p, pane, new); + const cc = modal.positionAt(new, nc); + const ac = modal.positionAt(new, na); + pane.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; + pane.cur_col = @intCast(cc.col); + if (pane.vsel.active) { + pane.vsel.row = @as(i32, @intCast(ac.row)) + eb.row0; + pane.vsel.col = @intCast(ac.col); + } + pane.msel.active = false; + pane.cur_pinned = true; + pane.sticky_col = -1; + pane.ensureCursorVisible(); +} + +pub fn normalIndent(p: *Pardes, pane: *Pane, cnt: usize, add: bool) void { + pane.select = false; + const sr = selRows(pane); + const eb = editText(p, pane, sr.lo_row, sr.hi_row, -1) orelse return; + const text = eb.text; + const range = pane.primaryRange(text, eb.row0); + const span = Pane.rangeLineSpan(text, range); + const arena = p.scratch.allocator(); + // one run of spaces, sliced per line: `>` never inserts more than this + const pad = arena.alloc(u8, modal.INDENT_W * cnt) catch return; + @memset(pad, ' '); + const chs = arena.alloc(TextChange, span.end - span.start + 1) catch return; + var chs_len: usize = 0; + var l = span.start; + while (l <= span.end) : (l += 1) { + const ls = modal.lineStartOffset(text, l); + const le = modal.lineEndOffset(text, l); + const line = text[ls..le]; + const nw = modal.firstNonWs(line); + if (nw == line.len) continue; // blank lines stay blank (helix) + if (add) { + const ins = modal.INDENT_W * cnt - (nw % modal.INDENT_W); + chs[chs_len] = .{ .from = ls, .to = ls, .ins = pad[0..ins] }; + chs_len += 1; + } else { + const want = modal.INDENT_W * cnt; + var w: usize = 0; + var pos: usize = 0; + while (pos < line.len) { + if (line[pos] == ' ') { + w += 1; + } else if (line[pos] == '\t') { + w = (w / modal.INDENT_W + 1) * modal.INDENT_W; + } else break; + pos += 1; + if (w >= want) break; + } + if (pos > 0) { + chs[chs_len] = .{ .from = ls, .to = ls + pos, .ins = "" }; + chs_len += 1; + } + } + } + if (chs_len == 0) return; + pushUndo(p, pane); + const cur_off = modal.offsetAt(text, .{ .row = @intCast(@max(0, pane.cur_row - eb.row0)), .col = @intCast(@max(0, pane.cur_col)) }); + const anc_off = if (pane.vsel.active) + modal.offsetAt(text, .{ .row = @intCast(@max(0, pane.vsel.row - eb.row0)), .col = @intCast(@max(0, pane.vsel.col)) }) + else + cur_off; + const new = applyChanges(p, text, chs[0..chs_len]) catch return; + const nc = mapThroughChanges(chs[0..chs_len], cur_off); + const na = mapThroughChanges(chs[0..chs_len], anc_off); + setEditText(p, pane, new); + const cc = modal.positionAt(new, nc); + const ac = modal.positionAt(new, na); + pane.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; + pane.cur_col = @intCast(cc.col); + if (pane.vsel.active) { + pane.vsel.row = @as(i32, @intCast(ac.row)) + eb.row0; + pane.vsel.col = @intCast(ac.col); + } + pane.msel.active = false; + pane.cur_pinned = true; + pane.sticky_col = -1; + pane.ensureCursorVisible(); +} + +pub fn normalToggleComment(p: *Pardes, pane: *Pane) void { + const expl = (pane.vsel.active and pane.vsel.explicit) or pane.msel.active; + pane.select = false; // helix exit_select_mode + // the buffer must cover every cursor's rows, not just the primary's + var b = selRows(pane); + for (pane.sels[0..pane.nsel]) |s| { + b.lo_row = @min(b.lo_row, @min(s.row, s.arow)); + b.hi_row = @max(b.hi_row, @max(s.row, s.arow)); + } + const surface = flatSurface(p, pane) catch return; + const last_row = @as(i32, @intCast(panes.File.textLineCount(pane, surface))) - 1; + const eb = editText(p, pane, b.lo_row, @min(b.hi_row + 1, last_row), -1) orelse return; + const text = eb.text; + var rs: [Pane.max_selections]modal.Selection = undefined; + const got = pane.ranges(text, eb.row0, &rs); + const nlines = modal.cursorLineCount(text); + const arena = p.scratch.allocator(); + // the lines the ranges cover, each ONE ONCE and in order (helix's + // min_next_line: two cursors on one line comment it once) + const lines = arena.alloc(usize, nlines) catch return; + var lines_len: usize = 0; + var next: usize = 0; + for (rs[0..got.n]) |r| { + const span = Pane.rangeLineSpan(text, r); + var l = @max(span.start, next); + const end = @min(span.end + 1, nlines); + while (l < end) : (l += 1) { + lines[lines_len] = l; + lines_len += 1; + } + next = @max(next, end); + } + const ext = if (pane.file) |f| std.fs.path.extension(f.path) else ""; + var token: []const u8 = config.comment_token_default; + lang: for (config.comment_tokens) |row| { + for (row.exts) |e| if (std.ascii.eqlIgnoreCase(ext, e)) { + token = row.token; + break :lang; + }; + } + var commented = true; + var indent: usize = std.math.maxInt(usize); + var margin: usize = 1; + var live: usize = 0; + for (lines[0..lines_len]) |l| { + const line = text[modal.lineStartOffset(text, l)..modal.lineEndOffset(text, l)]; + const nw = modal.firstNonWs(line); + if (nw == line.len) continue; + indent = @min(indent, nw); + if (!std.mem.startsWith(u8, line[nw..], token)) commented = false; + if (nw + token.len >= line.len or line[nw + token.len] != ' ') margin = 0; + live += 1; + } + if (live == 0) return; // nothing but blank lines + const ins = std.fmt.allocPrint(arena, "{s} ", .{token}) catch return; + const chs = arena.alloc(TextChange, lines_len) catch return; + var chs_len: usize = 0; + for (lines[0..lines_len]) |l| { + const ls = modal.lineStartOffset(text, l); + const le = modal.lineEndOffset(text, l); + const line = text[ls..le]; + if (modal.firstNonWs(line) == line.len) continue; // blank lines untouched + const at = ls + indent; + // the @min can never bind: margin is 1 only when every line has a + // space after its own token, which is a byte past `at + token.len` + chs[chs_len] = if (commented) + .{ .from = at, .to = @min(at + token.len + margin, le), .ins = "" } + else + .{ .from = at, .to = at, .ins = ins }; + chs_len += 1; + } + pushUndo(p, pane); + // one edit, and the WHOLE selection rides through it (helix maps the + // selection with the transaction) + var cells: [Pane.max_selections]Pane.SelRange = undefined; + const new = applyChanges(p, text, chs[0..chs_len]) catch return; + for (rs[0..got.n], 0..) |r, i| { + const c = Pane.rangeCells(new, .{ + .anchor = mapThroughChanges(chs[0..chs_len], r.anchor), + .head = mapThroughChanges(chs[0..chs_len], r.head), + }); + const cc = modal.positionAt(new, c.cur); + const ac = modal.positionAt(new, c.anc); + cells[i] = .{ + .row = @as(i32, @intCast(cc.row)) + eb.row0, + .col = @intCast(cc.col), + .arow = @as(i32, @intCast(ac.row)) + eb.row0, + .acol = @intCast(ac.col), + }; + } + setEditText(p, pane, new); + const t2 = flatSurface(p, pane) catch return; + for (cells[0..got.n], 0..) |s, i| rs[i] = Pane.cellRange(t2, s.arow, s.acol, s.row, s.col); + pane.setRanges(t2, rs[0..got.n], &.{}, got.pri, expl); +} + +pub fn normalAdjustNumber(p: *Pardes, pane: *Pane, delta: i64) void { + const eb = editTextEol(p, pane, selRows(pane)) orelse return; + const r = selRange(pane, eb.text, eb.row0); + const arena = p.scratch.allocator(); + const frag = modal.rangeText(arena, eb.text, r.a, r.b) catch return; + const rep = (modal.incrementDecimal(arena, frag, delta) catch null) orelse return; + pushUndo(p, pane); + pane.select = false; + const new = modal.replaceRange(p.gpa, eb.text, r.a, r.b, rep) catch return; + setEditText(p, pane, new); + const start = modal.lineStartOffset(new, r.a.row) + r.a.col; + const cc = modal.positionAt(new, modal.prevGrapheme(new, start + rep.len)); + pane.vsel = .{ .active = modal.nextGrapheme(rep, 0) < rep.len, .row = @as(i32, @intCast(r.a.row)) + eb.row0, .col = @intCast(r.a.col), .explicit = false }; + pane.msel.active = false; + pane.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; + pane.cur_col = @intCast(cc.col); + pane.cur_pinned = true; + pane.sticky_col = -1; + pane.ensureCursorVisible(); +} + +/// `]Space` / `[Space`: add blank lines below/above, the cursor staying +/// on its text line (the selection rides along) +pub fn addNewline(p: *Pardes, pane: *Pane, below: bool, cnt: usize) void { + const eb = editText(p, pane, pane.cur_row, pane.cur_row, -1) orelse return; + pushUndo(p, pane); + const row: usize = @intCast(@max(0, pane.cur_row - eb.row0)); + const arena = p.scratch.allocator(); + const nl = arena.alloc(u8, cnt) catch return; + @memset(nl, '\n'); + const llen = modal.lineSlice(eb.text, row).len; + const new = modal.insertAt(p.gpa, eb.text, .{ .row = row, .col = if (below) llen else 0 }, nl) catch return; + setEditText(p, pane, new); + if (!below) { + pane.cur_row += @intCast(cnt); // the line moved down; stay on it + if (pane.vsel.active and pane.vsel.row >= @as(i32, @intCast(row))) pane.vsel.row += @intCast(cnt); + } else if (pane.vsel.active and pane.vsel.row > @as(i32, @intCast(row))) { + pane.vsel.row += @intCast(cnt); + } + pane.cur_pinned = true; + pane.ensureCursorVisible(); +} + +/// pair mapping for m-mode: either bracket names the pair; anything else +/// surrounds with itself (quotes) +fn pairFor(ch: u21) ?struct { o: u8, c: u8 } { + return switch (ch) { + '(', ')' => .{ .o = '(', .c = ')' }, + '[', ']' => .{ .o = '[', .c = ']' }, + '{', '}' => .{ .o = '{', .c = '}' }, + '<', '>' => .{ .o = '<', .c = '>' }, + else => if (ch <= 0x7f) .{ .o = @intCast(ch), .c = @intCast(ch) } else null, + }; +} + +pub fn textobjectSelect(p: *Pardes, pane: *Pane, text: []const u8, obj: u21, around: bool) void { + const range = pane.primaryRange(text, 0); + switch (obj) { + 'w', 'W' => { + const r = modal.selectWord(text, range, around, obj == 'W'); + return pane.setRange(text, 0, r, false); + }, + 'p' => { + const r = modal.selectParagraph(text, range, around, 1); + return pane.setRange(text, 0, r, false); + }, + else => {}, + } + const lines = paneCursorLines(p, pane) catch return; + const cur = modal.positionAt(text, modal.selectionCursor(text, range)); + const pair: ?modal.Range = switch (obj) { + '\'', '"', '`' => modal.enclosingQuote(lines, cur, @intCast(obj)), + '(', ')' => modal.enclosingPair(lines, cur, '(', ')'), + '[', ']' => modal.enclosingPair(lines, cur, '[', ']'), + '{', '}' => modal.enclosingPair(lines, cur, '{', '}'), + '<', '>' => modal.enclosingPair(lines, cur, '<', '>'), + else => null, + }; + const pr = pair orelse return; + var a = modal.offsetAt(text, pr.a); + var head = modal.nextGrapheme(text, modal.offsetAt(text, pr.b)); + if (!around) { + // inside: shrink off the delimiters; an EMPTY pair collapses to + // a 1-wide cursor on the closing char (helix) + a = modal.nextGrapheme(text, a); + head = modal.prevGrapheme(text, head); + } + pane.setRange(text, 0, .{ .anchor = a, .head = head }, false); +} + +/// `ms`: wrap the selection (or the cursor char) in a pair; the wrap +/// including the pair becomes the selection +pub fn surroundAdd(p: *Pardes, pane: *Pane, ch: u21) void { + const pr = pairFor(ch) orelse return; + pane.select = false; + const eb = editTextEol(p, pane, selRows(pane)) orelse return; + const r = selRange(pane, eb.text, eb.row0); + pushUndo(p, pane); + const close_col = modal.nextGrapheme(modal.lineSlice(eb.text, r.b.row), r.b.col); + var new = modal.insertAt(p.gpa, eb.text, .{ .row = r.b.row, .col = close_col }, &[1]u8{pr.c}) catch return; + setEditText(p, pane, new); + new = modal.insertAt(p.gpa, new, .{ .row = r.a.row, .col = r.a.col }, &[1]u8{pr.o}) catch return; + setEditText(p, pane, new); + pane.msel.active = false; + pane.vsel = .{ .active = true, .row = @as(i32, @intCast(r.a.row)) + eb.row0, .col = @intCast(r.a.col), .explicit = false }; + pane.cur_row = @as(i32, @intCast(r.b.row)) + eb.row0; + pane.cur_col = @intCast(close_col + @as(usize, if (r.a.row == r.b.row) 1 else 0)); + pane.cur_pinned = true; + pane.sticky_col = -1; + pane.ensureCursorVisible(); +} + +/// `md`: delete the enclosing pair's chars; the cursor maps through +pub fn surroundDelete(p: *Pardes, pane: *Pane, ch: u21) void { + const pr = pairFor(ch) orelse return; + const lines = paneCursorLines(p, pane) catch return; + const cur = pane.toModalCursor(); + const r = (if (pr.o == pr.c) + modal.enclosingQuote(lines, cur, pr.o) + else + modal.enclosingPair(lines, cur, pr.o, pr.c)) orelse return; + const lo: i32 = @intCast(@min(r.a.row, cur.row)); + const hi: i32 = @intCast(@max(r.b.row, cur.row)); + const eb = editText(p, pane, lo, hi, -1) orelse return; + const text = eb.text; + const drow = -eb.row0; // surface row -> buffer row + const ra: modal.Cursor = .{ .row = @intCast(@as(i32, @intCast(r.a.row)) + drow), .col = r.a.col }; + const rb: modal.Cursor = .{ .row = @intCast(@as(i32, @intCast(r.b.row)) + drow), .col = r.b.col }; + pushUndo(p, pane); + const a_off = modal.offsetAt(text, ra); + const b_off = modal.offsetAt(text, rb); + const cur_off = modal.offsetAt(text, .{ .row = @intCast(@as(i32, @intCast(cur.row)) + drow), .col = cur.col }); + // the close first, so the open's position stays valid + var new = modal.deleteChar(p.gpa, text, rb) catch return; + setEditText(p, pane, new); + new = modal.deleteChar(p.gpa, new, ra) catch return; + setEditText(p, pane, new); + var nc = cur_off; + if (nc > b_off) nc -= 1; + if (nc > a_off) nc -= 1; + const cc = modal.positionAt(new, nc); + pane.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; + pane.cur_col = @intCast(cc.col); + pane.vsel.active = false; + pane.msel.active = false; + pane.cur_pinned = true; + pane.ensureCursorVisible(); +} + +/// `mr`: swap the enclosing pair's chars for 's +pub fn surroundReplace(p: *Pardes, pane: *Pane, from: u21, to: u21) void { + const fp = pairFor(from) orelse return; + const tp = pairFor(to) orelse return; + const lines = paneCursorLines(p, pane) catch return; + const cur = pane.toModalCursor(); + const r = (if (fp.o == fp.c) + modal.enclosingQuote(lines, cur, fp.o) + else + modal.enclosingPair(lines, cur, fp.o, fp.c)) orelse return; + const eb = editText(p, pane, @as(i32, @intCast(r.a.row)), @as(i32, @intCast(r.b.row)), -1) orelse return; + const drow = -eb.row0; // surface row -> buffer row + const ar: usize = @intCast(@as(i32, @intCast(r.a.row)) + drow); + const br: usize = @intCast(@as(i32, @intCast(r.b.row)) + drow); + pushUndo(p, pane); + const out = p.gpa.dupe(u8, eb.text) catch return; + out[modal.lineStartOffset(out, ar) + r.a.col] = tp.o; + out[modal.lineStartOffset(out, br) + r.b.col] = tp.c; + setEditText(p, pane, out); +} + +pub fn pushUndo(p: *Pardes, pane: *Pane) void { + // one keystroke, one undo step — even when it edited at ten cursors. + if (p.multi_on and !p.multi_first) return; + if (pane.file != null) return panes.File.pushUndo(p, pane); + panes.Terminal.pushUndo(p, pane); +} + +pub fn doUndo(p: *Pardes, pane: *Pane) void { + if (pane.file != null) return panes.File.undo(p, pane); + panes.Terminal.undo(p, pane); +} + +pub fn doRedo(p: *Pardes, pane: *Pane) void { + if (pane.file != null) return panes.File.redo(p, pane); + panes.Terminal.redo(p, pane); +} + +const PointerTextSelection = struct { + sel: Pane.Sel, + on_tag: bool, +}; + +pub fn pointerTextSelection(p: *const Pardes, id: usize, col: u16, row: u16, body_hit: ?Mouse.BodyHit, tag_hit: ?TagHit) ?PointerTextSelection { + if (p.panes[id] == null) return null; + const r = p.rects[id]; + if (col < r.x + config.GUTTER or col >= r.x + r.w or row < r.y or row >= r.y + r.h) return null; + const tag_y = if (p.settings.tag_bottom) r.y + r.h -| BOX_H else r.y; + const body_y = p.bodyTop(r); + const on_tag = row >= tag_y and row < tag_y + BOX_H; + if (on_tag and col < r.x + TAG_TEXT_INSET) return null; + // A notice chip is chrome: it sits OVER the body's top rows, so a + // click inside one belongs to nobody. The rest of those rows is + // ordinary body text and still answers normally. + if (!on_tag and row >= body_y and row - body_y < p.panes[id].?.notices.span() and + p.panes[id].?.notices.covers(row - body_y, col)) return null; + if (!on_tag) if (body_hit) |supplied| { + const mapped_hit = p.bodyHitForPane(id, supplied, false) orelse return null; + const c = @as(i32, mapped_hit.col) - config.GUTTER; + const v = @as(i32, mapped_hit.row) + BOX_H; + return .{ .sel = .{ .state = .dragging, .c0 = c, .c1 = c, .r0 = v, .r1 = v }, .on_tag = false }; + }; + const tag_col = if (on_tag) p.tagColumn(.pane, id, tag_hit, false) else null; + const c: i32 = (if (tag_col) |value| @as(i32, value) else @as(i32, col) - @as(i32, r.x + (if (on_tag) TAG_TEXT_INSET else config.GUTTER))) + + (if (on_tag) @as(i32, p.panes[id].?.tag_scroll) else 0); + const v: i32 = if (on_tag) 0 else @as(i32, row) - @as(i32, body_y) + @as(i32, BOX_H); + return .{ + .sel = .{ .state = .dragging, .c0 = c, .c1 = c, .r0 = v, .r1 = v }, + .on_tag = on_tag, + }; +} + +/// Drain the queue and say whether the shell was asked to do `want`. +fn drainedEffect(p: *Pardes, want: std.meta.Tag(Effect)) bool { + var seen = false; + while (p.nextEffect()) |effect| { + if (std.meta.activeTag(effect) == want) seen = true; + } + return seen; +} + +test "insert newline adds one indent level after a closing call" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile(" callback({})\n"); + pane.mode = .insert; + pane.cur_col = 16; + + p.update(.{ .key = .{ .cp = Key.enter } }); + + try std.testing.expectEqualSlices(u8, " callback({})\n \n", pane.file.?.content); + try std.testing.expectEqual(@as(i32, 1), pane.cur_row); + try std.testing.expectEqual(@as(i32, 8), pane.cur_col); +} + +test "insert and normal modes edit complete Unicode graphemes" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .cols = 60, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("a" ++ "e\u{301}" ++ "👩🏽\u{200d}🚀" ++ "界" ++ "z"); + + pane.mode = .insert; + pane.cur_col = 22; // on z, after the CJK grapheme + insertKey(p, pane, .{ .cp = Key.left }); + try std.testing.expectEqual(@as(i32, 19), pane.cur_col); + insertKey(p, pane, .{ .cp = Key.left }); + try std.testing.expectEqual(@as(i32, 4), pane.cur_col); + insertKey(p, pane, .{ .cp = Key.right }); + try std.testing.expectEqual(@as(i32, 19), pane.cur_col); + insertKey(p, pane, .{ .cp = Key.backspace }); + try std.testing.expectEqualStrings("ae\u{301}界z", pane.file.?.content); + try std.testing.expectEqual(@as(i32, 4), pane.cur_col); + + pane.mode = .normal; + pane.cur_col = 1; + normalDelete(p, pane, false); + try std.testing.expectEqualStrings("a界z", pane.file.?.content); + try std.testing.expectEqual(@as(i32, 1), pane.cur_col); + normalReplaceChar(p, pane, 'λ'); + try std.testing.expectEqualStrings("aλz", pane.file.?.content); + + panes.File.setContent(p, &pane.file.?, try gpa.dupe(u8, "界a")); + pane.cur_col = 3; + pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + normalReplaceChar(p, pane, 'λ'); + try std.testing.expectEqualStrings("λλ", pane.file.?.content); + try std.testing.expectEqual(@as(i32, 2), pane.cur_col); + try std.testing.expectEqual(@as(i32, 0), pane.vsel.col); + + panes.File.setContent(p, &pane.file.?, try gpa.dupe(u8, "界a")); + pane.cur_col = 0; + pane.vsel = .{ .active = true, .row = 0, .col = 3, .explicit = true }; + normalReplaceChar(p, pane, 'λ'); + try std.testing.expectEqualStrings("λλ", pane.file.?.content); + try std.testing.expectEqual(@as(i32, 0), pane.cur_col); + try std.testing.expectEqual(@as(i32, 2), pane.vsel.col); +} + +test "flat text replacement preserves a terminal fragment origin and direction" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 12 }); + defer p.deinit(); + p.update(.{ .output = .{ .pane = 0, .bytes = "first\r\nsecond\r\nthird\r\n界a\r\n" } }); + const pane = p.panes[0].?; + pane.mode = .normal; + pane.cur_pinned = true; + pane.cur_row = 3; + pane.cur_col = 0; + pane.vsel = .{ .active = true, .row = 3, .col = 3, .explicit = true }; + normalReplaceChar(p, pane, 'λ'); + try std.testing.expectEqual(@as(i32, 3), pane.ovl.?.row); + try std.testing.expectEqualStrings("λλ", pane.ovl.?.text); + try std.testing.expectEqual(@as(i32, 3), pane.cur_row); + try std.testing.expectEqual(@as(i32, 0), pane.cur_col); + try std.testing.expectEqual(@as(i32, 3), pane.vsel.row); + try std.testing.expectEqual(@as(i32, 2), pane.vsel.col); + try std.testing.expect(pane.vsel.active and pane.vsel.explicit); + const lines = try paneCursorLines(p, pane); + try std.testing.expectEqualStrings("first", lines[0]); + try std.testing.expectEqualStrings("λλ", lines[3]); + const flat = try flatSurface(p, pane); + try std.testing.expect(std.mem.startsWith(u8, flat, "first\nsecond\nthird\nλλ\n")); +} + +test "only the SPC clipboard commands cross to the system clipboard" { + if (platform == .web) return; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); + defer p.deinit(); + p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); + const pane = p.panes[0].?; + + _ = drainedEffect(p, .set_clipboard); + p.update(.{ .key = .{ .cp = 'y' } }); + try std.testing.expect(p.yank != null and p.yank.?.len > 0); + try std.testing.expect(!drainedEffect(p, .set_clipboard)); + p.update(.{ .key = .{ .cp = 'd' } }); + try std.testing.expect(!drainedEffect(p, .set_clipboard)); + + // `SPC y` is the one that does, and it puts the SAME text there that `y` + // put in the register — it is the ordinary yank path plus the mirror. + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = 'y' } }); + const yanked = p.yank orelse return error.MissingYank; + try std.testing.expect(drainedEffect(p, .set_clipboard)); + + const before = pane.file.?.content.len; + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = 'p' } }); + try std.testing.expect(p.clip_pending != null); + try std.testing.expect(drainedEffect(p, .read_clipboard)); + try std.testing.expectEqual(before, pane.file.?.content.len); // nothing yet + p.update(.{ .paste = "PASTED" }); + try std.testing.expect(p.clip_pending == null); + try std.testing.expect(std.mem.indexOf(u8, pane.file.?.content, "PASTED") != null); + // ...and it did NOT land in the register on its way past, nor echo back + // out to the clipboard it came from. + try std.testing.expectEqualStrings(yanked, p.yank orelse ""); + try std.testing.expect(!drainedEffect(p, .set_clipboard)); + + // An UNSOLICITED paste — the outer terminal's bracketed paste, a Cmd-V — + // is the same event with no request behind it, and means paste after. + p.update(.{ .paste = "UNASKED" }); + try std.testing.expect(std.mem.indexOf(u8, pane.file.?.content, "UNASKED") != null); + try std.testing.expectEqualStrings(yanked, p.yank orelse ""); + + // A request the shell never answers dies at the next keystroke rather + // than firing late into whatever pane is focused by then. + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = 'P' } }); + try std.testing.expect(p.clip_pending != null); + p.update(.{ .key = .{ .cp = 'l' } }); + try std.testing.expect(p.clip_pending == null); +} diff --git a/src/fs.zig b/src/fs.zig index 4d8a5188..04a8ed36 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -40,6 +40,7 @@ const source_files = [_]Source{ .{ .path = "src/colors.zig", .contents = @embedFile("colors.zig") }, .{ .path = "src/Messages.zig", .contents = @embedFile("Messages.zig") }, .{ .path = "src/body_layer.zig", .contents = @embedFile("body_layer.zig") }, + .{ .path = "src/edit.zig", .contents = @embedFile("edit.zig") }, .{ .path = "src/File.zig", .contents = @embedFile("File.zig") }, .{ .path = "src/Output.zig", .contents = @embedFile("Output.zig") }, .{ .path = "src/Mini.zig", .contents = @embedFile("Mini.zig") }, diff --git a/src/pardes.zig b/src/pardes.zig index fe96a221..55e1dc7a 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -11,6 +11,7 @@ pub const syntax = @import("syntax.zig"); pub const locations_config = @import("locations_config.zig"); const tracy = @import("tracy.zig"); pub const panes = @import("panes.zig"); +pub const edit = @import("edit.zig"); pub const body_layer = @import("body_layer.zig"); pub const Messages = @import("Messages.zig"); pub const colors = @import("colors.zig"); @@ -432,6 +433,7 @@ pub const Loc = struct { }; test { + _ = @import("edit.zig"); _ = @import("Messages.zig"); _ = @import("selection_pipe.zig"); _ = @import("colors.zig"); @@ -664,22 +666,6 @@ test "the acme chords act once per selection, not once on the primary" { try std.testing.expectEqual(@as(u8, 0), p.panes[0].?.nsel); } -test "insert newline adds one indent level after a closing call" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = try p.setTestFile(" callback({})\n"); - pane.mode = .insert; - pane.cur_col = 16; - - p.update(.{ .key = .{ .cp = Key.enter } }); - - try std.testing.expectEqualSlices(u8, " callback({})\n \n", pane.file.?.content); - try std.testing.expectEqual(@as(i32, 1), pane.cur_row); - try std.testing.expectEqual(@as(i32, 8), pane.cur_col); -} - test "startup config runs builtin lines in order and isolates bad lines" { const p = try Pardes.init(std.testing.allocator, .{ .startup_config = @@ -2702,49 +2688,6 @@ test "every line of the board's boot buffer renders whole" { // ---- the boundary types ---- -test "insert and normal modes edit complete Unicode graphemes" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .cols = 60, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("a" ++ "e\u{301}" ++ "👩🏽\u{200d}🚀" ++ "界" ++ "z"); - - pane.mode = .insert; - pane.cur_col = 22; // on z, after the CJK grapheme - p.insertKey(pane, .{ .cp = Key.left }); - try std.testing.expectEqual(@as(i32, 19), pane.cur_col); - p.insertKey(pane, .{ .cp = Key.left }); - try std.testing.expectEqual(@as(i32, 4), pane.cur_col); - p.insertKey(pane, .{ .cp = Key.right }); - try std.testing.expectEqual(@as(i32, 19), pane.cur_col); - p.insertKey(pane, .{ .cp = Key.backspace }); - try std.testing.expectEqualStrings("ae\u{301}界z", pane.file.?.content); - try std.testing.expectEqual(@as(i32, 4), pane.cur_col); - - pane.mode = .normal; - pane.cur_col = 1; - p.normalDelete(pane, false); - try std.testing.expectEqualStrings("a界z", pane.file.?.content); - try std.testing.expectEqual(@as(i32, 1), pane.cur_col); - p.normalReplaceChar(pane, 'λ'); - try std.testing.expectEqualStrings("aλz", pane.file.?.content); - - panes.File.setContent(p, &pane.file.?, try gpa.dupe(u8, "界a")); - pane.cur_col = 3; - pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - p.normalReplaceChar(pane, 'λ'); - try std.testing.expectEqualStrings("λλ", pane.file.?.content); - try std.testing.expectEqual(@as(i32, 2), pane.cur_col); - try std.testing.expectEqual(@as(i32, 0), pane.vsel.col); - - panes.File.setContent(p, &pane.file.?, try gpa.dupe(u8, "界a")); - pane.cur_col = 0; - pane.vsel = .{ .active = true, .row = 0, .col = 3, .explicit = true }; - p.normalReplaceChar(pane, 'λ'); - try std.testing.expectEqualStrings("λλ", pane.file.?.content); - try std.testing.expectEqual(@as(i32, 0), pane.cur_col); - try std.testing.expectEqual(@as(i32, 2), pane.vsel.col); -} - test "flat text movement and selection replay need no scratch rows" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 }); @@ -2796,31 +2739,6 @@ test "flat text movement and selection replay need no scratch rows" { try std.testing.expectEqual(@as(i32, 8), pane.cur_col); } -test "flat text replacement preserves a terminal fragment origin and direction" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 12 }); - defer p.deinit(); - p.update(.{ .output = .{ .pane = 0, .bytes = "first\r\nsecond\r\nthird\r\n界a\r\n" } }); - const pane = p.panes[0].?; - pane.mode = .normal; - pane.cur_pinned = true; - pane.cur_row = 3; - pane.cur_col = 0; - pane.vsel = .{ .active = true, .row = 3, .col = 3, .explicit = true }; - p.normalReplaceChar(pane, 'λ'); - try std.testing.expectEqual(@as(i32, 3), pane.ovl.?.row); - try std.testing.expectEqualStrings("λλ", pane.ovl.?.text); - try std.testing.expectEqual(@as(i32, 3), pane.cur_row); - try std.testing.expectEqual(@as(i32, 0), pane.cur_col); - try std.testing.expectEqual(@as(i32, 3), pane.vsel.row); - try std.testing.expectEqual(@as(i32, 2), pane.vsel.col); - try std.testing.expect(pane.vsel.active and pane.vsel.explicit); - const lines = try p.paneCursorLines(pane); - try std.testing.expectEqualStrings("first", lines[0]); - try std.testing.expectEqualStrings("λλ", lines[3]); - const flat = try p.flatSurface(pane); - try std.testing.expect(std.mem.startsWith(u8, flat, "first\nsecond\nthird\nλλ\n")); -} - test "Unicode display cells map back to body and tag byte cursors" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .cols = 60, .rows = 12 }); @@ -3343,7 +3261,7 @@ pub const Key = struct { pub const delete: u21 = 0xF0009; }; -fn hit(key: Key, binding: []const config.Chord) bool { +pub fn hit(key: Key, binding: []const config.Chord) bool { for (binding) |c| { if (key.cp != c.cp or key.ctrl != c.ctrl or key.alt != c.alt) continue; if (c.shift and !key.shift) continue; @@ -3352,7 +3270,7 @@ fn hit(key: Key, binding: []const config.Chord) bool { return false; } -const sel_slot = @intFromEnum(config.select_button); +pub const sel_slot = @intFromEnum(config.select_button); fn isPrefix(key: Key, cp: u21) bool { return key.cp == cp and !key.ctrl and !key.alt; @@ -4777,7 +4695,7 @@ pub const Pardes = struct { in_len: usize = 0, yank: ?[]u8 = null, - clip_pending: ?ClipRequest = null, + clip_pending: ?edit.ClipRequest = null, /// the last serialized dump (gpa-owned), read by the write_dump effect dump_out: ?[]u8 = null, /// where the shell wrote the last dump (shell reports back after @@ -5031,7 +4949,7 @@ pub const Pardes = struct { } } - fn headerReplace(p: *Pardes, bytes: []const u8) void { + pub fn headerReplace(p: *Pardes, bytes: []const u8) void { const cur = p.topbar_col orelse return; const anchor = p.header_anchor orelse cur; const lo = @min(cur, anchor); @@ -5740,7 +5658,7 @@ pub const Pardes = struct { } } }, - .paste => |bytes| p.applyPaste(bytes), + .paste => |bytes| edit.applyPaste(p, bytes), .command => |line| _ = p.executeBuiltinLine(p.active, line), .pinch => |scale| p.ov_pinch_scale = scale, .touch_scroll => |delta| p.ov_touch_scroll_delta = delta, @@ -5776,97 +5694,6 @@ pub const Pardes = struct { } } - fn setYank(p: *Pardes, text: []const u8) void { - if (p.multi_on and !p.multi_first) { - const old = p.yank orelse ""; - const sep: []const u8 = if (text.len > 0 and text[text.len - 1] == '\n') "" else "\n"; - const joined = std.fmt.allocPrint(p.gpa, "{s}{s}{s}", .{ text, sep, old }) catch return; - if (p.yank) |y| p.gpa.free(y); - p.yank = joined; - return; - } - if (p.yank) |y| p.gpa.free(y); - p.yank = p.gpa.dupe(u8, text) catch null; - } - - fn setClipboard(p: *Pardes, text: []const u8) void { - p.setYank(text); - if (!p.multi_on or p.multi_first) p.emit(.{ .set_clipboard = {} }); - } - - /// Where a `SPC p` / `SPC P` / `SPC R` goes once the shell answers. - pub const ClipRequest = struct { - pane: usize, - serial: u32, - mode: enum { after, before, replace }, - }; - - pub fn clipRequest(p: *Pardes, id: usize, mode: @FieldType(ClipRequest, "mode")) void { - const pane = p.panes[id] orelse return; - p.clip_pending = .{ .pane = id, .serial = pane.serial, .mode = mode }; - p.emit(.read_clipboard); - } - - pub fn typeToTty(p: *Pardes, id: usize, pane: *const Pane, text: []const u8) void { - if (text.len == 0) return; - if (panes.Terminal.bracketedPaste(pane)) { - p.emitWrite(id, "\x1b[200~"); - p.emitWrite(id, text); - p.emitWrite(id, "\x1b[201~"); - return; - } - const cp = p.scratch.allocator().dupe(u8, text) catch return; - for (cp) |*ch| { - if (ch.* == '\n') ch.* = '\r'; - } - p.emitWrite(id, cp); - } - - fn applyPaste(p: *Pardes, bytes: []const u8) void { - if (p.topbar_col != null) { - p.clip_pending = null; - p.headerReplace(bytes); - return; - } - const req = p.clip_pending; - p.clip_pending = null; - if (bytes.len == 0) return; - const id = if (req) |r| r.pane else p.active; - const pane = p.panes[id] orelse return; - if (req) |r| if (pane.serial != r.serial) return; - p.active = id; - if (pane.tag_edit) { - const text = p.tagText(p.scratch.allocator(), pane) catch return; - const range: @TypeOf(tagSelBounds(pane)) = if (pane.tag_sel) tagSelBounds(pane) else .{ .lo = pane.tag_col, .hi = pane.tag_col }; - const hi = if (pane.tag_sel) modal.nextGrapheme(text, range.hi) else range.hi; - _ = p.replaceTagText(pane, range.lo, hi, bytes); - if (selRegexArmed(pane)) |a| p.applySelRegex(pane, a.pat, a.split); - return; - } - if (pane.isTerminal() and pane.mode == .tty) return p.typeToTty(id, pane, bytes); - switch (if (req) |r| r.mode else .after) { - .after => p.pasteText(pane, bytes, false, 1), - .before => p.pasteText(pane, bytes, true, 1), - .replace => p.replaceWithText(pane, bytes), - } - } - - pub fn clipYank(p: *Pardes, pane: *Pane, main_only: bool) void { - if (comptime pdf_enabled) if (pane.pdf) |pv| { - if (pv.selection_text.len > 0) p.setClipboard(pv.selection_text); - return; - }; - if (pane.nsel > 0 and !main_only) { - p.replaySels(pane, .{ .normal = .{ .edit = .{ .kind = .yank, .count = 1 } } }); - } else { - const others = pane.nsel; - pane.nsel = 0; - p.normalYank(pane); - pane.nsel = others; - } - p.emit(.{ .set_clipboard = {} }); - } - fn multiOnce(p: *Pardes) bool { if (!p.multi_on) return true; p.multi_stop = true; @@ -6004,7 +5831,7 @@ pub const Pardes = struct { pane.nsel_snap = 0; // ...and its s/S preview keeps whatever it previewed } - fn tagSelBounds(pane: *Pane) struct { lo: usize, hi: usize } { + pub fn tagSelBounds(pane: *Pane) struct { lo: usize, hi: usize } { const a: usize = pane.tag_anchor; const c: usize = pane.tag_col; return .{ .lo = @min(a, c), .hi = @max(a, c) }; @@ -6060,7 +5887,7 @@ pub const Pardes = struct { /// Replace one range in the rendered tag. The file name and command tail /// retain separate identities even when a selection crosses their boundary. - fn replaceTagText(p: *Pardes, pane: *Pane, start: usize, finish: usize, input: []const u8) bool { + pub fn replaceTagText(p: *Pardes, pane: *Pane, start: usize, finish: usize, input: []const u8) bool { const prefix_len = (p.tagPrefix(pane) catch return false).len; const end = prefix_len + pane.tag_tail_len; const lo = @min(start, end); @@ -6183,7 +6010,7 @@ pub const Pardes = struct { return; } if (hit(key, config.tag_yank)) { - if (p.tagChordText(pane)) |txt| p.setClipboard(txt); + if (p.tagChordText(pane)) |txt| edit.setClipboard(p, txt); return; } if (hit(key, config.insert) or hit(key, config.append) or @@ -6314,7 +6141,7 @@ pub const Pardes = struct { if (key.ctrl and (key.cp == 'c' or key.cp == 'x')) { const anchor = p.header_anchor orelse @as(u16, @intCast(cur)); if (anchor != cur) { - p.setClipboard(bar[@min(anchor, cur)..@max(anchor, cur)]); + edit.setClipboard(p, bar[@min(anchor, cur)..@max(anchor, cur)]); if (key.cp == 'x') p.headerReplace(""); } return; @@ -6388,140 +6215,6 @@ pub const Pardes = struct { if (col) |c| p.topbar_col = @intCast(@min(c, modal.prevGrapheme(bar, bar.len))); } - /// tag + '\n' + body: the full selectable pane text (row 0 = the tag) - fn paneText(p: *Pardes, pane: *Pane) ![]u8 { - const arena = p.scratch.allocator(); - return std.fmt.allocPrint(arena, "{s}\n{s}", .{ try p.tagText(arena, pane), try body_layer.bodyText(p, arena, pane) }); - } - - pub fn pointerSourceLine(pane: *Pane, terminal_lines: []const []const u8, row: i32) []const u8 { - if (row < 0) return ""; - if (pane.file != null) return panes.File.sourceLine(pane, row); - const index: usize = @intCast(row); - return if (index < terminal_lines.len) terminal_lines[index] else ""; - } - - fn capturePointerSelection(p: *Pardes, pane: *Pane, slot: usize) !void { - var gesture = pane.sel[slot]; - if (pane.hasPdf()) return; - if (gesture.state != .done or @min(gesture.r0, gesture.r1) < BOX_H) return; - if (pane.pointerSelection(gesture) != null) return; - pane.clearPointerSelection(slot); - // Refresh the source map before translating the rendered rectangle. - const raw = pane.isTerminal() and pane.mode == .tty; - const body = try body_layer.bodyText(p, p.scratch.allocator(), pane); - const terminal_lines = if (pane.file == null and !raw) try panes.Terminal.cursorLines(p, pane) else &.{}; - const first = @max(0, @min(gesture.r0, gesture.r1) - BOX_H); - const logical_rows = if (pane.body_rows > 0) pane.body_rows else pane.rows; - const last = @min(@as(i32, logical_rows) - 1, @max(gesture.r0, gesture.r1) - BOX_H); - if (first > last) return; - const rows = try p.gpa.alloc(Pane.PointerRow, @intCast(last - first + 1)); - errdefer p.gpa.free(rows); - const raw_text = if (raw) try p.gpa.dupe(u8, try p.selectionText(pane, gesture)) else null; - const prefix: i32 = if (pane.file != null) panes.File.gutterWidth(pane) else 0; - const left = @max(0, @min(gesture.c0, gesture.c1)); - const right = @max(0, @max(gesture.c0, gesture.c1)); - for (rows, 0..) |*row, index| { - const visible = first + @as(i32, @intCast(index)); - var source = pane.wrapAt(visible); - if (raw) source = .{ .line = panes.Terminal.gridOffset(pane) + visible, .at = 0 }; - const line = if (raw) modal.lineSlice(body, @intCast(visible)) else pointerSourceLine(pane, terminal_lines, source.line); - var next = pane.wrapAt(visible + 1); - if (raw) next = .{ .line = source.line + 1, .at = 0 }; - const end = if (next.line == source.line) @min(line.len, @as(usize, @intCast(@max(0, next.at)))) else line.len; - const start: usize = @intCast(@max(0, source.at)); - const lo = @min(end, panes.File.byteAtDisplayFrom(line, start, @intCast(@max(0, left - prefix)))); - const at = @min(end, panes.File.byteAtDisplayFrom(line, start, @intCast(@max(0, right - prefix)))); - row.* = .{ - .row = source.line, - .lo = lo, - .hi = if (right < prefix) lo else @min(end, modal.nextGrapheme(line, at)), - .raw_terminal = raw, - .prompt_bytes = if (raw) panes.Terminal.promptPrefixBytes(pane, source.line, line) else 0, - }; - const rendered = modal.lineSlice(body, @intCast(visible)); - const prefix_end = @min(rendered.len, @as(usize, @intCast(@min(prefix, right + 1)))); - const prefix_start = @min(prefix_end, @as(usize, @intCast(left))); - row.prefix_len = @intCast(prefix_end - prefix_start); - @memcpy(row.prefix[0..row.prefix_len], rendered[prefix_start..prefix_end]); - } - pane.next_pointer_selection += 1; - gesture.source_id = pane.next_pointer_selection; - pane.sel[slot] = gesture; - pane.pointer_selections[slot] = .{ .gesture = gesture, .rows = rows, .raw_text = raw_text }; - } - - /// block-selected text, newline-joined per row; reads the rendered screen - /// so typed text and shell output select identically. Scratch-owned. - fn selectionText(p: *Pardes, pane: *Pane, sl: Pane.Sel) ![]const u8 { - const arena = p.scratch.allocator(); - if (pane.rawPointerText(sl)) |text| return arena.dupe(u8, text); - if (pane.pointerSelection(sl)) |rows| { - const terminal_lines = if (pane.file == null) try panes.Terminal.cursorLines(p, pane) else &.{}; - var total: usize = rows.len -| 1; - for (rows) |row| { - const line = pointerSourceLine(pane, terminal_lines, row.row); - total += row.prefix_len + @min(row.hi, line.len) - @min(row.lo, line.len); - } - const result = try arena.alloc(u8, total); - var at: usize = 0; - for (rows, 0..) |row, index| { - if (index > 0) { - result[at] = '\n'; - at += 1; - } - @memcpy(result[at..][0..row.prefix_len], row.prefix[0..row.prefix_len]); - at += row.prefix_len; - const line = pointerSourceLine(pane, terminal_lines, row.row); - const bytes = line[@min(row.lo, line.len)..@min(row.hi, line.len)]; - @memcpy(result[at..][0..bytes.len], bytes); - at += bytes.len; - } - return result; - } - const r0 = @min(sl.r0, sl.r1); - const r1 = @max(sl.r0, sl.r1); - const c0: usize = @intCast(@max(0, @min(sl.c0, sl.c1))); - const c1: usize = @intCast(@max(0, @max(sl.c0, sl.c1))); - const text = try p.paneText(pane); - var total: usize = 0; - var selected: usize = 0; - var count_it = std.mem.splitAny(u8, text, "\n"); - var count_row: i32 = 0; - while (count_it.next()) |line| : (count_row += 1) { - if (count_row < r0 or count_row > r1) continue; - const b0 = @min(panes.File.renderedLineByteCol(pane, count_row, line, c0), line.len); - const b1 = modal.nextGrapheme(line, @min(panes.File.renderedLineByteCol(pane, count_row, line, c1), line.len)); - total += b1 - b0 + @intFromBool(selected > 0); - selected += 1; - } - const out = try arena.alloc(u8, total); - var at: usize = 0; - var it = std.mem.splitAny(u8, text, "\n"); - var v: i32 = 0; - var first = true; - while (it.next()) |line| : (v += 1) { - if (v < r0 or v > r1) continue; - if (!first) { - out[at] = '\n'; - at += 1; - } - first = false; - const b0 = @min(panes.File.renderedLineByteCol(pane, v, line, c0), line.len); - const b1 = modal.nextGrapheme(line, @min(panes.File.renderedLineByteCol(pane, v, line, c1), line.len)); - @memcpy(out[at..][0 .. b1 - b0], line[b0..b1]); - at += b1 - b0; - } - return out; - } - - fn spanHas(r: i32, c: i32, ar: i32, ac: i32, br: i32, bc: i32) bool { - const fwd = ar < br or (ar == br and ac <= bc); - const sr, const sc = if (fwd) .{ ar, ac } else .{ br, bc }; - const er, const ec = if (fwd) .{ br, bc } else .{ ar, ac }; - return (r > sr or (r == sr and c >= sc)) and (r < er or (r == er and c <= ec)); - } - const ExpandedWord = struct { lo: usize, hi: usize }; fn expandedWord(line: []const u8, col: usize) ?ExpandedWord { @@ -6538,7 +6231,7 @@ pub const Pardes = struct { fn expandedSel(p: *Pardes, pane: *Pane, at: Pane.Sel) ?Pane.Sel { var sl = at; if (sl.c0 != sl.c1 or sl.r0 != sl.r1) return sl; - const text = p.paneText(pane) catch return null; + const text = edit.paneText(p, pane) catch return null; var it = std.mem.splitAny(u8, text, "\n"); var v: i32 = 0; while (it.next()) |line| : (v += 1) { @@ -6561,7 +6254,7 @@ pub const Pardes = struct { const vcol = if (pane.file != null) panes.File.displayOffset(pane, pane.cur_row, w.at, pane.cur_col) + @as(i32, panes.File.gutterWidth(pane)) else blk: { - const lines = p.paneCursorLines(pane) catch break :blk pane.cur_col; + const lines = edit.paneCursorLines(p, pane) catch break :blk pane.cur_col; const local = pane.cur_row; if (local < 0 or @as(usize, @intCast(local)) >= lines.len) break :blk pane.cur_col; break :blk panes.File.lineDisplayOffset(lines[@intCast(local)], @intCast(@max(0, w.at)), @intCast(@max(0, pane.cur_col))); @@ -6569,18 +6262,9 @@ pub const Pardes = struct { return .{ .state = .done, .c0 = vcol, .c1 = vcol, .r0 = vrow, .r1 = vrow }; } - /// the active modal selection as text (v range or x lines), if any - fn currentSelText(p: *Pardes, pane: *Pane) ?[]const u8 { - if (comptime pdf_enabled) if (p.native_images) if (pane.pdf) |pv| - if (pv.selection != null and pv.selection_text.len > 0) return pv.selection_text; - if (pane.vsel.active) return p.vselText(pane); - if (!pane.msel.active) return null; - return p.yankRows(pane, @min(pane.msel.r0, pane.msel.r1), @max(pane.msel.r0, pane.msel.r1)); - } - fn chordEachSel(p: *Pardes, pane: *Pane, cmd: Builtin) bool { if (pane.nsel == 0) return false; - const text = p.flatSurface(pane) catch return false; + const text = edit.flatSurface(p, pane) catch return false; var ranges: [Pane.max_selections]modal.Selection = undefined; const got = pane.ranges(text, 0, &ranges); if (got.n < 2) return false; @@ -6643,7 +6327,8 @@ pub const Pardes = struct { const col = if (raw) @as(i32, @intCast(panes.File.rawAtDisplay(raw_line, @intCast(@max(0, clicked.c0))))) else if (clicked.r0 >= BOX_H) - p.paneByteAtDisplay( + edit.paneByteAtDisplay( + p, pane, wrapped.line, wrapped.at, @@ -6677,22 +6362,22 @@ pub const Pardes = struct { break :inside true; } break :inside false; - } else spanHas(clicked.r0, clicked.c0, kept.r0, kept.c0, kept.r1, kept.c1); + } else edit.spanHas(clicked.r0, clicked.c0, kept.r0, kept.c0, kept.r1, kept.c1); if (kept.state == .done and inside_kept) { - result.text = p.selectionText(pane, kept) catch null; + result.text = edit.selectionText(p, pane, kept) catch null; result.preview = kept; return result; } if (pane.vsel.active and pane.vsel.explicit and - spanHas(row, col, pane.vsel.row, pane.vsel.col, pane.cur_row, pane.cur_col)) + edit.spanHas(row, col, pane.vsel.row, pane.vsel.col, pane.cur_row, pane.cur_col)) { - result.text = p.currentSelText(pane); + result.text = edit.currentSelText(p, pane); return result; } if (pane.msel.active and row >= @min(pane.msel.r0, pane.msel.r1) and row <= @max(pane.msel.r0, pane.msel.r1)) { - result.text = p.currentSelText(pane); + result.text = edit.currentSelText(p, pane); return result; } if (pane.file != null and clicked.r0 >= BOX_H and clicked.r0 == clicked.r1 and clicked.c0 == clicked.c1) { @@ -6719,7 +6404,7 @@ pub const Pardes = struct { } const expanded = p.expandedSel(pane, clicked) orelse return result; - result.text = p.selectionText(pane, expanded) catch null; + result.text = edit.selectionText(p, pane, expanded) catch null; if (result.text == null or result.text.?.len == 0) return result; result.preview = expanded; result.expanded = expanded; @@ -6741,9 +6426,9 @@ pub const Pardes = struct { return pv.selection_text; }; if (t.sel[sel_slot].state == .done) { - if (p.selectionText(t, t.sel[sel_slot]) catch null) |tx| return tx; + if (edit.selectionText(p, t, t.sel[sel_slot]) catch null) |tx| return tx; } else if ((t.vsel.active and t.vsel.explicit) or t.msel.active) { - if (p.currentSelText(t)) |tx| return tx; + if (edit.currentSelText(p, t)) |tx| return tx; } } return null; @@ -6770,42 +6455,14 @@ pub const Pardes = struct { pane.mode = .normal; pane.normal.clear(); }, - .normal => p.enterInsert(pane, .at, 1), + .normal => edit.enterInsert(p, pane, .at, 1), .insert => { - p.exitInsert(pane); + edit.exitInsert(p, pane); if (pane.isTerminal()) panes.Terminal.enterTty(p, id); }, } } - fn exitInsert(p: *Pardes, pane: *Pane) void { - pane.mode = .normal; - pane.msel.active = false; - pane.normal.clear(); - if (pane.append_at) |aa| { - pane.append_at = null; - const text = p.flatSurface(pane) catch return; - const gap = modal.offsetAt(text, .{ .row = @intCast(@max(0, pane.cur_row)), .col = @intCast(@max(0, pane.cur_col)) }); - const a_off = modal.offsetAt(text, .{ .row = @intCast(@max(0, aa.row)), .col = @intCast(@max(0, aa.col)) }); - const back = @max(a_off, modal.prevGrapheme(text, gap)); - const bc = modal.positionAt(text, back); - pane.cur_row = @intCast(bc.row); - pane.cur_col = @intCast(bc.col); - pane.vsel = .{ .active = a_off != back, .row = aa.row, .col = aa.col, .explicit = false }; - pane.cur_pinned = true; - pane.ensureCursorVisible(); - for (pane.sels[0..pane.nsel]) |*s| { - const sgap = modal.offsetAt(text, .{ .row = @intCast(@max(0, s.row)), .col = @intCast(@max(0, s.col)) }); - const b2 = if (back == gap) sgap else modal.prevGrapheme(text, sgap); - const bp = modal.positionAt(text, b2); - s.row = @intCast(bp.row); - s.col = @intCast(bp.col); - s.arow = s.row; - s.acol = s.col; - } - } - } - /// One mode transition for the terminal tag command and the Ctrl-B chord. pub fn toggleTty(p: *Pardes, id: usize) void { const pane = p.panes[id] orelse return; @@ -6851,12 +6508,12 @@ pub const Pardes = struct { // account instead — a coding agent goes there hunting for an image. // A host that folds Shift into the letter says the same thing. if (key.ctrl and !key.alt and (key.cp == 'v' or key.cp == 'V')) { - if (key.shift or key.cp == 'V') return p.clipRequest(p.active, .after); + if (key.shift or key.cp == 'V') return edit.clipRequest(p, p.active, .after); // With something in the register this is a paste. With nothing // in it the chord is the program's -- vim's visual block, // readline's quoted-insert -- and swallowing it would make // Ctrl-V a black hole in every full-screen application. - if (p.yank) |text| return p.typeToTty(p.active, pane, text); + if (p.yank) |text| return edit.typeToTty(p, p.active, pane, text); } return panes.Terminal.forwardKey(p, p.active, key); } @@ -6935,12 +6592,12 @@ pub const Pardes = struct { } if (pane.mode == .normal and (hit(key, config.look_key) or hit(key, config.exec_key))) { const cmd = if (hit(key, config.look_key)) config.look_cmd else config.exec_cmd; - p.pinPaneCursor(pane); + edit.pinPaneCursor(p, pane); const explicit = (p.native_images and pane.hasPdfSelection()) or (pane.vsel.active and pane.vsel.explicit) or pane.msel.active; if (explicit) { if (p.chordEachSel(pane, cmd)) return; - if (p.currentSelText(pane)) |txt| { + if (edit.currentSelText(p, pane)) |txt| { pane.vsel.active = false; pane.msel.active = false; pane.select = false; @@ -6949,7 +6606,7 @@ pub const Pardes = struct { } } const sel = p.expandedSel(pane, p.cursorWordSel(pane)) orelse return; - const word = p.selectionText(pane, sel) catch return; + const word = edit.selectionText(p, pane, sel) catch return; p.runBuiltin(cmd, p.active, "", word); return; } @@ -6959,8 +6616,8 @@ pub const Pardes = struct { p.handleNormal(pane, key); }, .insert => { - if (hit(key, config.escape)) return p.exitInsert(pane); - p.handleInsert(pane, key); + if (hit(key, config.escape)) return edit.exitInsert(p, pane); + edit.handleInsert(p, pane, key); }, .tty => panes.Terminal.forwardKey(p, p.active, key), } @@ -6994,51 +6651,8 @@ pub const Pardes = struct { // ---- helix-modal normal mode ---- - pub fn paneCursorLines(p: *Pardes, pane: *Pane) ![]const []const u8 { - const arena = p.scratch.allocator(); - if (pane.file) |*f| return panes.File.cursorLines(arena, pane, f); - if (pane.hasPdf()) { - if (comptime pdf_enabled) return panes.Pdf.textLines(&pane.pdf.?, p.pdf_gpa, arena); - unreachable; - } - return panes.Terminal.cursorLines(p, pane); - } - - fn paneByteAtDisplay(p: *Pardes, pane: *Pane, row: i32, from_raw: i32, display_col: i32) i32 { - if (pane.file != null) - return panes.File.byteAtRowDisplay(pane, row, from_raw, display_col); - const lines = p.paneCursorLines(pane) catch return @max(0, from_raw + display_col); - const local = row; - if (local < 0 or @as(usize, @intCast(local)) >= lines.len) - return @max(0, from_raw + display_col); - return @intCast(panes.File.byteAtDisplayFrom( - lines[@intCast(local)], - @intCast(@max(0, from_raw)), - @intCast(@max(0, display_col)), - )); - } - - /// Freeze the live terminal cursor into the modal coordinate space. The - /// emulator reports screen cells; editing state stores UTF-8 byte offsets. - fn pinPaneCursor(p: *Pardes, pane: *Pane) void { - if (pane.cur_pinned) return; - pane.pinCursor(); - if (pane.file == null and !pane.hasPdf()) - pane.cur_col = p.paneByteAtDisplay(pane, pane.cur_row, 0, pane.cur_col); - } - - fn flatSurface(p: *Pardes, pane: *Pane) ![]const u8 { - if (pane.file) |f| return f.content; - if (pane.hasPdf()) { - if (comptime pdf_enabled) return pane.pdf.?.ensureText(p.pdf_gpa); - unreachable; - } - const lines = try panes.Terminal.cursorLines(p, pane); - return panes.Terminal.flatSurface(p, pane, lines); - } - fn startSelRegex(p: *Pardes, pane: *Pane, split: bool) void { - const text = p.flatSurface(pane) catch return; + const text = edit.flatSurface(p, pane) catch return; const got = pane.ranges(text, 0, &pane.sel_snap); pane.nsel_snap = @intCast(got.n); pane.sel_snap_pri = @intCast(got.pri); @@ -7060,8 +6674,8 @@ pub const Pardes = struct { return .{ .pat = armed[slash + 1 ..], .split = split }; } - fn applySelRegex(p: *Pardes, pane: *Pane, pat: []const u8, split: bool) void { - const text = p.flatSurface(pane) catch return; + pub fn applySelRegex(p: *Pardes, pane: *Pane, pat: []const u8, split: bool) void { + const text = edit.flatSurface(p, pane) catch return; const snap = pane.sel_snap[0..pane.nsel_snap]; var out: [Pane.max_selections]modal.Selection = undefined; var m: usize = 0; @@ -7147,7 +6761,7 @@ pub const Pardes = struct { insert: Key, }; - fn replaySels(p: *Pardes, pane: *Pane, what: Replay) void { + pub fn replaySels(p: *Pardes, pane: *Pane, what: Replay) void { const id = p.active; const serial = pane.serial; // the whole selection in pane coordinates, document order. pane.sels @@ -7210,14 +6824,14 @@ pub const Pardes = struct { pane.sticky_col = list[i].sticky; switch (what) { .normal => |normal_action| p.executeNormalAction(pane, normal_action), - .insert => |insert_key| p.insertKey(pane, insert_key), + .insert => |insert_key| edit.insertKey(p, pane, insert_key), } if (p.multi_stop) break; if (i == pri) { after = KeyState.of(pane); after_expl = pane.vsel.explicit; } - const text = p.flatSurface(pane) catch { + const text = edit.flatSurface(p, pane) catch { p.multi_stop = true; break; }; @@ -7236,7 +6850,7 @@ pub const Pardes = struct { if (pn.serial == serial) pn.nsel = 0; return; } - const text = p.flatSurface(pane) catch return; + const text = edit.flatSurface(p, pane) catch return; var rs: [Pane.max_selections]modal.Selection = undefined; var st: [Pane.max_selections]i32 = undefined; for (res[0..n], 0..) |r, k| { @@ -7248,11 +6862,6 @@ pub const Pardes = struct { pane.ensureCursorVisible(); // the view follows the PRIMARY, not the last pass } - fn paneWrapWidth(p: *const Pardes, pane: *const Pane) usize { - if (pane.file == null) return 0; - return panes.File.wrapWidth(pane, p.settings.wrap); - } - fn handleNormal(p: *Pardes, pane: *Pane, key: Key) void { const parsed = modal.Normal.parse(&pane.normal, normalInput(key)); const semantic = switch (parsed) { @@ -7271,9 +6880,9 @@ pub const Pardes = struct { pane.clearPointerSelection(sel_slot); pane.sel[sel_slot].state = .none; }; - if (raw_selection and semantic == .edit and semantic.edit.kind == .yank) return p.normalYank(pane); - p.pinPaneCursor(pane); - const text = p.flatSurface(pane) catch return; + if (raw_selection and semantic == .edit and semantic.edit.kind == .yank) return edit.normalYank(p, pane); + edit.pinPaneCursor(p, pane); + const text = edit.flatSurface(p, pane) catch return; const range = pane.primaryRange(text, 0); const cur = modal.selectionCursor(text, range); @@ -7302,8 +6911,8 @@ pub const Pardes = struct { if (nw == slice.len) return; return pane.pointMove(text, range, ls + nw); }, - .line_down => return pane.visualMove(text, range, true, go.count, p.paneWrapWidth(pane)), - .line_up => return pane.visualMove(text, range, false, go.count, p.paneWrapWidth(pane)), + .line_down => return pane.visualMove(text, range, true, go.count, edit.paneWrapWidth(p, pane)), + .line_up => return pane.visualMove(text, range, false, go.count, edit.paneWrapWidth(p, pane)), .column => { const line = modal.lineAtOffset(text, cur); const ls = modal.lineStartOffset(text, line); @@ -7348,21 +6957,21 @@ pub const Pardes = struct { find.count, ); }, - .replace_char => |char| return p.normalReplaceChar(pane, char), + .replace_char => |char| return edit.normalReplaceChar(p, pane, char), .match_bracket => { - const lines = p.paneCursorLines(pane) catch return; + const lines = edit.paneCursorLines(p, pane) catch return; const mc = modal.matchBracket(lines, modal.positionAt(text, cur)) orelse return; return pane.pointMove(text, range, modal.offsetAt(text, mc)); }, - .textobject => |object| return p.textobjectSelect(pane, text, object.char, object.around), - .surround_add => |char| return p.surroundAdd(pane, char), - .surround_delete => |char| return p.surroundDelete(pane, char), - .surround_replace => |replace| return p.surroundReplace(pane, replace.from, replace.to), + .textobject => |object| return edit.textobjectSelect(p, pane, text, object.char, object.around), + .surround_add => |char| return edit.surroundAdd(p, pane, char), + .surround_delete => |char| return edit.surroundDelete(p, pane, char), + .surround_replace => |replace| return edit.surroundReplace(p, pane, replace.from, replace.to), .paragraph => |paragraph| { const r2 = modal.moveParagraph(text, range, paragraph.count, paragraph.direction == .forward, pane.select); return pane.setRange(text, 0, r2, false); }, - .add_newline => |newline| return p.addNewline(pane, newline.direction == .forward, newline.count), + .add_newline => |newline| return edit.addNewline(p, pane, newline.direction == .forward, newline.count), .diagnostic => |diagnostic| { const fwd = diagnostic.direction == .forward; if (!diagnostic.endpoint) { @@ -7450,7 +7059,7 @@ pub const Pardes = struct { .up => return pane.scrollViewMove(text, range, -@as(i32, pane.rows)), }, .insert => |insert| { - const where: InsertAt = switch (insert.kind) { + const where: edit.InsertAt = switch (insert.kind) { .at => .at, .append => .append, .line_start => .line_start, @@ -7458,7 +7067,7 @@ pub const Pardes = struct { .open_below => .open_below, .open_above => .open_above, }; - return p.enterInsert(pane, where, insert.count); + return edit.enterInsert(p, pane, where, insert.count); }, .select => |select| switch (select.kind) { .mode => { @@ -7494,23 +7103,23 @@ pub const Pardes = struct { return pane.multiSelAction(text, multi.kind, multi.count); }, .select_regex => |split| return p.startSelRegex(pane, split), - .edit => |edit| switch (edit.kind) { - .delete => return p.normalDelete(pane, true), - .delete_noyank => return p.normalDelete(pane, false), - .change => return p.normalChange(pane), - .yank => return p.normalYank(pane), - .replace_with_yank => return p.normalReplaceYank(pane), - .paste_after => return p.pasteText(pane, p.yank orelse return, false, edit.count), - .paste_before => return p.pasteText(pane, p.yank orelse return, true, edit.count), - .switch_case => return p.normalCase(pane, .toggle), - .lowercase => return p.normalCase(pane, .lower), - .uppercase => return p.normalCase(pane, .upper), - .join_lines => return p.normalJoin(pane), - .indent => return p.normalIndent(pane, edit.count, true), - .unindent => return p.normalIndent(pane, edit.count, false), - .comment_toggle => return p.normalToggleComment(pane), - .undo => return p.doUndo(pane), - .redo => return p.doRedo(pane), + .edit => |op| switch (op.kind) { + .delete => return edit.normalDelete(p, pane, true), + .delete_noyank => return edit.normalDelete(p, pane, false), + .change => return edit.normalChange(p, pane), + .yank => return edit.normalYank(p, pane), + .replace_with_yank => return edit.normalReplaceYank(p, pane), + .paste_after => return edit.pasteText(p, pane, p.yank orelse return, false, op.count), + .paste_before => return edit.pasteText(p, pane, p.yank orelse return, true, op.count), + .switch_case => return edit.normalCase(p, pane, .toggle), + .lowercase => return edit.normalCase(p, pane, .lower), + .uppercase => return edit.normalCase(p, pane, .upper), + .join_lines => return edit.normalJoin(p, pane), + .indent => return edit.normalIndent(p, pane, op.count, true), + .unindent => return edit.normalIndent(p, pane, op.count, false), + .comment_toggle => return edit.normalToggleComment(p, pane), + .undo => return edit.doUndo(p, pane), + .redo => return edit.doRedo(p, pane), }, .lsp => |request| return p.lspRequest(p.active, switch (request) { .definition => .definition, @@ -7520,7 +7129,7 @@ pub const Pardes = struct { .references => .references, .format => .format, }, ""), - .adjust_number => |delta| return p.normalAdjustNumber(pane, delta), + .adjust_number => |delta| return edit.normalAdjustNumber(p, pane, delta), .leader => { p.leader_on = true; p.leader_n = 0; @@ -7693,7 +7302,7 @@ pub const Pardes = struct { p.active = rid; p.lookWalk(1); if (p.active != rid) return; // walked out of the list; leave it there - const txt = p.currentSelText(rp) orelse return; + const txt = edit.currentSelText(p, rp) orelse return; // spent, exactly as the look chord spends it (handleKey) rp.vsel.active = false; rp.msel.active = false; @@ -7756,7 +7365,7 @@ pub const Pardes = struct { anchor = found.anchor; } } else { - const lines = try p.paneCursorLines(pane); + const lines = try edit.paneCursorLines(p, pane); // a real file names itself; a terminal or an output buffer has no path const has_path = if (pane.file) |f| f.output == null else false; var idbuf: [16]u8 = undefined; @@ -8012,7 +7621,7 @@ pub const Pardes = struct { } @memcpy(replacement[write_at..], f.content[read_at..]); - p.pushUndo(pane); + edit.pushUndo(p, pane); panes.File.setContent(p, f, replacement); const cursor = modal.positionAt(f.content, mapped_cursor); pane.cur_row = @intCast(cursor.row); @@ -8050,7 +7659,7 @@ pub const Pardes = struct { } if (rows.len == 0) { if (w.kind == .completion and pane.mode == .insert and pane.nsel == 0 and - pane.cur_row == w.row and pane.cur_col == w.col) p.insertTab(pane); + pane.cur_row == w.row and pane.cur_col == w.col) edit.insertTab(p, pane); return; } @@ -8143,7 +7752,7 @@ pub const Pardes = struct { } fn lookSpanIn(p: *Pardes, pane: *Pane, from: LookFrom, delta: i32, budget: *usize) ?Pane.LookSpot { - const lines = p.paneCursorLines(pane) catch return null; + const lines = edit.paneCursorLines(p, pane) catch return null; const nrows: i32 = @intCast(lines.len); if (nrows == 0) return null; const grain: panes.Output.Grain = if (pane.file) |*f| panes.Output.grain(f.output) else .word; @@ -8219,7 +7828,7 @@ pub const Pardes = struct { } fn landLookSpot(p: *Pardes, id: usize, pane: *Pane, spot: Pane.LookSpot) void { - p.pinPaneCursor(pane); // fresh out of tty mode the cursor still tracks the shell + edit.pinPaneCursor(p, pane); // fresh out of tty mode the cursor still tracks the shell pane.vsel = .{ .active = true, .row = spot.row, .col = spot.col1, .explicit = true }; pane.msel.active = false; pane.nsel = 0; @@ -8232,1190 +7841,10 @@ pub const Pardes = struct { pane.ensureCursorVisible(); } - /// Route shared edit operations to a file's content or a terminal overlay. - fn editText(p: *Pardes, pane: *Pane, lo: i32, hi: i32, col: i32) ?panes.EditText { - if (pane.file) |f| return .{ .text = f.content, .row0 = 0 }; - if (pane.image != null or pane.hasPdf()) return null; - return panes.Terminal.editText(p, pane, lo, hi, col); - } - - fn editTextEol(p: *Pardes, pane: *Pane, b: Bounds) ?panes.EditText { - const eb = p.editText(pane, b.lo_row, b.hi_row, -1) orelse return null; - const r: usize = @intCast(@max(0, b.hi_row - eb.row0)); - if (r + 1 < modal.lineCount(eb.text)) return eb; - if (b.hi_col < @as(i32, @intCast(modal.lineSlice(eb.text, r).len))) return eb; - return p.editText(pane, b.lo_row, b.hi_row + 1, -1); - } - - /// install a rewritten editable text (frees the old one) - fn setEditText(p: *Pardes, pane: *Pane, new: []u8) void { - if (pane.file) |*f| return panes.File.setContent(p, f, new); - panes.Terminal.setEditText(p, pane, new); - } - - const InsertAt = enum { at, append, line_start, line_end, open_below, open_above }; - - /// Files insert only inside existing rows and at grapheme boundaries. - /// Terminal overlays can materialize virtual rows/columns, so keep those. - fn clampFileCursor(pane: *Pane) bool { - const file = if (pane.file) |*file| file else return false; - const old_row = pane.cur_row; - const old_col = pane.cur_col; - const last = panes.File.nlines(pane.gpa, file) -| 1; - pane.cur_row = @intCast(@min(last, @as(usize, @intCast(@max(0, pane.cur_row))))); - const line = panes.File.sourceLine(pane, pane.cur_row); - pane.cur_col = @intCast(modal.graphemeStart(line, @min(line.len, @as(usize, @intCast(@max(0, pane.cur_col)))))); - return old_row != pane.cur_row or old_col != pane.cur_col; - } - - fn enterInsert(p: *Pardes, pane: *Pane, where: InsertAt, cnt: usize) void { - if (pane.hasPdf()) return; - p.pinPaneCursor(pane); - _ = clampFileCursor(pane); - // snapshot once per insert session (WITH the pre-insert selection) so - // `u` undoes the whole session and restores what was selected - p.pushUndo(pane); - pane.select = false; - pane.append_at = null; - pane.sticky_col = -1; - const text = p.flatSurface(pane) catch { - pane.mode = .insert; - pane.msel.active = false; - pane.vsel.active = false; - pane.normal.clear(); - return; - }; - const cur = pane.toModalCursor(); - const line = panes.File.textLine(pane, text, cur.row); - const b: ?Bounds = if (pane.vsel.active) vselBounds(pane) else null; - switch (where) { - .at => { - if (b) |bb| { - pane.cur_row = bb.lo_row; - pane.cur_col = bb.lo_col; - pane.vsel = .{ .active = true, .row = bb.hi_row, .col = bb.hi_col, .explicit = false }; - pane.cur_pinned = true; - } - }, - .append => { - const lo_row = if (b) |bb| bb.lo_row else pane.cur_row; - const lo_col = if (b) |bb| bb.lo_col else pane.cur_col; - const hi_row = if (b) |bb| bb.hi_row else pane.cur_row; - const hi_col = if (b) |bb| bb.hi_col else pane.cur_col; - pane.append_at = .{ .row = lo_row, .col = lo_col }; - const gap = modal.nextGrapheme(text, modal.offsetAt(text, .{ .row = @intCast(@max(0, hi_row)), .col = @intCast(@max(0, hi_col)) })); - const gc = modal.positionAt(text, gap); - pane.cur_row = @intCast(gc.row); - pane.cur_col = @intCast(gc.col); - pane.vsel = .{ .active = b != null, .row = lo_row, .col = lo_col, .explicit = false }; - pane.cur_pinned = true; - }, - .line_start => { - pane.fromModalCursor(.{ .row = cur.row, .col = modal.firstNonWs(line) }); - pane.vsel.active = false; - }, - .line_end => { - pane.cur_col = @intCast(line.len); - pane.cur_pinned = true; - pane.vsel.active = false; - }, - .open_below, .open_above => { - pane.vsel.active = false; - const below = where == .open_below; - const abs: i32 = if (b) |bb| (if (below) bb.hi_row else bb.lo_row) else pane.cur_row; - const eb = p.editText(pane, abs, abs, -1) orelse return; - const row: usize = @intCast(@max(0, abs - eb.row0)); - const ind = modal.indentText(modal.lineSlice(eb.text, row)); - const arena = p.scratch.allocator(); - const block_len = std.math.mul(usize, cnt, ind.len + 1) catch return; - const block = arena.alloc(u8, block_len) catch return; - var block_at: usize = 0; - for (0..cnt) |_| { - if (below) { - block[block_at] = '\n'; - block_at += 1; - } - @memcpy(block[block_at..][0..ind.len], ind); - block_at += ind.len; - if (!below) { - block[block_at] = '\n'; - block_at += 1; - } - } - const at: modal.Cursor = if (below) - .{ .row = row, .col = modal.lineSlice(eb.text, row).len } - else - .{ .row = row, .col = 0 }; - const new = modal.insertAt(p.gpa, eb.text, at, block) catch return; - p.setEditText(pane, new); - pane.cur_row = @as(i32, @intCast(if (below) row + 1 else row)) + eb.row0; - pane.cur_col = @intCast(ind.len); - pane.cur_pinned = true; - if (cnt > 1 and !p.multi_on) opened: { - const t2 = p.flatSurface(pane) catch break :opened; - var rs: [Pane.max_selections]modal.Selection = undefined; - const m = @min(cnt, Pane.max_selections); - for (0..m) |k| { - const o = modal.offsetAt(t2, .{ .row = @intCast(@max(0, pane.cur_row) + @as(i32, @intCast(k))), .col = ind.len }); - rs[k] = .{ .anchor = o, .head = o }; - } - pane.setRanges(t2, rs[0..m], &.{}, 0, false); - } - }, - } - pane.mode = .insert; - pane.msel.active = false; - pane.normal.clear(); - pane.ensureCursorVisible(); - } - - fn handleInsert(p: *Pardes, pane: *Pane, key: Key) void { - if (pane.nsel == 0) return p.insertKey(pane, key); - p.replaySels(pane, .{ .insert = key }); - } - - fn insertKey(p: *Pardes, pane: *Pane, key: Key) void { - // helix aliases: normalize to the base key and re-dispatch - if (hit(key, config.insert_backspace_alias)) return p.insertKey(pane, .{ .cp = Key.backspace }); - if (hit(key, config.insert_enter_alias)) return p.insertKey(pane, .{ .cp = Key.enter }); - if (hit(key, config.insert_delete_alias)) return p.insertKey(pane, .{ .cp = Key.delete }); - pane.vsel.active = false; - if (!pane.cur_pinned) p.pinPaneCursor(pane); - _ = clampFileCursor(pane); - switch (key.cp) { - Key.left, Key.right, Key.up, Key.down => { - const lines = p.paneCursorLines(pane) catch return; - const cur0 = pane.toModalCursor(); - const nc = switch (key.cp) { - Key.left => modal.charLeft(lines, cur0), - Key.right => modal.charRight(lines, cur0), - Key.up => Pane.insertVerticalCursor(lines, cur0, false), - Key.down => Pane.insertVerticalCursor(lines, cur0, true), - else => cur0, - }; - pane.fromModalCursor(nc); - pane.ensureCursorVisible(); - return; - }, - Key.page_up, Key.page_down => { - // helix binds insert pageup/pagedown to the same view - // scroll + cursor snap as normal mode - const flat = p.flatSurface(pane) catch return; - const range = pane.primaryRange(flat, 0); - const step: i32 = @intCast(@max(1, pane.rows)); - pane.scrollViewMove(flat, range, if (key.cp == Key.page_down) step else -step); - return; - }, - Key.home => { - pane.cur_col = 0; - pane.cur_pinned = true; - pane.ensureCursorVisible(); - return; - }, - Key.end => { - // helix insert End: past the last char (goto_line_end_newline) - const text = p.flatSurface(pane) catch return; - const cur0 = pane.toModalCursor(); - pane.cur_col = @intCast(panes.File.textLine(pane, text, cur0.row).len); - pane.cur_pinned = true; - pane.ensureCursorVisible(); - return; - }, - else => {}, - } - const eb = p.editText(pane, pane.cur_row, pane.cur_row, pane.cur_col) orelse return; - const text = eb.text; - const c = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - eb.row0)), .col = @intCast(@max(0, pane.cur_col)) }; - if (key.text.len > 0 and !key.ctrl and !key.alt) { - const new = modal.insertAt(p.gpa, text, c, key.text) catch return; - p.setEditText(pane, new); - for (key.text) |ch| { - if (ch == '\n') { - pane.cur_row += 1; - pane.cur_col = 0; - } else pane.cur_col += 1; - } - pane.cur_pinned = true; - pane.ensureCursorVisible(); - return; - } - // helix insert-mode kills (word/line; deleteSpan is exclusive) - if (hit(key, config.delete_word_backward)) { - const e2 = if (c.row == 0 and c.col == 0) - (p.editText(pane, pane.cur_row - 1, pane.cur_row, 0) orelse return) - else - eb; - const c2 = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - e2.row0)), .col = c.col }; - const g = modal.offsetAt(e2.text, c2); - if (g == 0) return; - const wr = modal.moveWord(e2.text, .{ .anchor = g, .head = g }, 1, .prev_word_start); - const from = @min(wr.anchor, wr.head); - const fc = modal.positionAt(e2.text, from); - const new = modal.deleteSpan(p.gpa, e2.text, fc, c2) catch return; - p.setEditText(pane, new); - pane.cur_row = @as(i32, @intCast(fc.row)) + e2.row0; - pane.cur_col = @intCast(fc.col); - pane.cur_pinned = true; - pane.ensureCursorVisible(); - return; - } - if (hit(key, config.delete_word_forward)) { - const e2 = if (c.col >= modal.lineSlice(text, c.row).len and c.row + 1 >= modal.lineCount(text)) - (p.editText(pane, pane.cur_row, pane.cur_row + 1, pane.cur_col) orelse return) - else - eb; - const c2 = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - e2.row0)), .col = c.col }; - const g = modal.offsetAt(e2.text, c2); - const wr = modal.moveWord(e2.text, .{ .anchor = g, .head = g }, 1, .next_word_end); - const to = @max(wr.anchor, wr.head); - if (to <= g) return; - const new = modal.deleteSpan(p.gpa, e2.text, c2, modal.positionAt(e2.text, to)) catch return; - p.setEditText(pane, new); - pane.cur_pinned = true; - return; - } - if (hit(key, config.kill_to_line_start)) { - const new = modal.deleteSpan(p.gpa, text, .{ .row = c.row, .col = 0 }, c) catch return; - p.setEditText(pane, new); - pane.cur_col = 0; - pane.cur_pinned = true; - pane.ensureCursorVisible(); - return; - } - if (hit(key, config.kill_to_line_end)) { - const llen = modal.lineSlice(text, c.row).len; - const new = modal.deleteSpan(p.gpa, text, c, .{ .row = c.row, .col = llen }) catch return; - p.setEditText(pane, new); - pane.cur_pinned = true; - return; - } - switch (key.cp) { - Key.enter => { - const line = modal.lineSlice(text, c.row); - const indent = modal.newlineIndentWidth(line, c.col); - const arena = p.scratch.allocator(); - const block = arena.alloc(u8, 1 + indent) catch return; - block[0] = '\n'; - @memset(block[1..], ' '); - const new = modal.insertAt(p.gpa, text, c, block) catch return; - p.setEditText(pane, new); - pane.cur_row += 1; - pane.cur_col = @intCast(indent); - pane.cur_pinned = true; - pane.ensureCursorVisible(); - }, - Key.backspace => { - if (pane.cur_col > 0) { - const line = modal.lineSlice(text, c.row); - const prev = modal.prevGrapheme(line, c.col); - const new = modal.deleteChar(p.gpa, text, .{ .row = c.row, .col = prev }) catch return; - p.setEditText(pane, new); - pane.cur_col = @intCast(prev); - pane.cur_pinned = true; - pane.ensureCursorVisible(); - return; - } - // at col 0: eat the newline above (join up) — a terminal grows - // its buffer over the row it is joining with - if (pane.cur_row <= 0) return; - const e2 = p.editText(pane, pane.cur_row - 1, pane.cur_row, 0) orelse return; - const row: usize = @intCast(@max(0, pane.cur_row - e2.row0)); - if (row == 0) return; - const prevlen = modal.lineSlice(e2.text, row - 1).len; - const new = modal.deleteSpan(p.gpa, e2.text, .{ .row = row - 1, .col = prevlen }, .{ .row = row, .col = 0 }) catch return; - p.setEditText(pane, new); - pane.cur_row -= 1; - pane.cur_col = @intCast(prevlen); - pane.cur_pinned = true; - pane.ensureCursorVisible(); - }, - Key.delete => { - // delete forward; at line end eat the newline (join), which on - // a terminal means the buffer swallowing the row below - const line = modal.lineSlice(text, c.row); - if (c.col < line.len) { - const new = modal.deleteChar(p.gpa, text, c) catch return; - p.setEditText(pane, new); - } else { - const e2 = p.editText(pane, pane.cur_row, pane.cur_row + 1, pane.cur_col) orelse return; - const row: usize = @intCast(@max(0, pane.cur_row - e2.row0)); - if (row + 1 >= modal.lineCount(e2.text)) return; - const new = modal.deleteSpan(p.gpa, e2.text, .{ .row = row, .col = c.col }, .{ .row = row + 1, .col = 0 }) catch return; - p.setEditText(pane, new); - } - pane.cur_pinned = true; - }, - Key.tab => { - const ln = modal.lineSlice(text, c.row); - if (!p.multi_on and c.col > 0 and c.col <= ln.len and ln[c.col - 1] == '.') dot: { - const f = pane.file orelse break :dot; - if (f.output != null or !lsp.speaks(f.path)) break :dot; - const seq = p.lsp_seq; - p.lspRequest(p.active, .completion, ""); - if (p.lsp_seq != seq) return; - } - p.insertTab(pane); - }, - else => {}, - } - } - - fn insertTab(p: *Pardes, pane: *Pane) void { - const eb = p.editText(pane, pane.cur_row, pane.cur_row, pane.cur_col) orelse return; - const c: modal.Cursor = .{ - .row = @intCast(@max(0, pane.cur_row - eb.row0)), - .col = @intCast(@max(0, pane.cur_col)), - }; - const pad = modal.INDENT_W - (c.col % modal.INDENT_W); - const new = modal.insertAt(p.gpa, eb.text, c, " "[0..pad]) catch return; - p.setEditText(pane, new); - pane.cur_col += @intCast(pad); - pane.cur_pinned = true; - pane.ensureCursorVisible(); - } - - const Bounds = struct { lo_row: i32, lo_col: i32, hi_row: i32, hi_col: i32 }; - - /// a range's two cells, normalized to document order - pub fn cellBounds(s: Pane.SelRange) Bounds { - if (s.row < s.arow or (s.row == s.arow and s.col < s.acol)) - return .{ .lo_row = s.row, .lo_col = s.col, .hi_row = s.arow, .hi_col = s.acol }; - return .{ .lo_row = s.arow, .lo_col = s.acol, .hi_row = s.row, .hi_col = s.col }; - } - - /// the char selection [anchor, cursor] normalized to document order - fn vselBounds(pane: *Pane) Bounds { - return cellBounds(.{ .row = pane.cur_row, .col = pane.cur_col, .arow = pane.vsel.row, .acol = pane.vsel.col }); - } - fn vselText(p: *Pardes, pane: *Pane) []const u8 { - const arena = p.scratch.allocator(); - const b = vselBounds(pane); - const text = p.flatSurface(pane) catch return ""; - return modal.rangeText(arena, text, .{ .row = @intCast(@max(0, b.lo_row)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row)), .col = @intCast(@max(0, b.hi_col)) }) catch ""; - } - - /// join surface rows [r0, r1] (absolute) with '\n'; scratch-owned - fn yankRows(p: *Pardes, pane: *Pane, r0: i32, r1: i32) []const u8 { - const arena = p.scratch.allocator(); - const lines = p.paneCursorLines(pane) catch return ""; - const rows_count: usize = @intCast(@max(0, r1 - r0 + 1)); - var total: usize = rows_count -| 1; - var i = r0; - while (i <= r1) : (i += 1) { - if (i >= 0 and @as(usize, @intCast(i)) < lines.len) - total += lines[@intCast(i)].len; - } - const out = arena.alloc(u8, total) catch return ""; - var at: usize = 0; - i = r0; - while (i <= r1) : (i += 1) { - if (i > r0) { - out[at] = '\n'; - at += 1; - } - if (i >= 0 and @as(usize, @intCast(i)) < lines.len) { - const line = lines[@intCast(i)]; - @memcpy(out[at..][0..line.len], line); - at += line.len; - } - } - return out; - } - // ---- edit operations (d / c / y / p) ---- - fn normalDelete(p: *Pardes, pane: *Pane, yank: bool) void { - pane.select = false; // helix delete exits select mode - pane.sticky_col = -1; - if (pane.vsel.active) { - const b = vselBounds(pane); - if (yank) p.setYank(p.vselText(pane)); // read the range before the move - p.pushUndo(pane); - const eb = p.editTextEol(pane, b) orelse { - pane.vsel.active = false; - return; - }; - const d = modal.deleteRange(p.gpa, eb.text, .{ .row = @intCast(@max(0, b.lo_row - eb.row0)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row - eb.row0)), .col = @intCast(@max(0, b.hi_col)) }) catch { - pane.vsel.active = false; - return; - }; - p.setEditText(pane, d.content); - p.gpa.free(d.deleted); - pane.cur_row = b.lo_row; - pane.cur_col = b.lo_col; - pane.clampCursor(d.content, eb.row0); - return; - } - if (pane.msel.active) { - const r0 = @min(pane.msel.r0, pane.msel.r1); - const r1 = @max(pane.msel.r0, pane.msel.r1); - p.pushUndo(pane); - // linewise: a line's newline lives on the row below it - const eb = p.editText(pane, r0, r1 + 1, -1) orelse { - pane.msel.active = false; - return; - }; - const d = modal.deleteLines(p.gpa, eb.text, @intCast(@max(0, r0 - eb.row0)), @intCast(@max(0, r1 - eb.row0))) catch { - pane.msel.active = false; - return; - }; - p.setEditText(pane, d.content); - if (yank) p.setYank(d.deleted); - p.gpa.free(d.deleted); - const n = modal.lineCount(d.content); - pane.cur_row = @as(i32, @intCast(@min(@as(usize, @intCast(@max(0, r0 - eb.row0))), if (n == 0) 0 else n - 1))) + eb.row0; - pane.cur_col = 0; - pane.msel.active = false; - pane.cur_pinned = true; - pane.ensureCursorVisible(); - } else { - p.pushUndo(pane); - const eb = p.editTextEol(pane, .{ .lo_row = pane.cur_row, .lo_col = pane.cur_col, .hi_row = pane.cur_row, .hi_col = pane.cur_col }) orelse return; - const c = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - eb.row0)), .col = @intCast(@max(0, pane.cur_col)) }; - if (yank) { - const arena = p.scratch.allocator(); - p.setYank(modal.rangeText(arena, eb.text, c, c) catch return); - } - // deleteRange (not deleteChar): the cursor may sit ON a '\n' - // cell, where a delete joins the lines (helix 1-wide selection) - const d = modal.deleteRange(p.gpa, eb.text, c, c) catch return; - p.setEditText(pane, d.content); - p.gpa.free(d.deleted); - const llen = modal.lineSlice(d.content, c.row).len; - pane.cur_col = @min(pane.cur_col, @as(i32, @intCast(llen))); - } - } - - fn normalYank(p: *Pardes, pane: *Pane) void { - pane.select = false; - if (pane.rawPointerText(pane.sel[sel_slot])) |text| { - p.setYank(text); - return; - } - if (pane.vsel.active) { - p.setYank(p.vselText(pane)); - return; - } - if (pane.msel.active) { - p.setYank(p.yankRows(pane, @min(pane.msel.r0, pane.msel.r1), @max(pane.msel.r0, pane.msel.r1))); - } else { - const save = pane.vsel; - pane.vsel = .{ .active = true, .row = pane.cur_row, .col = pane.cur_col, .explicit = false }; - p.setYank(p.vselText(pane)); - pane.vsel = save; - } - } - - fn pasteText(p: *Pardes, pane: *Pane, y0: []const u8, before: bool, count: usize) void { - if (y0.len == 0) return; - p.pushUndo(pane); - pane.select = false; - pane.sticky_col = -1; - const arena = p.scratch.allocator(); - var y: []const u8 = y0; - if (count > 1) { - const total = std.math.mul(usize, count, y0.len) catch return; - const buf = arena.alloc(u8, total) catch return; - for (0..count) |i| @memcpy(buf[i * y0.len ..][0..y0.len], y0); - y = buf; - } - const b: Bounds = if (pane.vsel.active) - vselBounds(pane) - else - .{ .lo_row = pane.cur_row, .lo_col = pane.cur_col, .hi_row = pane.cur_row, .hi_col = pane.cur_col }; - const eb = p.editText(pane, b.lo_row, b.hi_row, -1) orelse return; - const row0 = eb.row0; - if (y[y.len - 1] == '\n') { - const block_text = y[0 .. y.len - 1]; - const n = @max(1, modal.lineCount(block_text)); - var out: []u8 = undefined; - if (before) { - const row: usize = @intCast(@max(0, b.lo_row - row0)); - const block = std.fmt.allocPrint(arena, "{s}\n", .{block_text}) catch return; - out = modal.insertAt(p.gpa, eb.text, .{ .row = row, .col = 0 }, block) catch return; - pane.cur_row = @as(i32, @intCast(row + n - 1)) + row0; - } else { - const row: usize = @intCast(@max(0, b.hi_row - row0)); - out = modal.pasteLineBelow(p.gpa, eb.text, row, block_text) catch return; - pane.cur_row = @as(i32, @intCast(row + n)) + row0; - } - p.setEditText(pane, out); - pane.vsel = .{ .active = true, .row = pane.cur_row - @as(i32, @intCast(n - 1)), .col = 0, .explicit = false }; - // cursor ON the last pasted line's '\n' (helix) - const llen = modal.lineSlice(out, @intCast(@max(0, pane.cur_row - row0))).len; - pane.cur_col = @intCast(llen); - } else { - const at: modal.Cursor = if (before) - .{ .row = @intCast(@max(0, b.lo_row - row0)), .col = @intCast(@max(0, b.lo_col)) } - else blk: { - const hrow: usize = @intCast(@max(0, b.hi_row - row0)); - const hi_col: usize = @intCast(@max(0, b.hi_col)); - const high_line = modal.lineSlice(eb.text, hrow); - if (hi_col >= high_line.len) break :blk .{ .row = hrow + 1, .col = 0 }; - const gcol = modal.nextGrapheme(high_line, hi_col); - break :blk .{ .row = hrow, .col = gcol }; - }; - const out = modal.insertAt(p.gpa, eb.text, at, y) catch return; - p.setEditText(pane, out); - pane.vsel = .{ .active = modal.nextGrapheme(y, 0) < y.len, .row = @as(i32, @intCast(at.row)) + row0, .col = @intCast(at.col), .explicit = false }; - const end = modal.advanceBy(at, y); - if (end.col > 0) { - pane.cur_row = @as(i32, @intCast(end.row)) + row0; - pane.cur_col = @intCast(modal.prevGrapheme(modal.lineSlice(out, end.row), end.col)); - } else { - pane.cur_row = @as(i32, @intCast(end.row -| 1)) + row0; - pane.cur_col = @intCast(modal.lineSlice(out, end.row -| 1).len); - } - } - pane.msel.active = false; - pane.cur_pinned = true; - pane.ensureCursorVisible(); - } - - fn normalChange(p: *Pardes, pane: *Pane) void { - pane.select = false; - pane.sticky_col = -1; - if (pane.vsel.active) { - const b = vselBounds(pane); - const eb = p.editTextEol(pane, b) orelse return; - const hi_len = modal.lineSlice(eb.text, @intCast(@max(0, b.hi_row - eb.row0))).len; - if (b.lo_col == 0 and @as(usize, @intCast(@max(0, b.hi_col))) >= hi_len) { - p.setYank(p.vselText(pane)); - p.pushUndo(pane); - const d = modal.deleteLines(p.gpa, eb.text, @intCast(@max(0, b.lo_row - eb.row0)), @intCast(@max(0, b.hi_row - eb.row0))) catch return; - p.setEditText(pane, d.content); - p.gpa.free(d.deleted); - const n = modal.lineCount(d.content); - const row: usize = @min(@as(usize, @intCast(@max(0, b.lo_row - eb.row0))), if (n == 0) 0 else n - 1); - const ind = modal.indentText(modal.lineSlice(d.content, row)); - const arena = p.scratch.allocator(); - const block = std.fmt.allocPrint(arena, "{s}\n", .{ind}) catch return; - const new = modal.insertAt(p.gpa, d.content, .{ .row = row, .col = 0 }, block) catch return; - p.setEditText(pane, new); - pane.cur_row = @as(i32, @intCast(row)) + eb.row0; - pane.cur_col = @intCast(ind.len); - pane.vsel.active = false; - pane.msel.active = false; - pane.cur_pinned = true; - pane.mode = .insert; - pane.normal.clear(); - pane.ensureCursorVisible(); - return; - } - p.setYank(p.vselText(pane)); - p.pushUndo(pane); - const d = modal.deleteRange(p.gpa, eb.text, .{ .row = @intCast(@max(0, b.lo_row - eb.row0)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row - eb.row0)), .col = @intCast(@max(0, b.hi_col)) }) catch { - pane.vsel.active = false; - return; - }; - p.setEditText(pane, d.content); - p.gpa.free(d.deleted); - pane.cur_row = b.lo_row; - pane.cur_col = b.lo_col; - pane.clampCursor(d.content, eb.row0); - pane.mode = .insert; - pane.normal.clear(); - return; - } - if (pane.msel.active) { - const r0 = @min(pane.msel.r0, pane.msel.r1); - const r1 = @max(pane.msel.r0, pane.msel.r1); - p.pushUndo(pane); - const eb = p.editText(pane, r0, r1 + 1, -1) orelse return; - const arena = p.scratch.allocator(); - const lo: usize = @intCast(@max(0, r0 - eb.row0)); - const hi: usize = @intCast(@max(0, r1 - eb.row0)); - var total: usize = hi - lo; - for (lo..hi + 1) |r| total += modal.lineSlice(eb.text, r).len; - const buf = arena.alloc(u8, total) catch return; - var at: usize = 0; - for (lo..hi + 1) |r| { - if (r > lo) { - buf[at] = '\n'; - at += 1; - } - const line = modal.lineSlice(eb.text, r); - @memcpy(buf[at..][0..line.len], line); - at += line.len; - } - p.setYank(buf); - var body: []u8 = eb.text; - if (hi > lo) { - const d = modal.deleteLines(p.gpa, body, lo + 1, hi) catch { - pane.msel.active = false; - return; - }; - p.setEditText(pane, d.content); - p.gpa.free(d.deleted); - body = d.content; - } - const cl = modal.clearLine(p.gpa, body, lo) catch { - pane.msel.active = false; - return; - }; - p.setEditText(pane, cl); - pane.cur_row = r0; - pane.cur_col = 0; - pane.msel.active = false; - pane.cur_pinned = true; - pane.mode = .insert; - pane.normal.clear(); - pane.ensureCursorVisible(); - } else { - p.pushUndo(pane); - const eb = p.editTextEol(pane, .{ .lo_row = pane.cur_row, .lo_col = pane.cur_col, .hi_row = pane.cur_row, .hi_col = pane.cur_col }) orelse return; - const c = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - eb.row0)), .col = @intCast(@max(0, pane.cur_col)) }; - const arena = p.scratch.allocator(); - p.setYank(modal.rangeText(arena, eb.text, c, c) catch return); - const d = modal.deleteRange(p.gpa, eb.text, c, c) catch return; - p.setEditText(pane, d.content); - p.gpa.free(d.deleted); - const llen = modal.lineSlice(d.content, c.row).len; - pane.cur_col = @min(pane.cur_col, @as(i32, @intCast(llen))); - pane.mode = .insert; - pane.normal.clear(); - } - } - - fn selRange(pane: *Pane, content: []const u8, row0: i32) modal.Range { - if (pane.vsel.active) { - const b = vselBounds(pane); - return .{ - .a = .{ .row = @intCast(@max(0, b.lo_row - row0)), .col = @intCast(@max(0, b.lo_col)) }, - .b = .{ .row = @intCast(@max(0, b.hi_row - row0)), .col = @intCast(@max(0, b.hi_col)) }, - }; - } - if (pane.msel.active) { - const r0: usize = @intCast(@max(0, @min(pane.msel.r0, pane.msel.r1) - row0)); - const r1: usize = @intCast(@max(0, @max(pane.msel.r0, pane.msel.r1) - row0)); - const llen = modal.lineSlice(content, r1).len; - return .{ .a = .{ .row = r0, .col = 0 }, .b = .{ .row = r1, .col = if (llen == 0) 0 else modal.prevGrapheme(modal.lineSlice(content, r1), llen) } }; - } - const c = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row - row0)), .col = @intCast(@max(0, pane.cur_col)) }; - return .{ .a = c, .b = c }; - } - - /// the rows an op's selection spans, absolute (what editText must cover) - fn selRows(pane: *Pane) Bounds { - if (pane.vsel.active) return vselBounds(pane); - if (pane.msel.active) return .{ - .lo_row = @min(pane.msel.r0, pane.msel.r1), - .lo_col = 0, - .hi_row = @max(pane.msel.r0, pane.msel.r1), - .hi_col = 0, - }; - return .{ .lo_row = pane.cur_row, .lo_col = pane.cur_col, .hi_row = pane.cur_row, .hi_col = pane.cur_col }; - } - - /// `r`: overwrite the selection (or the cursor char) with ch — - /// newlines included (helix), so `xrz` joins the selected lines - fn normalReplaceChar(p: *Pardes, pane: *Pane, ch: u21) void { - pane.select = false; - const eb = p.editTextEol(pane, selRows(pane)) orelse return; - const before = pane.primaryRange(eb.text, eb.row0); - const lo = @min(before.anchor, before.head); - const hi = @max(before.anchor, before.head); - var graphemes: usize = 0; - var at = lo; - while (at < hi) : (graphemes += 1) at = modal.nextGrapheme(eb.text, at); - var encoded: [4]u8 = undefined; - const encoded_len = std.unicode.utf8Encode(ch, &encoded) catch return; - const r = selRange(pane, eb.text, eb.row0); - p.pushUndo(pane); - const new = modal.replaceChars(p.gpa, eb.text, r.a, r.b, ch) catch return; - p.setEditText(pane, new); - const end = lo + graphemes * encoded_len; - const mapped: modal.Selection = if (before.head < before.anchor) - .{ .anchor = end, .head = lo } - else - .{ .anchor = lo, .head = end }; - pane.setRange(new, eb.row0, mapped, pane.vsel.explicit); - } - - /// `R`: replace the selection (or the cursor char) with the DEFAULT - /// register. `SPC R` is the same verb over the system clipboard. - fn normalReplaceYank(p: *Pardes, pane: *Pane) void { - p.replaceWithText(pane, p.yank orelse return); - } - - /// The pasted text becomes the selection, head on its last char (which for - /// a trailing-newline `y` is the '\n' cell of the last full line). - fn replaceWithText(p: *Pardes, pane: *Pane, y: []const u8) void { - if (y.len == 0) return; - pane.select = false; - const eb = p.editTextEol(pane, selRows(pane)) orelse return; - const r = selRange(pane, eb.text, eb.row0); - p.pushUndo(pane); - const new = modal.replaceRange(p.gpa, eb.text, r.a, r.b, y) catch return; - p.setEditText(pane, new); - pane.msel.active = false; - pane.vsel = .{ .active = modal.nextGrapheme(y, 0) < y.len, .row = @as(i32, @intCast(r.a.row)) + eb.row0, .col = @intCast(r.a.col), .explicit = false }; - const end = modal.advanceBy(r.a, y); - if (end.col > 0) { - pane.cur_row = @as(i32, @intCast(end.row)) + eb.row0; - pane.cur_col = @intCast(modal.prevGrapheme(modal.lineSlice(new, end.row), end.col)); - } else { - // the yank ended in '\n': the cursor lands ON that newline - pane.cur_row = @as(i32, @intCast(end.row -| 1)) + eb.row0; - pane.cur_col = @intCast(modal.lineSlice(new, end.row -| 1).len); - } - pane.cur_pinned = true; - pane.sticky_col = -1; - pane.ensureCursorVisible(); - } - - /// `~` / `` ` `` / ``Alt-` ``: case-map the selection (or the cursor char), - /// keeping the selection (helix) - fn normalCase(p: *Pardes, pane: *Pane, op: modal.CaseOp) void { - pane.select = false; - const eb = p.editTextEol(pane, selRows(pane)) orelse return; - const r = selRange(pane, eb.text, eb.row0); - p.pushUndo(pane); - const new = modal.changeCase(p.gpa, eb.text, r.a, r.b, op) catch return; - p.setEditText(pane, new); - } - - /// a change to apply to file content: delete [from, to), insert `ins` at - /// `from`. Ascending and disjoint. - const TextChange = struct { from: usize, to: usize, ins: []const u8 }; - - fn mapThroughChanges(chs: []const TextChange, pos: usize) usize { - var delta: i64 = 0; - for (chs) |ch| { - if (pos < ch.from) break; - if (ch.from == ch.to) { - delta += @intCast(ch.ins.len); - continue; - } - if (pos == ch.from) break; - if (pos < ch.to) return @intCast(@as(i64, @intCast(ch.from)) + delta); - delta += @as(i64, @intCast(ch.ins.len)) - @as(i64, @intCast(ch.to - ch.from)); - } - return @intCast(@as(i64, @intCast(pos)) + delta); - } - - /// apply a change list. gpa-owned result. - fn applyChanges(p: *Pardes, text: []const u8, chs: []const TextChange) ![]u8 { - var total = text.len; - for (chs) |ch| total = total - (ch.to - ch.from) + ch.ins.len; - const out = try p.gpa.alloc(u8, total); - errdefer p.gpa.free(out); - var copied: usize = 0; - var at: usize = 0; - for (chs) |ch| { - const unchanged = text[copied..ch.from]; - @memcpy(out[at..][0..unchanged.len], unchanged); - at += unchanged.len; - @memcpy(out[at..][0..ch.ins.len], ch.ins); - at += ch.ins.len; - copied = ch.to; - } - @memcpy(out[at..], text[copied..]); - return out; - } - - fn normalJoin(p: *Pardes, pane: *Pane) void { - // a join always eats the newline of its last line, so the buffer has - // to reach one row PAST the selection - const sr = selRows(pane); - const eb = p.editText(pane, sr.lo_row, sr.hi_row + 1, -1) orelse return; - const text = eb.text; - const range = pane.primaryRange(text, eb.row0); - const span = Pane.rangeLineSpan(text, range); - const nlines = modal.cursorLineCount(text); - var end = span.end; - if (span.start == end) end = @min(end + 1, nlines - 1); - if (end <= span.start) return; - const arena = p.scratch.allocator(); - const chs = arena.alloc(TextChange, end - span.start) catch return; - var chs_len: usize = 0; - var l = span.start; - while (l < end) : (l += 1) { - const from = modal.lineEndOffset(text, l); - var to = if (l + 1 >= nlines) text.len else modal.lineStartOffset(text, l + 1); - while (to < text.len and (text[to] == ' ' or text[to] == '\t')) to += 1; - const sep: []const u8 = if (to == modal.lineEndOffset(text, @min(l + 1, nlines - 1))) "" else " "; - chs[chs_len] = .{ .from = from, .to = to, .ins = sep }; - chs_len += 1; - } - if (chs_len == 0) return; - p.pushUndo(pane); - const cur_off = modal.offsetAt(text, .{ .row = @intCast(@max(0, pane.cur_row - eb.row0)), .col = @intCast(@max(0, pane.cur_col)) }); - const anc_off = if (pane.vsel.active) - modal.offsetAt(text, .{ .row = @intCast(@max(0, pane.vsel.row - eb.row0)), .col = @intCast(@max(0, pane.vsel.col)) }) - else - cur_off; - const new = p.applyChanges(text, chs[0..chs_len]) catch return; - const nc = mapThroughChanges(chs[0..chs_len], cur_off); - const na = mapThroughChanges(chs[0..chs_len], anc_off); - p.setEditText(pane, new); - const cc = modal.positionAt(new, nc); - const ac = modal.positionAt(new, na); - pane.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; - pane.cur_col = @intCast(cc.col); - if (pane.vsel.active) { - pane.vsel.row = @as(i32, @intCast(ac.row)) + eb.row0; - pane.vsel.col = @intCast(ac.col); - } - pane.msel.active = false; - pane.cur_pinned = true; - pane.sticky_col = -1; - pane.ensureCursorVisible(); - } - - fn normalIndent(p: *Pardes, pane: *Pane, cnt: usize, add: bool) void { - pane.select = false; - const sr = selRows(pane); - const eb = p.editText(pane, sr.lo_row, sr.hi_row, -1) orelse return; - const text = eb.text; - const range = pane.primaryRange(text, eb.row0); - const span = Pane.rangeLineSpan(text, range); - const arena = p.scratch.allocator(); - // one run of spaces, sliced per line: `>` never inserts more than this - const pad = arena.alloc(u8, modal.INDENT_W * cnt) catch return; - @memset(pad, ' '); - const chs = arena.alloc(TextChange, span.end - span.start + 1) catch return; - var chs_len: usize = 0; - var l = span.start; - while (l <= span.end) : (l += 1) { - const ls = modal.lineStartOffset(text, l); - const le = modal.lineEndOffset(text, l); - const line = text[ls..le]; - const nw = modal.firstNonWs(line); - if (nw == line.len) continue; // blank lines stay blank (helix) - if (add) { - const ins = modal.INDENT_W * cnt - (nw % modal.INDENT_W); - chs[chs_len] = .{ .from = ls, .to = ls, .ins = pad[0..ins] }; - chs_len += 1; - } else { - const want = modal.INDENT_W * cnt; - var w: usize = 0; - var pos: usize = 0; - while (pos < line.len) { - if (line[pos] == ' ') { - w += 1; - } else if (line[pos] == '\t') { - w = (w / modal.INDENT_W + 1) * modal.INDENT_W; - } else break; - pos += 1; - if (w >= want) break; - } - if (pos > 0) { - chs[chs_len] = .{ .from = ls, .to = ls + pos, .ins = "" }; - chs_len += 1; - } - } - } - if (chs_len == 0) return; - p.pushUndo(pane); - const cur_off = modal.offsetAt(text, .{ .row = @intCast(@max(0, pane.cur_row - eb.row0)), .col = @intCast(@max(0, pane.cur_col)) }); - const anc_off = if (pane.vsel.active) - modal.offsetAt(text, .{ .row = @intCast(@max(0, pane.vsel.row - eb.row0)), .col = @intCast(@max(0, pane.vsel.col)) }) - else - cur_off; - const new = p.applyChanges(text, chs[0..chs_len]) catch return; - const nc = mapThroughChanges(chs[0..chs_len], cur_off); - const na = mapThroughChanges(chs[0..chs_len], anc_off); - p.setEditText(pane, new); - const cc = modal.positionAt(new, nc); - const ac = modal.positionAt(new, na); - pane.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; - pane.cur_col = @intCast(cc.col); - if (pane.vsel.active) { - pane.vsel.row = @as(i32, @intCast(ac.row)) + eb.row0; - pane.vsel.col = @intCast(ac.col); - } - pane.msel.active = false; - pane.cur_pinned = true; - pane.sticky_col = -1; - pane.ensureCursorVisible(); - } - - fn normalToggleComment(p: *Pardes, pane: *Pane) void { - const expl = (pane.vsel.active and pane.vsel.explicit) or pane.msel.active; - pane.select = false; // helix exit_select_mode - // the buffer must cover every cursor's rows, not just the primary's - var b = selRows(pane); - for (pane.sels[0..pane.nsel]) |s| { - b.lo_row = @min(b.lo_row, @min(s.row, s.arow)); - b.hi_row = @max(b.hi_row, @max(s.row, s.arow)); - } - const surface = p.flatSurface(pane) catch return; - const last_row = @as(i32, @intCast(panes.File.textLineCount(pane, surface))) - 1; - const eb = p.editText(pane, b.lo_row, @min(b.hi_row + 1, last_row), -1) orelse return; - const text = eb.text; - var rs: [Pane.max_selections]modal.Selection = undefined; - const got = pane.ranges(text, eb.row0, &rs); - const nlines = modal.cursorLineCount(text); - const arena = p.scratch.allocator(); - // the lines the ranges cover, each ONE ONCE and in order (helix's - // min_next_line: two cursors on one line comment it once) - const lines = arena.alloc(usize, nlines) catch return; - var lines_len: usize = 0; - var next: usize = 0; - for (rs[0..got.n]) |r| { - const span = Pane.rangeLineSpan(text, r); - var l = @max(span.start, next); - const end = @min(span.end + 1, nlines); - while (l < end) : (l += 1) { - lines[lines_len] = l; - lines_len += 1; - } - next = @max(next, end); - } - const ext = if (pane.file) |f| std.fs.path.extension(f.path) else ""; - var token: []const u8 = config.comment_token_default; - lang: for (config.comment_tokens) |row| { - for (row.exts) |e| if (std.ascii.eqlIgnoreCase(ext, e)) { - token = row.token; - break :lang; - }; - } - var commented = true; - var indent: usize = std.math.maxInt(usize); - var margin: usize = 1; - var live: usize = 0; - for (lines[0..lines_len]) |l| { - const line = text[modal.lineStartOffset(text, l)..modal.lineEndOffset(text, l)]; - const nw = modal.firstNonWs(line); - if (nw == line.len) continue; - indent = @min(indent, nw); - if (!std.mem.startsWith(u8, line[nw..], token)) commented = false; - if (nw + token.len >= line.len or line[nw + token.len] != ' ') margin = 0; - live += 1; - } - if (live == 0) return; // nothing but blank lines - const ins = std.fmt.allocPrint(arena, "{s} ", .{token}) catch return; - const chs = arena.alloc(TextChange, lines_len) catch return; - var chs_len: usize = 0; - for (lines[0..lines_len]) |l| { - const ls = modal.lineStartOffset(text, l); - const le = modal.lineEndOffset(text, l); - const line = text[ls..le]; - if (modal.firstNonWs(line) == line.len) continue; // blank lines untouched - const at = ls + indent; - // the @min can never bind: margin is 1 only when every line has a - // space after its own token, which is a byte past `at + token.len` - chs[chs_len] = if (commented) - .{ .from = at, .to = @min(at + token.len + margin, le), .ins = "" } - else - .{ .from = at, .to = at, .ins = ins }; - chs_len += 1; - } - p.pushUndo(pane); - // one edit, and the WHOLE selection rides through it (helix maps the - // selection with the transaction) - var cells: [Pane.max_selections]Pane.SelRange = undefined; - const new = p.applyChanges(text, chs[0..chs_len]) catch return; - for (rs[0..got.n], 0..) |r, i| { - const c = Pane.rangeCells(new, .{ - .anchor = mapThroughChanges(chs[0..chs_len], r.anchor), - .head = mapThroughChanges(chs[0..chs_len], r.head), - }); - const cc = modal.positionAt(new, c.cur); - const ac = modal.positionAt(new, c.anc); - cells[i] = .{ - .row = @as(i32, @intCast(cc.row)) + eb.row0, - .col = @intCast(cc.col), - .arow = @as(i32, @intCast(ac.row)) + eb.row0, - .acol = @intCast(ac.col), - }; - } - p.setEditText(pane, new); - const t2 = p.flatSurface(pane) catch return; - for (cells[0..got.n], 0..) |s, i| rs[i] = Pane.cellRange(t2, s.arow, s.acol, s.row, s.col); - pane.setRanges(t2, rs[0..got.n], &.{}, got.pri, expl); - } - - fn normalAdjustNumber(p: *Pardes, pane: *Pane, delta: i64) void { - const eb = p.editTextEol(pane, selRows(pane)) orelse return; - const r = selRange(pane, eb.text, eb.row0); - const arena = p.scratch.allocator(); - const frag = modal.rangeText(arena, eb.text, r.a, r.b) catch return; - const rep = (modal.incrementDecimal(arena, frag, delta) catch null) orelse return; - p.pushUndo(pane); - pane.select = false; - const new = modal.replaceRange(p.gpa, eb.text, r.a, r.b, rep) catch return; - p.setEditText(pane, new); - const start = modal.lineStartOffset(new, r.a.row) + r.a.col; - const cc = modal.positionAt(new, modal.prevGrapheme(new, start + rep.len)); - pane.vsel = .{ .active = modal.nextGrapheme(rep, 0) < rep.len, .row = @as(i32, @intCast(r.a.row)) + eb.row0, .col = @intCast(r.a.col), .explicit = false }; - pane.msel.active = false; - pane.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; - pane.cur_col = @intCast(cc.col); - pane.cur_pinned = true; - pane.sticky_col = -1; - pane.ensureCursorVisible(); - } - - /// `]Space` / `[Space`: add blank lines below/above, the cursor staying - /// on its text line (the selection rides along) - fn addNewline(p: *Pardes, pane: *Pane, below: bool, cnt: usize) void { - const eb = p.editText(pane, pane.cur_row, pane.cur_row, -1) orelse return; - p.pushUndo(pane); - const row: usize = @intCast(@max(0, pane.cur_row - eb.row0)); - const arena = p.scratch.allocator(); - const nl = arena.alloc(u8, cnt) catch return; - @memset(nl, '\n'); - const llen = modal.lineSlice(eb.text, row).len; - const new = modal.insertAt(p.gpa, eb.text, .{ .row = row, .col = if (below) llen else 0 }, nl) catch return; - p.setEditText(pane, new); - if (!below) { - pane.cur_row += @intCast(cnt); // the line moved down; stay on it - if (pane.vsel.active and pane.vsel.row >= @as(i32, @intCast(row))) pane.vsel.row += @intCast(cnt); - } else if (pane.vsel.active and pane.vsel.row > @as(i32, @intCast(row))) { - pane.vsel.row += @intCast(cnt); - } - pane.cur_pinned = true; - pane.ensureCursorVisible(); - } - - /// pair mapping for m-mode: either bracket names the pair; anything else - /// surrounds with itself (quotes) - fn pairFor(ch: u21) ?struct { o: u8, c: u8 } { - return switch (ch) { - '(', ')' => .{ .o = '(', .c = ')' }, - '[', ']' => .{ .o = '[', .c = ']' }, - '{', '}' => .{ .o = '{', .c = '}' }, - '<', '>' => .{ .o = '<', .c = '>' }, - else => if (ch <= 0x7f) .{ .o = @intCast(ch), .c = @intCast(ch) } else null, - }; - } - - fn textobjectSelect(p: *Pardes, pane: *Pane, text: []const u8, obj: u21, around: bool) void { - const range = pane.primaryRange(text, 0); - switch (obj) { - 'w', 'W' => { - const r = modal.selectWord(text, range, around, obj == 'W'); - return pane.setRange(text, 0, r, false); - }, - 'p' => { - const r = modal.selectParagraph(text, range, around, 1); - return pane.setRange(text, 0, r, false); - }, - else => {}, - } - const lines = p.paneCursorLines(pane) catch return; - const cur = modal.positionAt(text, modal.selectionCursor(text, range)); - const pair: ?modal.Range = switch (obj) { - '\'', '"', '`' => modal.enclosingQuote(lines, cur, @intCast(obj)), - '(', ')' => modal.enclosingPair(lines, cur, '(', ')'), - '[', ']' => modal.enclosingPair(lines, cur, '[', ']'), - '{', '}' => modal.enclosingPair(lines, cur, '{', '}'), - '<', '>' => modal.enclosingPair(lines, cur, '<', '>'), - else => null, - }; - const pr = pair orelse return; - var a = modal.offsetAt(text, pr.a); - var head = modal.nextGrapheme(text, modal.offsetAt(text, pr.b)); - if (!around) { - // inside: shrink off the delimiters; an EMPTY pair collapses to - // a 1-wide cursor on the closing char (helix) - a = modal.nextGrapheme(text, a); - head = modal.prevGrapheme(text, head); - } - pane.setRange(text, 0, .{ .anchor = a, .head = head }, false); - } - - /// `ms`: wrap the selection (or the cursor char) in a pair; the wrap - /// including the pair becomes the selection - fn surroundAdd(p: *Pardes, pane: *Pane, ch: u21) void { - const pr = pairFor(ch) orelse return; - pane.select = false; - const eb = p.editTextEol(pane, selRows(pane)) orelse return; - const r = selRange(pane, eb.text, eb.row0); - p.pushUndo(pane); - const close_col = modal.nextGrapheme(modal.lineSlice(eb.text, r.b.row), r.b.col); - var new = modal.insertAt(p.gpa, eb.text, .{ .row = r.b.row, .col = close_col }, &[1]u8{pr.c}) catch return; - p.setEditText(pane, new); - new = modal.insertAt(p.gpa, new, .{ .row = r.a.row, .col = r.a.col }, &[1]u8{pr.o}) catch return; - p.setEditText(pane, new); - pane.msel.active = false; - pane.vsel = .{ .active = true, .row = @as(i32, @intCast(r.a.row)) + eb.row0, .col = @intCast(r.a.col), .explicit = false }; - pane.cur_row = @as(i32, @intCast(r.b.row)) + eb.row0; - pane.cur_col = @intCast(close_col + @as(usize, if (r.a.row == r.b.row) 1 else 0)); - pane.cur_pinned = true; - pane.sticky_col = -1; - pane.ensureCursorVisible(); - } - - /// `md`: delete the enclosing pair's chars; the cursor maps through - fn surroundDelete(p: *Pardes, pane: *Pane, ch: u21) void { - const pr = pairFor(ch) orelse return; - const lines = p.paneCursorLines(pane) catch return; - const cur = pane.toModalCursor(); - const r = (if (pr.o == pr.c) - modal.enclosingQuote(lines, cur, pr.o) - else - modal.enclosingPair(lines, cur, pr.o, pr.c)) orelse return; - const lo: i32 = @intCast(@min(r.a.row, cur.row)); - const hi: i32 = @intCast(@max(r.b.row, cur.row)); - const eb = p.editText(pane, lo, hi, -1) orelse return; - const text = eb.text; - const drow = -eb.row0; // surface row -> buffer row - const ra: modal.Cursor = .{ .row = @intCast(@as(i32, @intCast(r.a.row)) + drow), .col = r.a.col }; - const rb: modal.Cursor = .{ .row = @intCast(@as(i32, @intCast(r.b.row)) + drow), .col = r.b.col }; - p.pushUndo(pane); - const a_off = modal.offsetAt(text, ra); - const b_off = modal.offsetAt(text, rb); - const cur_off = modal.offsetAt(text, .{ .row = @intCast(@as(i32, @intCast(cur.row)) + drow), .col = cur.col }); - // the close first, so the open's position stays valid - var new = modal.deleteChar(p.gpa, text, rb) catch return; - p.setEditText(pane, new); - new = modal.deleteChar(p.gpa, new, ra) catch return; - p.setEditText(pane, new); - var nc = cur_off; - if (nc > b_off) nc -= 1; - if (nc > a_off) nc -= 1; - const cc = modal.positionAt(new, nc); - pane.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; - pane.cur_col = @intCast(cc.col); - pane.vsel.active = false; - pane.msel.active = false; - pane.cur_pinned = true; - pane.ensureCursorVisible(); - } - - /// `mr`: swap the enclosing pair's chars for 's - fn surroundReplace(p: *Pardes, pane: *Pane, from: u21, to: u21) void { - const fp = pairFor(from) orelse return; - const tp = pairFor(to) orelse return; - const lines = p.paneCursorLines(pane) catch return; - const cur = pane.toModalCursor(); - const r = (if (fp.o == fp.c) - modal.enclosingQuote(lines, cur, fp.o) - else - modal.enclosingPair(lines, cur, fp.o, fp.c)) orelse return; - const eb = p.editText(pane, @as(i32, @intCast(r.a.row)), @as(i32, @intCast(r.b.row)), -1) orelse return; - const drow = -eb.row0; // surface row -> buffer row - const ar: usize = @intCast(@as(i32, @intCast(r.a.row)) + drow); - const br: usize = @intCast(@as(i32, @intCast(r.b.row)) + drow); - p.pushUndo(pane); - const out = p.gpa.dupe(u8, eb.text) catch return; - out[modal.lineStartOffset(out, ar) + r.a.col] = tp.o; - out[modal.lineStartOffset(out, br) + r.b.col] = tp.c; - p.setEditText(pane, out); - } - // ---- dumb undo/redo: whole-state snapshots, one per edit op ---- - pub fn pushUndo(p: *Pardes, pane: *Pane) void { - // one keystroke, one undo step — even when it edited at ten cursors. - if (p.multi_on and !p.multi_first) return; - if (pane.file != null) return panes.File.pushUndo(p, pane); - panes.Terminal.pushUndo(p, pane); - } - - fn doUndo(p: *Pardes, pane: *Pane) void { - if (pane.file != null) return panes.File.undo(p, pane); - panes.Terminal.undo(p, pane); - } - - fn doRedo(p: *Pardes, pane: *Pane) void { - if (pane.file != null) return panes.File.redo(p, pane); - panes.Terminal.redo(p, pane); - } - pub const ChromeTarget = struct { col: u16, row: u16 }; pub fn chromeTarget(p: *const Pardes, col: u16, row: u16) ?ChromeTarget { @@ -9470,12 +7899,7 @@ pub const Pardes = struct { return null; } - const PointerTextSelection = struct { - sel: Pane.Sel, - on_tag: bool, - }; - - fn bodyHitForPane(p: *const Pardes, id: usize, supplied: Mouse.BodyHit, clamp: bool) ?Mouse.BodyHit { + pub fn bodyHitForPane(p: *const Pardes, id: usize, supplied: Mouse.BodyHit, clamp: bool) ?Mouse.BodyHit { const pane = p.panes[id] orelse return null; if (pane.file == null or (!clamp and supplied.serial != pane.serial)) return null; if (!std.math.isFinite(supplied.pixel_x) or !std.math.isFinite(supplied.pixel_y) or @@ -9499,35 +7923,6 @@ pub const Pardes = struct { return p.reprojectBodyHit(mapped_hit); } - fn pointerTextSelection(p: *const Pardes, id: usize, col: u16, row: u16, body_hit: ?Mouse.BodyHit, tag_hit: ?TagHit) ?PointerTextSelection { - if (p.panes[id] == null) return null; - const r = p.rects[id]; - if (col < r.x + config.GUTTER or col >= r.x + r.w or row < r.y or row >= r.y + r.h) return null; - const tag_y = if (p.settings.tag_bottom) r.y + r.h -| BOX_H else r.y; - const body_y = p.bodyTop(r); - const on_tag = row >= tag_y and row < tag_y + BOX_H; - if (on_tag and col < r.x + TAG_TEXT_INSET) return null; - // A notice chip is chrome: it sits OVER the body's top rows, so a - // click inside one belongs to nobody. The rest of those rows is - // ordinary body text and still answers normally. - if (!on_tag and row >= body_y and row - body_y < p.panes[id].?.notices.span() and - p.panes[id].?.notices.covers(row - body_y, col)) return null; - if (!on_tag) if (body_hit) |supplied| { - const mapped_hit = p.bodyHitForPane(id, supplied, false) orelse return null; - const c = @as(i32, mapped_hit.col) - config.GUTTER; - const v = @as(i32, mapped_hit.row) + BOX_H; - return .{ .sel = .{ .state = .dragging, .c0 = c, .c1 = c, .r0 = v, .r1 = v }, .on_tag = false }; - }; - const tag_col = if (on_tag) p.tagColumn(.pane, id, tag_hit, false) else null; - const c: i32 = (if (tag_col) |value| @as(i32, value) else @as(i32, col) - @as(i32, r.x + (if (on_tag) TAG_TEXT_INSET else config.GUTTER))) + - (if (on_tag) @as(i32, p.panes[id].?.tag_scroll) else 0); - const v: i32 = if (on_tag) 0 else @as(i32, row) - @as(i32, body_y) + @as(i32, BOX_H); - return .{ - .sel = .{ .state = .dragging, .c0 = c, .c1 = c, .r0 = v, .r1 = v }, - .on_tag = on_tag, - }; - } - fn cancelLookHover(p: *Pardes) void { if (comptime pdf_enabled) if (p.pdf_hover_preview) |*preview| { if (preview.pane < p.panes.len) if (p.panes[preview.pane]) |pane| { @@ -9554,7 +7949,7 @@ pub const Pardes = struct { return first.kind == second.kind and first.id == second.id and first.serial == second.serial and first.col == second.col; } - fn tagColumn(p: *const Pardes, kind: TagKind, id: usize, supplied: ?TagHit, clamp: bool) ?u16 { + pub fn tagColumn(p: *const Pardes, kind: TagKind, id: usize, supplied: ?TagHit, clamp: bool) ?u16 { var point = supplied orelse return null; if (point.kind != kind or point.id != id) return null; if (clamp) for (p.surface.tagLayers()) |*layer| { @@ -9581,7 +7976,7 @@ pub const Pardes = struct { _ = delay; const id = p.paneAt(col, row) orelse return p.cancelLookHover(); const pane = p.panes[id] orelse return p.cancelLookHover(); - _ = p.pointerTextSelection(id, col, row, body_hit, tag_hit) orelse return p.cancelLookHover(); + _ = edit.pointerTextSelection(p, id, col, row, body_hit, tag_hit) orelse return p.cancelLookHover(); if (comptime pdf_enabled) if (p.pdf_hover_preview) |shown| if (shown.col == col and shown.row == row and shown.pane == id and shown.serial == pane.serial) return; @@ -9614,7 +8009,7 @@ pub const Pardes = struct { if (waiting.frames < delay) return; const pane = p.panes[waiting.pane] orelse return p.cancelLookHover(); if (pane.serial != waiting.serial) return p.cancelLookHover(); - const pointed = p.pointerTextSelection(waiting.pane, waiting.col, waiting.row, waiting.body_hit, waiting.tag_hit) orelse + const pointed = edit.pointerTextSelection(p, waiting.pane, waiting.col, waiting.row, waiting.body_hit, waiting.tag_hit) orelse return p.cancelLookHover(); if (comptime pdf_enabled) if (!pointed.on_tag and panes.Pdf.paneNativeReady(p, pane)) { const probe = panes.Pdf.probeAt(p, pane, waiting.col, waiting.row) orelse @@ -9921,7 +8316,7 @@ pub const Pardes = struct { } else { p.active = id; if (pane.tag_edit) exitTagEdit(pane); - const pointed = p.pointerTextSelection(id, mcol, mrow, m.body_hit, p.pointer_tag_hit) orelse return; + const pointed = edit.pointerTextSelection(p, id, mcol, mrow, m.body_hit, p.pointer_tag_hit) orelse return; pane.clearPointerSelection(sel_slot); pane.sel[sel_slot] = pointed.sel; p.drag = .{ .select = .{ @@ -9977,7 +8372,7 @@ pub const Pardes = struct { const local = @as(i32, mrow) - @as(i32, body_y); if (m.button == config.look_button) p.active = id; pane.scrollBy(if (m.button == config.look_button) local else -local); - } else if (p.pointerTextSelection(id, mcol, mrow, m.body_hit, p.pointer_tag_hit)) |pointed| { + } else if (edit.pointerTextSelection(p, id, mcol, mrow, m.body_hit, p.pointer_tag_hit)) |pointed| { if (m.button == config.look_button) p.active = id; pane.clearPointerSelection(@intFromEnum(m.button)); pane.sel[@intFromEnum(m.button)] = pointed.sel; @@ -10148,7 +8543,7 @@ pub const Pardes = struct { const top = @min(sel.r0, sel.r1) - @as(i32, BOX_H); if (top < 0) break :lead null; const w = pane.wrapAt(top); - const col = p.paneByteAtDisplay(pane, w.line, w.at, @min(sel.c0, sel.c1) - panes.File.gutterWidth(pane)); + const col = edit.paneByteAtDisplay(p, pane, w.line, w.at, @min(sel.c0, sel.c1) - panes.File.gutterWidth(pane)); break :lead .{ .row = @intCast(@max(0, w.line)), .col = @intCast(@max(0, col)), @@ -10202,7 +8597,7 @@ pub const Pardes = struct { pane.append_at = null; pane.sticky_col = -1; pane.normal.clear(); - p.setYank(text); + edit.setYank(p, text); } fn dragRelease(p: *Pardes, button: Mouse.Button) void { @@ -10277,20 +8672,21 @@ pub const Pardes = struct { const sl = pane.sel[sel_slot]; const dragged = sl.c0 != sl.c1 or sl.r0 != sl.r1; if (!dragged) pane.sel[sel_slot].state = .none; - if (dragged) p.capturePointerSelection(pane, sel_slot) catch {}; + if (dragged) edit.capturePointerSelection(p, pane, sel_slot) catch {}; if (dragged and pane.mode == .tty) p.mirrorTtySelection(pane); const body_vis = sl.r1 - @as(i32, BOX_H); if (body_vis >= 0 and pane.mode != .tty) { const w = pane.wrapAt(body_vis); pane.cur_row = w.line; - pane.cur_col = p.paneByteAtDisplay( + pane.cur_col = edit.paneByteAtDisplay( + p, pane, w.line, w.at, sl.c1 - (if (pane.file != null) @as(i32, panes.File.gutterWidth(pane)) else 0), ); pane.cur_pinned = true; - if (clampFileCursor(pane)) pane.ensureCursorVisible(); + if (edit.clampFileCursor(pane)) pane.ensureCursorVisible(); pane.msel.active = false; pane.normal.clear(); pane.vsel.active = false; @@ -10325,7 +8721,7 @@ pub const Pardes = struct { if (s.button == config.look_button and clk.r0 >= BOX_H and pane.mode != .tty) { pane.cur_row = operand.row; pane.cur_col = operand.col; - if (clampFileCursor(pane)) pane.ensureCursorVisible(); + if (edit.clampFileCursor(pane)) pane.ensureCursorVisible(); } const txt = operand.text; const cmd = if (s.button == config.look_button) config.look_cmd else config.exec_cmd; @@ -10370,7 +8766,7 @@ pub const Pardes = struct { } // A PDF is immutable: its Cut-side chord is Snarf (copy), while // Paste has no document mutation to perform. - if (cut and pv.selection_text.len > 0) p.setYank(pv.selection_text); + if (cut and pv.selection_text.len > 0) edit.setYank(p, pv.selection_text); return; }; if (pane.mode == .tty) { @@ -10394,7 +8790,7 @@ pub const Pardes = struct { p.emitWrite(s.id, &[_]u8{ 0x1b, '[', 'M', 32, cb, rb, 0x1b, '[', 'M', 35, cb, rb }); } } - p.typeToTty(s.id, pane, y); + edit.typeToTty(p, s.id, pane, y); return; } if (!s.chorded) { @@ -10408,8 +8804,8 @@ pub const Pardes = struct { const w1 = pane.wrapAt(@max(0, sl.r1 - @as(i32, BOX_H))); const row0 = w0.line; const row1 = w1.line; - const col0 = p.paneByteAtDisplay(pane, w0.line, w0.at, sl.c0 - pfx); - const col1 = p.paneByteAtDisplay(pane, w1.line, w1.at, sl.c1 - pfx); + const col0 = edit.paneByteAtDisplay(p, pane, w0.line, w0.at, sl.c0 - pfx); + const col1 = edit.paneByteAtDisplay(p, pane, w1.line, w1.at, sl.c1 - pfx); pane.cur_row = row1; pane.cur_col = col1; pane.cur_pinned = true; @@ -10420,16 +8816,16 @@ pub const Pardes = struct { pane.nsel = 0; } if (cut) { - if (pane.vsel.active) p.normalDelete(pane, true); + if (pane.vsel.active) edit.normalDelete(p, pane, true); } else if (pane.vsel.active) { - p.normalReplaceYank(pane); + edit.normalReplaceYank(p, pane); } else if (pane.file) |*f| { // bare cursor: splice the register in literally (no linewise // magic) so a paste right after a cut restores the text exactly const y = p.yank orelse return; if (y.len == 0) return; const at = modal.Cursor{ .row = @intCast(@max(0, pane.cur_row)), .col = @intCast(@max(0, pane.cur_col)) }; - p.pushUndo(pane); + edit.pushUndo(p, pane); const new = modal.insertAt(p.gpa, f.content, at, y) catch return; panes.File.setContent(p, f, new); pane.vsel = .{ .active = modal.nextGrapheme(y, 0) < y.len, .row = @intCast(at.row), .col = @intCast(at.col), .explicit = false }; @@ -10445,7 +8841,7 @@ pub const Pardes = struct { pane.cur_pinned = true; pane.sticky_col = -1; pane.ensureCursorVisible(); - } else p.pasteText(pane, p.yank orelse return, true, 1); + } else edit.pasteText(p, pane, p.yank orelse return, true, 1); } /// Tty: a shell in the caller's directory, raw from the first frame, @@ -10658,7 +9054,7 @@ pub const Pardes = struct { } fn selectSpan(p: *Pardes, pane: *Pane, at: look.Spot) void { - const text = p.flatSurface(pane) catch return; + const text = edit.flatSurface(p, pane) catch return; // saturating, not `- 1`: `f.zig:0-5` is a legal thing to type and a // 1-based zero is the same nothing an absent number is const acol: i32 = @intCast(at.col -| 1); @@ -13040,66 +11436,6 @@ test "hopping between two panes does not grow the jump stack" { try std.testing.expectEqual(depth, p.njumps); } -/// Drain the queue and say whether the shell was asked to do `want`. -fn drainedEffect(p: *Pardes, want: std.meta.Tag(Effect)) bool { - var seen = false; - while (p.nextEffect()) |effect| { - if (std.meta.activeTag(effect) == want) seen = true; - } - return seen; -} - -test "only the SPC clipboard commands cross to the system clipboard" { - if (platform == .web) return; - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); - defer p.deinit(); - p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); - const pane = p.panes[0].?; - - _ = drainedEffect(p, .set_clipboard); - p.update(.{ .key = .{ .cp = 'y' } }); - try std.testing.expect(p.yank != null and p.yank.?.len > 0); - try std.testing.expect(!drainedEffect(p, .set_clipboard)); - p.update(.{ .key = .{ .cp = 'd' } }); - try std.testing.expect(!drainedEffect(p, .set_clipboard)); - - // `SPC y` is the one that does, and it puts the SAME text there that `y` - // put in the register — it is the ordinary yank path plus the mirror. - p.update(.{ .key = .{ .cp = ' ' } }); - p.update(.{ .key = .{ .cp = 'y' } }); - const yanked = p.yank orelse return error.MissingYank; - try std.testing.expect(drainedEffect(p, .set_clipboard)); - - const before = pane.file.?.content.len; - p.update(.{ .key = .{ .cp = ' ' } }); - p.update(.{ .key = .{ .cp = 'p' } }); - try std.testing.expect(p.clip_pending != null); - try std.testing.expect(drainedEffect(p, .read_clipboard)); - try std.testing.expectEqual(before, pane.file.?.content.len); // nothing yet - p.update(.{ .paste = "PASTED" }); - try std.testing.expect(p.clip_pending == null); - try std.testing.expect(std.mem.indexOf(u8, pane.file.?.content, "PASTED") != null); - // ...and it did NOT land in the register on its way past, nor echo back - // out to the clipboard it came from. - try std.testing.expectEqualStrings(yanked, p.yank orelse ""); - try std.testing.expect(!drainedEffect(p, .set_clipboard)); - - // An UNSOLICITED paste — the outer terminal's bracketed paste, a Cmd-V — - // is the same event with no request behind it, and means paste after. - p.update(.{ .paste = "UNASKED" }); - try std.testing.expect(std.mem.indexOf(u8, pane.file.?.content, "UNASKED") != null); - try std.testing.expectEqualStrings(yanked, p.yank orelse ""); - - // A request the shell never answers dies at the next keystroke rather - // than firing late into whatever pane is focused by then. - p.update(.{ .key = .{ .cp = ' ' } }); - p.update(.{ .key = .{ .cp = 'P' } }); - try std.testing.expect(p.clip_pending != null); - p.update(.{ .key = .{ .cp = 'l' } }); - try std.testing.expect(p.clip_pending == null); -} - /// Every `.write` a keystroke produced, joined — what the pane's program would /// actually have read off its pty. fn drainWrites(p: *Pardes, buf: []u8) []const u8 { @@ -13122,7 +11458,7 @@ test "raw tty forwards Ctrl letters and former global shortcuts to the child" { defer p.deinit(); var buf: [256]u8 = undefined; _ = drainWrites(p, &buf); - p.setYank("register text"); + edit.setYank(p, "register text"); for ('a'..'z' + 1) |letter| { if (letter == p.opts.tty_toggle) continue; // editor/TTY mode shortcut if (letter == 'v') continue; // the window's paste, asserted just below @@ -13507,7 +11843,7 @@ test "jump history clears selections and lands at the recorded cursor" { for ([_]u21{ 'o', 'i' }, 0..) |key, index| { pane.setRange(pane.file.?.content, 0, .{ .anchor = 1, .head = 5 }, true); pane.sel[0] = .{ .state = .done, .c0 = 2, .c1 = 5, .r0 = 1, .r1 = 1 }; - try p.capturePointerSelection(pane, 0); + try edit.capturePointerSelection(p, pane, 0); pane.select = true; p.update(.{ .key = .{ .cp = key, .ctrl = true } }); try std.testing.expectEqual(index, p.jcur); @@ -13542,10 +11878,10 @@ test "jump history terminal rectangle follows output and clears on reflow" { const pane = p.panes[0].?; p.update(.{ .output = .{ .pane = 0, .bytes = "alpha bravo\r\nsecond line\r\n" } }); pane.sel[0] = .{ .state = .done, .c0 = 0, .c1 = 4, .r0 = BOX_H, .r1 = BOX_H }; - try p.capturePointerSelection(pane, 0); - try std.testing.expectEqualStrings("alpha", try p.selectionText(pane, pane.sel[0])); + try edit.capturePointerSelection(p, pane, 0); + try std.testing.expectEqualStrings("alpha", try edit.selectionText(p, pane, pane.sel[0])); for (0..40) |_| p.update(.{ .output = .{ .pane = 0, .bytes = "more output\r\n" } }); - try std.testing.expectEqualStrings("alpha", try p.selectionText(pane, pane.sel[0])); + try std.testing.expectEqualStrings("alpha", try edit.selectionText(p, pane, pane.sel[0])); panes.Terminal.resizeGrid(pane, p.gpa, pane.cols - 1, pane.rows); try std.testing.expect(pane.pointerSelection(pane.sel[0]) == null); try std.testing.expectEqual(.none, pane.sel[0].state); diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index ea4a4d5b..90c8e951 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -7,6 +7,7 @@ const std = @import("std"); const filesystem = @import("fs.zig"); const pardes = @import("pardes.zig"); +const edit = @import("edit.zig"); const modal = @import("modal.zig"); const panes = @import("panes.zig"); const config = @import("config.zig"); @@ -662,7 +663,7 @@ pub fn pipeResponse( @memcpy(replacement[write_at..], f.content[read_at..]); // One async request is one history transaction, even at 64 cursors. - p.pushUndo(pane); + edit.pushUndo(p, pane); panes.File.setContent(p, f, replacement); pane.setRanges(f.content, new_ranges[0..n], &.{}, wait.primary, wait.explicit); pane.select = false; diff --git a/test/hxdiff.zig b/test/hxdiff.zig index 22d8067d..e79a6bb6 100644 --- a/test/hxdiff.zig +++ b/test/hxdiff.zig @@ -488,7 +488,7 @@ fn runCase(arena: std.mem.Allocator, case: Case, pane_kind: []const u8) !Result // the cursor's empty row below the output (see Pardes.shellRows), so // its line list is exactly a file content's — join it and you have the // same bytes, trailing newline included, with nothing to patch up. - const lines = try core.paneCursorLines(pane); + const lines = try pardes.edit.paneCursorLines(core, pane); var text_len: usize = if (lines.len == 0) 0 else lines.len - 1; for (lines) |ln| text_len = std.math.add(usize, text_len, ln.len) catch return error.OutOfMemory; const joined = try arena.alloc(u8, text_len); diff --git a/test/lspbench.zig b/test/lspbench.zig index 59cac155..7c4e8753 100644 --- a/test/lspbench.zig +++ b/test/lspbench.zig @@ -22,7 +22,7 @@ const Anchor = struct { }; const anchors = [_]Anchor{ - .{ .file = "src/pardes.zig", .needle = "modal.cursorLineCount(text)", .at = 6, .kind = .definition, .expect = "modal.zig" }, + .{ .file = "src/edit.zig", .needle = "modal.cursorLineCount(text)", .at = 6, .kind = .definition, .expect = "modal.zig" }, .{ .file = "src/lsp/lsp.zig", .needle = "lineCol(source", .at = 0, .kind = .definition, .expect = "lsp.zig" }, .{ .file = "src/lsp/lsp.zig", .needle = "std.mem.count(u8", .at = 8, .kind = .definition, .expect = "mem.zig" }, .{ .file = "src/lsp/lsp.zig", .needle = "lineCol(source", .at = 0, .kind = .hover, .expect = "" }, diff --git a/test/perf.zig b/test/perf.zig index 057725fd..7d3b5e3d 100644 --- a/test/perf.zig +++ b/test/perf.zig @@ -527,7 +527,7 @@ fn measureTerm(op: TermOp, fx: TermFixture, reps: usize) !Cell { pump(core); } _ = try frame(core); - const motion_rows = if (op == .key_down) (try core.paneCursorLines(pane)).len else 0; + const motion_rows = if (op == .key_down) (try pardes.edit.paneCursorLines(core, pane)).len else 0; if (op == .key_down and motion_rows < 3) return error.EmptyTerminalFixture; var cols = screen_cols; -- cgit v1.3 From d126715cd20a39dbc10870318968546f5e371b97 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 27 Sep 2026 22:42:54 -0300 Subject: Split the pipe's editor side out of selection_pipe.zig into Pipe.zig Pure move, no behaviour change: the editor half of `|` that the earlier change put at the end of selection_pipe.zig (PendingPipe, pipeMarker, submitPipe, pipeRequest, pipeFailed, pipeCut, pipeOutput, pipeResponse, and the eight pipe tests with nextPipeEffect) now lives in its own Pipe.zig, so the pipe's editor state can become Pipe's own fields next. selection_pipe.zig goes back to exactly what it was before this series: the native runner and the boundary values the shells hand to their workers. The moved code names the runner's types as `selection_pipe.X` again, as it did in pardes.zig; callers change from `selection_pipe.submitPipe(p, ..)` to `Pipe.submitPipe(p, ..)` (pardes.zig and the four shells). Co-Authored-By: Claude Opus 5.5 --- src/Pipe.zig | 534 +++++++++++++++++++++++++++++++++++++++++++++++ src/detached/server.zig | 3 +- src/fs.zig | 1 + src/gui/gui.zig | 3 +- src/macos.zig | 3 +- src/pardes.zig | 10 +- src/selection_pipe.zig | 542 +----------------------------------------------- src/tty/tty.zig | 3 +- 8 files changed, 554 insertions(+), 545 deletions(-) create mode 100644 src/Pipe.zig (limited to 'src/selection_pipe.zig') diff --git a/src/Pipe.zig b/src/Pipe.zig new file mode 100644 index 00000000..0fde654a --- /dev/null +++ b/src/Pipe.zig @@ -0,0 +1,534 @@ +//! The pipe inside the editor (`|`): the prompt, the request the core emits +//! for the shell to run through selection_pipe.zig, and the atomic edit its +//! answer makes. +const std = @import("std"); +const pardes = @import("pardes.zig"); +const modal = @import("modal.zig"); +const panes = @import("panes.zig"); +const config = @import("config.zig"); +const selection_pipe = @import("selection_pipe.zig"); +const edit = @import("edit.zig"); +const Pane = panes.Pane; +const Key = pardes.Key; +const Pardes = pardes.Pardes; + +fn nextPipeEffect(p: *Pardes) ?u32 { + while (p.nextEffect()) |effect| switch (effect) { + .pipe => |request| return request.id, + else => {}, + }; + return null; +} + +/// The tag-tail marker each behaviour arms with. One function so the prompt +/// that is DRAWN and the command that is PARSED can never disagree. +pub fn pipeMarker(how: modal.Normal.PipeBehavior) []const u8 { + return switch (how) { + .replace => config.pipe_marker, + .ignore => config.pipe_marker_to, + .insert => config.pipe_marker_insert, + .append => config.pipe_marker_append, + }; +} + +pub const PendingPipe = struct { + id: u32, + pane: usize, + serial: u32, + revision: u32, + command: []u8, + cwd: []u8, + inputs: []selection_pipe.Input, + ranges: [Pane.max_selections]modal.Selection, + primary: u8, + explicit: bool, + how: modal.Normal.PipeBehavior, + nranges: u8, + + pub fn deinit(wait: *PendingPipe, gpa: std.mem.Allocator) void { + gpa.free(wait.command); + gpa.free(wait.cwd); + for (wait.inputs) |input| gpa.free(@constCast(input.bytes)); + gpa.free(wait.inputs); + wait.* = undefined; + } + + fn request(wait: *const PendingPipe) selection_pipe.Request { + return .{ + .id = wait.id, + .command = wait.command, + .cwd = wait.cwd, + .inputs = wait.inputs, + }; + } +}; + +pub fn submitPipe(p: *Pardes, id: usize) void { + const pane = p.panes[id] orelse return; + const f = pane.file orelse return; + if (!panes.Output.fileTraits(f.output).saves) return; + const prompt = switch (pane.prompt) { + .pipe => |pipe| pipe, + else => return, + }; + const tail = pane.tagSlice(); + const armed = tail[@min(prompt.at, tail.len)..]; + const marker = pipeMarker(prompt.how); + if (!std.mem.startsWith(u8, armed, marker)) return; + const command = armed[marker.len..]; + if (command.len == 0) return; + + var ranges: [Pane.max_selections]modal.Selection = undefined; + const got = pane.ranges(f.content, 0, &ranges); + // `!`/`A-!` take no stdin and run ONCE — see `PendingPipe.nranges`. + const ninputs = if (prompt.how.pipes()) got.n else 1; + const inputs = p.gpa.alloc(selection_pipe.Input, ninputs) catch return; + var made: usize = 0; + for (ranges[0..ninputs], 0..) |range, i| { + const bytes = if (prompt.how.pipes()) bytes: { + const lo = @min(range.anchor, range.head); + const hi = @max(range.anchor, range.head); + if (hi > f.content.len) break; + break :bytes f.content[lo..hi]; + } else ""; + const copy = p.gpa.dupe(u8, bytes) catch break; + inputs[i] = .{ .bytes = copy }; + made += 1; + } + if (made != ninputs) { + for (inputs[0..made]) |input| p.gpa.free(@constCast(input.bytes)); + p.gpa.free(inputs); + return; + } + const command_copy = p.gpa.dupe(u8, command) catch { + for (inputs) |input| p.gpa.free(@constCast(input.bytes)); + p.gpa.free(inputs); + return; + }; + const cwd_copy = p.gpa.dupe(u8, Pardes.paneDir(pane)) catch { + p.gpa.free(command_copy); + for (inputs) |input| p.gpa.free(@constCast(input.bytes)); + p.gpa.free(inputs); + return; + }; + + p.pipe_seq +%= 1; + if (p.pipe_wait) |*old| old.deinit(p.gpa); + p.pipe_wait = .{ + .id = p.pipe_seq, + .pane = id, + .serial = pane.serial, + .revision = f.revision, + .command = command_copy, + .cwd = cwd_copy, + .inputs = inputs, + .ranges = ranges, + .primary = @intCast(got.pri), + .explicit = (pane.vsel.active and pane.vsel.explicit) or pane.msel.active, + .how = prompt.how, + .nranges = @intCast(got.n), + }; + p.emit(.{ .pipe = .{ .id = p.pipe_seq } }); +} + +/// Frontends call this while draining a .pipe effect, then immediately +/// copy the borrowed view into a worker-owned selection_pipe.Job. +pub fn pipeRequest(p: *const Pardes, id: u32) ?selection_pipe.Request { + if (p.pipe_wait) |*wait| { + if (wait.id == id) return wait.request(); + } + return null; +} + +fn pipeFailed(p: *Pardes, wait: *const PendingPipe, failure: ?selection_pipe.Failure) void { + var out: std.Io.Writer.Allocating = .init(p.gpa); + defer out.deinit(); + const w = &out.writer; + w.print("| {s}\n\n", .{wait.command}) catch return; + if (failure) |fail| { + if (wait.inputs.len > 1) + w.print("selection {d} of {d}: ", .{ fail.index + 1, wait.inputs.len }) catch return; + switch (fail.kind) { + .exit => w.print("exit status {d}\n", .{fail.code}) catch return, + .signal => w.writeAll("killed by a signal\n") catch return, + .timeout => w.print( + "still running after {d} seconds, and stopped\n", + .{selection_pipe.command_timeout_seconds}, + ) catch return, + .too_large => w.writeAll("produced more output than a filter may return\n") catch return, + .spawn => w.writeAll("could not be started\n") catch return, + .io => w.writeAll("could not be read\n") catch return, + } + if (fail.stderr.len > 0) w.print("\n{s}", .{fail.stderr}) catch return; + } else { + // No diagnosis at all: nobody ran it. The detached daemon, the + // browser and the board all leave `pipe` null on purpose. + w.writeAll("this session cannot run filters\n") catch return; + } + const content = out.toOwnedSlice() catch return; + const was = p.active; + panes.Output.openErrors(p, wait.pane, content) catch |err| { + p.gpa.free(content); + // Nowhere to put the report is itself worth one line. + p.reportError(wait.pane, "pipe", err); + return; + }; + if (p.panes[was] != null) p.active = was; +} + +fn pipeCut(wait: *const PendingPipe, content: []const u8, i: usize) ?struct { lo: usize, hi: usize } { + const range = wait.ranges[i]; + const lo = @min(range.anchor, range.head); + const hi = @max(range.anchor, range.head); + if (hi > content.len) return null; + return switch (wait.how) { + .replace, .ignore => .{ .lo = lo, .hi = hi }, + .insert => .{ .lo = lo, .hi = lo }, + .append => .{ .lo = hi, .hi = hi }, + }; +} + +fn pipeOutput( + wait: *const PendingPipe, + outputs: []const []const u8, + content: []const u8, + i: usize, +) []const u8 { + var out = if (wait.how.pipes()) outputs[i] else outputs[0]; + const range = wait.ranges[i]; + const lo = @min(range.anchor, range.head); + const hi = @max(range.anchor, range.head); + const selected = if (hi <= content.len) content[lo..hi] else ""; + if (!std.mem.endsWith(u8, selected, "\n") and std.mem.endsWith(u8, out, "\n")) { + out = out[0 .. out.len - 1]; + if (std.mem.endsWith(u8, out, "\r")) out = out[0 .. out.len - 1]; + } + return out; +} + +pub fn pipeResponse( + p: *Pardes, + id: u32, + success: bool, + outputs: []const []const u8, + failure: ?selection_pipe.Failure, +) void { + // A SUPERSEDED OR UNKNOWN id is the one silence worth keeping: it is + // the answer to a question nobody is still asking. + if (p.pipe_wait == null or p.pipe_wait.?.id != id) return; + var wait = p.pipe_wait.?; + p.pipe_wait = null; + defer wait.deinit(p.gpa); + + if (!success or outputs.len != wait.inputs.len) return pipeFailed(p, &wait, failure); + + const pane = p.panes[wait.pane] orelse return; + if (pane.serial != wait.serial) return; + const f = if (pane.file) |*file| file else return; + if (!panes.Output.fileTraits(f.output).saves) return; + if (f.revision != wait.revision) + return p.reportError(wait.pane, "pipe", error.FileChangedWhileFiltering); + + if (wait.how == .ignore) return; + + const n = wait.nranges; + var total_output: usize = 0; + var removed: usize = 0; + var previous_end: usize = 0; + for (0..n) |i| { + const cut = pipeCut(&wait, f.content, i) orelse return; + const output = pipeOutput(&wait, outputs, f.content, i); + if (output.len > selection_pipe.max_stdout_bytes) return; + total_output = std.math.add(usize, total_output, output.len) catch return; + if (total_output > selection_pipe.max_total_stdout_bytes) return; + if (cut.lo < previous_end) return; + removed = std.math.add(usize, removed, cut.hi - cut.lo) catch return; + previous_end = cut.hi; + } + const kept = f.content.len - removed; + const final_len = std.math.add(usize, kept, total_output) catch return; + const replacement = p.gpa.alloc(u8, final_len) catch return; + + var new_ranges: [Pane.max_selections]modal.Selection = undefined; + var read_at: usize = 0; + var write_at: usize = 0; + for (0..n) |i| { + const range = wait.ranges[i]; + const cut = pipeCut(&wait, f.content, i).?; + const output = pipeOutput(&wait, outputs, f.content, i); + @memcpy(replacement[write_at .. write_at + (cut.lo - read_at)], f.content[read_at..cut.lo]); + write_at += cut.lo - read_at; + const out_start = write_at; + @memcpy(replacement[write_at .. write_at + output.len], output); + write_at += output.len; + const out_end = write_at; + new_ranges[i] = if (range.anchor > range.head) + .{ .anchor = out_end, .head = out_start } + else + .{ .anchor = out_start, .head = out_end }; + read_at = cut.hi; + } + @memcpy(replacement[write_at..], f.content[read_at..]); + + // One async request is one history transaction, even at 64 cursors. + edit.pushUndo(p, pane); + panes.File.setContent(p, f, replacement); + pane.setRanges(f.content, new_ranges[0..n], &.{}, wait.primary, wait.explicit); + pane.select = false; + pane.ensureCursorVisible(); +} + +test "selection pipe prompt submits exact request and Escape cancels" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("one\ntwo\n"); + pane.cur_row = 0; + pane.cur_col = 2; + pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + + p.update(.{ .key = .{ .cp = '|' } }); + try std.testing.expect(pane.tag_edit and pane.prompt == .pipe); + try std.testing.expect(std.mem.endsWith(u8, pane.tagSlice(), config.pipe_marker)); + try std.testing.expect(nextPipeEffect(p) == null); + p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(!pane.tag_edit and pane.prompt != .pipe); + const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + const request = pipeRequest(p, id) orelse return error.MissingPipeRequest; + try std.testing.expectEqualStrings("tr a-z A-Z", request.command); + try std.testing.expectEqualStrings("/", request.cwd); + try std.testing.expectEqual(@as(usize, 1), request.inputs.len); + try std.testing.expectEqualSlices(u8, "one", request.inputs[0].bytes); + + const before = pane.file.?.content; + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(!pane.tag_edit and pane.prompt != .pipe); + try std.testing.expectEqualSlices(u8, before, pane.file.?.content); + try std.testing.expect(nextPipeEffect(p) == null); +} + +test "selection pipe replaces all ranges atomically and undo restores them" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("aa bb cc\n"); + const ranges = [_]modal.Selection{ + .{ .anchor = 0, .head = 2 }, + .{ .anchor = 6, .head = 8 }, + }; + pane.setRanges(pane.file.?.content, &ranges, &.{}, 0, true); + + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + const request = pipeRequest(p, id) orelse return error.MissingPipeRequest; + try std.testing.expectEqual(@as(usize, 2), request.inputs.len); + try std.testing.expectEqualSlices(u8, "aa", request.inputs[0].bytes); + try std.testing.expectEqualSlices(u8, "cc", request.inputs[1].bytes); + + var denied = std.testing.FailingAllocator.init(gpa, .{ .fail_index = 0 }); + p.scratch.deinit(); + p.scratch = .init(denied.allocator()); + const outputs: []const []const u8 = &.{ "AA\n", "" }; + p.update(.{ .pipe_resp = .{ .id = id, .success = true, .outputs = outputs } }); + try std.testing.expectEqualSlices(u8, "AA bb \n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 1), pane.file.?.history.undo_len); + try std.testing.expectEqual(@as(u8, 1), pane.nsel); + try std.testing.expectEqual(@as(i32, 1), pane.cur_col); + try std.testing.expectEqual(@as(i32, 6), pane.sels[0].col); + try std.testing.expect(!denied.has_induced_failure); + + p.update(.{ .key = .{ .cp = 'u' } }); + try std.testing.expectEqualSlices(u8, "aa bb cc\n", pane.file.?.content); +} + +test "the four shell behaviours put their output where helix puts it" { + const gpa = std.testing.allocator; + const Case = struct { + key: Key, + outputs: []const []const u8, + want: []const u8, + want_effect: bool = true, + }; + // One selection, `bb`, in `aa bb cc\n` — bytes 3..5. + for ([_]Case{ + // `|` replaces the selection with what the command wrote. + .{ .key = .{ .cp = '|' }, .outputs = &.{"XX"}, .want = "aa XX cc\n" }, + // `!` inserts BEFORE it, and the selection survives to its right. + .{ .key = .{ .cp = '!' }, .outputs = &.{"XX"}, .want = "aa XXbb cc\n" }, + // `A-!` appends AFTER it. + .{ .key = .{ .cp = '!', .alt = true }, .outputs = &.{"XX"}, .want = "aa bbXX cc\n" }, + // `A-|` runs the command and leaves the text completely alone: the + // point is the side effect, not the output. + .{ .key = .{ .cp = '|', .alt = true }, .outputs = &.{"XX"}, .want = "aa bb cc\n" }, + }) |case| { + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("aa bb cc\n"); + pane.setRanges(pane.file.?.content, &.{.{ .anchor = 3, .head = 5 }}, &.{}, 0, true); + + p.update(.{ .key = case.key }); + for ("cmd") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + p.update(.{ .pipe_resp = .{ .id = id, .success = true, .outputs = case.outputs } }); + try std.testing.expectEqualSlices(u8, case.want, pane.file.?.content); + } +} + +test "a command with no stdin runs once and every cursor gets that one answer" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("aa bb cc\n"); + const ranges = [_]modal.Selection{ + .{ .anchor = 0, .head = 2 }, + .{ .anchor = 6, .head = 8 }, + }; + pane.setRanges(pane.file.?.content, &ranges, &.{}, 0, true); + + p.update(.{ .key = .{ .cp = '!' } }); // insert-output: no stdin + for ("date") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + const request = pipeRequest(p, id) orelse return error.MissingPipeRequest; + + try std.testing.expectEqual(@as(usize, 1), request.inputs.len); + try std.testing.expectEqualSlices(u8, "", request.inputs[0].bytes); + + p.update(.{ .pipe_resp = .{ .id = id, .success = true, .outputs = &.{"T"} } }); + try std.testing.expectEqualSlices(u8, "Taa bb Tcc\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 1), pane.file.?.history.undo_len); +} + +test "selection pipe failure and stale completion never mutate the file" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("abc\n"); + pane.cur_col = 2; + pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const failed_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + p.update(.{ .pipe_resp = .{ .id = failed_id, .success = false, .outputs = &.{} } }); + try std.testing.expectEqualSlices(u8, "abc\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); + + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const stale_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + panes.File.setContent(p, &pane.file.?, try gpa.dupe(u8, "changed\n")); + p.update(.{ .pipe_resp = .{ .id = stale_id, .success = true, .outputs = &.{"ABC"} } }); + try std.testing.expectEqualSlices(u8, "changed\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); +} + +test "a failed filter opens an errors buffer carrying the command's own words" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("abc\n"); + pane.cur_col = 2; + pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + + p.update(.{ .key = .{ .cp = '|' } }); + for ("trr") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + + // The shape a real runner brings home for `| trr`: nonzero exit, and the + // shell's own sentence about it. + var stderr = "sh: line 1: trr: command not found\n".*; + p.update(.{ .pipe_resp = .{ + .id = id, + .success = false, + .outputs = &.{}, + .failure = .{ .kind = .exit, .code = 127, .stderr = &stderr }, + } }); + + // The text is untouched — a failed filter is not an edit... + try std.testing.expectEqualSlices(u8, "abc\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); + // ...and the cursor did not go anywhere, so `|` again edits the same file. + try std.testing.expectEqual(@as(usize, 0), p.active); + + var found: ?[]const u8 = null; + for (p.panes) |slot| { + const q = slot orelse continue; + const qf = q.file orelse continue; + const o = qf.output orelse continue; + if (std.meta.activeTag(o.from) == .errors) found = qf.content; + } + const report = found orelse return error.NoErrorsBuffer; + try std.testing.expect(std.mem.indexOf(u8, report, "| trr") != null); + try std.testing.expect(std.mem.indexOf(u8, report, "exit status 127") != null); + try std.testing.expect(std.mem.indexOf(u8, report, "command not found") != null); +} + +test "selection pipe rejects a reused pane slot and a superseded request" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + var pane = try p.setTestFile("old\n"); + pane.cur_col = 2; + pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const replaced_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + pane = try p.setTestFile("new\n"); // same slot, different serial + p.update(.{ .pipe_resp = .{ .id = replaced_id, .success = true, .outputs = &.{"OLD"} } }); + try std.testing.expectEqualSlices(u8, "new\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); + + pane.cur_col = 2; + pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const first_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + p.update(.{ .key = .{ .cp = '|' } }); + p.update(.{ .key = .{ .cp = 'c', .text = "tr a-z A-Z" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + const latest_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; + try std.testing.expect(first_id != latest_id); + + p.update(.{ .pipe_resp = .{ .id = first_id, .success = true, .outputs = &.{"BAD"} } }); + try std.testing.expectEqualSlices(u8, "new\n", pane.file.?.content); + p.update(.{ .pipe_resp = .{ .id = latest_id, .success = true, .outputs = &.{"NEW"} } }); + try std.testing.expectEqualSlices(u8, "NEW\n", pane.file.?.content); + try std.testing.expectEqual(@as(usize, 1), pane.file.?.history.undo_len); +} + +test "selection pipe binding is file-normal-only" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("x"); + + pane.file.?.output = .{ .from = .search }; + p.update(.{ .key = .{ .cp = '|' } }); + try std.testing.expect(pane.prompt != .pipe); + pane.file.?.output = null; + pane.mode = .insert; + p.update(.{ .key = .{ .cp = '|', .text = "|" } }); + try std.testing.expect(pane.prompt != .pipe); + try std.testing.expectEqualSlices(u8, "|x", pane.file.?.content); +} diff --git a/src/detached/server.zig b/src/detached/server.zig index b75fa5ad..9b23f8d6 100644 --- a/src/detached/server.zig +++ b/src/detached/server.zig @@ -9,6 +9,7 @@ const wire = @import("wire.zig"); const host_io = @import("../host_io.zig"); const selection_pipe = @import("../selection_pipe.zig"); +const Pipe = @import("../Pipe.zig"); const file_watch = @import("../file_watch.zig"); @@ -411,7 +412,7 @@ pub const Session = struct { s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); return; } - const request = selection_pipe.pipeRequest(s.core, id) orelse return; + const request = Pipe.pipeRequest(s.core, id) orelse return; const job = selection_pipe.Job.copy(s.worker_gpa, request) catch |err| { s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); return s.core.reportError(s.core.active, "pipe", err); diff --git a/src/fs.zig b/src/fs.zig index 825e63bb..5374c843 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -44,6 +44,7 @@ const source_files = [_]Source{ .{ .path = "src/normal.zig", .contents = @embedFile("normal.zig") }, .{ .path = "src/mouse.zig", .contents = @embedFile("mouse.zig") }, .{ .path = "src/exec.zig", .contents = @embedFile("exec.zig") }, + .{ .path = "src/Pipe.zig", .contents = @embedFile("Pipe.zig") }, .{ .path = "src/File.zig", .contents = @embedFile("File.zig") }, .{ .path = "src/Output.zig", .contents = @embedFile("Output.zig") }, .{ .path = "src/Mini.zig", .contents = @embedFile("Mini.zig") }, diff --git a/src/gui/gui.zig b/src/gui/gui.zig index f044ca17..ef94d5b3 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -16,6 +16,7 @@ const crt = @import("crt.zig"); const pet = @import("pet.zig"); const fonts = @import("../fonts.zig"); // the Font builtin's half of the seam const selection_pipe = @import("../selection_pipe.zig"); +const Pipe = @import("../Pipe.zig"); const c_heap = @import("c_heap"); const ninep_io = @import("../9p_io.zig"); @@ -1703,7 +1704,7 @@ fn spawnPipe( core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); return; } - const view = selection_pipe.pipeRequest(core, id) orelse return; + const view = Pipe.pipeRequest(core, id) orelse return; const job = selection_pipe.Job.copy(gpa, view) catch |err| { core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); return core.reportError(core.active, "pipe", err); diff --git a/src/macos.zig b/src/macos.zig index 78356810..dc5b79d0 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -18,6 +18,7 @@ const host_io = @import("host_io.zig"); const fonts = @import("fonts.zig"); // the shared fallback preference order const tracy = @import("tracy.zig"); // no-op unless -Dtracy names a checkout const selection_pipe = @import("selection_pipe.zig"); // Job, runJob and Tasks +const Pipe = @import("Pipe.zig"); const crash = @import("crash.zig"); pub const panic = std.debug.FullPanic(struct { @@ -2351,7 +2352,7 @@ fn pipeRequest(ctx: ?*anyopaque, id: u32) void { st.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); return; } - const view = selection_pipe.pipeRequest(st.core, id) orelse return; + const view = Pipe.pipeRequest(st.core, id) orelse return; const job = selection_pipe.Job.copy(st.gpa, view) catch |err| { st.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); return st.core.reportError(st.core.active, "pipe", err); diff --git a/src/pardes.zig b/src/pardes.zig index d2ac5868..00439a36 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -17,6 +17,7 @@ pub const normal = @import("normal.zig"); pub const edit = @import("edit.zig"); pub const body_layer = @import("body_layer.zig"); pub const Messages = @import("Messages.zig"); +pub const Pipe = @import("Pipe.zig"); pub const colors = @import("colors.zig"); pub const Theme = colors.Theme; pub const themes = colors.themes; @@ -443,6 +444,7 @@ test { _ = @import("edit.zig"); _ = @import("Messages.zig"); _ = @import("selection_pipe.zig"); + _ = @import("Pipe.zig"); _ = @import("colors.zig"); _ = @import("surface.zig"); _ = @import("body_layer.zig"); @@ -3745,7 +3747,7 @@ pub const Pardes = struct { } = null, pipe_seq: u32 = 0, - pipe_wait: ?selection_pipe.PendingPipe = null, + pipe_wait: ?Pipe.PendingPipe = null, fs: filesystem.Namespace = .{}, boot: Boot = .shell, @@ -4688,7 +4690,7 @@ pub const Pardes = struct { p.reportError(e.pane, "terminal exited; Del retries close", err); }, .lsp_resp => |r| p.lspResponse(r.id, r.rows), - .pipe_resp => |r| selection_pipe.pipeResponse(p, r.id, r.success, r.outputs, r.failure), + .pipe_resp => |r| Pipe.pipeResponse(p, r.id, r.success, r.outputs, r.failure), .file_changed => |fc| _ = p.applyWatchedFileChanged(fc.pane, fc.bytes), .key => |key| { p.can_ask = true; @@ -5386,7 +5388,7 @@ pub const Pardes = struct { const submit = hit(key, config.search_submit); switch (pane.prompt) { .pipe => |pipe| { - if (submit) selection_pipe.submitPipe(p, p.active); + if (submit) Pipe.submitPipe(p, p.active); pane.tag_tail_len = @min(pipe.at, pane.tag_tail_len); }, .save => |at| { @@ -5501,7 +5503,7 @@ pub const Pardes = struct { .pipe => |how| pipe: { const file = pane.file orelse return; if (!panes.Output.fileTraits(file.output).saves) return; - break :pipe selection_pipe.pipeMarker(how); + break :pipe Pipe.pipeMarker(how); }, }; p.seedTail(pane); diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index 90c8e951..e1a42962 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -1,19 +1,10 @@ -//! `|`: pipe every editor selection through one shell command. First the -//! native runner and its boundary-owned values: native shells call `Job.copy` -//! before starting a worker, then `runOne` on that worker, so no subprocess or -//! borrowed core memory reaches the editor/event-loop thread. Then the editor -//! side: the prompt, the request the core emits, and the atomic edit its -//! answer makes. +//! Native runner and boundary-owned values for `|`: pipe every editor +//! selection through one shell command. The core imports only the plain value +//! types below; native shells call `Job.copy` before starting a worker, then +//! `runOne` on that worker. No subprocess or borrowed core memory reaches the +//! editor/event-loop thread. const std = @import("std"); const filesystem = @import("fs.zig"); -const pardes = @import("pardes.zig"); -const edit = @import("edit.zig"); -const modal = @import("modal.zig"); -const panes = @import("panes.zig"); -const config = @import("config.zig"); -const Pane = panes.Pane; -const Key = pardes.Key; -const Pardes = pardes.Pardes; /// A deliberately finite answer. One selection cannot retain more than 1 MiB /// and a multi-selection command cannot retain more than 4 MiB in total. @@ -401,526 +392,3 @@ test "native pipe runner preserves stdin/stdout bytes and reports how it failed" .failed => return error.EarlyStdinCloseShouldNotFail, } } - -// ---- the pipe inside the editor: the prompt, the request, the atomic edit ---- - -fn nextPipeEffect(p: *Pardes) ?u32 { - while (p.nextEffect()) |effect| switch (effect) { - .pipe => |request| return request.id, - else => {}, - }; - return null; -} - -/// The tag-tail marker each behaviour arms with. One function so the prompt -/// that is DRAWN and the command that is PARSED can never disagree. -pub fn pipeMarker(how: modal.Normal.PipeBehavior) []const u8 { - return switch (how) { - .replace => config.pipe_marker, - .ignore => config.pipe_marker_to, - .insert => config.pipe_marker_insert, - .append => config.pipe_marker_append, - }; -} - -pub const PendingPipe = struct { - id: u32, - pane: usize, - serial: u32, - revision: u32, - command: []u8, - cwd: []u8, - inputs: []Input, - ranges: [Pane.max_selections]modal.Selection, - primary: u8, - explicit: bool, - how: modal.Normal.PipeBehavior, - nranges: u8, - - pub fn deinit(wait: *PendingPipe, gpa: std.mem.Allocator) void { - gpa.free(wait.command); - gpa.free(wait.cwd); - for (wait.inputs) |input| gpa.free(@constCast(input.bytes)); - gpa.free(wait.inputs); - wait.* = undefined; - } - - fn request(wait: *const PendingPipe) Request { - return .{ - .id = wait.id, - .command = wait.command, - .cwd = wait.cwd, - .inputs = wait.inputs, - }; - } -}; - -pub fn submitPipe(p: *Pardes, id: usize) void { - const pane = p.panes[id] orelse return; - const f = pane.file orelse return; - if (!panes.Output.fileTraits(f.output).saves) return; - const prompt = switch (pane.prompt) { - .pipe => |pipe| pipe, - else => return, - }; - const tail = pane.tagSlice(); - const armed = tail[@min(prompt.at, tail.len)..]; - const marker = pipeMarker(prompt.how); - if (!std.mem.startsWith(u8, armed, marker)) return; - const command = armed[marker.len..]; - if (command.len == 0) return; - - var ranges: [Pane.max_selections]modal.Selection = undefined; - const got = pane.ranges(f.content, 0, &ranges); - // `!`/`A-!` take no stdin and run ONCE — see `PendingPipe.nranges`. - const ninputs = if (prompt.how.pipes()) got.n else 1; - const inputs = p.gpa.alloc(Input, ninputs) catch return; - var made: usize = 0; - for (ranges[0..ninputs], 0..) |range, i| { - const bytes = if (prompt.how.pipes()) bytes: { - const lo = @min(range.anchor, range.head); - const hi = @max(range.anchor, range.head); - if (hi > f.content.len) break; - break :bytes f.content[lo..hi]; - } else ""; - const copy = p.gpa.dupe(u8, bytes) catch break; - inputs[i] = .{ .bytes = copy }; - made += 1; - } - if (made != ninputs) { - for (inputs[0..made]) |input| p.gpa.free(@constCast(input.bytes)); - p.gpa.free(inputs); - return; - } - const command_copy = p.gpa.dupe(u8, command) catch { - for (inputs) |input| p.gpa.free(@constCast(input.bytes)); - p.gpa.free(inputs); - return; - }; - const cwd_copy = p.gpa.dupe(u8, Pardes.paneDir(pane)) catch { - p.gpa.free(command_copy); - for (inputs) |input| p.gpa.free(@constCast(input.bytes)); - p.gpa.free(inputs); - return; - }; - - p.pipe_seq +%= 1; - if (p.pipe_wait) |*old| old.deinit(p.gpa); - p.pipe_wait = .{ - .id = p.pipe_seq, - .pane = id, - .serial = pane.serial, - .revision = f.revision, - .command = command_copy, - .cwd = cwd_copy, - .inputs = inputs, - .ranges = ranges, - .primary = @intCast(got.pri), - .explicit = (pane.vsel.active and pane.vsel.explicit) or pane.msel.active, - .how = prompt.how, - .nranges = @intCast(got.n), - }; - p.emit(.{ .pipe = .{ .id = p.pipe_seq } }); -} - -/// Frontends call this while draining a .pipe effect, then immediately -/// copy the borrowed view into a worker-owned selection_pipe.Job. -pub fn pipeRequest(p: *const Pardes, id: u32) ?Request { - if (p.pipe_wait) |*wait| { - if (wait.id == id) return wait.request(); - } - return null; -} - -fn pipeFailed(p: *Pardes, wait: *const PendingPipe, failure: ?Failure) void { - var out: std.Io.Writer.Allocating = .init(p.gpa); - defer out.deinit(); - const w = &out.writer; - w.print("| {s}\n\n", .{wait.command}) catch return; - if (failure) |fail| { - if (wait.inputs.len > 1) - w.print("selection {d} of {d}: ", .{ fail.index + 1, wait.inputs.len }) catch return; - switch (fail.kind) { - .exit => w.print("exit status {d}\n", .{fail.code}) catch return, - .signal => w.writeAll("killed by a signal\n") catch return, - .timeout => w.print( - "still running after {d} seconds, and stopped\n", - .{command_timeout_seconds}, - ) catch return, - .too_large => w.writeAll("produced more output than a filter may return\n") catch return, - .spawn => w.writeAll("could not be started\n") catch return, - .io => w.writeAll("could not be read\n") catch return, - } - if (fail.stderr.len > 0) w.print("\n{s}", .{fail.stderr}) catch return; - } else { - // No diagnosis at all: nobody ran it. The detached daemon, the - // browser and the board all leave `pipe` null on purpose. - w.writeAll("this session cannot run filters\n") catch return; - } - const content = out.toOwnedSlice() catch return; - const was = p.active; - panes.Output.openErrors(p, wait.pane, content) catch |err| { - p.gpa.free(content); - // Nowhere to put the report is itself worth one line. - p.reportError(wait.pane, "pipe", err); - return; - }; - if (p.panes[was] != null) p.active = was; -} - -fn pipeCut(wait: *const PendingPipe, content: []const u8, i: usize) ?struct { lo: usize, hi: usize } { - const range = wait.ranges[i]; - const lo = @min(range.anchor, range.head); - const hi = @max(range.anchor, range.head); - if (hi > content.len) return null; - return switch (wait.how) { - .replace, .ignore => .{ .lo = lo, .hi = hi }, - .insert => .{ .lo = lo, .hi = lo }, - .append => .{ .lo = hi, .hi = hi }, - }; -} - -fn pipeOutput( - wait: *const PendingPipe, - outputs: []const []const u8, - content: []const u8, - i: usize, -) []const u8 { - var out = if (wait.how.pipes()) outputs[i] else outputs[0]; - const range = wait.ranges[i]; - const lo = @min(range.anchor, range.head); - const hi = @max(range.anchor, range.head); - const selected = if (hi <= content.len) content[lo..hi] else ""; - if (!std.mem.endsWith(u8, selected, "\n") and std.mem.endsWith(u8, out, "\n")) { - out = out[0 .. out.len - 1]; - if (std.mem.endsWith(u8, out, "\r")) out = out[0 .. out.len - 1]; - } - return out; -} - -pub fn pipeResponse( - p: *Pardes, - id: u32, - success: bool, - outputs: []const []const u8, - failure: ?Failure, -) void { - // A SUPERSEDED OR UNKNOWN id is the one silence worth keeping: it is - // the answer to a question nobody is still asking. - if (p.pipe_wait == null or p.pipe_wait.?.id != id) return; - var wait = p.pipe_wait.?; - p.pipe_wait = null; - defer wait.deinit(p.gpa); - - if (!success or outputs.len != wait.inputs.len) return pipeFailed(p, &wait, failure); - - const pane = p.panes[wait.pane] orelse return; - if (pane.serial != wait.serial) return; - const f = if (pane.file) |*file| file else return; - if (!panes.Output.fileTraits(f.output).saves) return; - if (f.revision != wait.revision) - return p.reportError(wait.pane, "pipe", error.FileChangedWhileFiltering); - - if (wait.how == .ignore) return; - - const n = wait.nranges; - var total_output: usize = 0; - var removed: usize = 0; - var previous_end: usize = 0; - for (0..n) |i| { - const cut = pipeCut(&wait, f.content, i) orelse return; - const output = pipeOutput(&wait, outputs, f.content, i); - if (output.len > max_stdout_bytes) return; - total_output = std.math.add(usize, total_output, output.len) catch return; - if (total_output > max_total_stdout_bytes) return; - if (cut.lo < previous_end) return; - removed = std.math.add(usize, removed, cut.hi - cut.lo) catch return; - previous_end = cut.hi; - } - const kept = f.content.len - removed; - const final_len = std.math.add(usize, kept, total_output) catch return; - const replacement = p.gpa.alloc(u8, final_len) catch return; - - var new_ranges: [Pane.max_selections]modal.Selection = undefined; - var read_at: usize = 0; - var write_at: usize = 0; - for (0..n) |i| { - const range = wait.ranges[i]; - const cut = pipeCut(&wait, f.content, i).?; - const output = pipeOutput(&wait, outputs, f.content, i); - @memcpy(replacement[write_at .. write_at + (cut.lo - read_at)], f.content[read_at..cut.lo]); - write_at += cut.lo - read_at; - const out_start = write_at; - @memcpy(replacement[write_at .. write_at + output.len], output); - write_at += output.len; - const out_end = write_at; - new_ranges[i] = if (range.anchor > range.head) - .{ .anchor = out_end, .head = out_start } - else - .{ .anchor = out_start, .head = out_end }; - read_at = cut.hi; - } - @memcpy(replacement[write_at..], f.content[read_at..]); - - // One async request is one history transaction, even at 64 cursors. - edit.pushUndo(p, pane); - panes.File.setContent(p, f, replacement); - pane.setRanges(f.content, new_ranges[0..n], &.{}, wait.primary, wait.explicit); - pane.select = false; - pane.ensureCursorVisible(); -} - -test "selection pipe prompt submits exact request and Escape cancels" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = try p.setTestFile("one\ntwo\n"); - pane.cur_row = 0; - pane.cur_col = 2; - pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - - p.update(.{ .key = .{ .cp = '|' } }); - try std.testing.expect(pane.tag_edit and pane.prompt == .pipe); - try std.testing.expect(std.mem.endsWith(u8, pane.tagSlice(), config.pipe_marker)); - try std.testing.expect(nextPipeEffect(p) == null); - p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - try std.testing.expect(!pane.tag_edit and pane.prompt != .pipe); - const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; - const request = pipeRequest(p, id) orelse return error.MissingPipeRequest; - try std.testing.expectEqualStrings("tr a-z A-Z", request.command); - try std.testing.expectEqualStrings("/", request.cwd); - try std.testing.expectEqual(@as(usize, 1), request.inputs.len); - try std.testing.expectEqualSlices(u8, "one", request.inputs[0].bytes); - - const before = pane.file.?.content; - p.update(.{ .key = .{ .cp = '|' } }); - p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(!pane.tag_edit and pane.prompt != .pipe); - try std.testing.expectEqualSlices(u8, before, pane.file.?.content); - try std.testing.expect(nextPipeEffect(p) == null); -} - -test "selection pipe replaces all ranges atomically and undo restores them" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = try p.setTestFile("aa bb cc\n"); - const ranges = [_]modal.Selection{ - .{ .anchor = 0, .head = 2 }, - .{ .anchor = 6, .head = 8 }, - }; - pane.setRanges(pane.file.?.content, &ranges, &.{}, 0, true); - - p.update(.{ .key = .{ .cp = '|' } }); - p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; - const request = pipeRequest(p, id) orelse return error.MissingPipeRequest; - try std.testing.expectEqual(@as(usize, 2), request.inputs.len); - try std.testing.expectEqualSlices(u8, "aa", request.inputs[0].bytes); - try std.testing.expectEqualSlices(u8, "cc", request.inputs[1].bytes); - - var denied = std.testing.FailingAllocator.init(gpa, .{ .fail_index = 0 }); - p.scratch.deinit(); - p.scratch = .init(denied.allocator()); - const outputs: []const []const u8 = &.{ "AA\n", "" }; - p.update(.{ .pipe_resp = .{ .id = id, .success = true, .outputs = outputs } }); - try std.testing.expectEqualSlices(u8, "AA bb \n", pane.file.?.content); - try std.testing.expectEqual(@as(usize, 1), pane.file.?.history.undo_len); - try std.testing.expectEqual(@as(u8, 1), pane.nsel); - try std.testing.expectEqual(@as(i32, 1), pane.cur_col); - try std.testing.expectEqual(@as(i32, 6), pane.sels[0].col); - try std.testing.expect(!denied.has_induced_failure); - - p.update(.{ .key = .{ .cp = 'u' } }); - try std.testing.expectEqualSlices(u8, "aa bb cc\n", pane.file.?.content); -} - -test "the four shell behaviours put their output where helix puts it" { - const gpa = std.testing.allocator; - const Case = struct { - key: Key, - outputs: []const []const u8, - want: []const u8, - want_effect: bool = true, - }; - // One selection, `bb`, in `aa bb cc\n` — bytes 3..5. - for ([_]Case{ - // `|` replaces the selection with what the command wrote. - .{ .key = .{ .cp = '|' }, .outputs = &.{"XX"}, .want = "aa XX cc\n" }, - // `!` inserts BEFORE it, and the selection survives to its right. - .{ .key = .{ .cp = '!' }, .outputs = &.{"XX"}, .want = "aa XXbb cc\n" }, - // `A-!` appends AFTER it. - .{ .key = .{ .cp = '!', .alt = true }, .outputs = &.{"XX"}, .want = "aa bbXX cc\n" }, - // `A-|` runs the command and leaves the text completely alone: the - // point is the side effect, not the output. - .{ .key = .{ .cp = '|', .alt = true }, .outputs = &.{"XX"}, .want = "aa bb cc\n" }, - }) |case| { - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = try p.setTestFile("aa bb cc\n"); - pane.setRanges(pane.file.?.content, &.{.{ .anchor = 3, .head = 5 }}, &.{}, 0, true); - - p.update(.{ .key = case.key }); - for ("cmd") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; - p.update(.{ .pipe_resp = .{ .id = id, .success = true, .outputs = case.outputs } }); - try std.testing.expectEqualSlices(u8, case.want, pane.file.?.content); - } -} - -test "a command with no stdin runs once and every cursor gets that one answer" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = try p.setTestFile("aa bb cc\n"); - const ranges = [_]modal.Selection{ - .{ .anchor = 0, .head = 2 }, - .{ .anchor = 6, .head = 8 }, - }; - pane.setRanges(pane.file.?.content, &ranges, &.{}, 0, true); - - p.update(.{ .key = .{ .cp = '!' } }); // insert-output: no stdin - for ("date") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; - const request = pipeRequest(p, id) orelse return error.MissingPipeRequest; - - try std.testing.expectEqual(@as(usize, 1), request.inputs.len); - try std.testing.expectEqualSlices(u8, "", request.inputs[0].bytes); - - p.update(.{ .pipe_resp = .{ .id = id, .success = true, .outputs = &.{"T"} } }); - try std.testing.expectEqualSlices(u8, "Taa bb Tcc\n", pane.file.?.content); - try std.testing.expectEqual(@as(usize, 1), pane.file.?.history.undo_len); -} - -test "selection pipe failure and stale completion never mutate the file" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = try p.setTestFile("abc\n"); - pane.cur_col = 2; - pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - - p.update(.{ .key = .{ .cp = '|' } }); - p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - const failed_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; - p.update(.{ .pipe_resp = .{ .id = failed_id, .success = false, .outputs = &.{} } }); - try std.testing.expectEqualSlices(u8, "abc\n", pane.file.?.content); - try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); - - p.update(.{ .key = .{ .cp = '|' } }); - p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - const stale_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; - panes.File.setContent(p, &pane.file.?, try gpa.dupe(u8, "changed\n")); - p.update(.{ .pipe_resp = .{ .id = stale_id, .success = true, .outputs = &.{"ABC"} } }); - try std.testing.expectEqualSlices(u8, "changed\n", pane.file.?.content); - try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); -} - -test "a failed filter opens an errors buffer carrying the command's own words" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = try p.setTestFile("abc\n"); - pane.cur_col = 2; - pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - - p.update(.{ .key = .{ .cp = '|' } }); - for ("trr") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - const id = nextPipeEffect(p) orelse return error.MissingPipeEffect; - - // The shape a real runner brings home for `| trr`: nonzero exit, and the - // shell's own sentence about it. - var stderr = "sh: line 1: trr: command not found\n".*; - p.update(.{ .pipe_resp = .{ - .id = id, - .success = false, - .outputs = &.{}, - .failure = .{ .kind = .exit, .code = 127, .stderr = &stderr }, - } }); - - // The text is untouched — a failed filter is not an edit... - try std.testing.expectEqualSlices(u8, "abc\n", pane.file.?.content); - try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); - // ...and the cursor did not go anywhere, so `|` again edits the same file. - try std.testing.expectEqual(@as(usize, 0), p.active); - - var found: ?[]const u8 = null; - for (p.panes) |slot| { - const q = slot orelse continue; - const qf = q.file orelse continue; - const o = qf.output orelse continue; - if (std.meta.activeTag(o.from) == .errors) found = qf.content; - } - const report = found orelse return error.NoErrorsBuffer; - try std.testing.expect(std.mem.indexOf(u8, report, "| trr") != null); - try std.testing.expect(std.mem.indexOf(u8, report, "exit status 127") != null); - try std.testing.expect(std.mem.indexOf(u8, report, "command not found") != null); -} - -test "selection pipe rejects a reused pane slot and a superseded request" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - var pane = try p.setTestFile("old\n"); - pane.cur_col = 2; - pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - - p.update(.{ .key = .{ .cp = '|' } }); - p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - const replaced_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; - pane = try p.setTestFile("new\n"); // same slot, different serial - p.update(.{ .pipe_resp = .{ .id = replaced_id, .success = true, .outputs = &.{"OLD"} } }); - try std.testing.expectEqualSlices(u8, "new\n", pane.file.?.content); - try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); - - pane.cur_col = 2; - pane.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - p.update(.{ .key = .{ .cp = '|' } }); - p.update(.{ .key = .{ .cp = 'c', .text = "cat" } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - const first_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; - p.update(.{ .key = .{ .cp = '|' } }); - p.update(.{ .key = .{ .cp = 'c', .text = "tr a-z A-Z" } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - const latest_id = nextPipeEffect(p) orelse return error.MissingPipeEffect; - try std.testing.expect(first_id != latest_id); - - p.update(.{ .pipe_resp = .{ .id = first_id, .success = true, .outputs = &.{"BAD"} } }); - try std.testing.expectEqualSlices(u8, "new\n", pane.file.?.content); - p.update(.{ .pipe_resp = .{ .id = latest_id, .success = true, .outputs = &.{"NEW"} } }); - try std.testing.expectEqualSlices(u8, "NEW\n", pane.file.?.content); - try std.testing.expectEqual(@as(usize, 1), pane.file.?.history.undo_len); -} - -test "selection pipe binding is file-normal-only" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = try p.setTestFile("x"); - - pane.file.?.output = .{ .from = .search }; - p.update(.{ .key = .{ .cp = '|' } }); - try std.testing.expect(pane.prompt != .pipe); - pane.file.?.output = null; - pane.mode = .insert; - p.update(.{ .key = .{ .cp = '|', .text = "|" } }); - try std.testing.expect(pane.prompt != .pipe); - try std.testing.expectEqualSlices(u8, "|x", pane.file.?.content); -} diff --git a/src/tty/tty.zig b/src/tty/tty.zig index fe968650..24403ba8 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -11,6 +11,7 @@ const look = @import("../look.zig"); const message = pardes.Messages.Message; const file_watch = @import("../file_watch.zig"); const selection_pipe = @import("../selection_pipe.zig"); +const Pipe = @import("../Pipe.zig"); const ninep_io = @import("../9p_io.zig"); const panel_compositor = @import("panel_compositor.zig"); const detached_client = @import("../detached/client.zig"); @@ -1325,7 +1326,7 @@ const Shell = struct { s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); return; } - const view = selection_pipe.pipeRequest(s.core, id) orelse return; + const view = Pipe.pipeRequest(s.core, id) orelse return; const job = selection_pipe.Job.copy(s.gpa, view) catch |err| { s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); return s.core.reportError(s.core.active, "pipe", err); -- cgit v1.3 From 164da9fc0a7ab3c7ed39ea0a2dd6bbff3c36cdaf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 29 Sep 2026 13:39:12 -0300 Subject: Every child starts with its signals at their defaults and none blocked Shells, command panes, language servers, the link opener and the v9fs mount inherited the editor's signal mask (the tty's blocked SIGWINCH) and anything it ignored (a SIGHUP nohup ignored), since only handlers reset at exec. Each fork now resets every disposition and clears the mask before its exec (resetChildSignals); std's spawn for selection pipes runs with the mask cleared across it. Co-Authored-By: Claude Opus 5.5 --- src/host_io.zig | 43 ++++++++++++++++++++++++++++++++++++++++--- src/linux/v9fs.zig | 10 ++++++++++ src/look.zig | 1 + src/lsp/lsp_client.zig | 1 + src/selection_pipe.zig | 8 ++++++++ 5 files changed, 60 insertions(+), 3 deletions(-) (limited to 'src/selection_pipe.zig') diff --git a/src/host_io.zig b/src/host_io.zig index 9d4b27c2..bbd664a9 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -1075,6 +1075,45 @@ pub const Shell = struct { } }; +/// In a child between fork and exec: every signal back to its default and +/// none blocked, whatever this process ignores or blocks (the tty's SIGWINCH, +/// a SIGHUP nohup ignored). A handler resets at exec by itself; an ignored +/// signal and the mask do not. Async-signal-safe: sigaction and sigprocmask. +pub fn resetChildSignals() void { + const default: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.DFL }, .mask = posix.sigemptyset(), .flags = 0 }; + var sig: u8 = 1; + while (sig < 65) : (sig += 1) { + if (sig == @intFromEnum(posix.SIG.KILL) or sig == @intFromEnum(posix.SIG.STOP)) continue; + _ = std.c.sigaction(@enumFromInt(sig), &default, null); + } + const none = posix.sigemptyset(); + posix.sigprocmask(posix.SIG.SETMASK, &none, null); +} + +test "a child starts with every signal at its default and none blocked" { + var blocked = posix.sigemptyset(); + posix.sigaddset(&blocked, posix.SIG.WINCH); + posix.sigprocmask(posix.SIG.BLOCK, &blocked, null); + defer posix.sigprocmask(posix.SIG.UNBLOCK, &blocked, null); + const ignore: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.IGN }, .mask = posix.sigemptyset(), .flags = 0 }; + var was: posix.Sigaction = undefined; + posix.sigaction(posix.SIG.HUP, &ignore, &was); + defer posix.sigaction(posix.SIG.HUP, &was, null); + const pid = libc.fork(); + if (pid == 0) { + resetChildSignals(); + var now: posix.Sigaction = undefined; + posix.sigaction(posix.SIG.HUP, null, &now); + var mask = posix.sigemptyset(); + posix.sigprocmask(posix.SIG.BLOCK, null, &mask); + const ok = now.handler.handler == posix.SIG.DFL and !posix.sigismember(&mask, posix.SIG.WINCH); + libc._exit(if (ok) 0 else 1); + } + var status: c_int = 0; + _ = libc.waitpid(pid, &status, 0); + try std.testing.expectEqual(@as(c_int, 0), status); +} + extern "c" fn forkpty(amaster: *c_int, name: ?[*:0]u8, termp: ?*const anyopaque, winp: ?*const posix.winsize) c_int; extern "c" fn execv(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; extern "c" fn execve(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8) c_int; @@ -1278,9 +1317,7 @@ pub fn forkShell( return error.ForkFailed; } if (pid == 0) { - var set = posix.sigemptyset(); - posix.sigaddset(&set, posix.SIG.WINCH); - posix.sigprocmask(posix.SIG.UNBLOCK, &set, null); + resetChildSignals(); if (cwd_z) |path| if (chdir(path.ptr) != 0) { const why = [_]u8{ 'c', @truncate(@intFromEnum(libc.errno(@as(c_int, -1)))) }; _ = libc.write(told[1], &why, why.len); diff --git a/src/linux/v9fs.zig b/src/linux/v9fs.zig index 4e18660d..5e031bfe 100644 --- a/src/linux/v9fs.zig +++ b/src/linux/v9fs.zig @@ -118,6 +118,16 @@ fn launch(arena: std.mem.Allocator, args: []const [:0]const u8) !u8 { const pid = fork(); if (pid < 0) return error.ForkFailed; if (pid == 0) { + // host_io's resetChildSignals, here in its own module: every signal + // back to its default and none blocked before the exec. + const default: std.posix.Sigaction = .{ .handler = .{ .handler = std.posix.SIG.DFL }, .mask = std.posix.sigemptyset(), .flags = 0 }; + var sig: u8 = 1; + while (sig < 65) : (sig += 1) { + if (sig == @intFromEnum(std.posix.SIG.KILL) or sig == @intFromEnum(std.posix.SIG.STOP)) continue; + _ = std.c.sigaction(@enumFromInt(sig), &default, null); + } + const none = std.posix.sigemptyset(); + std.posix.sigprocmask(std.posix.SIG.SETMASK, &none, null); _ = execvp("sudo", argv.ptr); _exit(127); } diff --git a/src/look.zig b/src/look.zig index 5cf26240..4e948fad 100644 --- a/src/look.zig +++ b/src/look.zig @@ -49,6 +49,7 @@ pub fn openLink(url: []const u8) void { if (pid < 0) return; if (pid == 0) { if (fork() == 0) { + @import("host_io.zig").resetChildSignals(); const argv: [3:null]?[*:0]const u8 = .{ opener, url_z.ptr, null }; _ = execv(opener, &argv); } diff --git a/src/lsp/lsp_client.zig b/src/lsp/lsp_client.zig index 55df1f91..91ea67d6 100644 --- a/src/lsp/lsp_client.zig +++ b/src/lsp/lsp_client.zig @@ -1148,6 +1148,7 @@ fn ensure(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trac // signal-safe calls, no allocation, no locks. (Same rule as tty.zig's // forkShell.) _ = setsid(); // Ctrl-C in pardes's terminal is not the server's business + @import("../host_io.zig").resetChildSignals(); _ = libc.dup2(sv[1], 0); _ = libc.dup2(sv[1], 1); const devnull = libc.open("/dev/null", .{ .ACCMODE = .WRONLY }); diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index e1a42962..44cc1709 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -202,6 +202,14 @@ pub fn runOne( if (std.mem.indexOfScalar(u8, command, 0) != null or std.mem.indexOfScalar(u8, cwd, 0) != null) return fail.k(.spawn); + // std's spawn runs no code in the child: this thread's mask, which the + // child inherits, is cleared across the fork (only the tty's SIGWINCH is + // ever blocked, and its default is to be ignored). A handler resets at + // exec by itself, and nothing here is ignored. + const none = std.posix.sigemptyset(); + var kept: std.posix.sigset_t = undefined; + std.posix.sigprocmask(std.posix.SIG.SETMASK, &none, &kept); + defer std.posix.sigprocmask(std.posix.SIG.SETMASK, &kept, null); var child = std.process.spawn(io, .{ .argv = &.{ "/bin/sh", "-c", command }, .cwd = if (cwd.len == 0) .inherit else .{ .path = cwd }, -- cgit v1.3 From 805b9f188768fee523d462411d846912e0877ff3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 1 Oct 2026 17:43:39 -0300 Subject: Edit runs acme's file and shell commands: < | > through the session's Shell off the loop (one undo step, a failure changes nothing, its stderr in +Errors, a 9P write answered when done), X and Y over the open text panes, b B D e r f w and the "file" address; checked against sam -d, and the reference's Edit section and Coming from acme row say so Co-Authored-By: Claude Opus 5.5 --- docs/typ/builtins-gui.json | 2 +- docs/typ/builtins-tty.json | 2 +- docs/typ/reference.typ | 43 ++- src/9p_io.zig | 7 + src/Pipe.zig | 63 ++-- src/builtins.zig | 35 +-- src/edit_cmd.zig | 487 +++++++++++++++++++++++++++++++ src/fs.zig | 3 + src/ninep/ctl.zig | 7 + src/pardes.zig | 50 +++- src/sam_edit.zig | 705 ++++++++++++++++++++++++++++++++++++++------- src/selection_pipe.zig | 90 +++++- test/fs.py | 59 ++++ 13 files changed, 1377 insertions(+), 176 deletions(-) create mode 100644 src/edit_cmd.zig (limited to 'src/selection_pipe.zig') diff --git a/docs/typ/builtins-gui.json b/docs/typ/builtins-gui.json index 95eeaa07..b42f9c72 100644 --- a/docs/typ/builtins-gui.json +++ b/docs/typ/builtins-gui.json @@ -32,7 +32,7 @@ {"name": "DumpConfig", "leader": null, "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Open the settings in effect, each a word to click, and the startup file's path."}, {"name": "DumpDir", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": null, "doc": "The directory Dump writes to and a relative Restore reads from; bare, the default."}, {"name": "DumpThemes", "leader": null, "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Write the bundled themes into the config directory's themes, to edit."}, - {"name": "Edit", "leader": null, "also": [], "arg": true, "scope": "both", "setting": false, "choices": null, "doc": "acme's Edit: sam's command language over the pane's body, from a tag, a pane ctl or exec."}, + {"name": "Edit", "leader": null, "also": [], "arg": true, "scope": "both", "setting": false, "choices": null, "doc": "acme's Edit: sam's command language over the open text panes, from a tag, a pane ctl or exec, starting in the pane's body."}, {"name": "EffectCode", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "Open the source of a panel transition or post shader, by name."}, {"name": "Exec", "leader": null, "also": ["tab", "middle-click"], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Execute its argument as a middle click on it would, a builtin or a shell command."}, {"name": "Exit", "leader": "SPC q", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Quit the editor, refusing once while a pane holds unsaved text: an Exit with nothing edited since discards it."}, diff --git a/docs/typ/builtins-tty.json b/docs/typ/builtins-tty.json index 887393ac..53bcf033 100644 --- a/docs/typ/builtins-tty.json +++ b/docs/typ/builtins-tty.json @@ -29,7 +29,7 @@ {"name": "DumpConfig", "leader": null, "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Open the settings in effect, each a word to click, and the startup file's path."}, {"name": "DumpDir", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": null, "doc": "The directory Dump writes to and a relative Restore reads from; bare, the default."}, {"name": "DumpThemes", "leader": null, "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Write the bundled themes into the config directory's themes, to edit."}, - {"name": "Edit", "leader": null, "also": [], "arg": true, "scope": "both", "setting": false, "choices": null, "doc": "acme's Edit: sam's command language over the pane's body, from a tag, a pane ctl or exec."}, + {"name": "Edit", "leader": null, "also": [], "arg": true, "scope": "both", "setting": false, "choices": null, "doc": "acme's Edit: sam's command language over the open text panes, from a tag, a pane ctl or exec, starting in the pane's body."}, {"name": "EffectCode", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "Open the source of a panel transition or post shader, by name."}, {"name": "Exec", "leader": null, "also": ["tab", "middle-click"], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Execute its argument as a middle click on it would, a builtin or a shell command."}, {"name": "Exit", "leader": "SPC q", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Quit the editor, refusing once while a pane holds unsaved text: an Exit with nothing edited since discards it."}, diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index 5bcd8f6f..321860d6 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -98,8 +98,8 @@ own), never a C library string; a bad setting value is quoted, the value itself (`bad value in control message; takes on, off "maybe"`); a ctl refusal quotes the offending word alone (`wrong #args in control message "Newcol"`). Through 9ns the kernel sees an errno 9ns reads from those words (cloud9's `fs.enameErrno`): the first of these that the -words hold, case aside, wins, and anything else is EIO (`Modified`, an -#word("Edit") command pardes leaves out): +words hold, case aside, wins, and anything else is EIO (`Modified`, a +sam command pardes's #word("Edit") leaves out, `B #word("Edit") `` on a pane's #file("ctl") or #file("exec") (or the -root's, at the pane with the keyboard) runs acme's #word("Edit") on the body: addresses as -above, commands `x y g v c a i d s p = m t u` and `{ }`. All changes are -one undo step, applied only if every command succeeds; a failure changes -nothing and fails the write with acme's words (`Edit: no substitution`). -An `x` that finds nothing succeeds silently. `p` and `=` print to the -directory's `+Errors`. Not there: `b B D e r w f X Y`, `< | >`, -`\1`-`\9`. In `s`, `&` is the match (`\&` a literal); in `c`, `a`, `i` it -is a literal. `y` yields the stretch before the first match too. Braces -take a command a line, so a block goes on one open, in one write or +root's, at the pane with the keyboard) runs acme's #word("Edit") over the open text +panes, from that pane's body. Addresses are as above, and `"re"` is the one +open file whose line matches. Commands: + +- `x y g v c a i d s p = m t u` and `{ }`. +- `X`/`Y` run a command in each open file whose line (` +. /path`, `'` when + edited) matches, or does not: `X/'/w` writes every edited file. +- `b` makes a file current, `B` opens files, `D` closes panes (an edited one + on the second asking, as #word("Del")). +- `e` reads a file over the whole text and names the pane after it, `r` + over dot. `w` writes all of it, or the address, to its file or a name; + all of it to its own name leaves it clean. `f` names the pane and prints + its line. +- `cmd` sends dot to it and prints what it writes. + +Each command runs in its file's directory through #word("Shell"), off the +loop, so the editor goes on; the write that ran the Edit is answered when +they are done, and a line after it in the same write is refused. A command +that fails (an exit status, 10 seconds, 1 MiB of output) fails the Edit, its +stderr in `+Errors`. Each file's changes are one undo step, applied only if +every command succeeds; a failure changes nothing and fails the write with +acme's words (`Edit: no substitution`), as does a file edited while the +commands ran. An `x` that finds nothing succeeds silently. `p`, `=` and `>` +print to the directory's `+Errors`. Not there: `B /\\n}\\n' > $p/ctl") @@ -1040,7 +1059,7 @@ with Client(sys.argv[1]) as c: # a socket path, or (ip, port) [`Snarf`, `Cut`, `Paste`], [refused: the chords, #key("y"), #key("p")], [`Font`, `Send`, `ID`], [#word("Font"); #word("Repl") and #word("Exec"); `$winid`], [acme's other builtins], [refused: `Sort`, `Local`, `Abort` (plan9port's debugging word)], - [Edit], [no `< | >`, `X`, `Y`, `b B D e r w f`, no `\1`-`\9`; an alternation takes the first branch that matches, not the longest (#doc("fs", section: "edit"))], + [Edit], [every acme command but `B ` command changes nothing, where acme puts in what it wrote (#doc("fs", section: "edit"))], [`win`], [#word("Tty") is a VT terminal pane; #file("pty/run") runs a line at its prompt and answers `exit N` and the output; with #word("Repl") bound, #word("Exec") on a source pane types the text into it], [a directory window], [as acme's: a text pane named `dir/` (#file("index") kind `text`, #file("ctl") isdir 1), its entries in columns sorted bytewise, dotfiles shown, a directory's marked `/`; blanks pad the columns where acme's tabs do; a #word("Look") at an entry is from that directory, `Get` or a look at it again reads it again, and an edit is kept until then rather than lost to a resize; never dirty, #word("Save") refuses it; #word("DirLook") `terminal` types `ls` into a terminal there instead], [the plumber], [none: #word("Look") on an `http://` or `https://` URL runs `xdg-open` (`open` on macOS); files, addresses and directories follow built-in rules], diff --git a/src/9p_io.zig b/src/9p_io.zig index 68f6f6b7..671a1852 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -268,6 +268,9 @@ pub const Listener = struct { const changes = pardes.ctlfs.changesPane(core, req); const fills = pardes.ctlfs.handsOff(core, req); const reply = core.serveFs(req); + // Taken now: a request served while this one waits starts afresh. + const edit_at = core.fs.edit_answer_at; + core.fs.edit_answer_at = null; if (req.op == .release and quiet) l.collectOs(); // A close's held last line runs now when no step is out, so its err // is logged before the close is answered, as before; with a step @@ -346,6 +349,10 @@ pub const Listener = struct { } }; } + // An Edit whose `<`, `|` or `>` commands run off the loop: the write + // is answered once they have and the Edit is applied, or failed + // (late_failure), as `echo Edit , | sort > ctl` must return sorted. + if (edit_at) |n| if (l.core == core) pardes.turn.awaitEdit(n); core.fs.write_waits = false; // ponytail: one slot, so a failure of another client's effects that // settle in the same wait is told to this write too. diff --git a/src/Pipe.zig b/src/Pipe.zig index 48e98b7b..6981c023 100644 --- a/src/Pipe.zig +++ b/src/Pipe.zig @@ -8,6 +8,7 @@ const panes = @import("panes.zig"); const config = @import("config.zig"); const selection_pipe = @import("selection_pipe.zig"); const edit = @import("edit.zig"); +const edit_cmd = @import("edit_cmd.zig"); const Pane = panes.Pane; const Text = panes.Text; const Key = pardes.Key; @@ -18,6 +19,21 @@ const Pipe = @This(); seq: u32 = 0, /// The request in flight, and what it needs to apply its answer. wait: ?PendingPipe = null, +/// An Edit waiting on its `<`, `|` and `>` commands, which share the ids. +edit_run: ?*edit_cmd.Pending = null, +/// What an Edit's `w` took, for the `edit_write` effect to write. +writes: std.ArrayList(edit_cmd.Write) = .empty, + +pub fn deinit(pipe: *Pipe, gpa: std.mem.Allocator) void { + if (pipe.wait) |*wait| wait.deinit(gpa); + if (pipe.edit_run) |pending| { + pending.deinit(gpa); + // A 9P write waiting on it is answered (the core is going). + pardes.turn.editAnswered(); + } + for (pipe.writes.items) |*w| w.deinit(gpa); + pipe.writes.deinit(gpa); +} fn nextPipeEffect(p: *Pardes) ?u32 { while (p.nextEffect()) |effect| switch (effect) { @@ -164,6 +180,7 @@ pub fn pipeRequest(pipe: *const Pipe, id: u32) ?selection_pipe.Request { if (pipe.wait) |*wait| { if (wait.id == id) return wait.request(); } + if (pipe.edit_run) |pending| if (pending.id == id) return pending.request; return null; } @@ -172,26 +189,9 @@ fn pipeFailed(p: *Pardes, wait: *const PendingPipe, failure: ?selection_pipe.Fai defer out.deinit(); const w = &out.writer; w.print("| {s}\n\n", .{wait.command}) catch return; - if (failure) |fail| { - if (wait.inputs.len > 1) - w.print("selection {d} of {d}: ", .{ fail.index + 1, wait.inputs.len }) catch return; - switch (fail.kind) { - .exit => w.print("exit status {d}\n", .{fail.code}) catch return, - .signal => w.writeAll("killed by a signal\n") catch return, - .timeout => w.print( - "still running after {d} seconds, and stopped\n", - .{selection_pipe.command_timeout_seconds}, - ) catch return, - .too_large => w.writeAll("produced more output than a filter may return\n") catch return, - .spawn => w.writeAll("could not be started\n") catch return, - .io => w.writeAll("could not be read\n") catch return, - } - if (fail.stderr.len > 0) w.print("\n{s}", .{fail.stderr}) catch return; - } else { - // No diagnosis at all: nobody ran it. The detached daemon, the - // browser and the board all leave `pipe` null on purpose. - w.writeAll("this session cannot run filters\n") catch return; - } + if (wait.inputs.len > 1) if (failure) |fail| + w.print("selection {d} of {d}: ", .{ fail.index + 1, wait.inputs.len }) catch return; + writeFailure(w, failure) catch return; const content = out.toOwnedSlice() catch return; const was = p.active; panes.Output.openErrors(p, wait.pane, content) catch |err| { @@ -203,6 +203,28 @@ fn pipeFailed(p: *Pardes, wait: *const PendingPipe, failure: ?selection_pipe.Fai if (p.panes[was] != null) p.active = was; } +/// How a filter failed, a line, and the command's stderr after it: what +/// +Errors says of a `|` and of an Edit's `< | >`. +pub fn writeFailure(w: *std.Io.Writer, failure: ?selection_pipe.Failure) !void { + const fail = failure orelse { + // No diagnosis at all: nobody ran it. The detached daemon, the + // browser and the board all leave `pipe` null on purpose. + return w.writeAll("this session cannot run filters\n"); + }; + switch (fail.kind) { + .exit => try w.print("exit status {d}\n", .{fail.code}), + .signal => try w.writeAll("killed by a signal\n"), + .timeout => try w.print( + "still running after {d} seconds, and stopped\n", + .{selection_pipe.command_timeout_seconds}, + ), + .too_large => try w.writeAll("produced more output than a filter may return\n"), + .spawn => try w.writeAll("could not be started\n"), + .io => try w.writeAll("could not be read\n"), + } + if (fail.stderr.len > 0) try w.print("\n{s}", .{fail.stderr}); +} + fn pipeCut(wait: *const PendingPipe, content: []const u8, i: usize) ?struct { lo: usize, hi: usize } { const range = wait.ranges[i]; const lo = @min(range.anchor, range.head); @@ -240,6 +262,7 @@ pub fn pipeResponse( outputs: []const []const u8, failure: ?selection_pipe.Failure, ) void { + if (p.pipe.edit_run) |pending| if (pending.id == id) return edit_cmd.finish(p, success, outputs, failure); // A SUPERSEDED OR UNKNOWN id is the one silence worth keeping: it is // the answer to a question nobody is still asking. if (p.pipe.wait == null or p.pipe.wait.?.id != id) return; diff --git a/src/builtins.zig b/src/builtins.zig index da87ba37..ed6ac467 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -555,9 +555,9 @@ fn warnModified(c: Ctx, asking: Pane.Discarding) bool { /// Which panes a refusal asks about: every one (Exit, Restore), one (Del), /// or a column's (Delcol). -const Asked = union(enum) { all, pane: usize, column: usize }; +pub const Asked = union(enum) { all, pane: usize, column: usize }; -fn warnModifiedIn(c: Ctx, asking: Pane.Discarding, which: Asked) bool { +pub fn warnModifiedIn(c: Ctx, asking: Pane.Discarding, which: Asked) bool { // Every pane asked about is an `unsaved ` record, as // acme's rowclean names them all (wind.c:511); the refusal a write // gets names the one, or counts them, never a list cut short. @@ -1330,36 +1330,15 @@ pub const Togglettymode = struct { }; /// acme's Edit (exec.c `edit`, edit.c): sam's command language over the -/// pane's body, from a tag, a pane ctl or exec. One undo step; a failure -/// changes nothing and says why. src/sam_edit.zig has what is left out. +/// open text panes, from a tag, a pane ctl or exec, starting in the pane's +/// body. One undo step in each file it changes; a failure changes nothing +/// and says why. src/edit_cmd.zig runs it; src/sam_edit.zig has what is +/// left out. pub const Edit = struct { pub const takes_arg = true; pub const requires_arg = true; pub fn run(c: Ctx) void { - const sam = @import("sam_edit.zig"); - const ninep_pane = @import("ninep/pane.zig"); - const f = ninep_pane.fileOf(c.pane) orelse return c.p.reportFailure(c.id, "Edit: only a text pane is edited"); - const arena = c.p.scratch.allocator(); - var why: sam.Why = .{}; - var said: [260]u8 = undefined; - const res = sam.run(arena, f.content, ninep_pane.dotOf(c.pane), f.path, c.arg orelse "", &why) catch |err| switch (err) { - error.Edit => return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Edit: {s}", .{why.text()}) catch "Edit: failed"), - error.OutOfMemory => return c.p.reportError(c.id, "Edit", err), - }; - if (res.undo != 0) { - for (0..@abs(res.undo)) |_| if (res.undo > 0) panes.File.undo(c.p, c.pane) else panes.File.redo(c.p, c.pane); - return; - } - if (res.ops.len > 0) { - const new = sam.apply(c.p.gpa, f.content, res.ops) catch |err| return c.p.reportError(c.id, "Edit", err); - // `s/a/a/` changes nothing: no undo step, no new revision. - if (std.mem.eql(u8, new, f.content)) c.p.gpa.free(new) else { - panes.File.pushUndo(c.p, c.pane); - panes.File.setContent(c.p, f, new); - } - } - ninep_pane.setDot(c.pane, sam.moveDot(res.dot, res.ops)); - if (res.printed.len > 0) _ = ninep_pane.appendErrors(c.p, c.id, res.printed); + @import("edit_cmd.zig").run(c.p, c.id, c.arg orelse ""); } }; diff --git a/src/edit_cmd.zig b/src/edit_cmd.zig new file mode 100644 index 00000000..7f5c6279 --- /dev/null +++ b/src/edit_cmd.zig @@ -0,0 +1,487 @@ +//! acme's Edit in the editor (exec.c `edit`, edit.c editcmd): the panes an +//! Edit sees and what it does to them. src/sam_edit.zig runs the commands +//! over the open text panes; here they are handed over, and what came of +//! them applied: each file's changes as one undo step, the dots, `w`'s +//! writes, the names `e` and `f` gave, `D`'s closes, and what was printed, +//! to +Errors. +//! +//! An Edit with `<`, `|` or `>` runs their commands off the loop, as the +//! `|` key's filter does (selection_pipe.zig, Pipe.zig): the commands go out +//! as one pipe request, each in its file's directory through the session's +//! Shell, and the Edit is applied when they have all answered -- or not at +//! all, when one fails or a file it changes was edited meanwhile. One Edit +//! waits at a time; a 9P write that ran it is answered when it is done +//! (9p_io, `fs.edit_answer_at`). +const std = @import("std"); +const pardes = @import("pardes.zig"); +const sam = @import("sam_edit.zig"); +const panes = @import("panes.zig"); +const layout = @import("layout.zig"); +const look = @import("look.zig"); +const builtins = @import("builtins.zig"); +const filesystem = @import("fs.zig"); +const selection_pipe = @import("selection_pipe.zig"); +const Pipe = @import("Pipe.zig"); +const ninep_pane = @import("ninep/pane.zig"); +const Pardes = pardes.Pardes; + +/// An Edit whose commands are running: what it came to, and the request +/// for them, all in `arena`. +pub const Pending = struct { + id: u32 = 0, + arena: std.heap.ArenaAllocator, + /// The pane the Edit ran from, which hears how it went. + origin: usize, + origin_serial: u32, + res: sam.Result = undefined, + /// Each file's pane serial and text revision when the Edit read it. + serials: []u32 = &.{}, + revisions: []u32 = &.{}, + request: selection_pipe.Request = undefined, + + pub fn deinit(pd: *Pending, gpa: std.mem.Allocator) void { + pd.arena.deinit(); + gpa.destroy(pd); + } +}; + +/// What `w` took, written by the `edit_write` effect (pardes.zig perform). +pub const Write = struct { + pane: u8, + serial: u32, + path: []u8, + bytes: []u8, + /// The pane comes clean if its text is still this revision. + clean: ?u32 = null, + /// A scratch written whole is that file from then on (exec.saveTo). + promote: bool = false, + + pub fn deinit(w: *Write, gpa: std.mem.Allocator) void { + gpa.free(w.path); + gpa.free(w.bytes); + } +}; + +/// `B`, `e` and `r` through the editor. +const World = struct { + p: *Pardes, + from: usize, + + fn open(ctx: *anyopaque, path: []const u8, why: *sam.Why) ?sam.File { + const w: *World = @ptrCast(@alignCast(ctx)); + const p = w.p; + if (comptime pardes.hosted) if (filesystem.localPath(path)) |local| if (@import("exec.zig").isDirectory(local)) + return said(sam.File, why, "B: {s} is a directory", .{path}); + // A look opens it, or finds it open, where a look would; the + // keyboard stays where the Edit ran. + const was = p.active; + look.lookAt(p, w.from, path); + if (p.panes[was] != null) p.active = was; + for (p.panes, 0..) |slot, id| { + const pane = slot orelse continue; + const f = ninep_pane.fileOf(pane) orelse continue; + if (!std.mem.eql(u8, f.path, path)) continue; + return fileOf(pane, id); + } + return said(sam.File, why, "B: cannot open {s}", .{path}); + } + + fn read(ctx: *anyopaque, arena: std.mem.Allocator, path: []const u8, why: *sam.Why) ?[]const u8 { + const w: *World = @ptrCast(@alignCast(ctx)); + if (comptime pardes.hosted) if (filesystem.localPath(path)) |local| if (@import("exec.zig").isDirectory(local)) + return said([]const u8, why, "{s} is a directory", .{path}); + const bytes = filesystem.read(w.p, path) catch |err| { + const name = @errorName(err); + const words = if (std.mem.eql(u8, name, "FileNotFound")) + "file does not exist" + else if (std.mem.eql(u8, name, "AccessDenied") or std.mem.eql(u8, name, "PermissionDenied")) + "permission denied" + else + name; + return said([]const u8, why, "can't open {s}: {s}", .{ path, words }); + }; + defer w.p.gpa.free(bytes); + return arena.dupe(u8, bytes) catch return said([]const u8, why, "can't read {s}: out of memory", .{path}); + } + + fn said(comptime T: type, why: *sam.Why, comptime fmt: []const u8, args: anytype) ?T { + why.len = if (std.fmt.bufPrint(&why.buf, fmt, args)) |s| s.len else |_| 0; + return null; + } +}; + +fn fileOf(pane: *panes.Pane, id: usize) sam.File { + const f = ninep_pane.fileOf(pane).?; + return .{ + .name = f.path, + .text = f.content, + .dot = ninep_pane.dotOf(pane), + .dirty = ninep_pane.dirtyOf(pane), + .scratch = f.output != null, + .id = id, + }; +} + +/// `Edit command` from pane `id`, a text pane. +pub fn run(p: *Pardes, id: usize, command: []const u8) void { + const pane = p.panes[id] orelse return; + if (ninep_pane.fileOf(pane) == null) return p.reportFailure(id, "Edit: only a text pane is edited"); + // acme's Edit blocks the editor while its commands run; here the + // editor goes on, and a second Edit waits its turn. + if (p.pipe.edit_run != null) return p.reportFailure(id, "Edit: another Edit's commands are still running"); + const pd = p.gpa.create(Pending) catch |err| return p.reportError(id, "Edit", err); + pd.* = .{ .arena = .init(p.gpa), .origin = id, .origin_serial = pane.serial }; + var keep = false; + defer if (!keep) pd.deinit(p.gpa); + const arena = pd.arena.allocator(); + + // Every text pane, column by column as acme's allwindows goes, then any + // not in a column; the Edit starts in its own. + var files: std.ArrayList(sam.File) = .empty; + var cur: usize = 0; + var seen: [pardes.MAX_PANES]bool = @splat(false); + for (0..p.ncol) |c| { + for (p.col_panes[c][0..p.col_n[c]]) |slot| addFile(p, &files, arena, slot, id, &cur, &seen) catch |err| return p.reportError(id, "Edit", err); + } + for (0..pardes.MAX_PANES) |slot| addFile(p, &files, arena, slot, id, &cur, &seen) catch |err| return p.reportError(id, "Edit", err); + + var world: World = .{ .p = p, .from = id }; + var why: sam.Why = .{}; + var text: [260]u8 = undefined; + const res = sam.run(arena, files.items, cur, command, .{ .ctx = &world, .open = World.open, .read = World.read }, &why) catch |err| switch (err) { + error.Edit => return p.reportFailure(id, std.fmt.bufPrint(&text, "Edit: {s}", .{why.text()}) catch "Edit: failed"), + error.OutOfMemory => return p.reportError(id, "Edit", err), + }; + if (res.undo != 0) { + for (0..@abs(res.undo)) |_| if (res.undo > 0) panes.File.undo(p, pane) else panes.File.redo(p, pane); + return; + } + pd.res = res; + // What each file was, to know at the end that it still is. + pd.serials = arena.alloc(u32, res.files.len) catch |err| return p.reportError(id, "Edit", err); + pd.revisions = arena.alloc(u32, res.files.len) catch |err| return p.reportError(id, "Edit", err); + for (res.files, 0..) |f, i| { + const q = p.panes[f.id].?; + pd.serials[i] = q.serial; + pd.revisions[i] = q.file.?.revision; + } + if (res.jobs.len == 0) return commit(p, pd, &.{}); + + // The commands, as one pipe request: each with its input copied (the + // text may change while they run, and the Edit is then refused), in + // its file's directory. + const n = res.jobs.len; + const commands = arena.alloc([]const u8, n) catch |err| return p.reportError(id, "Edit", err); + const cwds = arena.alloc([]const u8, n) catch |err| return p.reportError(id, "Edit", err); + const inputs = arena.alloc(selection_pipe.Input, n) catch |err| return p.reportError(id, "Edit", err); + for (res.jobs, 0..) |job, i| { + const f = res.files[job.file]; + commands[i] = job.command; + cwds[i] = Pardes.paneDir(p.panes[f.id].?); + inputs[i] = .{ .bytes = if (job.c == '<') "" else arena.dupe(u8, f.text[job.q0..job.q1]) catch |err| return p.reportError(id, "Edit", err) }; + } + p.pipe.seq +%= 1; + pd.id = p.pipe.seq; + pd.request = .{ + .id = pd.id, + .command = commands[0], + .cwd = cwds[0], + .inputs = inputs, + .commands = commands, + .cwds = cwds, + .shell = arena.dupe(u8, p.shellBin()) catch |err| return p.reportError(id, "Edit", err), + }; + keep = true; + p.pipe.edit_run = pd; + p.emit(.{ .pipe = .{ .id = pd.id } }); + // A 9P write that ran it is answered once it is done. + if (p.fs.serving) p.fs.edit_answer_at = pardes.turn.edit_answers; +} + +fn addFile(p: *Pardes, files: *std.ArrayList(sam.File), arena: std.mem.Allocator, slot: usize, from: usize, cur: *usize, seen: *[pardes.MAX_PANES]bool) !void { + if (seen[slot]) return; + seen[slot] = true; + const pane = p.panes[slot] orelse return; + if (pane.file == null) return; + if (slot == from) cur.* = files.items.len; + try files.append(arena, fileOf(pane, slot)); +} + +/// The commands answered (pipe_resp): the Edit applied, or why not. +pub fn finish(p: *Pardes, success: bool, outputs: []const []const u8, failure: ?selection_pipe.Failure) void { + const pd = p.pipe.edit_run orelse return; + p.pipe.edit_run = null; + defer pd.deinit(p.gpa); + defer pardes.turn.editAnswered(); + const at = if (p.panes[pd.origin]) |pane| (if (pane.serial == pd.origin_serial) pd.origin else p.active) else p.active; + // Whatever fails from here fails the 9P write that ran it too. + const failures = p.fs.failures; + defer if (p.fs.failures != failures) p.noteLateFailure(at); + if (success and outputs.len == pd.res.jobs.len) return commit(p, pd, outputs); + + // Which command, in +Errors with its own words; the Edit changed nothing. + const job = if (failure) |f| (if (f.index < pd.res.jobs.len) pd.res.jobs[f.index] else pd.res.jobs[0]) else pd.res.jobs[0]; + var out: std.Io.Writer.Allocating = .init(p.gpa); + defer out.deinit(); + const w = &out.writer; + var line: [256]u8 = undefined; + report: { + w.print("Edit: {c} {s}\n\n", .{ job.c, job.command }) catch break :report; + if (pd.res.jobs.len > 1) if (failure) |f| w.print("command {d} of {d}: ", .{ f.index + 1, pd.res.jobs.len }) catch break :report; + const why_at = out.written().len; + Pipe.writeFailure(w, failure) catch break :report; + const why = std.mem.sliceTo(out.written()[why_at..], '\n'); + const said = std.fmt.bufPrint(&line, "Edit: {c} {s}: {s}", .{ job.c, job.command[0..@min(job.command.len, 120)], why }) catch "Edit: a command failed"; + _ = ninep_pane.appendErrors(p, at, out.written()); + return p.reportFailure(at, said); + } + p.reportFailure(at, "Edit: a command failed"); +} + +/// Apply what the Edit came to, `outputs` its jobs' answers: refused, all +/// of it, if a pane it changes is not the one it read any more. +fn commit(p: *Pardes, pd: *Pending, outputs: []const []const u8) void { + const res = pd.res; + const at = if (p.panes[pd.origin]) |pane| (if (pane.serial == pd.origin_serial) pd.origin else p.active) else p.active; + var text: [320]u8 = undefined; + for (res.files, 0..) |f, i| { + if (f.ops.items.len == 0 and !f.close and !f.renamed and !f.moved) continue; + const pane = p.panes[f.id] orelse return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} closed while its commands ran; nothing changed", .{f.name}) catch "Edit: a pane closed"); + if (pane.serial != pd.serials[i]) return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} closed while its commands ran; nothing changed", .{f.name}) catch "Edit: a pane closed"); + if (f.ops.items.len > 0 and pane.file.?.revision != pd.revisions[i]) + return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} changed while its commands ran; nothing changed", .{f.name}) catch "Edit: a file changed"); + } + // `w`'s bytes, written after this step, before anything else is. + for (res.writes) |wr| { + const f = res.files[wr.file]; + const pane = p.panes[f.id].?; + const path = p.gpa.dupe(u8, wr.path) catch |err| return p.reportError(at, "Edit", err); + const bytes = p.gpa.dupe(u8, wr.bytes) catch |err| { + p.gpa.free(path); + return p.reportError(at, "Edit", err); + }; + const own = std.mem.eql(u8, wr.path, pane.file.?.path); + p.pipe.writes.append(p.gpa, .{ + .pane = @intCast(f.id), + .serial = pane.serial, + .path = path, + .bytes = bytes, + // Its own name, all of it, and nothing after the w changes it: + // clean, as acme's putfile leaves it. + .clean = if (own and wr.whole and !f.scratch and f.ops.items.len == 0) pane.file.?.revision else null, + .promote = wr.whole and f.scratch, + }) catch |err| { + p.gpa.free(path); + p.gpa.free(bytes); + return p.reportError(at, "Edit", err); + }; + } + if (res.writes.len > 0) p.emit(.edit_write); + for (res.files) |f| { + const pane = p.panes[f.id] orelse continue; + const pf = ninep_pane.fileOf(pane) orelse continue; + const ops = f.ops.items; + for (ops) |*o| if (o.job) |j| { + o.text = outputs[j]; + }; + if (ops.len > 0) { + const new = sam.apply(p.gpa, pf.content, ops) catch |err| return p.reportError(at, "Edit", err); + // `s/a/a/` changes nothing: no undo step, no new revision. + if (std.mem.eql(u8, new, pf.content)) p.gpa.free(new) else { + panes.File.pushUndo(p, pane); + panes.File.setContent(p, pf, new); + } + // e read its own file over it: what is on disk again. + if (f.clean) { + pf.saved_revision = pf.revision; + pf.disk_gone = false; + } + } + if (ops.len > 0 or f.moved) ninep_pane.setDot(pane, sam.moveDot(f.dot, ops)); + if (f.renamed) ninep_pane.nameBuffer(p, f.id, f.name, false) catch |err| p.reportError(f.id, "Edit", err); + } + // What was printed, `>`'s output in its place among it. + if (res.printed.len > 0 or outputs.len > 0) { + var out: std.ArrayList(u8) = .empty; + defer out.deinit(p.gpa); + var from: usize = 0; + for (res.jobs, 0..) |job, j| { + if (job.c != '>') continue; + out.appendSlice(p.gpa, res.printed[from..job.at]) catch return; + out.appendSlice(p.gpa, outputs[j]) catch return; + from = job.at; + } + out.appendSlice(p.gpa, res.printed[from..]) catch return; + if (out.items.len > 0) _ = ninep_pane.appendErrors(p, at, out.items); + } + // D's panes last, each as Del closes one: an edited one is warned about + // once, and closes on the next. + for (res.files) |f| { + if (!f.close) continue; + const pane = p.panes[f.id] orelse continue; + if (builtins.warnModifiedIn(.{ .p = p, .pane = pane, .id = f.id, .txt = "", .arg = null }, .Del, .{ .pane = f.id })) continue; + p.removePane(f.id, null) catch |err| p.reportError(f.id, "Edit", err); + } +} + +const testing = std.testing; +const th = @import("ninep/testing.zig"); +const tree = @import("ninep/tree.zig"); + +/// The answer of the Edit waiting on its commands: as a host's worker +/// brings it home, run for real when `real`. +fn answer(p: *Pardes, outputs: []const []const u8) void { + const pd = p.pipe.edit_run.?; + p.update(.{ .pipe_resp = .{ .id = pd.id, .success = true, .outputs = outputs } }); +} + +fn runReal(p: *Pardes) !void { + const pd = p.pipe.edit_run.?; + const job = try selection_pipe.Job.copy(testing.allocator, pd.request); + defer job.deinit(testing.allocator); + var response = selection_pipe.runJob(testing.allocator, testing.io, job); + defer response.deinit(testing.allocator); + p.update(.{ .pipe_resp = .{ .id = pd.id, .success = response.success, .outputs = response.outputs, .failure = response.failure } }); +} + +fn errorsText(p: *Pardes) ?[]const u8 { + for (p.panes) |slot| { + const q = slot orelse continue; + const qf = q.file orelse continue; + const o = qf.output orelse continue; + if (std.meta.activeTag(o.from) == .errors) return qf.content; + } + return null; +} + +test "Edit , | sort runs off the loop, through the session's shell in the file's directory, and is one undo step" { + const p = try th.withFile(testing.allocator, "b\na\nc\n"); + defer p.deinit(); + const serial = th.serialOf(p); + const answers = pardes.turn.edit_answers; + try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(serial, .ctl), "Edit , | sort\n").reply.status); + // Nothing changed yet: the command is out. + try testing.expectEqualStrings("b\na\nc\n", p.panes[0].?.file.?.content); + const req = p.pipe.pipeRequest(p.pipe.edit_run.?.id).?; + try testing.expectEqualStrings("sort", req.commands[0]); + try testing.expectEqualStrings("b\na\nc\n", req.inputs[0].bytes); + try testing.expectEqualStrings("/", req.cwds[0]); + try testing.expectEqualStrings(p.shellBin(), req.shell); + // A second Edit waits its turn rather than run beside it. + try testing.expectEqual(tree.E.IO, th.wr(p, tree.Node.of(serial, .ctl), "Edit ,d\n").errno()); + answer(p, &.{"a\nb\nc\n"}); + try testing.expect(p.pipe.edit_run == null); + try testing.expectEqual(answers +% 1, pardes.turn.edit_answers); + try testing.expectEqualStrings("a\nb\nc\n", p.panes[0].?.file.?.content); + try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(serial, .ctl), "Undo\n").reply.status); + try testing.expectEqualStrings("b\na\nc\n", p.panes[0].?.file.?.content); + // What follows it in the same write would run before its commands + // answer: refused, and the Edit goes on. + const after = th.wr(p, tree.Node.of(serial, .ctl), "Edit , | sort\nSave\n"); + try testing.expectEqual(tree.E.INVAL, after.errno()); + try testing.expectEqualStrings(@import("ninep/ctl.zig").e_after_edit, after.reply.ename); + answer(p, &.{"a\nb\nc\n"}); + try testing.expectEqualStrings("a\nb\nc\n", p.panes[0].?.file.?.content); +} + +test "Edit's < | and > with a real shell: a command per address, > printed to +Errors" { + if (comptime !pardes.hosted) return error.SkipZigTest; + const p = try th.withFile(testing.allocator, "one two\nthree\n"); + defer p.deinit(); + const ctl = tree.Node.of(th.serialOf(p), .ctl); + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit ,x/[a-z]+/ | tr a-z A-Z\n").reply.status); + try testing.expectEqual(@as(usize, 3), p.pipe.edit_run.?.request.inputs.len); + try runReal(p); + try testing.expectEqualStrings("ONE TWO\nTHREE\n", p.panes[0].?.file.?.content); + try testing.expectEqual(@as(usize, 1), p.panes[0].?.file.?.history.undo_len); + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit 2 < printf new\n").reply.status); + try runReal(p); + try testing.expectEqualStrings("ONE TWO\nnew", p.panes[0].?.file.?.content); + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit , > wc -l\n").reply.status); + try runReal(p); + try testing.expectEqualStrings("ONE TWO\nnew", p.panes[0].?.file.?.content); + try testing.expectEqualStrings("1", std.mem.trim(u8, errorsText(p).?, " \n")); +} + +test "a failing command changes nothing and says why, its stderr in +Errors" { + if (comptime !pardes.hosted) return error.SkipZigTest; + const p = try th.withFile(testing.allocator, "keep\n"); + defer p.deinit(); + const ctl = tree.Node.of(th.serialOf(p), .ctl); + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit , | echo boom >&2; exit 3\n").reply.status); + p.fs.late_failure_len = 0; + try runReal(p); + try testing.expectEqualStrings("keep\n", p.panes[0].?.file.?.content); + try testing.expectEqual(@as(usize, 0), p.panes[0].?.file.?.history.undo_len); + try testing.expectEqualStrings("Edit: | echo boom >&2; exit 3: exit status 3", p.fs.late_failure[0..p.fs.late_failure_len]); + const report = errorsText(p).?; + try testing.expect(std.mem.indexOf(u8, report, "Edit: | echo boom") != null); + try testing.expect(std.mem.indexOf(u8, report, "boom\n") != null); +} + +test "an Edit whose file is edited while its commands run is refused whole" { + const p = try th.withFile(testing.allocator, "abc\n"); + defer p.deinit(); + try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(th.serialOf(p), .ctl), "Edit , | cat\n").reply.status); + panes.File.setContent(p, &p.panes[0].?.file.?, try testing.allocator.dupe(u8, "typed\n")); + p.fs.late_failure_len = 0; + answer(p, &.{"ABC\n"}); + try testing.expectEqualStrings("typed\n", p.panes[0].?.file.?.content); + try testing.expectEqualStrings("Edit: /test.txt changed while its commands ran; nothing changed", p.fs.late_failure[0..p.fs.late_failure_len]); +} + +test "X over open files, w, e, r, f, B and D on panes and the disk" { + if (comptime !pardes.hosted or !filesystem.platform_has_fs) return error.SkipZigTest; + const gpa = testing.allocator; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.c", .data = "int foo;\n" }); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "b.h", .data = "foo\n" }); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.c", .data = "foo bar\n" }); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "data", .data = "DATA\n" }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; + const p = try th.withFile(gpa, "scratch\n"); + defer p.deinit(); + var path: [4200]u8 = undefined; + look.lookAt(p, 0, try std.fmt.bufPrint(&path, "{s}/a.c", .{dir})); + const a = p.active; + look.lookAt(p, 0, try std.fmt.bufPrint(&path, "{s}/b.h", .{dir})); + p.active = 0; + const ctl = tree.Node.of(th.serialOf(p), .ctl); + // X: every open file whose line matches, each from its own dot. + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit X/\\.[ch]$/ ,s/foo/FOO/g\n").reply.status); + try testing.expectEqualStrings("int FOO;\n", p.panes[a].?.file.?.content); + try testing.expectEqualStrings("scratch\n", p.panes[0].?.file.?.content); + // B opens a file, which a `"re"` address then edits, from pane 0. + var cmd: [4400]u8 = undefined; + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, try std.fmt.bufPrint(&cmd, "Edit B {s}/c.c\n", .{dir})).reply.status); + try testing.expectEqual(@as(usize, 0), p.active); + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit \"c\\.c$\" 1 s/bar/BAR/\n").reply.status); + const c = for (p.panes, 0..) |slot, i| { + if (slot) |q| if (q.file) |f| if (std.mem.endsWith(u8, f.path, "/c.c")) break i; + } else return error.NotOpened; + try testing.expectEqualStrings("foo BAR\n", p.panes[c].?.file.?.content); + // w of all of a file to its own name leaves it clean; r reads into dot. + try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(p.panes[a].?.serial, .ctl), "Edit w\n").reply.status); + p.perform(.edit_write); // the effect th.wr took off the queue + try testing.expectEqualStrings("int FOO;\n", p.fallback.get(p.panes[a].?.file.?.path).?); + try testing.expect(!ninep_pane.dirtyOf(p.panes[a].?)); + try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(p.panes[c].?.serial, .ctl), "Edit 1 r data\n").reply.status); + try testing.expectEqualStrings("DATA\n", p.panes[c].?.file.?.content); + // e of an edited file is refused; f names one and prints it. + try testing.expectEqual(tree.E.IO, th.wr(p, tree.Node.of(p.panes[c].?.serial, .ctl), "Edit e\n").errno()); + try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(p.panes[c].?.serial, .ctl), "Edit f d.c\n").reply.status); + try testing.expect(std.mem.endsWith(u8, p.panes[c].?.file.?.path, "/d.c")); + try testing.expect(std.mem.indexOf(u8, errorsText(p).?, "'+. ") != null); + // D closes a pane by name, an edited one (b.h, by the X) on the + // second asking, as Del does. + const b_serial = for (p.panes) |slot| { + if (slot) |q| if (q.file) |f| if (std.mem.endsWith(u8, f.path, "/b.h")) break q.serial; + } else return error.NotOpened; + const a_ctl = tree.Node.of(p.panes[a].?.serial, .ctl); + try testing.expect(th.wr(p, a_ctl, "Edit D b.h\n").reply.status == .err); + try testing.expect(p.paneBySerial(b_serial) != null); + try testing.expectEqual(tree.Status.ok, th.wr(p, a_ctl, "Edit D b.h\n").reply.status); + try testing.expect(p.paneBySerial(b_serial) == null); +} diff --git a/src/fs.zig b/src/fs.zig index 7a56d387..2a5102f4 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -1613,6 +1613,9 @@ pub const Namespace = struct { /// A 9P write asked a language server something: the answer count it /// waits to change (`turn.lsp_answers`), so its failure is the write's. lsp_answer_at: ?u64 = null, + /// A write ran an Edit whose `<`, `|` or `>` commands are running: it is + /// answered once `turn.edit_answers` passes this, the Edit done. + edit_answer_at: ?u64 = null, /// The pane a language answer filled (a Rename's preview): what the /// waiting 9P write answers once it is in (9p_io). lsp_result: ?u32 = null, diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 599dd0d6..ac5d64d8 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -203,6 +203,7 @@ pub fn command(p: *Pardes, req: Req, serial: ?u32, exec: bool, in_tag: bool) Rep for (text) |c| if ((c < ' ' and c != '\t' and c != '\n') or c == 0x7f) return tree.failText(req.tag, E.INVAL, e_control); if (exec) if (tooLong(req, text)) |refusal| return refusal; if (!apply) continue; + if (p.fs.edit_answer_at != null) return tree.failText(req.tag, E.INVAL, e_after_edit); var id = if (serial) |s| p.paneBySerial(s) orelse break else p.active; // No pane at all: the root's look and exec act in the session's // directory. An exec runs as the workspace tag's; a look from a @@ -259,6 +260,11 @@ pub fn tooLong(req: Req, line: []const u8) ?Reply { pub const e_control = "invalid command line: it holds a control character (or DEL) other than a tab"; +/// An Edit whose `<`, `|` or `>` commands are running answers its write +/// when they are done (edit_cmd.zig); a line after it in the same write +/// would run before them, on the text they are about to change. +pub const e_after_edit = "invalid command line: an Edit running commands ends its write; write what follows after it"; + /// Runs one click (`run`) as a 9P write's: a builtin that fails there fails /// the write, with its words and an err record, and no msg, as a ctl line /// does (runBuiltin). The refusal, or null. @@ -959,6 +965,7 @@ pub fn writePane(p: *Pardes, req: Req, pane: *Pane) Reply { var it = Messages.init(p, req.data); while (it.next()) |line_in| { if (line_in.len == 0 or exec_line.isComment(line_in)) continue; + if (apply and p.fs.edit_answer_at != null) return tree.failText(req.tag, E.INVAL, e_after_edit); // A line before this one may have closed the pane (`Del`). if (apply and p.paneBySerial(serial) == null) return Reply.fail(req.tag, E.NOENT); // acme's ctl words: done by the file that replaces each, or the diff --git a/src/pardes.zig b/src/pardes.zig index 7ae73636..cf3d5128 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -123,6 +123,9 @@ pub const Turn = struct { /// Bumped each time a language server's answer (or its lack) is taken /// in (`lspResponse`): a write that asked one waits for this. lsp_answers: u64 = 0, + /// Bumped each time an Edit's commands (`< | >`) have answered and the + /// Edit is done, or failed (edit_cmd.zig): a write that ran one waits. + edit_answers: u64 = 0, /// All of the above, and `out` reaching zero. settled: std.Io.Condition = .init, @@ -220,6 +223,16 @@ pub const Turn = struct { while (t.lsp_answers == answers) t.settled.wait(t.io.?, &t.mutex) catch return; } + pub fn editAnswered(t: *Turn) void { + t.edit_answers +%= 1; + const io = t.io orelse return; + t.settled.broadcast(io); + } + + pub fn awaitEdit(t: *Turn, answers: u64) void { + while (t.edit_answers == answers) t.settled.wait(t.io.?, &t.mutex) catch return; + } + pub fn awaitRestored(t: *Turn, restores: u64) void { while (t.restores == restores) t.settled.wait(t.io.?, &t.mutex) catch return; } @@ -483,6 +496,7 @@ test { _ = @import("exec.zig"); _ = @import("look.zig"); _ = @import("sam_edit.zig"); + _ = @import("edit_cmd.zig"); _ = @import("mouse.zig"); _ = @import("normal.zig"); _ = @import("regexp.zig"); @@ -3702,6 +3716,8 @@ pub const Effect = union(enum) { /// Snapshot the matching request with pipeRequest(id), then run it away /// from the UI/event loop and answer with pipe_resp. pipe: struct { id: u32 }, + /// Write what an Edit's `w` took (`Pipe.writes`), in order. + edit_write, watch: struct { pane: u8, on: bool, mode: WatchMode = .reconcile }, theme_file: struct { generation: u32, on: bool }, /// Write the build-time theme ring below the per-user config directory. @@ -4907,7 +4923,7 @@ pub const Pardes = struct { if (p.custom_theme) |theme_value| std.zon.parse.free(gpa, theme_value); if (p.chord_arg) |a| gpa.free(a); for (p.spawn_dirs) |d| if (d) |dir| gpa.free(dir); - if (p.pipe.wait) |*wait| wait.deinit(gpa); + p.pipe.deinit(gpa); p.locations_cache.deinit(p.tree_sitter_gpa); p.fs.deinit(gpa); p.shell_rows.reset(gpa); @@ -5795,6 +5811,37 @@ pub const Pardes = struct { f(p.host.ctx, q.id) else p.update(.{ .pipe_resp = .{ .id = q.id, .success = false, .outputs = &.{} } }), + .edit_write => { + // Each write takes the bytes `w` took when it ran: the text + // as it was before the Edit's changes. + var writes = p.pipe.writes; + p.pipe.writes = .empty; + defer { + for (writes.items) |*w| w.deinit(p.gpa); + writes.deinit(p.gpa); + } + for (writes.items) |w| { + const was = if (p.panes[w.pane]) |pane| (if (pane.file) |f| f.saved_revision else 0) else 0; + const failures = p.fs.failures; + makeParents(w.path); + p.hostWriteFile(w.pane, w.path, w.bytes); + const pane = p.panes[w.pane] orelse continue; + if (pane.serial != w.serial) continue; + const f = if (pane.file) |*file| file else continue; + // Failed, it changes nothing, as save_text's does not. + if (p.fs.failures != failures) { + f.saved_revision = was; + continue; + } + if (w.promote) { + p.promoteSaved(w.pane, w.path); + } else if (w.clean != null and w.clean.? == f.revision) { + f.saved_revision = f.revision; + f.disk_gone = false; + ctlfs.events.noteLog(p, .save, pane); + } else ctlfs.events.noteLogAs(p, .save, pane, w.path); + } + }, .watch => |w| if (v.watch_file) |f| f(p.host.ctx, w.pane, p.watchPath(w.pane) orelse "", w.on, w.mode) else { @@ -5881,6 +5928,7 @@ pub const Pardes = struct { look.cancelLookHover(p); p.fs.serving = true; p.fs.lsp_answer_at = null; + p.fs.edit_answer_at = null; const reply = ctlfs.handle(p, req); p.fs.serving = false; // The one-failure rule is a write's: a refused open (its OTRUNC's diff --git a/src/sam_edit.zig b/src/sam_edit.zig index 9b055ef9..3f23aeff 100644 --- a/src/sam_edit.zig +++ b/src/sam_edit.zig @@ -1,18 +1,29 @@ //! acme's Edit (editors/acme/edit.c, ecmd.c, elog.c): sam's command -//! language over one pane's body. The commands are parsed as edit.c parses -//! them, run as ecmd.c runs them against the text as it was before the Edit, -//! and their changes collected in order, as elog.c logs them, then applied -//! at once: an Edit is one undo step, and one that fails changes nothing. +//! language over the open text panes. The commands are parsed as edit.c +//! parses them, run as ecmd.c runs them against the text as it was before +//! the Edit, and their changes collected in order, a log a file, as elog.c +//! logs them, then applied at once: an Edit is one undo step in each file it +//! changes, and one that fails changes nothing. //! //! Addresses are the `addr` file's (src/ninep/addr.zig), so they take //! pardes's `line:col` too; regular expressions are src/regexp.zig's, mvzr //! searched as sam searches, with its step budget. //! -//! Left out: the file commands (`b B D e r w f X Y`), the pipes (`< | >`), -//! the `'` and `"` addresses, and `\1`..`\9` in `s` (mvzr has no -//! submatches). `u` undoes or redoes only as a whole Edit. acme warns about -//! changes out of sequence (elog.c:130) and applies them anyway; here the -//! Edit is refused. +//! What touches more than the text is the caller's (src/EditCmd.zig): `<`, +//! `|` and `>` are collected as jobs, a command to run per address, whose +//! output fills the change they hold the place of; `w` as the bytes to +//! write; `D`, and the names `e` and `f` set, as marks on the file; `B` and +//! the reads of `e` and `r` through `World`, at once, as acme does them. +//! +//! Left out: the `'` address, `B `) refuses the whole Edit, where sam +//! warns and puts in what it wrote; `D` of a file the Edit changed is +//! refused rather than drop the changes; `e` of an edited file is refused +//! every time, not once. const std = @import("std"); const regexp = @import("regexp.zig"); const addr_lang = @import("ninep/addr.zig"); @@ -26,14 +37,62 @@ fn stepRune(text: []const u8, p: usize) usize { pub const Range = addr_lang.Range; -pub const Op = struct { q0: u32, q1: u32, text: []const u8 }; +/// A change to the text as it was: `job`, when set, is a `<` or `|` whose +/// command's output is the text, known once it has run. +pub const Op = struct { q0: u32, q1: u32, text: []const u8, job: ?u32 = null }; + +/// One open text pane, as an Edit sees and leaves it. +pub const File = struct { + /// Its absolute path; after `e` or `f`, the name they gave it. + name: []const u8, + text: []const u8, + dot: Range = .{}, + /// Edited since it was saved: the `'` of acme's file line. + dirty: bool = false, + /// No file of its own (a +Errors, a +New): `e`, `r` and `w` want a name. + scratch: bool = false, + /// The caller's: which pane it is. + id: usize = 0, + ops: std.ArrayList(Op) = .empty, + /// `e` or `f` named it `name`. + renamed: bool = false, + /// `D` closes it once the Edit is done. + close: bool = false, + /// `e` read its own file whole: it is clean again once applied. + clean: bool = false, + /// A command left a dot in it. + moved: bool = false, +}; + +/// A command for `<`, `|` or `>` (`c`), run in `file`'s directory, its +/// stdin `file`'s text in `q0..q1` (none for `<`). `>`'s output is printed +/// at `at` in `Result.printed`. +pub const Job = struct { c: u8, command: []const u8, file: u32, q0: u32, q1: u32, at: usize }; -/// What an Edit came to: the changes in order, the dot it leaves (in the -/// text before them), and what `p` and `=` printed. +/// `w`: `bytes` to `path`, `whole` when they are all of `file`'s text. +pub const Write = struct { file: u32, path: []const u8, bytes: []const u8, whole: bool }; + +/// What the editor does for `B`, `e` and `r` while an Edit runs. +pub const World = struct { + ctx: *anyopaque, + /// The text pane holding the absolute `path`, opened if it is not open: + /// `B`. Null, and `why` set, when it cannot be. + open: *const fn (ctx: *anyopaque, path: []const u8, why: *Why) ?File, + /// The bytes of the absolute `path`, in `arena`: `e` and `r`. + read: *const fn (ctx: *anyopaque, arena: std.mem.Allocator, path: []const u8, why: *Why) ?[]const u8, +}; + +/// What an Edit came to: every file (those `B` opened after the ones it +/// was given), each with its changes in order and the dot it leaves (in the +/// text before them); the jobs and writes; and what `p`, `=`, `b`, `f` and +/// `X` printed. pub const Result = struct { - ops: []const Op, - dot: Range, - printed: []const u8, + files: []File, + /// The current file at the end (`b` moves it). + cur: usize, + jobs: []const Job = &.{}, + writes: []const Write = &.{}, + printed: []const u8 = "", /// A bare `u`/`u-3`: undo (positive) or redo (negative) that many. undo: i32 = 0, }; @@ -49,9 +108,17 @@ pub const Why = struct { } }; +/// The most commands an Edit's `<`, `|` and `>` may run: each is a process. +pub const max_jobs = 1024; + const Cmd = struct { c: u8, addr: ?[]const u8 = null, + /// A `"re"` address: the file whose line matches. + file_re: ?[]const u8 = null, + /// A `,` after it, whose right side acme evaluates in the current file + /// (ecmd.c cmdaddress): only `;` carries the file over. + file_comma: bool = false, re: ?[]const u8 = null, text: []const u8 = "", sub: ?*Cmd = null, @@ -61,9 +128,10 @@ const Cmd = struct { to: ?[]const u8 = null, }; -/// Runs `command` over `text` from `dot`, the file named `name` (for `=`). +/// Runs `command` over `files` from `files[cur]`, whose dots are where +/// each starts. `world` answers `B`, `e` and `r`; with none they fail. /// Everything returned lives in `arena`. -pub fn run(arena: std.mem.Allocator, text: []const u8, dot: Range, name: []const u8, command: []const u8, why: *Why) Failure!Result { +pub fn run(arena: std.mem.Allocator, files: []const File, cur: usize, command: []const u8, world: ?World, why: *Why) Failure!Result { var src = try arena.alloc(u8, command.len + 1); @memcpy(src[0..command.len], command); src[command.len] = '\n'; @@ -80,23 +148,32 @@ pub fn run(arena: std.mem.Allocator, text: []const u8, dot: Range, name: []const // reaches Edit unfinished was cut short (a write, an open closed), and // half of it inserted is worse than none. if (ps.open_text) return fail(why, "a, c or i text not ended by a . line", .{}); - const cmds = first orelse return .{ .ops = &.{}, .dot = dot, .printed = "" }; + var ex: Exec = .{ .arena = arena, .why = why, .world = world, .cur = cur }; + try ex.files.appendSlice(arena, files); + const cmds = first orelse return .{ .files = ex.files.items, .cur = cur }; // acme's u runs at once (ecmd.c:532), outside the edit log; here it is // an Edit of its own. if (cmds.c == 'u') { if (cmds.next != null) return fail(why, "u is only an Edit of its own here", .{}); - return .{ .ops = &.{}, .dot = dot, .printed = "", .undo = cmds.num }; + return .{ .files = ex.files.items, .cur = cur, .undo = cmds.num }; } - var ex: Exec = .{ .arena = arena, .text = text, .name = name, .why = why }; - var d = dot; + // editthread: each command from the current file, which `b` changes. var it: ?*Cmd = cmds; - while (it) |c| : (it = c.next) d = try ex.exec(c, d); - return .{ .ops = ex.ops.items, .dot = d, .printed = ex.out.items }; + while (it) |c| : (it = c.next) { + const fi = ex.cur; + const d = try ex.exec(c, fi, ex.files.items[fi].dot); + ex.files.items[fi].dot = d; + ex.files.items[fi].moved = true; + } + return .{ + .files = ex.files.items, + .cur = ex.cur, + .jobs = ex.jobs.items, + .writes = ex.writes.items, + .printed = ex.out.items, + }; } -/// Whether `command` stops inside a `{` group or an `a`, `c` or `i` text -/// block still waiting for its `.` line: a writer of lines (a ctl, exec) -/// hands an Edit the lines after it until it does not. /// What an Edit command still waits for: nothing, the `.` line that ends /// an a, c or i text block, or the `}` of a group. pub const Wait = enum { none, text, group }; @@ -131,6 +208,9 @@ fn groupOpen(command: []const u8) bool { return depth > 0; } +/// Whether `command` stops inside a `{` group or an `a`, `c` or `i` text +/// block still waiting for its `.` line: a writer of lines (a ctl, exec) +/// hands an Edit the lines after it until it does not. pub fn needsMore(arena: std.mem.Allocator, command: []const u8) bool { var why: Why = .{}; const src = std.fmt.allocPrint(arena, "{s}\n", .{command}) catch return false; @@ -158,6 +238,8 @@ const Parser = struct { braced: bool = false, /// A text block of `a`, `c` or `i` ran out before its `.` line. open_text: bool = false, + /// The address being read had a `,` after a `"re"`. + file_comma: bool = false, why: *Why, fn getch(ps: *Parser) ?u8 { @@ -207,8 +289,9 @@ const Parser = struct { } /// An address's text, blanks dropped, or null: the syntax of edit.c's - /// compoundaddr and simpleaddr, evaluated later by addr.zig. - fn address(ps: *Parser) Failure!?[]const u8 { + /// compoundaddr and simpleaddr, evaluated later by addr.zig. A `"re"` + /// first is the file the rest addresses, kept in `file_re`. + fn address(ps: *Parser, file_re: ?*?[]const u8) Failure!?[]const u8 { var out: std.ArrayList(u8) = .empty; while (true) { const c = ps.skipbl() orelse break; @@ -216,6 +299,7 @@ const Parser = struct { '0'...'9', '#', '.', '$', '+', '-', ',', ';' => { ps.i += 1; try out.append(ps.arena, c); + if (c == ',' and file_re != null and file_re.?.* != null) ps.file_comma = true; // pardes's line:col if (c >= '0' and c <= '9' and ps.nextc() == ':' and ps.i + 1 < ps.src.len and ps.src[ps.i + 1] >= '0' and ps.src[ps.i + 1] <= '9') @@ -242,7 +326,13 @@ const Parser = struct { } }, '\'' => return fail(ps.why, "can't handle '", .{}), - '"' => return fail(ps.why, "a file address is no address pardes's Edit takes", .{}), + '"' => { + // Only first, as the file the rest of it addresses. + const into = file_re orelse return fail(ps.why, "bad address", .{}); + if (out.items.len > 0 or into.* != null) return fail(ps.why, "bad address", .{}); + ps.i += 1; + into.* = try ps.getregexp('"'); + }, else => break, } } @@ -322,13 +412,33 @@ const Parser = struct { return s; } + /// edit.c:404, collecttoken: up to a byte of `end`, blanks before it + /// kept (the command trims them); a word wants its newline after it. + fn collecttoken(ps: *Parser, end: []const u8) Failure![]const u8 { + const start = ps.i; + while (ps.nextc()) |c| : (ps.i += 1) { + if (c != ' ' and c != '\t') break; + } + while (ps.nextc()) |c| : (ps.i += 1) { + if (std.mem.indexOfScalar(u8, end, c) != null) break; + } + const s = ps.src[start..ps.i]; + if (ps.nextc() == '\n') ps.i += 1 else try ps.atnl(); + return s; + } + /// edit.c:467, parsecmd. fn parse(ps: *Parser, nest: u32) Failure!?*Cmd { - const a = try ps.address(); + var file_re: ?[]const u8 = null; + ps.file_comma = false; + const a = try ps.address(&file_re); _ = ps.skipbl() orelse return null; const c = ps.getch() orelse return null; const cmd = try ps.arena.create(Cmd); - cmd.* = .{ .c = c, .addr = a }; + cmd.* = .{ .c = c, .addr = a, .file_re = file_re, .file_comma = ps.file_comma }; + // aNo in edit.c's cmdtab. + if ((a != null or file_re != null) and std.mem.indexOfScalar(u8, "bBDefuXY", c) != null) + return fail(ps.why, "command takes no address", .{}); switch (c) { '\n' => {}, 'a', 'c', 'i' => cmd.text = try ps.collecttext(), @@ -362,10 +472,11 @@ const Parser = struct { } try ps.atnl(); }, - 'x', 'y', 'g', 'v' => { - // x without a pattern is every line (ecmd.c:568, linelooper). + 'x', 'y', 'g', 'v', 'X', 'Y' => { + // x without a pattern is every line (ecmd.c:568, + // linelooper), X every file. const n = ps.nextc(); - if (c != 'x' or (n != ' ' and n != '\t' and n != '\n')) { + if ((c != 'x' and c != 'X') or (n != ' ' and n != '\t' and n != '\n')) { _ = ps.skipbl(); const delim = ps.getch() orelse '\n'; if (delim == '\n') return fail(ps.why, "no address", .{}); @@ -375,21 +486,23 @@ const Parser = struct { if (ps.skipbl() == '\n') { ps.i += 1; const p = try ps.arena.create(Cmd); - p.* = .{ .c = 'p' }; + p.* = .{ .c = if (c == 'X' or c == 'Y') 'f' else 'p' }; cmd.sub = p; } else { cmd.sub = (try ps.parse(nest)) orelse return fail(ps.why, "defcmd", .{}); } }, 'm', 't' => { - cmd.to = (try ps.address()) orelse return fail(ps.why, "bad address", .{}); + cmd.to = (try ps.address(null)) orelse return fail(ps.why, "bad address", .{}); try ps.atnl(); }, 'u' => { - if (a != null) return fail(ps.why, "command takes no address", .{}); cmd.num = ps.getnum(2); try ps.atnl(); }, + // edit.c's cmdtab: a line for these, a word for those. + 'b', 'B', 'D', '<', '|', '>' => cmd.text = try ps.collecttoken("\n"), + 'e', 'f', 'r', 'w' => cmd.text = try ps.collecttoken(" \t\n"), '{' => { var last: ?*Cmd = null; while (true) { @@ -410,7 +523,6 @@ const Parser = struct { if (nest == 0) return fail(ps.why, "right brace with no left brace", .{}); return null; }, - 'b', 'B', 'D', 'e', 'r', 'w', 'f', 'X', 'Y', '<', '|', '>' => return fail(ps.why, "{c} is a sam command pardes's Edit leaves out (it has no file or shell commands)", .{c}), else => return fail(ps.why, "unknown command {c}", .{c}), } return cmd; @@ -419,23 +531,35 @@ const Parser = struct { // ---- running: ecmd.c, logging: elog.c ---- +const e_noname = "no file name given"; + const Exec = struct { arena: std.mem.Allocator, - text: []const u8, - name: []const u8, why: *Why, - ops: std.ArrayList(Op) = .empty, + world: ?World, + files: std.ArrayList(File) = .empty, + /// The current file: where the next top-level command runs. + cur: usize, + jobs: std.ArrayList(Job) = .empty, + writes: std.ArrayList(Write) = .empty, out: std.ArrayList(u8) = .empty, nest: u32 = 0, + /// Inside an X or Y, which do not nest (ecmd.c Glooping). + looping: bool = false, fn clip(n: usize) u32 { return std.math.cast(u32, n) orelse std.math.maxInt(u32); } - /// An address from `dot`, or `dot` when there is none (aDot). - fn address(ex: *Exec, expr: ?[]const u8, dot: Range) Failure!Range { + fn text(ex: *const Exec, fi: usize) []const u8 { + return ex.files.items[fi].text; + } + + /// An address in file `fi` from `dot`, or `dot` when there is none + /// (aDot). + fn address(ex: *Exec, fi: usize, expr: ?[]const u8, dot: Range) Failure!Range { const e = expr orelse return dot; - var a: addr_lang.Addr = .{ .text = ex.text, .lim = null, .expr = e }; + var a: addr_lang.Addr = .{ .text = ex.text(fi), .lim = null, .expr = e }; // A miss names the pattern that missed, not the whole Edit line. const r = a.address(dot) orelse return if (std.mem.eql(u8, a.err, addr_lang.e_no_match)) fail(ex.why, "no match for regexp {s}", .{e}) @@ -448,13 +572,14 @@ const Exec = struct { /// elog.c's order: each change at or after the end of the one before, /// since they all address the text as it was. - fn log(ex: *Exec, q0: u32, q1: u32, text: []const u8) Failure!void { - if (q0 == q1 and text.len == 0) return; - if (ex.ops.items.len > 0) { - const prev = ex.ops.items[ex.ops.items.len - 1]; + fn log(ex: *Exec, fi: usize, q0: u32, q1: u32, bytes: []const u8, job: ?u32) Failure!void { + if (q0 == q1 and bytes.len == 0 and job == null) return; + const ops = &ex.files.items[fi].ops; + if (ops.items.len > 0) { + const prev = ops.items[ops.items.len - 1]; if (q0 < prev.q1 or q0 < prev.q0) return fail(ex.why, "changes out of sequence", .{}); } - try ex.ops.append(ex.arena, .{ .q0 = q0, .q1 = q1, .text = text }); + try ops.append(ex.arena, .{ .q0 = q0, .q1 = q1, .text = bytes, .job = job }); } fn compile(ex: *Exec, pat: []const u8, c: u8) Failure!regexp.Regex { @@ -464,7 +589,10 @@ const Exec = struct { error.WideRange => fail(ex.why, "{s}", .{regexp.Regex.e_wide}), error.NegatedRunes => fail(ex.why, "{s}", .{regexp.Regex.e_negated}), error.Mixed => fail(ex.why, "{s}", .{regexp.Regex.e_mixed}), - error.Bad => fail(ex.why, "bad regexp in {c} command", .{c}), + error.Bad => if (c == '"' or c == 'X' or c == 'Y') + fail(ex.why, "bad regexp in file match", .{}) + else + fail(ex.why, "bad regexp in {c} command", .{c}), }; } @@ -472,84 +600,175 @@ const Exec = struct { /// (addr.zig): mvzr matches bytes, so `.` would take one byte of `é` /// and x, y and s would split it. From `from` on only, so a match never /// reaches back into what the last one took. - fn find(ex: *Exec, rx: *regexp.Regex, from: usize, hi: usize) Failure!?regexp.Regex.Match { + fn find(ex: *Exec, rx: *regexp.Regex, in: []const u8, from: usize, hi: usize) Failure!?regexp.Regex.Match { if (from > hi) return null; // The budget is each search's own: an x over 100k lines makes 100k // searches, none of which is the slow one. rx.steps = regexp.Regex.budget; - const m = (rx.find(ex.text, from, hi, hi) catch return fail(ex.why, "{s}", .{addr_lang.e_slow})) orelse return null; - var start = modal.runeStart(ex.text, m.start); - if (start < from) start = modal.runeEnd(ex.text, m.start); - return .{ .start = start, .end = @max(start, modal.runeEnd(ex.text, m.end)) }; + const m = (rx.find(in, from, hi, hi) catch return fail(ex.why, "{s}", .{addr_lang.e_slow})) orelse return null; + var start = modal.runeStart(in, m.start); + if (start < from) start = modal.runeEnd(in, m.start); + return .{ .start = start, .end = @max(start, modal.runeEnd(in, m.end)) }; + } + + /// ecmd.c pfilename: `'` if edited, `+`, `.` if current, and the name. + fn fileLine(ex: *Exec, fi: usize) Failure![]const u8 { + const f = &ex.files.items[fi]; + return std.fmt.allocPrint(ex.arena, "{c}+{c} {s}\n", .{ + @as(u8, if (f.dirty and !f.scratch) '\'' else ' '), + @as(u8, if (fi == ex.cur) '.' else ' '), + f.name, + }); + } + + /// ecmd.c filematch: the file's line matches `pat`. + fn fileMatch(ex: *Exec, fi: usize, pat: []const u8) Failure!bool { + var rx = try ex.compile(pat, 'X'); + const line = try ex.fileLine(fi); + return (try ex.find(&rx, line, 0, line.len)) != null; + } + + /// ecmd.c matchfile: the one open file whose line matches `pat`. + fn matchFile(ex: *Exec, pat: []const u8) Failure!usize { + var found: ?usize = null; + for (ex.files.items, 0..) |f, i| { + if (f.close or f.scratch) continue; + if (!try ex.fileMatch(i, pat)) continue; + if (found != null) return fail(ex.why, "too many files match \"{s}\"", .{pat}); + found = i; + } + return found orelse fail(ex.why, "no file matches \"{s}\"", .{pat}); + } + + /// `name` as a path: itself when absolute, else in `fi`'s directory + /// (acme's dirname). + fn pathIn(ex: *Exec, fi: usize, name: []const u8) Failure![]const u8 { + if (name.len > 0 and name[0] == '/') return name; + const dir = std.fs.path.dirnamePosix(ex.files.items[fi].name) orelse "/"; + return std.fs.path.resolvePosix(ex.arena, &.{ dir, name }); + } + + /// The open file named `path`, a scratch too. + fn lookFile(ex: *Exec, path: []const u8) ?usize { + for (ex.files.items, 0..) |f, i| if (!f.close and std.mem.eql(u8, f.name, path)) return i; + return null; + } + + /// ecmd.c cmdname: the name a file command means: the one given, in the + /// file's directory, else the file's own (none for a scratch). `set` + /// names the file so (`e`, `f`), as an unnamed one always is. + fn cmdName(ex: *Exec, fi: usize, given: []const u8, set: bool) Failure!?[]const u8 { + const s = std.mem.trim(u8, given, " \t"); + const f = &ex.files.items[fi]; + if (s.len == 0) return if (f.scratch) null else f.name; + const full = try ex.pathIn(fi, s); + if (set and !std.mem.eql(u8, full, f.name)) { + if (ex.lookFile(full)) |other| if (other != fi) + try ex.out.print(ex.arena, "warning: duplicate file name \"{s}\"\n", .{full}); + f.name = full; + f.renamed = true; + f.scratch = false; + } + return full; + } + + fn readFile(ex: *Exec, path: []const u8) Failure![]const u8 { + const w = ex.world orelse return fail(ex.why, "can't open {s}: no files here", .{path}); + return w.read(w.ctx, ex.arena, path, ex.why) orelse error.Edit; + } + + /// ecmd.c:62, cmdexec: runs `c` in file `fi` from `dot`, answering the + /// dot it leaves there. + fn exec(ex: *Exec, c: *Cmd, fi_in: usize, dot_in: Range) Failure!Range { + var fi = fi_in; + var dot = dot_in; + if (ex.files.items[fi].close) return fail(ex.why, "no current window", .{}); + // A `"re"` address: the command runs in that file, from its dot. + if (c.file_re) |pat| { + fi = try ex.matchFile(pat); + dot = ex.files.items[fi].dot; + if (c.file_comma and fi != fi_in) return fail(ex.why, "addresses in different files", .{}); + } + // w's default address is the whole file (aAll). + const r = if (c.c == 'w' and c.addr == null) + Range{ .q0 = 0, .q1 = clip(ex.text(fi).len) } + else + try ex.address(fi, c.addr, dot); + const d = try ex.execIn(c, fi, r); + if (fi == fi_in) return d; + ex.files.items[fi].dot = d; + ex.files.items[fi].moved = true; + return dot_in; } - /// ecmd.c:62, cmdexec: runs `c` from `dot`, answering the dot it leaves. - fn exec(ex: *Exec, c: *Cmd, dot: Range) Failure!Range { - const r = try ex.address(c.addr, dot); + /// The command itself, at `r` in file `fi`. + fn execIn(ex: *Exec, c: *Cmd, fi: usize, r: Range) Failure!Range { + const txt = ex.text(fi); switch (c.c) { '\n' => return r, 'a' => { - try ex.log(r.q1, r.q1, c.text); + try ex.log(fi, r.q1, r.q1, c.text, null); return .{ .q0 = r.q1, .q1 = r.q1 }; }, 'i' => { - try ex.log(r.q0, r.q0, c.text); + try ex.log(fi, r.q0, r.q0, c.text, null); return .{ .q0 = r.q0, .q1 = r.q0 }; }, 'c' => { - try ex.log(r.q0, r.q1, c.text); + try ex.log(fi, r.q0, r.q1, c.text, null); return .{ .q0 = r.q0, .q1 = r.q0 }; }, 'd' => { - try ex.log(r.q0, r.q1, ""); + try ex.log(fi, r.q0, r.q1, "", null); return .{ .q0 = r.q0, .q1 = r.q0 }; }, 'p' => { - try ex.out.appendSlice(ex.arena, ex.text[r.q0..r.q1]); + try ex.out.appendSlice(ex.arena, txt[r.q0..r.q1]); return r; }, '=' => { // ecmd.c:665, printposn + const name = ex.files.items[fi].name; if (c.text.len > 0) { - try ex.out.print(ex.arena, "{s}:#{d}", .{ ex.name, r.q0 }); + try ex.out.print(ex.arena, "{s}:#{d}", .{ name, r.q0 }); if (r.q1 != r.q0) try ex.out.print(ex.arena, ",#{d}", .{r.q1}); } else { - const l1 = 1 + std.mem.count(u8, ex.text[0..r.q0], "\n"); - var l2 = l1 + std.mem.count(u8, ex.text[r.q0..r.q1], "\n"); - if (r.q1 > r.q0 and ex.text[r.q1 - 1] == '\n') l2 -= 1; - try ex.out.print(ex.arena, "{s}:{d}", .{ ex.name, l1 }); + const l1 = 1 + std.mem.count(u8, txt[0..r.q0], "\n"); + var l2 = l1 + std.mem.count(u8, txt[r.q0..r.q1], "\n"); + if (r.q1 > r.q0 and txt[r.q1 - 1] == '\n') l2 -= 1; + try ex.out.print(ex.arena, "{s}:{d}", .{ name, l1 }); if (l2 != l1) try ex.out.print(ex.arena, ",{d}", .{l2}); } try ex.out.append(ex.arena, '\n'); return r; }, - 's' => return ex.substitute(c, r), + 's' => return ex.substitute(c, fi, r), 'x', 'y' => { - const ranges = if (c.re) |pat| try ex.looper(pat, r, c.c == 'x') else try ex.lines(r); + const ranges = if (c.re) |pat| try ex.looper(pat, fi, r, c.c == 'x') else try ex.lines(fi, r); ex.nest += 1; defer ex.nest -= 1; var d = r; - for (ranges) |each| d = try ex.exec(c.sub.?, each); + for (ranges) |each| d = try ex.exec(c.sub.?, fi, each); return d; }, 'g', 'v' => { var rx = try ex.compile(c.re.?, c.c); - const hit = (try ex.find(&rx, r.q0, r.q1)) != null; - if (hit != (c.c == 'v')) return ex.exec(c.sub.?, r); + const hit = (try ex.find(&rx, txt, r.q0, r.q1)) != null; + if (hit != (c.c == 'v')) return ex.exec(c.sub.?, fi, r); return r; }, 'm', 't' => { - const to = try ex.address(c.to, dot); - const bytes = ex.text[r.q0..r.q1]; + const to = try ex.address(fi, c.to, r); + const bytes = txt[r.q0..r.q1]; if (c.c == 't') { - try ex.log(to.q1, to.q1, bytes); + try ex.log(fi, to.q1, to.q1, bytes, null); } else if (r.q1 <= to.q0) { // ecmd.c:408, move - try ex.log(r.q0, r.q1, ""); - try ex.log(to.q1, to.q1, bytes); + try ex.log(fi, r.q0, r.q1, "", null); + try ex.log(fi, to.q1, to.q1, bytes, null); } else if (r.q0 >= to.q1) { - try ex.log(to.q1, to.q1, bytes); - try ex.log(r.q0, r.q1, ""); + try ex.log(fi, to.q1, to.q1, bytes, null); + try ex.log(fi, r.q0, r.q1, "", null); } else if (!(r.q0 == to.q0 and r.q1 == to.q1)) { return fail(ex.why, "move overlaps itself", .{}); } @@ -558,28 +777,142 @@ const Exec = struct { 'u' => return fail(ex.why, "u is only an Edit of its own here", .{}), '{' => { var it = c.sub; - while (it) |each| : (it = each.next) _ = try ex.exec(each, r); + while (it) |each| : (it = each.next) _ = try ex.exec(each, fi, r); + return r; + }, + 'b' => { + // ecmd.c b_cmd, tofile: the file named so, exactly or in + // the current file's directory. + const name = std.mem.trim(u8, c.text, " \t"); + const at = ex.lookFile(name) orelse ex.lookFile(try ex.pathIn(fi, name)); + const to = if (at) |i| (if (ex.files.items[i].scratch) null else i) else null; + const target = to orelse return fail(ex.why, "no such file \"{s}\"", .{name}); + // The `.` goes with it in the line printed. + ex.cur = target; + if (ex.nest == 0) try ex.out.appendSlice(ex.arena, try ex.fileLine(target)); + return r; + }, + 'B' => { + const list = std.mem.trim(u8, c.text, " \t"); + if (list.len > 0 and list[0] == '<') return fail(ex.why, "B { + const list = std.mem.trim(u8, c.text, " \t"); + if (list.len > 0 and list[0] == '<') return fail(ex.why, "D { + // ecmd.c e_cmd: e reads a file over all of this one, r over + // dot; e names this file after it. + const f = &ex.files.items[fi]; + if (c.c == 'e' and f.dirty and !f.scratch) return fail(ex.why, "{s} modified", .{f.name}); + const own = f.name; + const path = (try ex.cmdName(fi, c.text, c.c == 'e')) orelse return fail(ex.why, e_noname, .{}); + const bytes = try ex.readFile(path); + const q: Range = if (c.c == 'e') .{ .q0 = 0, .q1 = clip(txt.len) } else r; + try ex.log(fi, q.q0, q.q1, bytes, null); + if (c.c == 'e' and std.mem.eql(u8, path, own)) ex.files.items[fi].clean = true; + return r; + }, + 'w' => { + if (ex.files.items[fi].ops.items.len > 0) return fail(ex.why, "can't write file with pending modifications", .{}); + const path = (try ex.cmdName(fi, c.text, false)) orelse return fail(ex.why, "no name specified for 'w' command", .{}); + try ex.writes.append(ex.arena, .{ + .file = @intCast(fi), + .path = path, + .bytes = try ex.arena.dupe(u8, txt[r.q0..r.q1]), + .whole = r.q0 == 0 and r.q1 == txt.len, + }); + return r; + }, + 'f' => { + _ = try ex.cmdName(fi, c.text, true); + try ex.out.appendSlice(ex.arena, try ex.fileLine(fi)); + return r; + }, + 'X', 'Y' => { + // ecmd.c filelooper: the files whose line matches (X) or + // does not (Y), each from its own dot; X alone is every + // file with a name. + if (ex.looping) return fail(ex.why, "can't nest {c} command", .{c.c}); + ex.looping = true; + defer ex.looping = false; + ex.nest += 1; + defer ex.nest -= 1; + var chosen: std.ArrayList(usize) = .empty; + for (ex.files.items, 0..) |f, i| { + if (f.close) continue; + if (c.re == null and f.scratch) continue; + if (c.re == null or (try ex.fileMatch(i, c.re.?)) == (c.c == 'X')) try chosen.append(ex.arena, i); + } + for (chosen.items) |i| { + const d = try ex.exec(c.sub.?, i, ex.files.items[i].dot); + ex.files.items[i].dot = d; + ex.files.items[i].moved = true; + } + return r; + }, + '<', '|', '>' => { + // ecmd.c runpipe: the command's output replaces dot (`<`, + // `|`), dot is its input (`|`, `>`), and `>`'s output is + // printed. + const command = std.mem.trim(u8, c.text, " \t"); + if (command.len == 0) return fail(ex.why, "no command specified for {c}", .{c.c}); + if (ex.jobs.items.len >= max_jobs) return fail(ex.why, "too many commands: more than {d} for one Edit", .{max_jobs}); + const job: u32 = @intCast(ex.jobs.items.len); + try ex.jobs.append(ex.arena, .{ .c = c.c, .command = command, .file = @intCast(fi), .q0 = r.q0, .q1 = r.q1, .at = ex.out.items.len }); + if (c.c != '>') try ex.log(fi, r.q0, r.q1, "", job); return r; }, else => return fail(ex.why, "unknown command {c}", .{c.c}), } } + /// ecmd.c D1: closed once the Edit is done. One with changes pending + /// in this Edit is refused rather than have them dropped. + fn closeFile(ex: *Exec, fi: usize) Failure!void { + const f = &ex.files.items[fi]; + if (f.ops.items.len > 0) return fail(ex.why, "D: {s} has changes pending in this Edit", .{f.name}); + f.close = true; + } + /// ecmd.c:443, s_cmd. - fn substitute(ex: *Exec, c: *Cmd, r: Range) Failure!Range { + fn substitute(ex: *Exec, c: *Cmd, fi: usize, r: Range) Failure!Range { + const txt = ex.text(fi); var rx = try ex.compile(c.re.?, 's'); var n = c.num; var op: ?usize = null; var p1: usize = r.q0; var did = false; while (p1 <= r.q1) { - const m = (try ex.find(&rx, p1, r.q1)) orelse break; + const m = (try ex.find(&rx, txt, p1, r.q1)) orelse break; if (m.start == m.end) { if (op != null and m.start == op.?) { - p1 = stepRune(ex.text, p1); + p1 = stepRune(txt, p1); continue; } - p1 = stepRune(ex.text, m.end); + p1 = stepRune(txt, m.end); } else p1 = m.end; op = m.end; n -= 1; @@ -594,10 +927,10 @@ const Exec = struct { if (e >= '1' and e <= '9') return fail(ex.why, "no \\{c}: mvzr keeps no submatches", .{e}); try buf.append(ex.arena, e); } else if (ch == '&') { - try buf.appendSlice(ex.arena, ex.text[m.start..m.end]); + try buf.appendSlice(ex.arena, txt[m.start..m.end]); } else try buf.append(ex.arena, ch); } - try ex.log(clip(m.start), clip(m.end), buf.items); + try ex.log(fi, clip(m.start), clip(m.end), buf.items, null); did = true; if (!c.global) break; } @@ -609,7 +942,8 @@ const Exec = struct { } /// ecmd.c:796, looper: the matches (x) or what lies between them (y). - fn looper(ex: *Exec, pat: []const u8, r: Range, xy: bool) Failure![]Range { + fn looper(ex: *Exec, pat: []const u8, fi: usize, r: Range, xy: bool) Failure![]Range { + const txt = ex.text(fi); var rx = try ex.compile(pat, if (xy) 'x' else 'y'); var out: std.ArrayList(Range) = .empty; var op: ?usize = if (xy) null else r.q0; @@ -617,13 +951,13 @@ const Exec = struct { while (p <= r.q1) { var tr: Range = undefined; var sel_end: usize = undefined; - if (try ex.find(&rx, p, r.q1)) |m| { + if (try ex.find(&rx, txt, p, r.q1)) |m| { if (m.start == m.end) { if (op != null and m.start == op.?) { - p = stepRune(ex.text, p); + p = stepRune(txt, p); continue; } - p = stepRune(ex.text, m.end); + p = stepRune(txt, m.end); } else p = m.end; tr = if (xy) .{ .q0 = clip(m.start), .q1 = clip(m.end) } else .{ .q0 = clip(op.?), .q1 = clip(m.start) }; sel_end = m.end; @@ -642,12 +976,13 @@ const Exec = struct { /// ecmd.c:840, linelooper: each line in the range, the first from where /// the range starts, the last cut where it ends. - fn lines(ex: *Exec, r: Range) Failure![]Range { + fn lines(ex: *Exec, fi: usize, r: Range) Failure![]Range { + const txt = ex.text(fi); var out: std.ArrayList(Range) = .empty; var p: usize = r.q0; while (p < r.q1) { - const nl = std.mem.indexOfScalarPos(u8, ex.text, p, '\n'); - const e = @min(if (nl) |i| i + 1 else ex.text.len, r.q1); + const nl = std.mem.indexOfScalarPos(u8, txt, p, '\n'); + const e = @min(if (nl) |i| i + 1 else txt.len, r.q1); if (e <= p) break; try out.append(ex.arena, .{ .q0 = clip(p), .q1 = clip(e) }); p = e; @@ -656,7 +991,8 @@ const Exec = struct { } }; -/// The text after `ops`, which are in order and do not overlap. +/// The text after `ops`, which are in order and do not overlap (a job's +/// with its output in place). pub fn apply(gpa: std.mem.Allocator, text: []const u8, ops: []const Op) ![]u8 { var n: usize = text.len; for (ops) |o| n = n - (o.q1 - o.q0) + o.text.len; @@ -675,9 +1011,9 @@ pub fn apply(gpa: std.mem.Allocator, text: []const u8, ops: []const Op) ![]u8 { return out; } -/// Where `dot` lands after `ops`: each end moved by the changes before it; -/// an empty dot where a change starts selects what it put there, as -/// elogapply does (elog.c:283). +/// Where `dot` lands after `ops` (their texts final): each end moved by the +/// changes before it; an empty dot where a change starts selects what it +/// put there, as elogapply does (elog.c:283). pub fn moveDot(dot: Range, ops: []const Op) Range { var shift: i64 = 0; for (ops) |o| { @@ -702,11 +1038,11 @@ fn edited(text: []const u8, command: []const u8) ![]u8 { var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena_state.deinit(); var why: Why = .{}; - const res = run(arena_state.allocator(), text, .{}, "t", command, &why) catch |err| { + const res = run(arena_state.allocator(), &.{.{ .name = "t", .text = text }}, 0, command, null, &why) catch |err| { if (err == error.Edit) std.debug.print("Edit: {s}\n", .{why.text()}); return err; }; - return apply(std.testing.allocator, text, res.ops); + return apply(std.testing.allocator, text, res.files[0].ops.items); } fn expectEdit(text: []const u8, command: []const u8, want: []const u8) !void { @@ -768,13 +1104,21 @@ test "an Edit that fails halfway changes nothing, and says why in acme's words" .{ "}", "right brace with no left brace" }, .{ ",x/foo/{", "unmatched `{'" }, .{ ",x/foo/{\nd", "unmatched `{'" }, - .{ "w /tmp/x", "w is a sam command pardes's Edit leaves out (it has no file or shell commands)" }, + .{ "B are jobs: a command per address, its output the change" { + var fx: Fixture = .init(); + defer fx.deinit(); + const res = try fx.edit(0, ",x/foo|bar/ | tr a-z A-Z\n$ > wc -c\n$ < date"); + try std.testing.expectEqual(@as(usize, 4), res.jobs.len); + try std.testing.expectEqual(@as(u8, '|'), res.jobs[0].c); + try std.testing.expectEqualStrings("tr a-z A-Z", res.jobs[0].command); + try std.testing.expectEqualStrings("foo", res.files[0].text[res.jobs[0].q0..res.jobs[0].q1]); + try std.testing.expectEqualStrings("bar", res.files[0].text[res.jobs[1].q0..res.jobs[1].q1]); + try std.testing.expectEqual(@as(u8, '>'), res.jobs[2].c); + try std.testing.expectEqual(@as(u8, '<'), res.jobs[3].c); + // `>` changes nothing; `|` and `<` hold their output's place. + const ops = res.files[0].ops.items; + try std.testing.expectEqual(@as(usize, 3), ops.len); + try std.testing.expectEqual(@as(?u32, 0), ops[0].job); + try std.testing.expectEqual(@as(?u32, 1), ops[1].job); + try std.testing.expectEqual(@as(?u32, 3), ops[2].job); + // ...out of order with the x before it: `1 <` reaches back. + try std.testing.expectError(error.Edit, fx.edit(0, ",x/bar/ | cat\n1 < date")); + try std.testing.expectEqualStrings("changes out of sequence", fx.why.text()); + // Across files with X: each job is its file's. + const x = try fx.edit(0, "X/\\.c$/ , | sort"); + try std.testing.expectEqual(@as(usize, 2), x.jobs.len); + try std.testing.expectEqual(@as(u32, 2), x.jobs[1].file); +} diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index 44cc1709..4fb024bb 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -18,11 +18,18 @@ pub const command_timeout_seconds: u64 = 10; pub const Input = struct { bytes: []const u8 }; /// Borrowed view exposed by the core while an effect is being drained. +/// An Edit's `<`, `|` and `>` (src/edit_cmd.zig) run a command of their own +/// per input, each in its file's directory: `commands` and `cwds`, when +/// given, name input i's, and `shell` (the session's Shell) runs them, +/// /bin/sh when it is empty. pub const Request = struct { id: u32, command: []const u8, cwd: []const u8, inputs: []const Input, + commands: []const []const u8 = &.{}, + cwds: []const []const u8 = &.{}, + shell: []const u8 = "", }; /// Worker-owned snapshot. `copy` is intentionally called synchronously while @@ -33,6 +40,10 @@ pub const Job = struct { command: []u8, cwd: []u8, inputs: [][]u8, + /// Input i's own command and directory, when the request gave them. + commands: [][]u8 = &.{}, + cwds: [][]u8 = &.{}, + shell: []u8 = &.{}, pub fn copy(gpa: std.mem.Allocator, request: Request) !*Job { const job = try gpa.create(Job); @@ -46,22 +57,45 @@ pub const Job = struct { errdefer gpa.free(job.command); job.cwd = try gpa.dupe(u8, filesystem.localPath(request.cwd) orelse request.cwd); errdefer gpa.free(job.cwd); - job.inputs = try gpa.alloc([]u8, request.inputs.len); - errdefer gpa.free(job.inputs); + job.inputs = try dupeAll(gpa, request.inputs.len, request.inputs, false); + errdefer freeAll(gpa, job.inputs); + job.commands = try dupeAll(gpa, request.commands.len, request.commands, false); + errdefer freeAll(gpa, job.commands); + job.cwds = try dupeAll(gpa, request.cwds.len, request.cwds, true); + errdefer freeAll(gpa, job.cwds); + job.shell = try gpa.dupe(u8, request.shell); + return job; + } + + /// Owned copies of `items` (Inputs or strings); a directory as the + /// host's own path, as `cwd` is. + fn dupeAll(gpa: std.mem.Allocator, n: usize, items: anytype, dirs: bool) ![][]u8 { + const out = try gpa.alloc([]u8, n); var made: usize = 0; - errdefer for (job.inputs[0..made]) |input| gpa.free(input); - for (request.inputs, 0..) |input, i| { - job.inputs[i] = try gpa.dupe(u8, input.bytes); + errdefer { + for (out[0..made]) |item| gpa.free(item); + gpa.free(out); + } + for (items) |item| { + const bytes: []const u8 = if (@TypeOf(item) == Input) item.bytes else item; + out[made] = try gpa.dupe(u8, if (dirs) filesystem.localPath(bytes) orelse bytes else bytes); made += 1; } - return job; + return out; + } + + fn freeAll(gpa: std.mem.Allocator, items: [][]u8) void { + for (items) |item| gpa.free(item); + gpa.free(items); } pub fn deinit(job: *Job, gpa: std.mem.Allocator) void { gpa.free(job.command); gpa.free(job.cwd); - for (job.inputs) |input| gpa.free(input); - gpa.free(job.inputs); + freeAll(gpa, job.inputs); + freeAll(gpa, job.commands); + freeAll(gpa, job.cwds); + gpa.free(job.shell); gpa.destroy(job); } }; @@ -193,6 +227,18 @@ pub fn runOne( command: []const u8, cwd: []const u8, input: []const u8, +) Outcome { + return runIn(gpa, io, "", command, cwd, input); +} + +/// `runOne` through `shell -c` (/bin/sh when it is empty). +pub fn runIn( + gpa: std.mem.Allocator, + io: std.Io, + shell: []const u8, + command: []const u8, + cwd: []const u8, + input: []const u8, ) Outcome { const fail = struct { fn k(kind: Failure.Kind) Outcome { @@ -200,7 +246,8 @@ pub fn runOne( } }; if (std.mem.indexOfScalar(u8, command, 0) != null or - std.mem.indexOfScalar(u8, cwd, 0) != null) return fail.k(.spawn); + std.mem.indexOfScalar(u8, cwd, 0) != null or + std.mem.indexOfScalar(u8, shell, 0) != null) return fail.k(.spawn); // std's spawn runs no code in the child: this thread's mask, which the // child inherits, is cleared across the fork (only the tty's SIGWINCH is @@ -211,7 +258,7 @@ pub fn runOne( std.posix.sigprocmask(std.posix.SIG.SETMASK, &none, &kept); defer std.posix.sigprocmask(std.posix.SIG.SETMASK, &kept, null); var child = std.process.spawn(io, .{ - .argv = &.{ "/bin/sh", "-c", command }, + .argv = &.{ if (shell.len == 0) "/bin/sh" else shell, "-c", command }, .cwd = if (cwd.len == 0) .inherit else .{ .path = cwd }, .stdin = .pipe, .stdout = .pipe, @@ -301,7 +348,9 @@ pub fn runJob(gpa: std.mem.Allocator, io: std.Io, job: *const Job) Response { var made: usize = 0; var total: usize = 0; for (job.inputs, 0..) |input, i| { - const output = switch (runOne(gpa, io, job.command, job.cwd, input)) { + const command = if (job.commands.len > 0) job.commands[i] else job.command; + const cwd = if (job.cwds.len > 0) job.cwds[i] else job.cwd; + const output = switch (runIn(gpa, io, job.shell, command, cwd, input)) { .ok => |bytes| bytes, .failed => |f| { // WHICH selection, because with several cursors "it failed" is @@ -400,3 +449,22 @@ test "native pipe runner preserves stdin/stdout bytes and reports how it failed" .failed => return error.EarlyStdinCloseShouldNotFail, } } + +test "a request may give each input its own command and directory, run by the shell it names" { + const gpa = std.testing.allocator; + const job = try Job.copy(gpa, .{ + .id = 3, + .command = "", + .cwd = "", + .inputs = &.{ .{ .bytes = "" }, .{ .bytes = "abc" } }, + .commands = &.{ "pwd", "tr a-z A-Z" }, + .cwds = &.{ "/tmp", "/" }, + .shell = "/bin/sh", + }); + defer job.deinit(gpa); + var response = runJob(gpa, std.testing.io, job); + defer response.deinit(gpa); + try std.testing.expect(response.success); + try std.testing.expectEqualStrings("/tmp\n", response.outputs[0]); + try std.testing.expectEqualStrings("ABC", response.outputs[1]); +} diff --git a/test/fs.py b/test/fs.py index 65b354d9..1303db0d 100644 --- a/test/fs.py +++ b/test/fs.py @@ -1008,9 +1008,68 @@ def cut_writes(binary): assert (screen['cols'], screen['rows']) == (132, 40), (screen['cols'], screen['rows']) +def edit_commands(binary): + """Edit's `< | >` run off the loop and answer the write that ran them + once applied; a failing one changes nothing and fails the write in its + words; X edits every open file it matches; w writes. The editor answers + other requests while a slow command runs.""" + with tempfile.TemporaryDirectory(prefix='pardes-edit-') as directory: + root = Path(directory) + (root / 'a.txt').write_bytes(b'pear\napple\nfig\n') + (root / 'b.txt').write_bytes(b'apple pie\n') + with session(binary, root, 'edit') as (client, address): + a = look(client, str(root / 'a.txt')) + b = look(client, str(root / 'b.txt')) + # The write returns with the text sorted, through the session's + # shell in the file's directory. + client.write(f'/pane/{a}/ctl', b'Edit , | sort\n') + assert client.read(f'/pane/{a}/body') == b'apple\nfig\npear\n' + client.write(f'/pane/{a}/ctl', b'Edit $ < pwd\n') + assert client.read(f'/pane/{a}/body') == f'apple\nfig\npear\n{root}\n'.encode() + client.write(f'/pane/{a}/ctl', b'Undo\n') + assert client.read(f'/pane/{a}/body') == b'apple\nfig\npear\n' + # A per-match command, one undo step. + client.write(f'/pane/{a}/ctl', b'Edit ,x/[a-z]+/ | tr a-z A-Z\n') + assert client.read(f'/pane/{a}/body') == b'APPLE\nFIG\nPEAR\n' + # A failing command: nothing changed, the write fails in words, + # its stderr in +Errors. + try: + client.write(f'/pane/{a}/ctl', b'Edit , | echo nope >&2; exit 4\n') + raise AssertionError('a failing Edit command was taken') + except OSError as why: + assert 'exit status 4' in str(why), why + assert client.read(f'/pane/{a}/body') == b'APPLE\nFIG\nPEAR\n' + index = client.read('/index').decode() + errors = [line.split()[0] for line in index.splitlines() if line.endswith(('+Errors', f'{root}/+Errors')) or '+Errors' in line] + assert errors and any(b'nope' in client.read(f'/pane/{e}/body') for e in errors), index + # While a slow command runs, the editor answers. + done = threading.Event() + + def slow(): + with Client(address) as other: + other.write(f'/pane/{b}/ctl', b'Edit , | sleep 1; cat\n') + done.set() + worker = threading.Thread(target=slow) + worker.start() + time.sleep(.2) + began = time.monotonic() + client.read('/index') + assert time.monotonic() - began < .5 and not done.is_set(), 'the editor waited on the command' + worker.join(timeout=15) + assert done.is_set() + # X: each open file whose line matches; w writes it. + client.write(f'/pane/{a}/ctl', b'Edit X/b\\.txt$/ ,s/apple/APPLE/\n') + assert client.read(f'/pane/{b}/body') == b'APPLE pie\n' + client.write(f'/pane/{b}/ctl', b'Edit w\n') + assert (root / 'b.txt').read_bytes() == b'APPLE pie\n' + client.write(f'/pane/{a}/ctl', b'Edit 1 w first.txt\n') + assert (root / 'first.txt').read_bytes() == b'APPLE\n' + + def test(binary, quic=False): started = time.monotonic() cut_writes(binary) + edit_commands(binary) odd_paths(binary) delcol_last_keeps_window(binary) run_file(binary) -- cgit v1.3 From 406f483288c7689bdd2e1f3f68b276c61ca9412a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 1 Oct 2026 20:50:48 -0300 Subject: An Edit's held write: < | > commands no longer block their 9P connection (answered like a held read, so a status read or a filter reading the session's own mount runs beside it), a Tflush or hang-up of the write kills the commands' process groups and changes nothing; e loads by Get's way (asked once, clean after, undo puts the name back); ~ in e r w f B; B checks every name first; an Edit that runs commands is a write of its own, refused up front with other lines; Edit's +Errors output keeps the keyboard where it was; X goes in pane order; a refused write open says its errno; the reference's Coming from acme rows say what Get file's undo, failure and directory refusal, Putall's answer, Zerox across Dump and Incl's directories now do Co-Authored-By: Claude Opus 5.5 --- docs/typ/reference.typ | 39 +++++---- src/9p_io.zig | 73 ++++++++++++++-- src/Pipe.zig | 6 +- src/edit_cmd.zig | 225 +++++++++++++++++++++++++++++++++++++++---------- src/fs.zig | 65 +++++++++++++- src/ninep/ctl.zig | 27 ++++-- src/pardes.zig | 20 ++--- src/sam_edit.zig | 138 ++++++++++++++++++++++++------ src/selection_pipe.zig | 120 +++++++++++++++++++++++++- test/fs.py | 31 +++++++ test/selfmount.py | 26 ++++++ 11 files changed, 649 insertions(+), 121 deletions(-) (limited to 'src/selection_pipe.zig') diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index 321860d6..efc08a9b 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -689,21 +689,30 @@ open file whose line matches. Commands: - `x y g v c a i d s p = m t u` and `{ }`. - `X`/`Y` run a command in each open file whose line (` +. /path`, `'` when - edited) matches, or does not: `X/'/w` writes every edited file. -- `b` makes a file current, `B` opens files, `D` closes panes (an edited one - on the second asking, as #word("Del")). -- `e` reads a file over the whole text and names the pane after it, `r` - over dot. `w` writes all of it, or the address, to its file or a name; - all of it to its own name leaves it clean. `f` names the pane and prints - its line. + edited) matches, or does not, in the order the panes were opened (as + #file("index") lists them): `X/'/w` writes every edited file. +- `b` makes a file current; `B` opens files, every name checked before any + opens; `D` closes panes (an edited one on the second asking, as + #word("Del")). +- `e` loads a file over the whole text as #word("Get") `file` does, once the + rest of the Edit is done: the pane takes its name and is clean, one undo + step puts both back, and unsaved edits are asked about once; the file + takes no other command in that Edit. `r` reads a file over dot. `w` writes + all of it, or the address, to its file or a name; all of it to its own + name leaves it clean. `f` names the pane and prints its line. File names + are relative to the file's directory, and `~` is the home directory. - `cmd` sends dot to it and prints what it writes. Each command runs in its file's directory through #word("Shell"), off the -loop, so the editor goes on; the write that ran the Edit is answered when -they are done, and a line after it in the same write is refused. A command -that fails (an exit status, 10 seconds, 1 MiB of output) fails the Edit, its -stderr in `+Errors`. Each file's changes are one undo step, applied only if +loop, so the editor goes on. The write that ran the Edit is held and +answered when they are done, its connection serving other requests +meanwhile (a command may read the session through its mount); a flush of +that write (an interrupted writer) kills the commands and changes nothing. +Such an Edit is a write of its own: one with other lines is refused before +any runs. One Edit's commands run at a time in a session, at most 1024 of +them. A command that fails (an exit status, 10 seconds, 1 MiB of output) +fails the Edit, its stderr in `+Errors`. Each file's changes are one undo step, applied only if every command succeeds; a failure changes nothing and fails the write with acme's words (`Edit: no substitution`), as does a file edited while the commands ran. An `x` that finds nothing succeeds silently. `p`, `=` and `>` @@ -1049,12 +1058,12 @@ with Client(sys.argv[1]) as c: # a socket path, or (ip, port) [`addr` reset on first open], [#file("addr") is the pane's; no open resets it (write `0`)], [OTRUNC on `data` ignored], [`: > data` deletes the addressed text; OTRUNC on #file("body") (`9p write`) replaces it all], [event flags 1, 2, 4, 8], [1, 4 and 8; never 2, so no expansion record follows], - [`Get`], [#word("Get") reads the file again, refusing unsaved edits once; `Get file` loads that file into the pane; in a directory pane it reads the directory again (also `get` on #file("ctl"))], + [`Get`], [#word("Get") reads the file again, refusing unsaved edits once; `Get file` loads that file into the pane, which takes its name, and one that fails changes nothing; #word("Undo") after it, or after #word("Edit")'s `e`, puts the old name back with the old text and its saved state; in a directory pane #word("Get") reads the directory again and `Get file` is refused (#word("Look") opens the file) (also `get` on #file("ctl"))], [`Put`, `Delete`, `Load`], [#word("Save"); a #word("Del") that does not ask; #word("Restore")], - [`Putall`], [#word("Putall"): every pane with unsaved edits to a file is saved, a refused one said], - [`Zerox`], [#word("Zerox"): a second pane on the buffer, one text, undo and unsaved state, its own scroll and cursor; an event reader on one hears no edit made through the other], + [`Putall`], [#word("Putall"): every pane with unsaved edits to a file is saved, a refused one said; with a refusal, its write is answered once the other saves have landed], + [`Zerox`], [#word("Zerox"): a second pane on the buffer, one text, undo and unsaved state, its own scroll and cursor; an event reader on one hears no edit made through the other; the twins are kept across #word("Dump") and #word("Restore")], [`Tab`], [#word("Tab") `N`, 1-16: for every pane, not per window; bare, it says the width], - [`Incl`], [#word("Incl"): for the session, not per window, seeded with `/usr/include` and `/usr/local/include`; a name found nowhere else (`stdio.h`, ``) is tried there], + [`Incl`], [#word("Incl"): for the session, not per window, seeded with `/usr/include` and `/usr/local/include`; it takes absolute (or `~`) directories that exist, and `Incl -` alone clears the list; a name found nowhere else (`stdio.h`, ``) is tried there], [`Indent`], [always on: #key("Enter"), #key("o") and #key("O") keep the line's indent as it is], [`Snarf`, `Cut`, `Paste`], [refused: the chords, #key("y"), #key("p")], [`Font`, `Send`, `ID`], [#word("Font"); #word("Repl") and #word("Exec"); `$winid`], diff --git a/src/9p_io.zig b/src/9p_io.zig index 46bbb2ba..a01eb491 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -180,6 +180,10 @@ pub const Listener = struct { restore_writer: std.atomic.Value(bool) = .init(false), /// Clients turned away with every slot taken, since the log last said so. refused: std.atomic.Value(u32) = .init(0), + /// Writes being served right now, a retry of a parked one included: a + /// held Edit write a retry has taken out of its park is not flushed + /// (watchEdit). + writes_out: std.atomic.Value(u32) = .init(0), /// Connections accepted so far, and the count each slot's connection /// was accepted at: a Restore cuts those accepted before it (`reset`), /// and a new client in a freed slot has a later stamp. Written on the @@ -241,6 +245,10 @@ pub const Listener = struct { fn onServe(ctx: ?*anyopaque, conn: *Runner.Conn, req: pardes.ctlfs.Req) void { const l = of(ctx); + if (req.op == .write) _ = l.writes_out.fetchAdd(1, .acq_rel); + defer if (req.op == .write) { + _ = l.writes_out.fetchSub(1, .acq_rel); + }; // Once `stop` has begun the editor is tearing down and may never // rest again: answer without waiting on it. if (l.stopping.load(.acquire)) { @@ -268,9 +276,11 @@ pub const Listener = struct { const changes = pardes.ctlfs.changesPane(core, req); const fills = pardes.ctlfs.handsOff(core, req); const reply = core.serveFs(req); - // Taken now: a request served while this one waits starts afresh. - const edit_at = core.fs.edit_answer_at; - core.fs.edit_answer_at = null; + const edit_started = core.fs.edit_started; + core.fs.edit_started = false; + // The held Edit write asked again by a retry, its Edit still + // running: parked again, as it was. + if (reply.status == .again and req.op == .write) return conn.reply(&reply, ""); if (req.op == .release and quiet) l.collectOs(); // A close's held last line runs now when no step is out, so its err // is logged before the close is answered, as before; with a step @@ -335,6 +345,19 @@ pub const Listener = struct { // is on its way out and will settle nothing this could wait for, // and `deinit` lets the answer out before it cuts the connections. if (core.quit) return conn.reply(&reply, ""); + // An Edit whose `<`, `|` or `>` commands run off the loop: the write + // is held, as a read with nothing yet is, and answered when the Edit + // is done (answerHeld), so the connection serves on meanwhile -- a + // filter that reads this session through a mount among what it + // serves. A flush of it, or a hang-up, stops the Edit (watchEdit). + if (edit_started and core.pipe.edit_run != null) { + const later: pardes.ctlfs.Reply = .{ .tag = req.tag, .status = .again }; + const ticket = conn.hold(&later) orelse return; + core.fs.edit_hold = .{ .asker = conn, .slot = ticket.slot, .seq = ticket.seq, .tag = req.tag, .node = req.node, .handle = req.handle, .written = reply.written }; + const watcher = std.Thread.spawn(.{}, watchEdit, .{ l, conn, ticket }) catch return; + watcher.detach(); + return; + } const restoring = core.restore_req != null; // Set with the turn still held, so `reset` sees it and waits. if (restoring) l.restore_writer.store(true, .release); @@ -360,10 +383,6 @@ pub const Listener = struct { } }; } - // An Edit whose `<`, `|` or `>` commands run off the loop: the write - // is answered once they have and the Edit is applied, or failed - // (late_failure), as `echo Edit , | sort > ctl` must return sorted. - if (edit_at) |n| if (l.core == core) pardes.turn.awaitEdit(n); core.fs.write_waits = false; // ponytail: one slot, so a failure of another client's effects that // settle in the same wait is told to this write too. @@ -418,6 +437,46 @@ pub const Listener = struct { const conn: *Runner.Conn = @ptrCast(@alignCast(held.asker)); if (!conn.answerWith(held.ticket, Held{ .core = core, .req = held.req }, Held.make)) o.held = null; } + // An Edit's held write, its Edit done. Not waiting: a retry has it + // out, and its asking answers it (Pardes.serveFs); or it was + // flushed, and watchEdit lets the record go. + if (core.fs.edit_hold) |h| if (h.done) { + const conn: *Runner.Conn = @ptrCast(@alignCast(h.asker)); + _ = conn.answerWith(.{ .slot = h.slot, .seq = h.seq }, core, struct { + fn make(c: *pardes.Pardes) ?Runner.Conn.Answer { + return .{ .reply = pardes.filesystem.EditHold.answer(&c.fs) }; + } + }.make); + }; + } + + /// While an Edit's write is held: when nobody waits for it any more -- + /// flushed (an interrupted writer), its fid clunked, its connection + /// hung up -- the Edit is stopped, its commands killed, nothing + /// changed (edit_cmd.cancel). The engine tells the backend nothing of a + /// flush, so it is looked for, every 100 ms; a park out for a retry is + /// not waiting either, so only three looks in a row with no write being + /// served count. + fn watchEdit(l: *Listener, conn: *Runner.Conn, ticket: cloud9.fs.Ticket) void { + const io = pardes.turn.io orelse return; + var misses: u8 = 0; + while (!l.stopping.load(.acquire)) { + io.sleep(.fromMilliseconds(100), .awake) catch return; + _ = pardes.turn.take(); + defer pardes.turn.give(); + const core = l.core; + const h = core.fs.edit_hold orelse return; + if (h.slot != ticket.slot or h.seq != ticket.seq or h.asker != @as(*anyopaque, conn)) return; + if (conn.waiting(ticket) or l.writes_out.load(.acquire) != 0) { + misses = 0; + continue; + } + misses += 1; + if (misses < 3) continue; + if (h.done) core.fs.edit_hold = null else @import("edit_cmd.zig").cancel(core); + l.kick(); + return; + } } /// A held read asked again, to make its answer while its park waits. diff --git a/src/Pipe.zig b/src/Pipe.zig index 6981c023..850f9c64 100644 --- a/src/Pipe.zig +++ b/src/Pipe.zig @@ -26,11 +26,7 @@ writes: std.ArrayList(edit_cmd.Write) = .empty, pub fn deinit(pipe: *Pipe, gpa: std.mem.Allocator) void { if (pipe.wait) |*wait| wait.deinit(gpa); - if (pipe.edit_run) |pending| { - pending.deinit(gpa); - // A 9P write waiting on it is answered (the core is going). - pardes.turn.editAnswered(); - } + if (pipe.edit_run) |pending| pending.deinit(gpa); for (pipe.writes.items) |*w| w.deinit(gpa); pipe.writes.deinit(gpa); } diff --git a/src/edit_cmd.zig b/src/edit_cmd.zig index 7f5c6279..4e2c09fd 100644 --- a/src/edit_cmd.zig +++ b/src/edit_cmd.zig @@ -11,7 +11,8 @@ //! Shell, and the Edit is applied when they have all answered -- or not at //! all, when one fails or a file it changes was edited meanwhile. One Edit //! waits at a time; a 9P write that ran it is answered when it is done -//! (9p_io, `fs.edit_answer_at`). +//! (9p_io, `fs.edit_hold`), the connection serving on meanwhile; a flush +//! of that write stops the commands and changes nothing. const std = @import("std"); const pardes = @import("pardes.zig"); const sam = @import("sam_edit.zig"); @@ -67,6 +68,27 @@ const World = struct { p: *Pardes, from: usize, + fn check(ctx: *anyopaque, path: []const u8, why: *sam.Why) bool { + const w: *World = @ptrCast(@alignCast(ctx)); + for (w.p.panes) |slot| { + const pane = slot orelse continue; + const f = ninep_pane.fileOf(pane) orelse continue; + if (std.mem.eql(u8, f.path, path)) return true; + } + if (comptime !pardes.hosted) return true; + const local = filesystem.localPath(path) orelse return true; + if (@import("exec.zig").isDirectory(local)) return said(bool, why, "B: {s} is a directory", .{path}) orelse false; + var buf: [4096]u8 = undefined; + if (filesystem.resolveOs(local, &buf) == null) return said(bool, why, "B: {s}: no such file", .{path}) orelse false; + return true; + } + + fn refuseGet(ctx: *anyopaque, id: usize, why: *sam.Why) bool { + const w: *World = @ptrCast(@alignCast(ctx)); + const words = @import("ninep/ctl.zig").getRefused(w.p, w.p.panes[id] orelse return false, "e") orelse return false; + return said(bool, why, "{s}", .{words}) orelse true; + } + fn open(ctx: *anyopaque, path: []const u8, why: *sam.Why) ?sam.File { const w: *World = @ptrCast(@alignCast(ctx)); const p = w.p; @@ -135,20 +157,27 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void { defer if (!keep) pd.deinit(p.gpa); const arena = pd.arena.allocator(); - // Every text pane, column by column as acme's allwindows goes, then any - // not in a column; the Edit starts in its own. + // Every text pane in the order they were made, as /index lists them, + // so X goes the same way whatever the layout; the Edit starts in its own. var files: std.ArrayList(sam.File) = .empty; - var cur: usize = 0; - var seen: [pardes.MAX_PANES]bool = @splat(false); - for (0..p.ncol) |c| { - for (p.col_panes[c][0..p.col_n[c]]) |slot| addFile(p, &files, arena, slot, id, &cur, &seen) catch |err| return p.reportError(id, "Edit", err); + for (p.panes, 0..) |slot, i| { + const q = slot orelse continue; + if (q.file == null) continue; + files.append(arena, fileOf(q, i)) catch |err| return p.reportError(id, "Edit", err); } - for (0..pardes.MAX_PANES) |slot| addFile(p, &files, arena, slot, id, &cur, &seen) catch |err| return p.reportError(id, "Edit", err); + std.mem.sort(sam.File, files.items, p, struct { + fn before(core: *Pardes, a: sam.File, b: sam.File) bool { + return core.panes[a.id].?.serial < core.panes[b.id].?.serial; + } + }.before); + const cur = for (files.items, 0..) |f, i| { + if (f.id == id) break i; + } else unreachable; var world: World = .{ .p = p, .from = id }; var why: sam.Why = .{}; var text: [260]u8 = undefined; - const res = sam.run(arena, files.items, cur, command, .{ .ctx = &world, .open = World.open, .read = World.read }, &why) catch |err| switch (err) { + const res = sam.run(arena, files.items, cur, command, .{ .ctx = &world, .check = World.check, .open = World.open, .read = World.read, .refuseGet = World.refuseGet }, &why) catch |err| switch (err) { error.Edit => return p.reportFailure(id, std.fmt.bufPrint(&text, "Edit: {s}", .{why.text()}) catch "Edit: failed"), error.OutOfMemory => return p.reportError(id, "Edit", err), }; @@ -190,21 +219,45 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void { .commands = commands, .cwds = cwds, .shell = arena.dupe(u8, p.shellBin()) catch |err| return p.reportError(id, "Edit", err), + .token = selection_pipe.newToken(), }; keep = true; p.pipe.edit_run = pd; p.emit(.{ .pipe = .{ .id = pd.id } }); - // A 9P write that ran it is answered once it is done. - if (p.fs.serving) p.fs.edit_answer_at = pardes.turn.edit_answers; + // A 9P write that ran it is held, and answered once it is done + // (9p_io, fs.edit_hold); the connection serves on meanwhile. + if (p.fs.serving) p.fs.edit_started = true; +} + +/// The Edit waiting on its commands, stopped: its write was flushed, or +/// its connection hung up (9p_io). Its commands are killed, and nothing +/// changes. +pub fn cancel(p: *Pardes) void { + p.fs.edit_hold = null; + const pd = p.pipe.edit_run orelse return; + p.pipe.edit_run = null; + selection_pipe.stop(pd.request.token); + if (p.panes[pd.origin]) |pane| if (pane.serial == pd.origin_serial) + p.setMessage(pd.origin, "Edit: interrupted; nothing changed"); + pd.deinit(p.gpa); } -fn addFile(p: *Pardes, files: *std.ArrayList(sam.File), arena: std.mem.Allocator, slot: usize, from: usize, cur: *usize, seen: *[pardes.MAX_PANES]bool) !void { - if (seen[slot]) return; - seen[slot] = true; - const pane = p.panes[slot] orelse return; - if (pane.file == null) return; - if (slot == from) cur.* = files.items.len; - try files.append(arena, fileOf(pane, slot)); +/// The held 9P write that ran the Edit has its answer: done, failed in the +/// words said on pane `at` when `failed`. +fn answerHold(p: *Pardes, at: usize, failed: bool) void { + const h = if (p.fs.edit_hold) |*held| held else return; + h.done = true; + p.fs.news = true; + if (!failed) return; + const words = if (p.fs.said_at == p.fs.failures and p.fs.said_len > 0) + p.fs.said[0..p.fs.said_len] + else if (p.panes[at]) |pane| pane.msg[0..pane.msg_len] else "Edit: failed"; + const kept = @import("Messages.zig").clip(words, h.said.len); + @memcpy(h.said[0..kept.len], kept); + h.said_len = @intCast(kept.len); + h.errno = @import("ninep/ctl.zig").failureErrno(kept); + // The write's err record, as a refusal answered at once has. + pardes.ctlfs.events.noteError(p, .{ .tag = h.tag, .op = .write, .node = h.node, .handle = h.handle }, @import("ninep/tree.zig").failText(h.tag, h.errno, h.said[0..h.said_len])); } /// The commands answered (pipe_resp): the Edit applied, or why not. @@ -212,11 +265,10 @@ pub fn finish(p: *Pardes, success: bool, outputs: []const []const u8, failure: ? const pd = p.pipe.edit_run orelse return; p.pipe.edit_run = null; defer pd.deinit(p.gpa); - defer pardes.turn.editAnswered(); const at = if (p.panes[pd.origin]) |pane| (if (pane.serial == pd.origin_serial) pd.origin else p.active) else p.active; // Whatever fails from here fails the 9P write that ran it too. const failures = p.fs.failures; - defer if (p.fs.failures != failures) p.noteLateFailure(at); + defer answerHold(p, at, p.fs.failures != failures); if (success and outputs.len == pd.res.jobs.len) return commit(p, pd, outputs); // Which command, in +Errors with its own words; the Edit changed nothing. @@ -232,7 +284,9 @@ pub fn finish(p: *Pardes, success: bool, outputs: []const []const u8, failure: ? Pipe.writeFailure(w, failure) catch break :report; const why = std.mem.sliceTo(out.written()[why_at..], '\n'); const said = std.fmt.bufPrint(&line, "Edit: {c} {s}: {s}", .{ job.c, job.command[0..@min(job.command.len, 120)], why }) catch "Edit: a command failed"; + const was = p.active; _ = ninep_pane.appendErrors(p, at, out.written()); + if (p.panes[was] != null) p.active = was; return p.reportFailure(at, said); } p.reportFailure(at, "Edit: a command failed"); @@ -245,12 +299,21 @@ fn commit(p: *Pardes, pd: *Pending, outputs: []const []const u8) void { const at = if (p.panes[pd.origin]) |pane| (if (pane.serial == pd.origin_serial) pd.origin else p.active) else p.active; var text: [320]u8 = undefined; for (res.files, 0..) |f, i| { - if (f.ops.items.len == 0 and !f.close and !f.renamed and !f.moved) continue; + if (f.ops.items.len == 0 and !f.close and !f.renamed and !f.moved and f.get == null) continue; const pane = p.panes[f.id] orelse return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} closed while its commands ran; nothing changed", .{f.name}) catch "Edit: a pane closed"); if (pane.serial != pd.serials[i]) return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} closed while its commands ran; nothing changed", .{f.name}) catch "Edit: a pane closed"); - if (f.ops.items.len > 0 and pane.file.?.revision != pd.revisions[i]) + if ((f.ops.items.len > 0 or f.get != null) and pane.file.?.revision != pd.revisions[i]) return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} changed while its commands ran; nothing changed", .{f.name}) catch "Edit: a file changed"); } + // e first, Get's way: read before anything is changed, so one that + // cannot be read changes nothing; its own undo step puts the old name + // back with the old text, and it leaves the pane clean. + for (res.files) |f| { + const path = f.get orelse continue; + var failed: anyerror = error.Unexpected; + const errno = @import("ninep/ctl.zig").getFile(p, p.panes[f.id].?, path, &failed); + if (errno != 0) return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: e {s}: {s}", .{ path, if (errno == @import("ninep/tree.zig").E.NOENT) "no such file" else "cannot be read" }) catch "Edit: e failed"); + } // `w`'s bytes, written after this step, before anything else is. for (res.writes) |wr| { const f = res.files[wr.file]; @@ -291,11 +354,6 @@ fn commit(p: *Pardes, pd: *Pending, outputs: []const []const u8) void { panes.File.pushUndo(p, pane); panes.File.setContent(p, pf, new); } - // e read its own file over it: what is on disk again. - if (f.clean) { - pf.saved_revision = pf.revision; - pf.disk_gone = false; - } } if (ops.len > 0 or f.moved) ninep_pane.setDot(pane, sam.moveDot(f.dot, ops)); if (f.renamed) ninep_pane.nameBuffer(p, f.id, f.name, false) catch |err| p.reportError(f.id, "Edit", err); @@ -312,7 +370,11 @@ fn commit(p: *Pardes, pd: *Pending, outputs: []const []const u8) void { from = job.at; } out.appendSlice(p.gpa, res.printed[from..]) catch return; + // Printed, the keyboard stays where it was: a +Errors made for it + // does not take it, so the next Edit lands where this one did. + const was = p.active; if (out.items.len > 0) _ = ninep_pane.appendErrors(p, at, out.items); + if (p.panes[was] != null) p.active = was; } // D's panes last, each as Del closes one: an edited one is warned about // once, and closes on the next. @@ -344,6 +406,17 @@ fn runReal(p: *Pardes) !void { p.update(.{ .pipe_resp = .{ .id = pd.id, .success = response.success, .outputs = response.outputs, .failure = response.failure } }); } +/// Whether any +Errors holds `needle`. +fn errorsHas(p: *Pardes, needle: []const u8) bool { + for (p.panes) |slot| { + const q = slot orelse continue; + const qf = q.file orelse continue; + const o = qf.output orelse continue; + if (std.meta.activeTag(o.from) == .errors and std.mem.indexOf(u8, qf.content, needle) != null) return true; + } + return false; +} + fn errorsText(p: *Pardes) ?[]const u8 { for (p.panes) |slot| { const q = slot orelse continue; @@ -358,8 +431,9 @@ test "Edit , | sort runs off the loop, through the session's shell in the file's const p = try th.withFile(testing.allocator, "b\na\nc\n"); defer p.deinit(); const serial = th.serialOf(p); - const answers = pardes.turn.edit_answers; try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(serial, .ctl), "Edit , | sort\n").reply.status); + // The listener is told to hold the write. + try testing.expect(p.fs.edit_started); // Nothing changed yet: the command is out. try testing.expectEqualStrings("b\na\nc\n", p.panes[0].?.file.?.content); const req = p.pipe.pipeRequest(p.pipe.edit_run.?.id).?; @@ -371,17 +445,55 @@ test "Edit , | sort runs off the loop, through the session's shell in the file's try testing.expectEqual(tree.E.IO, th.wr(p, tree.Node.of(serial, .ctl), "Edit ,d\n").errno()); answer(p, &.{"a\nb\nc\n"}); try testing.expect(p.pipe.edit_run == null); - try testing.expectEqual(answers +% 1, pardes.turn.edit_answers); try testing.expectEqualStrings("a\nb\nc\n", p.panes[0].?.file.?.content); try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(serial, .ctl), "Undo\n").reply.status); try testing.expectEqualStrings("b\na\nc\n", p.panes[0].?.file.?.content); - // What follows it in the same write would run before its commands - // answer: refused, and the Edit goes on. - const after = th.wr(p, tree.Node.of(serial, .ctl), "Edit , | sort\nSave\n"); - try testing.expectEqual(tree.E.INVAL, after.errno()); - try testing.expectEqualStrings(@import("ninep/ctl.zig").e_after_edit, after.reply.ename); - answer(p, &.{"a\nb\nc\n"}); - try testing.expectEqualStrings("a\nb\nc\n", p.panes[0].?.file.?.content); + // Another line in its write would run before its commands answer: the + // whole write is refused before any line runs, the one before it too. + for ([_][]const u8{ "Edit , | sort\nSave\n", "Msg before\nEdit , | sort\nMsg after\n" }) |two| { + const after = th.wr(p, tree.Node.of(serial, .ctl), two); + try testing.expectEqual(tree.E.INVAL, after.errno()); + try testing.expectEqualStrings(@import("ninep/ctl.zig").e_after_edit, after.reply.ename); + try testing.expect(p.pipe.edit_run == null and !th.logHas(p, "before")); + } + try testing.expectEqual(tree.E.INVAL, th.wr(p, @intFromEnum(tree.TopFile.exec), "Msg before\nEdit , | sort\n").errno()); +} + +test "the held write asked again is parked again, then answered once, and a flush stops the Edit" { + const p = try th.withFile(testing.allocator, "b\na\n"); + defer p.deinit(); + const ctl = tree.Node.of(th.serialOf(p), .ctl); + const req: tree.Req = .{ .tag = 7, .op = .write, .node = ctl, .handle = 0, .data = "Edit , | sort\n" }; + try testing.expectEqual(tree.Status.ok, p.serveFs(req).status); + // As 9p_io holds it. + p.fs.edit_hold = .{ .asker = @ptrCast(p), .slot = 0, .seq = 1, .tag = 7, .node = ctl, .handle = 0, .written = @intCast(req.data.len) }; + // A retry of its park: still waiting, never run a second time. + try testing.expectEqual(tree.Status.again, p.serveFs(req).status); + try testing.expect(p.pipe.edit_run != null); + answer(p, &.{"a\nb\n"}); + try testing.expect(p.fs.edit_hold.?.done); + const done = p.serveFs(req); + try testing.expectEqual(tree.Status.ok, done.status); + try testing.expectEqual(@as(u32, @intCast(req.data.len)), done.written); + try testing.expect(p.fs.edit_hold == null); + try testing.expectEqualStrings("a\nb\n", p.panes[0].?.file.?.content); + // A failure is the held write's answer, in the Edit's words. + _ = p.serveFs(.{ .tag = 8, .op = .write, .node = ctl, .data = "Edit , | false\n" }); + p.fs.edit_hold = .{ .asker = @ptrCast(p), .slot = 0, .seq = 2, .tag = 8, .node = ctl, .handle = 0, .written = 15 }; + p.update(.{ .pipe_resp = .{ .id = p.pipe.edit_run.?.id, .success = false, .outputs = &.{}, .failure = .{ .kind = .exit, .code = 1 } } }); + const failed = filesystem.EditHold.answer(&p.fs); + try testing.expectEqual(tree.Status.err, failed.status); + try testing.expectEqualStrings("Edit: | false: exit status 1", failed.ename); + // Flushed (or hung up): its commands stopped, nothing changed, and the + // next Edit is free to run; the stopped one's late answer is no one's. + _ = p.serveFs(.{ .tag = 9, .op = .write, .node = ctl, .data = "Edit , | sort -r\n" }); + const stale = p.pipe.edit_run.?.id; + p.fs.edit_hold = .{ .asker = @ptrCast(p), .slot = 0, .seq = 3, .tag = 9, .node = ctl, .handle = 0, .written = 18 }; + cancel(p); + try testing.expect(p.pipe.edit_run == null and p.fs.edit_hold == null); + p.update(.{ .pipe_resp = .{ .id = stale, .success = true, .outputs = &.{"b\na\n"} } }); + try testing.expectEqualStrings("a\nb\n", p.panes[0].?.file.?.content); + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit , | cat\n").reply.status); } test "Edit's < | and > with a real shell: a command per address, > printed to +Errors" { @@ -399,6 +511,8 @@ test "Edit's < | and > with a real shell: a command per address, > printed to +E try testing.expectEqualStrings("ONE TWO\nnew", p.panes[0].?.file.?.content); try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit , > wc -l\n").reply.status); try runReal(p); + // The +Errors it opened did not take the keyboard. + try testing.expectEqual(@as(usize, 0), p.active); try testing.expectEqualStrings("ONE TWO\nnew", p.panes[0].?.file.?.content); try testing.expectEqualStrings("1", std.mem.trim(u8, errorsText(p).?, " \n")); } @@ -409,11 +523,12 @@ test "a failing command changes nothing and says why, its stderr in +Errors" { defer p.deinit(); const ctl = tree.Node.of(th.serialOf(p), .ctl); try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit , | echo boom >&2; exit 3\n").reply.status); - p.fs.late_failure_len = 0; + p.fs.edit_hold = .{ .asker = @ptrCast(p), .slot = 0, .seq = 1, .tag = 2, .node = ctl, .handle = 0, .written = 1 }; try runReal(p); try testing.expectEqualStrings("keep\n", p.panes[0].?.file.?.content); try testing.expectEqual(@as(usize, 0), p.panes[0].?.file.?.history.undo_len); - try testing.expectEqualStrings("Edit: | echo boom >&2; exit 3: exit status 3", p.fs.late_failure[0..p.fs.late_failure_len]); + // The held write's answer is the failure, in its words. + try testing.expectEqualStrings("Edit: | echo boom >&2; exit 3: exit status 3", filesystem.EditHold.answer(&p.fs).ename); const report = errorsText(p).?; try testing.expect(std.mem.indexOf(u8, report, "Edit: | echo boom") != null); try testing.expect(std.mem.indexOf(u8, report, "boom\n") != null); @@ -422,12 +537,13 @@ test "a failing command changes nothing and says why, its stderr in +Errors" { test "an Edit whose file is edited while its commands run is refused whole" { const p = try th.withFile(testing.allocator, "abc\n"); defer p.deinit(); - try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(th.serialOf(p), .ctl), "Edit , | cat\n").reply.status); + const ctl = tree.Node.of(th.serialOf(p), .ctl); + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit , | cat\n").reply.status); + p.fs.edit_hold = .{ .asker = @ptrCast(p), .slot = 0, .seq = 1, .tag = 2, .node = ctl, .handle = 0, .written = 1 }; panes.File.setContent(p, &p.panes[0].?.file.?, try testing.allocator.dupe(u8, "typed\n")); - p.fs.late_failure_len = 0; answer(p, &.{"ABC\n"}); try testing.expectEqualStrings("typed\n", p.panes[0].?.file.?.content); - try testing.expectEqualStrings("Edit: /test.txt changed while its commands ran; nothing changed", p.fs.late_failure[0..p.fs.late_failure_len]); + try testing.expectEqualStrings("Edit: /test.txt changed while its commands ran; nothing changed", filesystem.EditHold.answer(&p.fs).ename); } test "X over open files, w, e, r, f, B and D on panes and the disk" { @@ -439,6 +555,7 @@ test "X over open files, w, e, r, f, B and D on panes and the disk" { try tmp.dir.writeFile(testing.io, .{ .sub_path = "b.h", .data = "foo\n" }); try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.c", .data = "foo bar\n" }); try tmp.dir.writeFile(testing.io, .{ .sub_path = "data", .data = "DATA\n" }); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "n.c", .data = "new\n" }); var dir_buf: [4096]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; const p = try th.withFile(gpa, "scratch\n"); @@ -453,6 +570,11 @@ test "X over open files, w, e, r, f, B and D on panes and the disk" { try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit X/\\.[ch]$/ ,s/foo/FOO/g\n").reply.status); try testing.expectEqualStrings("int FOO;\n", p.panes[a].?.file.?.content); try testing.expectEqualStrings("scratch\n", p.panes[0].?.file.?.content); + // X alone lists them in the order they were opened, as /index does. + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit X\n").reply.status); + const listed = errorsText(p).?; + try testing.expect(std.mem.indexOf(u8, listed, "/test.txt").? < std.mem.indexOf(u8, listed, "/a.c").?); + try testing.expect(std.mem.indexOf(u8, listed, "/a.c").? < std.mem.indexOf(u8, listed, "/b.h").?); // B opens a file, which a `"re"` address then edits, from pane 0. var cmd: [4400]u8 = undefined; try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, try std.fmt.bufPrint(&cmd, "Edit B {s}/c.c\n", .{dir})).reply.status); @@ -469,11 +591,26 @@ test "X over open files, w, e, r, f, B and D on panes and the disk" { try testing.expect(!ninep_pane.dirtyOf(p.panes[a].?)); try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(p.panes[c].?.serial, .ctl), "Edit 1 r data\n").reply.status); try testing.expectEqualStrings("DATA\n", p.panes[c].?.file.?.content); - // e of an edited file is refused; f names one and prints it. - try testing.expectEqual(tree.E.IO, th.wr(p, tree.Node.of(p.panes[c].?.serial, .ctl), "Edit e\n").errno()); + // e of an edited file is asked about once, as Get asks; again, it is + // read in Get's way, which leaves the pane clean and named after it. + const c_ctl = tree.Node.of(p.panes[c].?.serial, .ctl); + try testing.expect(th.wr(p, c_ctl, "Edit e data\n").reply.status == .err); + try testing.expectEqualStrings("DATA\n", p.panes[c].?.file.?.content); + try testing.expect(ninep_pane.dirtyOf(p.panes[c].?)); + try testing.expectEqual(tree.Status.ok, th.wr(p, c_ctl, "Edit e data\n").reply.status); + try testing.expect(std.mem.endsWith(u8, p.panes[c].?.file.?.path, "/data")); + try testing.expect(!ninep_pane.dirtyOf(p.panes[c].?)); + // B checks every name first: a directory among them opens none. + const before = p.next_serial; + try testing.expect(th.wr(p, ctl, try std.fmt.bufPrint(&cmd, "Edit B {s}/n.c {s}\n", .{ dir, dir })).reply.status == .err); + try testing.expectEqual(before, p.next_serial); + // f names one and prints it. + try testing.expectEqual(tree.Status.ok, th.wr(p, c_ctl, "Edit 1 c/edited/\n").reply.status); try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(p.panes[c].?.serial, .ctl), "Edit f d.c\n").reply.status); try testing.expect(std.mem.endsWith(u8, p.panes[c].?.file.?.path, "/d.c")); - try testing.expect(std.mem.indexOf(u8, errorsText(p).?, "'+. ") != null); + // In its directory's +Errors, or the log when the screen has no room + // left for one. + try testing.expect(errorsHas(p, "'+. ") or th.logHas(p, "'+. ")); // D closes a pane by name, an edited one (b.h, by the X) on the // second asking, as Del does. const b_serial = for (p.panes) |slot| { diff --git a/src/fs.zig b/src/fs.zig index 2a5102f4..db07672e 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -85,6 +85,14 @@ pub const WriteError = error{ NoSpaceLeft, OpenFailed, WriteFailed, + // The rest are open(2)'s errno, named so errorWords says them. + NoSuchFileOrDirectory, + NotDir, + InvalidArgument, + FileBusy, + SymLinkLoop, + NoSuchDeviceOrAddress, + FileTooBig, }; /// The host takes the bytes. The caller has given the turn up (`write`), @@ -102,6 +110,13 @@ pub fn writeFile(path: []const u8, bytes: []const u8) WriteError!void { .ROFS => error.ReadOnlyFilesystem, .NOSPC, .DQUOT => error.NoSpaceLeft, .NAMETOOLONG => error.PathTooLong, + .NOENT => error.NoSuchFileOrDirectory, + .NOTDIR => error.NotDir, + .INVAL => error.InvalidArgument, + .BUSY, .TXTBSY => error.FileBusy, + .LOOP => error.SymLinkLoop, + .NXIO, .NODEV => error.NoSuchDeviceOrAddress, + .FBIG => error.FileTooBig, else => error.OpenFailed, }; var off: usize = 0; @@ -1554,6 +1569,36 @@ fn readFileLimit(gpa: std.mem.Allocator, path: []const u8, limit: usize) ![]u8 { return buf; } +/// A 9P write of an Edit held while its commands run (Namespace.edit_hold). +pub const EditHold = struct { + /// The listener's: the connection that asked, and its park. + asker: *anyopaque, + slot: u16, + seq: u64, + tag: u64, + node: u64, + handle: u32, + written: u32, + /// The Edit is done: applied, or failed with `said`. + done: bool = false, + errno: u16 = 0, + said: [256]u8 = undefined, + said_len: u16 = 0, + + pub fn names(h: *const EditHold, req: tree.Req) bool { + return req.op == .write and req.tag == h.tag and req.node == h.node and req.handle == h.handle; + } + + /// Its answer, the held record let go: the words copied out first. + pub fn answer(ns: *Namespace) tree.Reply { + const h = ns.edit_hold.?; + ns.edit_hold = null; + if (h.errno == 0) return .{ .tag = h.tag, .written = h.written }; + @memcpy(ns.edit_said[0..h.said_len], h.said[0..h.said_len]); + return tree.failText(h.tag, h.errno, ns.edit_said[0..h.said_len]); + } +}; + pub const Namespace = struct { socket_path: []const u8 = "", tcp_address: ?std.Io.net.IpAddress = null, @@ -1613,9 +1658,15 @@ pub const Namespace = struct { /// A 9P write asked a language server something: the answer count it /// waits to change (`turn.lsp_answers`), so its failure is the write's. lsp_answer_at: ?u64 = null, - /// A write ran an Edit whose `<`, `|` or `>` commands are running: it is - /// answered once `turn.edit_answers` passes this, the Edit done. - edit_answer_at: ?u64 = null, + /// The request being served started an Edit whose `<`, `|` or `>` + /// commands run off the loop (edit_cmd.zig): the listener holds it. + edit_started: bool = false, + /// That write, held: answered once `done` (9p_io answerHeld), or + /// asked again by a retry of its park (Pardes.serveFs); a flush or a + /// hang-up stops the Edit (9p_io watchEdit). + edit_hold: ?EditHold = null, + /// The words a held Edit write was refused with, kept for its answer. + edit_said: [256]u8 = undefined, /// The pane a language answer filled (a Rename's preview): what the /// waiting 9P write answers once it is in (9p_io). lsp_result: ?u32 = null, @@ -1757,3 +1808,11 @@ test { test "the tree's README lists src/ only when the build serves it" { try std.testing.expectEqual(limits.embedded_sources, std.mem.indexOf(u8, help, "src/") != null); } + +test "a write open(2) refuses says its errno in words" { + if (comptime @import("builtin").os.tag != .linux or !pardes.hosted) return error.SkipZigTest; + // /proc takes no new file: ENOENT, which used to read "open failed". + try std.testing.expectError(error.NoSuchFileOrDirectory, writeFile("/proc/pardes-no-such-file", "x")); + var words: [128]u8 = undefined; + try std.testing.expectEqualStrings("no such file or directory", pardes.Messages.errorWords(error.NoSuchFileOrDirectory, &words)); +} diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 274caf88..8ece7ead 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -193,6 +193,7 @@ pub fn command(p: *Pardes, req: Req, serial: ?u32, exec: bool, in_tag: bool) Rep // The root's lines run at the keyboard's pane: a refusal before any runs // is that pane's too, in its err record. p.fs.ran_at = if (serial == null) (if (p.panes[p.active]) |pane| pane.serial else 0) else 0; + if (editNotAlone(p, req.data)) return tree.failText(req.tag, E.INVAL, e_after_edit); for ([2]bool{ false, true }) |apply| { if (apply) p.fs.results_len = 0; var it = Messages.init(p, req.data); @@ -203,7 +204,6 @@ pub fn command(p: *Pardes, req: Req, serial: ?u32, exec: bool, in_tag: bool) Rep for (text) |c| if ((c < ' ' and c != '\t' and c != '\n') or c == 0x7f) return tree.failText(req.tag, E.INVAL, e_control); if (exec) if (tooLong(req, text)) |refusal| return refusal; if (!apply) continue; - if (p.fs.edit_answer_at != null) return tree.failText(req.tag, E.INVAL, e_after_edit); var id = if (serial) |s| p.paneBySerial(s) orelse break else p.active; // No pane at all: the root's look and exec act in the session's // directory. An exec runs as the workspace tag's; a look from a @@ -260,10 +260,25 @@ pub fn tooLong(req: Req, line: []const u8) ?Reply { pub const e_control = "invalid command line: it holds a control character (or DEL) other than a tab"; -/// An Edit whose `<`, `|` or `>` commands are running answers its write -/// when they are done (edit_cmd.zig); a line after it in the same write -/// would run before them, on the text they are about to change. -pub const e_after_edit = "invalid command line: an Edit running commands ends its write; write what follows after it"; +/// An Edit whose `<`, `|` or `>` commands run answers its write when they +/// are done (edit_cmd.zig); a line after it in the same write would run +/// before them, on the text they are about to change. Refused up front, +/// before any line runs. +pub const e_after_edit = "invalid command line: an Edit that runs commands is a write of its own"; + +/// Whether `data` holds an Edit that runs commands and any other line. +fn editNotAlone(p: *Pardes, data: []const u8) bool { + const sam = @import("../sam_edit.zig"); + var lines: usize = 0; + var runs = false; + var it = Messages.init(p, data); + while (it.next()) |line| { + if (line.len == 0 or exec_line.isComment(line)) continue; + lines += 1; + if (std.mem.startsWith(u8, line, "Edit") and sam.runsCommands(p.scratch.allocator(), line["Edit".len..])) runs = true; + } + return runs and lines > 1; +} /// Runs one click (`run`) as a 9P write's: a builtin that fails there fails /// the write, with its words and an err record, and no msg, as a ctl line @@ -959,13 +974,13 @@ pub fn writePane(p: *Pardes, req: Req, pane: *Pane) Reply { const serial = pane.serial; // Checked whole before anything applies, so a write that must wait for // the lock has done nothing yet when it goes again. + if (editNotAlone(p, req.data)) return tree.failText(req.tag, E.INVAL, e_after_edit); for ([2]bool{ false, true }) |apply| { if (apply) p.fs.results_len = 0; var held = !other and pane.fs.lock != null; var it = Messages.init(p, req.data); while (it.next()) |line_in| { if (line_in.len == 0 or exec_line.isComment(line_in)) continue; - if (apply and p.fs.edit_answer_at != null) return tree.failText(req.tag, E.INVAL, e_after_edit); // A line before this one may have closed the pane (`Del`). if (apply and p.paneBySerial(serial) == null) return Reply.fail(req.tag, E.NOENT); // acme's ctl words: done by the file that replaces each, or the diff --git a/src/pardes.zig b/src/pardes.zig index cf3d5128..e92f9623 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -123,9 +123,6 @@ pub const Turn = struct { /// Bumped each time a language server's answer (or its lack) is taken /// in (`lspResponse`): a write that asked one waits for this. lsp_answers: u64 = 0, - /// Bumped each time an Edit's commands (`< | >`) have answered and the - /// Edit is done, or failed (edit_cmd.zig): a write that ran one waits. - edit_answers: u64 = 0, /// All of the above, and `out` reaching zero. settled: std.Io.Condition = .init, @@ -223,15 +220,6 @@ pub const Turn = struct { while (t.lsp_answers == answers) t.settled.wait(t.io.?, &t.mutex) catch return; } - pub fn editAnswered(t: *Turn) void { - t.edit_answers +%= 1; - const io = t.io orelse return; - t.settled.broadcast(io); - } - - pub fn awaitEdit(t: *Turn, answers: u64) void { - while (t.edit_answers == answers) t.settled.wait(t.io.?, &t.mutex) catch return; - } pub fn awaitRestored(t: *Turn, restores: u64) void { while (t.restores == restores) t.settled.wait(t.io.?, &t.mutex) catch return; @@ -5918,6 +5906,12 @@ pub const Pardes = struct { /// that changes a pane costs a frame, which is why a round trip on a /// local socket costs microseconds and not a vsync. pub fn serveFs(p: *Pardes, req: ctlfs.Req) ctlfs.Reply { + // The held write of an Edit whose commands run, asked again by a + // retry of its park: still waiting, or its answer, never run twice. + if (p.fs.edit_hold) |*h| if (h.names(req)) { + if (!h.done) return .{ .tag = req.tag, .status = .again }; + return filesystem.EditHold.answer(&p.fs); + }; defer panes.File.syncTwins(p); if (!ctlfs.changesPane(p, req)) return ctlfs.handle(p, req); // An address, a limit or the mark flag is nothing drawn: a script's @@ -5928,7 +5922,7 @@ pub const Pardes = struct { look.cancelLookHover(p); p.fs.serving = true; p.fs.lsp_answer_at = null; - p.fs.edit_answer_at = null; + p.fs.edit_started = false; const reply = ctlfs.handle(p, req); p.fs.serving = false; // The one-failure rule is a write's: a refused open (its OTRUNC's diff --git a/src/sam_edit.zig b/src/sam_edit.zig index 3f23aeff..d0c51bdb 100644 --- a/src/sam_edit.zig +++ b/src/sam_edit.zig @@ -12,8 +12,8 @@ //! What touches more than the text is the caller's (src/EditCmd.zig): `<`, //! `|` and `>` are collected as jobs, a command to run per address, whose //! output fills the change they hold the place of; `w` as the bytes to -//! write; `D`, and the names `e` and `f` set, as marks on the file; `B` and -//! the reads of `e` and `r` through `World`, at once, as acme does them. +//! write; `D`, `e`'s load and the name `f` sets, as marks on the file; `B` +//! and the reads of `e` and `r` through `World`, at once, as acme does them. //! //! Left out: the `'` address, `B `) refuses the whole Edit, where sam //! warns and puts in what it wrote; `D` of a file the Edit changed is -//! refused rather than drop the changes; `e` of an edited file is refused -//! every time, not once. +//! refused rather than drop the changes; `e` loads its file once the Edit +//! is done, by Get's way (ninep/ctl.zig getFile), so the file takes no +//! other command in the same Edit. const std = @import("std"); const regexp = @import("regexp.zig"); const addr_lang = @import("ninep/addr.zig"); @@ -58,8 +59,10 @@ pub const File = struct { renamed: bool = false, /// `D` closes it once the Edit is done. close: bool = false, - /// `e` read its own file whole: it is clean again once applied. - clean: bool = false, + /// `e` loads this file into it once the Edit is done (the editor's Get + /// path, which renames the pane and leaves it clean); the file takes no + /// more commands in the Edit. + get: ?[]const u8 = null, /// A command left a dot in it. moved: bool = false, }; @@ -75,11 +78,17 @@ pub const Write = struct { file: u32, path: []const u8, bytes: []const u8, whole /// What the editor does for `B`, `e` and `r` while an Edit runs. pub const World = struct { ctx: *anyopaque, + /// Whether `B` could open the absolute `path`: false, and `why` set, + /// when it could not, before any is opened. + check: *const fn (ctx: *anyopaque, path: []const u8, why: *Why) bool, /// The text pane holding the absolute `path`, opened if it is not open: /// `B`. Null, and `why` set, when it cannot be. open: *const fn (ctx: *anyopaque, path: []const u8, why: *Why) ?File, /// The bytes of the absolute `path`, in `arena`: `e` and `r`. read: *const fn (ctx: *anyopaque, arena: std.mem.Allocator, path: []const u8, why: *Why) ?[]const u8, + /// `e` over unsaved edits in the pane `id`: asked once, as Get asks; + /// true, and `why` set, when it is refused this time. + refuseGet: *const fn (ctx: *anyopaque, id: usize, why: *Why) bool, }; /// What an Edit came to: every file (those `B` opened after the ones it @@ -221,6 +230,28 @@ pub fn needsMore(arena: std.mem.Allocator, command: []const u8) bool { } } +/// Whether `command` holds a `<`, `|` or `>`: its write is answered only +/// when they have run (ninep/ctl.zig refuses other lines beside it). +pub fn runsCommands(arena: std.mem.Allocator, command: []const u8) bool { + var why: Why = .{}; + const src = std.fmt.allocPrint(arena, "{s}\n", .{command}) catch return false; + var ps: Parser = .{ .arena = arena, .src = src, .why = &why }; + const Walk = struct { + fn any(c: ?*Cmd) bool { + var it = c; + while (it) |each| : (it = each.next) { + if (std.mem.indexOfScalar(u8, "<|>", each.c) != null) return true; + if (any(each.sub)) return true; + } + return false; + } + }; + while (true) { + const c = (ps.parse(0) catch return false) orelse return false; + if (Walk.any(c)) return true; + } +} + fn fail(why: *Why, comptime fmt: []const u8, args: anytype) Failure { why.len = if (std.fmt.bufPrint(&why.buf, fmt, args)) |s| s.len else |_| 0; return error.Edit; @@ -642,7 +673,10 @@ const Exec = struct { /// `name` as a path: itself when absolute, else in `fi`'s directory /// (acme's dirname). - fn pathIn(ex: *Exec, fi: usize, name: []const u8) Failure![]const u8 { + fn pathIn(ex: *Exec, fi: usize, name_in: []const u8) Failure![]const u8 { + // `~` and `~user` as Get, Incl and Save read them. + var home: [4096]u8 = undefined; + const name = try ex.arena.dupe(u8, @import("fs.zig").expandHome(name_in, &home)); if (name.len > 0 and name[0] == '/') return name; const dir = std.fs.path.dirnamePosix(ex.files.items[fi].name) orelse "/"; return std.fs.path.resolvePosix(ex.arena, &.{ dir, name }); @@ -683,6 +717,7 @@ const Exec = struct { var fi = fi_in; var dot = dot_in; if (ex.files.items[fi].close) return fail(ex.why, "no current window", .{}); + if (ex.files.items[fi].get) |path| return fail(ex.why, "{s} is read in by e at the Edit's end: no more commands in it", .{path}); // A `"re"` address: the command runs in that file, from its dot. if (c.file_re) |pat| { fi = try ex.matchFile(pat); @@ -797,6 +832,9 @@ const Exec = struct { if (list.len > 0 and list[0] == '<') return fail(ex.why, "B { - // ecmd.c e_cmd: e reads a file over all of this one, r over - // dot; e names this file after it. + 'r' => { + // ecmd.c e_cmd: r reads a file over dot. + const path = (try ex.cmdName(fi, c.text, false)) orelse return fail(ex.why, e_noname, .{}); + try ex.log(fi, r.q0, r.q1, try ex.readFile(path), null); + return r; + }, + 'e' => { + // ecmd.c e_cmd: a file read over all of this one, which takes + // its name: Get's way (ctl.getFile), once the Edit is done, + // asking first, once, over unsaved edits. const f = &ex.files.items[fi]; - if (c.c == 'e' and f.dirty and !f.scratch) return fail(ex.why, "{s} modified", .{f.name}); - const own = f.name; - const path = (try ex.cmdName(fi, c.text, c.c == 'e')) orelse return fail(ex.why, e_noname, .{}); - const bytes = try ex.readFile(path); - const q: Range = if (c.c == 'e') .{ .q0 = 0, .q1 = clip(txt.len) } else r; - try ex.log(fi, q.q0, q.q1, bytes, null); - if (c.c == 'e' and std.mem.eql(u8, path, own)) ex.files.items[fi].clean = true; + if (f.ops.items.len > 0) return fail(ex.why, "e: {s} has changes pending in this Edit", .{f.name}); + const given = std.mem.trim(u8, c.text, " \t"); + const path = if (given.len == 0) + (if (f.scratch) return fail(ex.why, e_noname, .{}) else f.name) + else + try ex.pathIn(fi, given); + const w = ex.world orelse return fail(ex.why, "can't open {s}: no files here", .{path}); + if (w.refuseGet(w.ctx, f.id, ex.why)) return error.Edit; + _ = try ex.readFile(path); + f.get = path; + f.name = path; return r; }, 'w' => { @@ -893,7 +942,7 @@ const Exec = struct { /// in this Edit is refused rather than have them dropped. fn closeFile(ex: *Exec, fi: usize) Failure!void { const f = &ex.files.items[fi]; - if (f.ops.items.len > 0) return fail(ex.why, "D: {s} has changes pending in this Edit", .{f.name}); + if (f.ops.items.len > 0 or f.get != null) return fail(ex.why, "D: {s} has changes pending in this Edit", .{f.name}); f.close = true; } @@ -1166,7 +1215,23 @@ const Fixture = struct { .{ .name = "/src/+Errors", .text = "foo\n", .id = 3, .scratch = true }, }; + var opened: usize = 0; + + fn check(_: *anyopaque, path: []const u8, why: *Why) bool { + if (std.mem.eql(u8, path, "/src/new.c")) return true; + why.len = if (std.fmt.bufPrint(&why.buf, "B: cannot open {s}", .{path})) |said| said.len else |_| 0; + return false; + } + + /// The edited file, b.h, is asked about: refused. + fn refuseGet(_: *anyopaque, id: usize, why: *Why) bool { + if (id != 1) return false; + why.len = (std.fmt.bufPrint(&why.buf, "/src/b.h: Modified (e again to discard)", .{}) catch unreachable).len; + return true; + } + fn open(_: *anyopaque, path: []const u8, why: *Why) ?File { + opened += 1; if (std.mem.eql(u8, path, "/src/new.c")) return .{ .name = "/src/new.c", .text = "new foo\n", .id = 9 }; why.len = if (std.fmt.bufPrint(&why.buf, "B: cannot open {s}", .{path})) |said| said.len else |_| 0; return null; @@ -1188,7 +1253,7 @@ const Fixture = struct { fn edit(fx: *Fixture, from: usize, command: []const u8) !Result { var ctx: u8 = 0; - const world: World = .{ .ctx = &ctx, .open = open, .read = read }; + const world: World = .{ .ctx = &ctx, .check = check, .open = open, .read = read, .refuseGet = refuseGet }; return run(fx.arena_state.allocator(), &files, from, command, world, &fx.why); } @@ -1247,17 +1312,23 @@ test "b moves the current file, \"re\" addresses another, and f names one" { test "e and r read a file into the text, w writes it, D and B close and open panes" { var fx: Fixture = .init(); defer fx.deinit(); - // r replaces dot; e the whole text, and names the file after it. + // r replaces dot; e loads a file over all of it at the end, by Get's + // way, which names the pane after it. const r = try fx.edit(0, "2 r data"); try std.testing.expectEqualStrings("int foo;\nDATA\n", try fx.after(r, 0)); try std.testing.expect(!r.files[0].renamed); - const e = try fx.edit(0, "e /src/data"); - try std.testing.expectEqualStrings("DATA\n", try fx.after(e, 0)); - try std.testing.expectEqualStrings("/src/data", e.files[0].name); - try std.testing.expect(!e.files[0].clean); - // e of an edited file is refused, as acme's winclean refuses it. + const e = try fx.edit(0, "e data"); + try std.testing.expectEqualStrings("/src/data", e.files[0].get.?); + try std.testing.expectEqual(@as(usize, 0), e.files[0].ops.items.len); + // ...and takes no more commands, nor follows another change. + try std.testing.expectError(error.Edit, fx.edit(0, "e data\n,d")); + try std.testing.expectError(error.Edit, fx.edit(0, "1d\ne data")); + // e of an edited file is asked about, as Get asks. try std.testing.expectError(error.Edit, fx.edit(1, "e")); - try std.testing.expectEqualStrings("/src/b.h modified", fx.why.text()); + try std.testing.expectEqualStrings("/src/b.h: Modified (e again to discard)", fx.why.text()); + // `~` is the home directory, as Get and Save read it. + const home = try fx.edit(0, "w ~/t.txt"); + try std.testing.expect(home.writes[0].path[0] == '/' and std.mem.indexOfScalar(u8, home.writes[0].path, '~') == null); try std.testing.expectError(error.Edit, fx.edit(0, "r nothing")); try std.testing.expectEqualStrings("can't open /src/nothing: file does not exist", fx.why.text()); // w: all of it by default, to its own name; an address and a name. @@ -1282,6 +1353,10 @@ test "e and r read a file into the text, w writes it, D and B close and open pan try std.testing.expectEqualStrings("old foo\n", try fx.after(b, 4)); try std.testing.expectError(error.Edit, fx.edit(0, "B gone.c")); try std.testing.expectEqualStrings("B: cannot open /src/gone.c", fx.why.text()); + // Every name is checked first: one that cannot open opens none. + Fixture.opened = 0; + try std.testing.expectError(error.Edit, fx.edit(0, "B new.c gone.c")); + try std.testing.expectEqual(@as(usize, 0), Fixture.opened); } test "<, | and > are jobs: a command per address, its output the change" { @@ -1309,3 +1384,14 @@ test "<, | and > are jobs: a command per address, its output the change" { try std.testing.expectEqual(@as(usize, 2), x.jobs.len); try std.testing.expectEqual(@as(u32, 2), x.jobs[1].file); } + +test "an Edit that runs commands is told from one that does not" { + var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena_state.deinit(); + const a = arena_state.allocator(); + try std.testing.expect(runsCommands(a, ", | sort")); + try std.testing.expect(runsCommands(a, ",x/a/{\n< date\n}")); + try std.testing.expect(runsCommands(a, "X/c/ , > wc")); + try std.testing.expect(!runsCommands(a, ",s/|/>/g")); + try std.testing.expect(!runsCommands(a, ",x/ max_stdout_bytes or stderr_reader.buffered().len > max_stderr_bytes) return fail.k(.too_large); multi_reader.checkAnyError() catch return fail.k(.io); + if (token != 0) Running.remove(pid); const term = child.wait(io) catch return fail.k(.io); writer.?.join(); writer = null; @@ -339,6 +414,12 @@ pub fn runIn( return .{ .ok = stdout }; } +fn tokenStopped(token: u32) bool { + Running.take(); + defer Running.lock.unlock(); + return Running.isStopped(token); +} + /// Invoke the command independently for every selection. The response is all /// or nothing: one failure frees every earlier stdout and returns a failed, /// empty answer for the core to ignore. @@ -350,7 +431,11 @@ pub fn runJob(gpa: std.mem.Allocator, io: std.Io, job: *const Job) Response { for (job.inputs, 0..) |input, i| { const command = if (job.commands.len > 0) job.commands[i] else job.command; const cwd = if (job.cwds.len > 0) job.cwds[i] else job.cwd; - const output = switch (runIn(gpa, io, job.shell, command, cwd, input)) { + if (job.token != 0 and tokenStopped(job.token)) { + response.failure = .{ .kind = .signal, .index = @intCast(i) }; + break; + } + const output = switch (runIn(gpa, io, job.shell, command, cwd, input, job.token)) { .ok => |bytes| bytes, .failed => |f| { // WHICH selection, because with several cursors "it failed" is @@ -468,3 +553,34 @@ test "a request may give each input its own command and directory, run by the sh try std.testing.expectEqualStrings("/tmp\n", response.outputs[0]); try std.testing.expectEqualStrings("ABC", response.outputs[1]); } + +test "stop kills a running request's commands, the whole group, and starts none after" { + const gpa = std.testing.allocator; + const token = newToken(); + const job = try Job.copy(gpa, .{ + .id = 1, + .command = "", + .cwd = "", + .inputs = &.{ .{ .bytes = "" }, .{ .bytes = "" } }, + .commands = &.{ "sleep 30; echo late", "echo never" }, + .token = token, + }); + defer job.deinit(gpa); + const Run = struct { + fn go(j: *const Job, out: *Response) void { + out.* = runJob(std.testing.allocator, std.testing.io, j); + } + }; + var response: Response = undefined; + const started = std.Io.Clock.awake.now(std.testing.io); + const thread = try std.Thread.spawn(.{}, Run.go, .{ job, &response }); + std.Io.sleep(std.testing.io, .fromMilliseconds(300), .awake) catch {}; + stop(token); + thread.join(); + defer response.deinit(gpa); + try std.testing.expect(!response.success); + try std.testing.expectEqual(Failure.Kind.signal, response.failure.?.kind); + try std.testing.expectEqual(@as(u32, 0), response.failure.?.index); + const took = started.durationTo(std.Io.Clock.awake.now(std.testing.io)); + try std.testing.expect(took.toMilliseconds() < 5000); +} diff --git a/test/fs.py b/test/fs.py index 59668c57..b593981c 100644 --- a/test/fs.py +++ b/test/fs.py @@ -1085,6 +1085,37 @@ def edit_commands(binary): assert time.monotonic() - began < .5 and not done.is_set(), 'the editor waited on the command' worker.join(timeout=15) assert done.is_set() + # On the same connection: the Edit's write is held, and a read sent + # after it is answered first; a Tflush of it stops it, changes + # nothing, and frees the way for the next Edit. + def send(kind, tag, payload): + client.socket.sendall(struct.pack(' {mount}/pane/{edited}/ctl & sleep .5; " + f"s=$(date +%s%N); timeout 5 cat {mount}/status > /dev/null; e=$(date +%s%N); " + f"echo $(( (e - s) / 1000000 )) > {took}; wait\n").encode()) + ms = int(took.read_text()) if took.exists() else -1 + took.unlink(missing_ok=True) + check(ran == '0' and 0 <= ms < 1000, f'a status read beside a slow Edit answers at once ({ms} ms)') + check(client.read(f'/pane/{edited}/body') == b'apple\npear\n', 'and the slow Edit applied') + ran = run(f"echo 'Edit $ < cat {mount}/pane/{edited}/body' > {mount}/pane/{edited}/ctl\n".encode()) + check(ran == '0' and client.read(f'/pane/{edited}/body') == b'apple\npear\napple\npear\n', + f'an Edit whose command reads the session through its mount ({ran!r})') + # An interrupted Edit write is flushed: its command is stopped and + # nothing changes, and the next Edit runs. + flushed = Path(f'/tmp/{name}.flushed') + ran = run((f"timeout -s INT 1 sh -c \"echo 'Edit , | sleep 4; tr a-z A-Z' > {mount}/pane/{edited}/ctl\"; " + f"echo $? > {flushed}; sleep 1; echo 'Edit 1 | tr p P' > {mount}/pane/{edited}/ctl; echo $? >> {flushed}\n").encode()) + codes = flushed.read_text().split() if flushed.exists() else [] + flushed.unlink(missing_ok=True) + time.sleep(4.5) + check(codes == ['124', '0'] and client.read(f'/pane/{edited}/body') == b'aPPle\npear\napple\npear\n', + f'an interrupted Edit changes nothing, and the next runs ({codes!r}, {client.read(f"/pane/{edited}/body")!r})') client.write('/exec', b'Msg still here\n') text = ''.join(cell[0] for cell in client.screen()['cells']) check('still here' in text, 'the editor answers after all of that') -- cgit v1.3 From 3ddd34bcd94ba95d9cacbf6afcb9f89a16a414ce Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 1 Oct 2026 22:58:12 -0300 Subject: Edit's w to a file changed on disk since read is refused once, as Save is (w again to overwrite), the rest of an X/'/w written and the refused named; Edit filters get a pane command's environment ($PARDES_MOUNT, $PARDES_9P, $PARDES_PID, $winid); an Edit with no commands runs beside one whose commands run unless it touches a file that one changes, refused busy (EBUSY) as is a second Edit with commands Co-Authored-By: Claude Opus 5.5 --- docs/typ/reference.typ | 12 +++-- src/9p_io.zig | 16 +++++-- src/edit_cmd.zig | 117 ++++++++++++++++++++++++++++++++++++++++++++++--- src/host_io.zig | 14 ++++++ src/ninep/ctl.zig | 3 ++ src/selection_pipe.zig | 19 +++++++- test/fs.py | 21 +++++++++ test/selfmount.py | 7 +++ 8 files changed, 195 insertions(+), 14 deletions(-) (limited to 'src/selection_pipe.zig') diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index efc08a9b..f094a224 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -699,19 +699,25 @@ open file whose line matches. Commands: step puts both back, and unsaved edits are asked about once; the file takes no other command in that Edit. `r` reads a file over dot. `w` writes all of it, or the address, to its file or a name; all of it to its own - name leaves it clean. `f` names the pane and prints its line. File names + name leaves it clean. A `w` to its own file changed on disk since it was + read is refused once, as #word("Save") is (`modified on disk since read (w + again to overwrite)`), and the next `w` writes it; in an `X` the other + files are written, and the refusal names those it left. `f` names the pane and prints its line. File names are relative to the file's directory, and `~` is the home directory. - `cmd` sends dot to it and prints what it writes. Each command runs in its file's directory through #word("Shell"), off the -loop, so the editor goes on. The write that ran the Edit is held and +loop, so the editor goes on, with what a pane's command is given: +`$PARDES_MOUNT`, `$PARDES_9P`, `$PARDES_PID`, and `$winid` the Edit's pane. The write that ran the Edit is held and answered when they are done, its connection serving other requests meanwhile (a command may read the session through its mount); a flush of that write (an interrupted writer) kills the commands and changes nothing. Such an Edit is a write of its own: one with other lines is refused before any runs. One Edit's commands run at a time in a session, at most 1024 of -them. A command that fails (an exit status, 10 seconds, 1 MiB of output) +them. While they run, an Edit with no commands runs as usual unless it +would change, undo, rename, close or write a file the running one changes; +that, and a second Edit with commands, is refused `busy` (EBUSY). A command that fails (an exit status, 10 seconds, 1 MiB of output) fails the Edit, its stderr in `+Errors`. Each file's changes are one undo step, applied only if every command succeeds; a failure changes nothing and fails the write with acme's words (`Edit: no substitution`), as does a file edited while the diff --git a/src/9p_io.zig b/src/9p_io.zig index a01eb491..18c51ece 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -1992,6 +1992,16 @@ pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes) ?*Listene /// open to scripts and to mounted shells — while withholding `PARDES_PID`, /// which is the whole of what `--nested` means. pub fn exportPaneEnv(listener: ?*const Listener, serial: u32, adopts: bool) void { + exportEnv(if (listener) |l| l.path() else null, serial, adopts); +} + +/// exportPaneEnv for a command run off the loop (an Edit's filter), with +/// this process's own socket: what a pane's command would be given. +pub fn exportSessionEnv(serial: u32, adopts: bool) void { + exportEnv(if (own_socket_len > 0) own_socket[0..own_socket_len] else null, serial, adopts); +} + +fn exportEnv(socket: ?[]const u8, serial: u32, adopts: bool) void { var announced = false; if (adopts) announcing: { var buf: [16]u8 = undefined; @@ -2001,13 +2011,13 @@ pub fn exportPaneEnv(listener: ?*const Listener, serial: u32, adopts: bool) void } if (!announced) _ = unsetenv("PARDES_PID"); - if (listener) |l| exporting: { + if (socket) |own| exporting: { var sock: [sun_path_len]u8 = undefined; - const path = std.fmt.bufPrintSentinel(&sock, "{s}", .{l.path()}, 0) catch break :exporting; + const path = std.fmt.bufPrintSentinel(&sock, "{s}", .{own}, 0) catch break :exporting; var buf: [16]u8 = undefined; const id = std.fmt.bufPrintSentinel(&buf, "{d}", .{serial}, 0) catch break :exporting; if (setenv("PARDES_9P", path, 1) != 0) break :exporting; - exportMount(l.path()); + exportMount(own); if (setenv("PARDES_PANE", id, 1) == 0) return; } _ = unsetenv("PARDES_9P"); diff --git a/src/edit_cmd.zig b/src/edit_cmd.zig index 4e2c09fd..ebeae223 100644 --- a/src/edit_cmd.zig +++ b/src/edit_cmd.zig @@ -148,9 +148,6 @@ fn fileOf(pane: *panes.Pane, id: usize) sam.File { pub fn run(p: *Pardes, id: usize, command: []const u8) void { const pane = p.panes[id] orelse return; if (ninep_pane.fileOf(pane) == null) return p.reportFailure(id, "Edit: only a text pane is edited"); - // acme's Edit blocks the editor while its commands run; here the - // editor goes on, and a second Edit waits its turn. - if (p.pipe.edit_run != null) return p.reportFailure(id, "Edit: another Edit's commands are still running"); const pd = p.gpa.create(Pending) catch |err| return p.reportError(id, "Edit", err); pd.* = .{ .arena = .init(p.gpa), .origin = id, .origin_serial = pane.serial }; var keep = false; @@ -181,6 +178,22 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void { error.Edit => return p.reportFailure(id, std.fmt.bufPrint(&text, "Edit: {s}", .{why.text()}) catch "Edit: failed"), error.OutOfMemory => return p.reportError(id, "Edit", err), }; + // acme's Edit blocks the editor while its commands run; here the editor + // goes on, and so does any Edit that runs no command and leaves alone + // the files the running one will change. A second Edit with commands, + // or one that would change such a file, is refused, busy. + if (p.pipe.edit_run) |running| { + if (res.jobs.len > 0) return p.reportFailure(id, "Edit: busy: another Edit's commands are running; one Edit runs commands at a time"); + for (res.files, 0..) |f, i| { + const touches = if (res.undo != 0) f.id == id else changes(f) or for (res.writes) |w| { + if (w.file == i) break true; + } else false; + if (!touches) continue; + const q = p.panes[f.id] orelse continue; + for (running.res.files, 0..) |g, j| if (g.id == f.id and running.serials[j] == q.serial and changes(g)) + return p.reportFailure(id, std.fmt.bufPrint(&text, "Edit: busy: {s}: an Edit's commands are running on it", .{f.name}) catch "Edit: busy"); + } + } if (res.undo != 0) { for (0..@abs(res.undo)) |_| if (res.undo > 0) panes.File.undo(p, pane) else panes.File.redo(p, pane); return; @@ -220,6 +233,9 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void { .cwds = cwds, .shell = arena.dupe(u8, p.shellBin()) catch |err| return p.reportError(id, "Edit", err), .token = selection_pipe.newToken(), + // What a pane's command is given: $PARDES_MOUNT, $PARDES_9P, + // $PARDES_PID, $winid as the Edit's pane. + .env = if (comptime pardes.hosted) @import("host_io.zig").commandEnv(arena, pane.serial, !p.opts.nested) catch &.{} else &.{}, }; keep = true; p.pipe.edit_run = pd; @@ -229,6 +245,11 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void { if (p.fs.serving) p.fs.edit_started = true; } +/// Whether the Edit changes file `f`: its text, its name, or its pane. +fn changes(f: sam.File) bool { + return f.ops.items.len > 0 or f.get != null or f.close or f.renamed; +} + /// The Edit waiting on its commands, stopped: its write was flushed, or /// its connection hung up (9p_io). Its commands are killed, and nothing /// changes. @@ -314,10 +335,22 @@ fn commit(p: *Pardes, pd: *Pending, outputs: []const []const u8) void { const errno = @import("ninep/ctl.zig").getFile(p, p.panes[f.id].?, path, &failed); if (errno != 0) return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: e {s}: {s}", .{ path, if (errno == @import("ninep/tree.zig").E.NOENT) "no such file" else "cannot be read" }) catch "Edit: e failed"); } - // `w`'s bytes, written after this step, before anything else is. + // `w`'s bytes, written after this step, before anything else is. To + // its own file changed on disk since it was read, as Save is: refused + // once, said, and written by the next (exec.saveFile); the others go. + var refused: std.Io.Writer.Allocating = .init(p.gpa); + defer refused.deinit(); for (res.writes) |wr| { const f = res.files[wr.file]; const pane = p.panes[f.id].?; + if (pane.file) |*pf| if (!f.scratch and std.mem.eql(u8, wr.path, pf.path)) if (pf.disk_newer != null) { + if (!pf.disk_newer_warned) { + pf.disk_newer_warned = true; + refused.writer.print("{s}{s}", .{ if (refused.written().len > 0) ", " else "", pf.path }) catch {}; + continue; + } + pf.disk_newer = null; + }; const path = p.gpa.dupe(u8, wr.path) catch |err| return p.reportError(at, "Edit", err); const bytes = p.gpa.dupe(u8, wr.bytes) catch |err| { p.gpa.free(path); @@ -376,6 +409,10 @@ fn commit(p: *Pardes, pd: *Pending, outputs: []const []const u8) void { if (out.items.len > 0) _ = ninep_pane.appendErrors(p, at, out.items); if (p.panes[was] != null) p.active = was; } + if (refused.written().len > 0) { + var said: [320]u8 = undefined; + p.reportFailure(at, std.fmt.bufPrint(&said, "Edit: w: {s} modified on disk since read (w again to overwrite)", .{refused.written()[0..@min(refused.written().len, 200)]}) catch "Edit: w: modified on disk since read (w again to overwrite)"); + } // D's panes last, each as Del closes one: an edited one is warned about // once, and closes on the next. for (res.files) |f| { @@ -441,8 +478,8 @@ test "Edit , | sort runs off the loop, through the session's shell in the file's try testing.expectEqualStrings("b\na\nc\n", req.inputs[0].bytes); try testing.expectEqualStrings("/", req.cwds[0]); try testing.expectEqualStrings(p.shellBin(), req.shell); - // A second Edit waits its turn rather than run beside it. - try testing.expectEqual(tree.E.IO, th.wr(p, tree.Node.of(serial, .ctl), "Edit ,d\n").errno()); + // An Edit that would change the file being filtered is busy. + try testing.expectEqual(tree.E.BUSY, th.wr(p, tree.Node.of(serial, .ctl), "Edit ,d\n").errno()); answer(p, &.{"a\nb\nc\n"}); try testing.expect(p.pipe.edit_run == null); try testing.expectEqualStrings("a\nb\nc\n", p.panes[0].?.file.?.content); @@ -622,3 +659,71 @@ test "X over open files, w, e, r, f, B and D on panes and the disk" { try testing.expectEqual(tree.Status.ok, th.wr(p, a_ctl, "Edit D b.h\n").reply.status); try testing.expect(p.paneBySerial(b_serial) == null); } + +test "an Edit's w to a file changed on disk is refused once, as Save is, and the others are written" { + if (comptime !pardes.hosted or !filesystem.platform_has_fs) return error.SkipZigTest; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "a\n" }); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "b.txt", .data = "b\n" }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; + const p = try th.withFile(testing.allocator, "scratch\n"); + defer p.deinit(); + var path: [4200]u8 = undefined; + look.lookAt(p, 0, try std.fmt.bufPrint(&path, "{s}/a.txt", .{dir})); + const a = p.active; + look.lookAt(p, 0, try std.fmt.bufPrint(&path, "{s}/b.txt", .{dir})); + const b = p.active; + p.active = 0; + for ([_]usize{ a, b }) |i| try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(p.panes[i].?.serial, .ctl), "Edit $a/more/\n").reply.status); + // a.txt changed on disk since it was read (the watch says so). + p.panes[a].?.file.?.disk_newer = 1; + const ctl = tree.Node.of(th.serialOf(p), .ctl); + const first = th.wr(p, ctl, "Edit X/'/w\n"); + try testing.expect(first.reply.status == .err); + try testing.expect(std.mem.indexOf(u8, first.reply.ename, "a.txt modified on disk since read (w again to overwrite)") != null); + try testing.expect(std.mem.indexOf(u8, first.reply.ename, "b.txt") == null); + // b.txt is written all the same; a.txt is not. + try testing.expectEqual(@as(usize, 1), p.pipe.writes.items.len); + try testing.expect(std.mem.endsWith(u8, p.pipe.writes.items[0].path, "/b.txt")); + p.perform(.edit_write); + try testing.expect(ninep_pane.dirtyOf(p.panes[a].?)); + // The same w again overwrites it, as a second Save does. + try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(p.panes[a].?.serial, .ctl), "Edit w\n").reply.status); + try testing.expectEqual(@as(usize, 1), p.pipe.writes.items.len); + p.perform(.edit_write); + try testing.expect(!ninep_pane.dirtyOf(p.panes[a].?)); +} + +test "an Edit with no command runs beside one whose commands run, unless it touches what that one changes" { + if (comptime !pardes.hosted or !filesystem.platform_has_fs) return error.SkipZigTest; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "other.txt", .data = "x\n" }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; + const p = try th.withFile(testing.allocator, "b\na\n"); + defer p.deinit(); + var path: [4200]u8 = undefined; + look.lookAt(p, 0, try std.fmt.bufPrint(&path, "{s}/other.txt", .{dir})); + const other = p.active; + p.active = 0; + const ctl = tree.Node.of(th.serialOf(p), .ctl); + const other_ctl = tree.Node.of(p.panes[other].?.serial, .ctl); + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit , | sort\n").reply.status); + // Another pane's plain s runs while the filter does. + try testing.expectEqual(tree.Status.ok, th.wr(p, other_ctl, "Edit ,s/x/y/\n").reply.status); + try testing.expectEqualStrings("y\n", p.panes[other].?.file.?.content); + // One that changes the filtered file, or runs commands of its own, is + // busy, EBUSY; a print of the filtered file is no change, and runs. + for ([_][]const u8{ "Edit ,s/a/A/\n", "Edit u\n" }) |line| { + const busy = th.wr(p, ctl, line); + try testing.expectEqual(tree.E.BUSY, busy.errno()); + try testing.expect(std.mem.indexOf(u8, busy.reply.ename, "busy: ") != null); + } + try testing.expectEqual(tree.E.BUSY, th.wr(p, other_ctl, "Edit , | cat\n").errno()); + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit 1p\n").reply.status); + answer(p, &.{"a\nb\n"}); + try testing.expectEqualStrings("a\nb\n", p.panes[0].?.file.?.content); +} diff --git a/src/host_io.zig b/src/host_io.zig index ee503061..7314affc 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -1469,6 +1469,20 @@ test "the child environment replaces the launcher's terminal identity exactly on extern "c" fn unsetenv(name: [*:0]const u8) c_int; +/// The environment a pane's command is given (exportPaneEnv, `$winid`, +/// ChildEnv), as `NAME=value` lines in `arena`, for a command that is not a +/// pane's: an Edit's filters, run from pane `serial`. +pub fn commandEnv(arena: std.mem.Allocator, serial: u32, adopts: bool) ![]const []const u8 { + ninep_io.exportSessionEnv(serial, adopts); + exportWinid(serial); + var slots: ChildEnv.Slots = undefined; + const envp = ChildEnv.build(&slots, .command) orelse return error.EnvironmentTooLarge; + var out: std.ArrayList([]const u8) = .empty; + var i: usize = 0; + while (envp[i]) |entry| : (i += 1) try out.append(arena, try arena.dupe(u8, std.mem.span(entry))); + return out.items; +} + fn exportWinid(serial: u32) void { var buf: [16]u8 = undefined; if (serial != 0) if (std.fmt.bufPrintSentinel(&buf, "{d}", .{serial}, 0)) |text| { diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 8ece7ead..917c9c39 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -318,6 +318,9 @@ pub fn captured(p: *Pardes, req: Req, id: usize, exec: bool, text: []const u8, i /// there (`no such`, `not found`, the words 9ns reads as ENOENT too), else /// EIO. pub fn failureErrno(failure: []const u8) u16 { + // Refused while something else runs (an Edit's commands): EBUSY, the + // word 9ns reads as it. + if (std.mem.indexOf(u8, failure, "busy: ") != null) return E.BUSY; if (std.mem.indexOf(u8, failure, "no such") != null or std.mem.indexOf(u8, failure, "not found") != null) return E.NOENT; // A pattern refused (Edit's too) is malformed input, as 9ns reads it; // so is a word this build has not (`invalid: acme's Put ...`). diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index fb6587f9..66444b04 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -32,6 +32,9 @@ pub const Request = struct { shell: []const u8 = "", /// Nonzero: `stop(token)` from another thread kills its commands. token: u32 = 0, + /// The commands' environment, `NAME=value` each, when given; else this + /// process's. + env: []const []const u8 = &.{}, }; /// Worker-owned snapshot. `copy` is intentionally called synchronously while @@ -47,6 +50,7 @@ pub const Job = struct { cwds: [][]u8 = &.{}, shell: []u8 = &.{}, token: u32 = 0, + env: [][]u8 = &.{}, pub fn copy(gpa: std.mem.Allocator, request: Request) !*Job { const job = try gpa.create(Job); @@ -68,6 +72,8 @@ pub const Job = struct { job.cwds = try dupeAll(gpa, request.cwds.len, request.cwds, true); errdefer freeAll(gpa, job.cwds); job.shell = try gpa.dupe(u8, request.shell); + errdefer gpa.free(job.shell); + job.env = try dupeAll(gpa, request.env.len, request.env, false); return job; } @@ -100,6 +106,7 @@ pub const Job = struct { freeAll(gpa, job.commands); freeAll(gpa, job.cwds); gpa.free(job.shell); + freeAll(gpa, job.env); gpa.destroy(job); } }; @@ -232,7 +239,7 @@ pub fn runOne( cwd: []const u8, input: []const u8, ) Outcome { - return runIn(gpa, io, "", command, cwd, input, 0); + return runIn(gpa, io, "", command, cwd, input, 0, &.{}); } /// The commands running for a stoppable request (`Request.token`), so @@ -306,6 +313,7 @@ pub fn runIn( cwd: []const u8, input: []const u8, token: u32, + env: []const []const u8, ) Outcome { const fail = struct { fn k(kind: Failure.Kind) Outcome { @@ -320,6 +328,12 @@ pub fn runIn( // child inherits, is cleared across the fork (only the tty's SIGWINCH is // ever blocked, and its default is to be ignored). A handler resets at // exec by itself, and nothing here is ignored. + var environ: std.process.Environ.Map = .init(gpa); + defer environ.deinit(); + for (env) |line| { + const eq = std.mem.indexOfScalar(u8, line, '=') orelse continue; + environ.put(line[0..eq], line[eq + 1 ..]) catch return fail.k(.spawn); + } const none = std.posix.sigemptyset(); var kept: std.posix.sigset_t = undefined; std.posix.sigprocmask(std.posix.SIG.SETMASK, &none, &kept); @@ -327,6 +341,7 @@ pub fn runIn( var child = std.process.spawn(io, .{ .argv = &.{ if (shell.len == 0) "/bin/sh" else shell, "-c", command }, .cwd = if (cwd.len == 0) .inherit else .{ .path = cwd }, + .environ_map = if (env.len > 0) &environ else null, .stdin = .pipe, .stdout = .pipe, .stderr = .pipe, @@ -435,7 +450,7 @@ pub fn runJob(gpa: std.mem.Allocator, io: std.Io, job: *const Job) Response { response.failure = .{ .kind = .signal, .index = @intCast(i) }; break; } - const output = switch (runIn(gpa, io, job.shell, command, cwd, input, job.token)) { + const output = switch (runIn(gpa, io, job.shell, command, cwd, input, job.token, job.env)) { .ok => |bytes| bytes, .failed => |f| { // WHICH selection, because with several cursors "it failed" is diff --git a/test/fs.py b/test/fs.py index b593981c..f954b508 100644 --- a/test/fs.py +++ b/test/fs.py @@ -1123,6 +1123,27 @@ def edit_commands(binary): assert (root / 'b.txt').read_bytes() == b'APPLE pie\n' client.write(f'/pane/{a}/ctl', b'Edit 1 w first.txt\n') assert (root / 'first.txt').read_bytes() == b'APPLE\n' + # A file changed on disk under unsaved edits: Edit's w refuses + # once, as Save does, and the next w overwrites it. + w1 = root / 'w1.txt' + w1.write_bytes(b'one\n') + w = look(client, str(w1)) + client.write(f'/pane/{w}/ctl', b'Edit $a/two/\n') + seen = client.read('/log').decode().count(f'\nchanged {w}') + with open(w1, 'ab') as f: + f.write(b'external\n') + deadline = time.monotonic() + 10 + while client.read('/log').decode().count(f'\nchanged {w}') == seen: + assert time.monotonic() < deadline, 'the change on disk was not heard' + time.sleep(.05) + try: + client.write(f'/pane/{w}/ctl', b'Edit w\n') + raise AssertionError('w overwrote a file changed on disk') + except OSError as why: + assert 'modified on disk since read (w again to overwrite)' in str(why), why + assert w1.read_bytes() == b'one\nexternal\n' + client.write(f'/pane/{w}/ctl', b'Edit w\n') + assert w1.read_bytes() == b'one\ntwo', w1.read_bytes() def test(binary, quic=False): diff --git a/test/selfmount.py b/test/selfmount.py index c8a81df9..347093e1 100644 --- a/test/selfmount.py +++ b/test/selfmount.py @@ -307,6 +307,13 @@ def run(binary): ran = run(f"echo 'Edit $ < cat {mount}/pane/{edited}/body' > {mount}/pane/{edited}/ctl\n".encode()) check(ran == '0' and client.read(f'/pane/{edited}/body') == b'apple\npear\napple\npear\n', f'an Edit whose command reads the session through its mount ({ran!r})') + # A filter gets what a pane's command gets: the session's mount, + # socket and pid. + env_pane = int(client.read('/pane/new')) + ran = run(f"echo 'Edit $ < printf \"%s|%s|%s\" \"$PARDES_MOUNT\" \"$PARDES_9P\" \"$PARDES_PID\"' > {mount}/pane/{env_pane}/ctl\n".encode()) + got_env = client.read(f'/pane/{env_pane}/body').decode().split('|') + check(ran == '0' and len(got_env) == 3 and got_env[0] == mount and got_env[1] == sock and got_env[2].isdigit(), + f'an Edit filter has $PARDES_MOUNT, $PARDES_9P and $PARDES_PID ({got_env!r})') # An interrupted Edit write is flushed: its command is stopped and # nothing changes, and the next Edit runs. flushed = Path(f'/tmp/{name}.flushed') -- cgit v1.3 From 97c939b981bc7fbf1a58511a9d83d9f1e12a8f16 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 2 Oct 2026 00:50:41 -0300 Subject: An Edit filter that can never be reaped no longer holds the Edit's answer: at the limit the command's group is killed and reaped off the answer's path, the stdin writer owning its input, so a filter writing its own ctl with > or >> answers EIO at 10 s instead of hanging; a running Edit keeps its own copies of the names it reports, and the panes it changes refuse Del, delete, rmdir, Undo, Redo, Get and Zerox (busy, EBUSY); e and r say unreadable files in Get's words; the reference says both sides of a filter's own ctl write need <> Co-Authored-By: Claude Opus 5.5 --- docs/typ/reference.typ | 16 +++++++-- src/builtins.zig | 5 +++ src/edit_cmd.zig | 94 +++++++++++++++++++++++++++++++++++++++++++------- src/ninep/ctl.zig | 1 + src/ninep/tree.zig | 2 ++ src/selection_pipe.zig | 74 +++++++++++++++++++++++++++++++-------- test/selfmount.py | 35 ++++++++++++++++++- 7 files changed, 196 insertions(+), 31 deletions(-) (limited to 'src/selection_pipe.zig') diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index ed88905a..52a128cb 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -714,13 +714,23 @@ Each command runs in its file's directory through #word("Shell"), off the loop, so the editor goes on, with what a pane's command is given: `$PARDES_MOUNT`, `$PARDES_9P`, `$PARDES_PID`, and `$winid` the Edit's pane. The write that ran the Edit is held and answered when they are done, its connection serving other requests -meanwhile (a command may read the session through its mount; one that writes the #file("ctl") its Edit came by must not truncate it, `1<>ctl` rather than `>ctl`, as Linux holds the inode lock through an O_TRUNC open and the write waits out the 10-second limit); a flush of -that write (an interrupted writer) kills the commands and changes nothing. +meanwhile (a command may read the session through its mount). A command +that writes the #file("ctl") its Edit came by needs `<>` on both sides, the +Edit's write (`exec 3<>$p/ctl; echo 'Edit ...' >&3`) and its own (`1<>`): +with `>` or `>>` on either, the kernel holds the second write until the +first is answered, the command is stopped at its 10-second limit, and the +Edit fails (EIO), changing nothing. For the same reason a second Edit written +with `>` to that #file("ctl") waits for the first to finish rather than being +refused; through its own `<>` open, or another pane's #file("ctl"), it is +refused `busy` (EBUSY) at once. A flush of the Edit's write (an interrupted +writer) kills the commands and changes nothing. Such an Edit is a write of its own: one with other lines is refused before any runs. One Edit's commands run at a time in a session, at most 1024 of them. While they run, an Edit with no commands runs as usual unless it would change, undo, rename, close or write a file the running one changes; -that, and a second Edit with commands, is refused `busy` (EBUSY). A command that fails (an exit status, 10 seconds, 1 MiB of output) +that, and a second Edit with commands, is refused `busy` (EBUSY), as are +#word("Del") (acme's `delete`), `rmdir`, #word("Undo"), #word("Redo"), #word("Get") and #word("Zerox") on a pane +it changes or runs a command on; a session that ends lets the commands go. A command that fails (an exit status, 10 seconds, 1 MiB of output) fails the Edit, its stderr in `+Errors`. Each file's changes are one undo step, applied only if every command succeeds; a failure changes nothing and fails the write with acme's words (`Edit: no substitution`), as does a file edited while the diff --git a/src/builtins.zig b/src/builtins.zig index 77e1169d..84904959 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1141,6 +1141,7 @@ pub const Get = struct { pub const takes_arg = true; pub fn run(c: Ctx) void { const ctl = @import("ninep/ctl.zig"); + if (@import("edit_cmd.zig").busyOn(c.p, c.id)) return c.p.reportFailure(c.id, "Get: " ++ @import("edit_cmd.zig").e_busy); const f = if (c.pane.file) |*file| file else return c.p.reportFailure(c.id, "Get: only a file pane takes it"); const typed = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); // A directory pane: the directory read again (Dir.zig), as acme's @@ -1247,6 +1248,7 @@ pub const Incl = struct { /// unsaved state, with a scroll and a cursor of its own (File.zerox). pub const Zerox = struct { pub fn run(c: Ctx) void { + if (@import("edit_cmd.zig").busyOn(c.p, c.id)) return c.p.reportFailure(c.id, "Zerox: " ++ @import("edit_cmd.zig").e_busy); const f = c.pane.file orelse return c.p.reportFailure(c.id, "Zerox: only a file pane takes it"); if (f.output != null or f.listing != null or f.mini != null) return c.p.reportFailure(c.id, "Zerox: only a file pane takes it"); const free = c.p.freeSlot() orelse return c.p.reportError(c.id, "Zerox", error.NoPaneSlots); @@ -1302,6 +1304,7 @@ pub const Del = struct { pub const takes_arg = true; pub fn run(c: Ctx) void { const side = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); + if (@import("edit_cmd.zig").busyOn(c.p, c.id)) return c.p.reportFailure(c.id, "Del: " ++ @import("edit_cmd.zig").e_busy); // acme's Del asks winclean first (exec.c del): unsaved text is warned // about once, and the same Del again, nothing edited since, closes. if (warnModifiedIn(c, .Del, .{ .pane = c.id })) return; @@ -1380,6 +1383,7 @@ pub const Edit = struct { /// limits.undo_max steps (256). pub const Undo = struct { pub fn run(c: Ctx) void { + if (@import("edit_cmd.zig").busyOn(c.p, c.id)) return c.p.reportFailure(c.id, "Undo: " ++ @import("edit_cmd.zig").e_busy); if (c.pane.file) |f| if (f.history.undo_len == 0) return c.p.setMessage(c.id, "Undo: nothing to undo"); edit.doUndo(c.p, &c.pane.body); } @@ -1388,6 +1392,7 @@ pub const Undo = struct { /// Redo the last undone edit. pub const Redo = struct { pub fn run(c: Ctx) void { + if (@import("edit_cmd.zig").busyOn(c.p, c.id)) return c.p.reportFailure(c.id, "Redo: " ++ @import("edit_cmd.zig").e_busy); if (c.pane.file) |f| if (f.history.redo_len == 0) return c.p.setMessage(c.id, "Redo: nothing to redo"); edit.doRedo(c.p, &c.pane.body); } diff --git a/src/edit_cmd.zig b/src/edit_cmd.zig index ebeae223..83cc7e4f 100644 --- a/src/edit_cmd.zig +++ b/src/edit_cmd.zig @@ -67,6 +67,7 @@ pub const Write = struct { const World = struct { p: *Pardes, from: usize, + arena: std.mem.Allocator, fn check(ctx: *anyopaque, path: []const u8, why: *sam.Why) bool { const w: *World = @ptrCast(@alignCast(ctx)); @@ -103,7 +104,7 @@ const World = struct { const pane = slot orelse continue; const f = ninep_pane.fileOf(pane) orelse continue; if (!std.mem.eql(u8, f.path, path)) continue; - return fileOf(pane, id); + return fileOf(w.arena, pane, id) catch said(sam.File, why, "B: out of memory", .{}); } return said(sam.File, why, "B: cannot open {s}", .{path}); } @@ -113,13 +114,9 @@ const World = struct { if (comptime pardes.hosted) if (filesystem.localPath(path)) |local| if (@import("exec.zig").isDirectory(local)) return said([]const u8, why, "{s} is a directory", .{path}); const bytes = filesystem.read(w.p, path) catch |err| { - const name = @errorName(err); - const words = if (std.mem.eql(u8, name, "FileNotFound")) - "file does not exist" - else if (std.mem.eql(u8, name, "AccessDenied") or std.mem.eql(u8, name, "PermissionDenied")) - "permission denied" - else - name; + // In the words Get says them (builtins.Get). + var buf: [128]u8 = undefined; + const words = if (err == error.FileNotFound) "no such file" else pardes.Messages.errorWords(err, &buf); return said([]const u8, why, "can't open {s}: {s}", .{ path, words }); }; defer w.p.gpa.free(bytes); @@ -132,10 +129,13 @@ const World = struct { } }; -fn fileOf(pane: *panes.Pane, id: usize) sam.File { +/// What the Edit sees of a pane. Its name is a copy in `arena`: the pane +/// may be gone by the time a running Edit's end says it. Its text is the +/// pane's own, read only while the Edit runs its commands' parse. +fn fileOf(arena: std.mem.Allocator, pane: *panes.Pane, id: usize) !sam.File { const f = ninep_pane.fileOf(pane).?; return .{ - .name = f.path, + .name = try arena.dupe(u8, f.path), .text = f.content, .dot = ninep_pane.dotOf(pane), .dirty = ninep_pane.dirtyOf(pane), @@ -160,7 +160,7 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void { for (p.panes, 0..) |slot, i| { const q = slot orelse continue; if (q.file == null) continue; - files.append(arena, fileOf(q, i)) catch |err| return p.reportError(id, "Edit", err); + files.append(arena, fileOf(arena, q, i) catch |err| return p.reportError(id, "Edit", err)) catch |err| return p.reportError(id, "Edit", err); } std.mem.sort(sam.File, files.items, p, struct { fn before(core: *Pardes, a: sam.File, b: sam.File) bool { @@ -171,7 +171,7 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void { if (f.id == id) break i; } else unreachable; - var world: World = .{ .p = p, .from = id }; + var world: World = .{ .p = p, .from = id, .arena = arena }; var why: sam.Why = .{}; var text: [260]u8 = undefined; const res = sam.run(arena, files.items, cur, command, .{ .ctx = &world, .check = World.check, .open = World.open, .read = World.read, .refuseGet = World.refuseGet }, &why) catch |err| switch (err) { @@ -219,7 +219,7 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void { for (res.jobs, 0..) |job, i| { const f = res.files[job.file]; commands[i] = job.command; - cwds[i] = Pardes.paneDir(p.panes[f.id].?); + cwds[i] = arena.dupe(u8, Pardes.paneDir(p.panes[f.id].?)) catch |err| return p.reportError(id, "Edit", err); inputs[i] = .{ .bytes = if (job.c == '<') "" else arena.dupe(u8, f.text[job.q0..job.q1]) catch |err| return p.reportError(id, "Edit", err) }; } p.pipe.seq +%= 1; @@ -245,6 +245,29 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void { if (p.fs.serving) p.fs.edit_started = true; } +/// What a pane whose Edit's commands are running refuses to do beside them: +/// close, undo, redo, Get, Zerox (busyOn). +pub const e_busy = "busy: an Edit's commands are running on it"; + +/// Whether pane `id` (or a Zerox twin of it) is one the Edit whose commands +/// are running will change or run a command on. +pub fn busyOn(p: *Pardes, id: usize) bool { + const pd = p.pipe.edit_run orelse return false; + const pane = p.panes[id] orelse return false; + const twin = if (pane.file) |f| f.twin else 0; + for (pd.res.files, 0..) |f, i| { + const used = changes(f) or for (pd.res.jobs) |job| { + if (job.file == i) break true; + } else false; + if (!used) continue; + const q = p.panes[f.id] orelse continue; + if (q.serial != pd.serials[i]) continue; + if (f.id == id) return true; + if (twin != 0) if (q.file) |qf| if (qf.twin == twin) return true; + } + return false; +} + /// Whether the Edit changes file `f`: its text, its name, or its pane. fn changes(f: sam.File) bool { return f.ops.items.len > 0 or f.get != null or f.close or f.renamed; @@ -727,3 +750,48 @@ test "an Edit with no command runs beside one whose commands run, unless it touc answer(p, &.{"a\nb\n"}); try testing.expectEqualStrings("a\nb\n", p.panes[0].?.file.?.content); } + +test "a pane an Edit's commands will change is not closed under them, and one closed anyway is named, not read freed" { + const p = try th.withFile(testing.allocator, "b\na\n"); + defer p.deinit(); + const serial = th.serialOf(p); + const ctl = tree.Node.of(serial, .ctl); + try testing.expectEqual(tree.Status.ok, th.wr(p, ctl, "Edit ,|sleep 0.8; cat\n").reply.status); + // Del, acme's delete, rmdir, Undo, Redo, Get, Zerox: busy, EBUSY. + for ([_][]const u8{ "delete\n", "Del\n", "Undo\n", "Redo\n", "get\n", "Get\n", "Zerox\n" }) |line| { + const busy = th.wr(p, ctl, line); + try testing.expectEqual(tree.E.BUSY, busy.errno()); + try testing.expect(std.mem.indexOf(u8, busy.reply.ename, e_busy) != null); + } + try testing.expectEqual(tree.E.BUSY, th.rmdir(p, tree.Node.of(serial, .dir)).errno()); + try testing.expect(p.paneBySerial(serial) != null); + // Closed by a way that does not ask (a session's own teardown of one + // pane): the Edit's end names it from its own copy. + p.fs.edit_hold = .{ .asker = @ptrCast(p), .slot = 0, .seq = 1, .tag = 2, .node = ctl, .handle = 0, .written = 1 }; + try p.removePane(0, null); + answer(p, &.{"a\nb\n"}); + try testing.expectEqualStrings("Edit: /test.txt closed while its commands ran; nothing changed", filesystem.EditHold.answer(&p.fs).ename); +} + +test "a session that ends while an Edit's commands run lets the Edit go" { + const p = try th.withFile(testing.allocator, "x\n"); + try testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(th.serialOf(p), .ctl), "Edit ,|sleep 5; cat\n").reply.status); + try testing.expect(p.pipe.edit_run != null); + p.deinit(); // the testing allocator says if anything is kept +} + +test "e and r say a file that cannot be read as Get says it" { + if (comptime !pardes.hosted or !filesystem.platform_has_fs) return error.SkipZigTest; + const p = try th.withFile(testing.allocator, "x\n"); + defer p.deinit(); + const ctl = tree.Node.of(th.serialOf(p), .ctl); + for ([_][2][]const u8{ + .{ "Edit 0r /dev/zero\n", "not a regular file" }, + .{ "Edit 0r /tmp\n", "is a directory" }, + .{ "Edit 0r /nonexistent-pardes-file\n", "no such file" }, + }) |c| { + const r = th.wr(p, ctl, c[0]); + try testing.expect(r.reply.status == .err); + try testing.expect(std.mem.indexOf(u8, r.reply.ename, c[1]) != null); + } +} diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index c9ce41e1..105de07c 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -1003,6 +1003,7 @@ pub fn writePane(p: *Pardes, req: Req, pane: *Pane) Reply { } if (std.mem.eql(u8, line, "get")) { if (!apply) continue; + if (@import("../edit_cmd.zig").busyOn(p, p.paneBySerial(serial).?)) return tree.failText(req.tag, E.BUSY, "get: " ++ @import("../edit_cmd.zig").e_busy); // Quoting the line, as a builtin's refusal (Save's) does. if (getRefused(p, pane, "get")) |said_in_ename| { // refuse writes ename, where the words are: copy them out. diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index c78a1aa2..8d3c599f 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -937,6 +937,8 @@ fn remove(p: *Pardes, req: Req) Reply { }; if (t.file != .dir) return Reply.fail(req.tag, E.PERM); const id = p.paneBySerial(t.serial) orelse return Reply.fail(req.tag, E.NOENT); + // A pane an Edit's running commands will change stays until they end. + if (@import("../edit_cmd.zig").busyOn(p, id)) return failText(req.tag, E.BUSY, @import("../edit_cmd.zig").e_busy); p.removePane(id, null) catch return Reply.fail(req.tag, E.IO); return .{ .tag = req.tag }; } diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index 66444b04..c3e5e3a6 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -214,17 +214,53 @@ pub const Tasks = struct { } }; +/// The stdin writer's own: its copy of the input, freed by the writer when +/// it is done, so a writer stuck on a command that never reads (and cannot +/// be killed, below) can be let go of without its input dying under it. const WriterContext = struct { + gpa: std.mem.Allocator, io: std.Io, file: std.Io.File, - input: []const u8, - ok: bool = false, + input: []u8, }; fn writeInput(context: *WriterContext) void { - defer context.file.close(context.io); + defer { + context.file.close(context.io); + context.gpa.free(context.input); + context.gpa.destroy(context); + } context.file.writeStreamingAll(context.io, context.input) catch return; - context.ok = true; +} + +/// Kills the command's group, then `abandon`s it. +fn abandonNow(io: std.Io, child: *std.process.Child, pid: std.posix.pid_t) void { + // Before the shell is reaped, while its group id cannot be another's. + std.posix.kill(-pid, .KILL) catch {}; + std.posix.kill(pid, .KILL) catch {}; + abandon(io, child.*); + child.id = null; + child.stdout = null; + child.stderr = null; +} + +/// A command given up on, killed and reaped where nothing waits for it: a +/// process in uninterruptible sleep (a write to a file of this very +/// session, through its mount, waiting on the request whose answer is +/// waiting on this command) dies only when that request is answered, and +/// the answer must not wait on its death. +fn abandon(io: std.Io, child: std.process.Child) void { + const Reap = struct { + fn run(i: std.Io, c: std.process.Child) void { + var reaped = c; + reaped.kill(i); + } + }; + const thread = std.Thread.spawn(.{}, Reap.run, .{ io, child }) catch { + var c = child; + return c.kill(io); + }; + thread.detach(); } /// Run one POSIX shell command with exact stdin, concurrently draining stdout @@ -350,23 +386,33 @@ pub fn runIn( }) catch return fail.k(.spawn); const pid = child.id.?; - var writer_context: WriterContext = .{ + const writer_context = gpa.create(WriterContext) catch { + abandonNow(io, &child, pid); + return fail.k(.spawn); + }; + writer_context.* = .{ + .gpa = gpa, .io = io, .file = child.stdin.?, - .input = input, + .input = gpa.dupe(u8, input) catch { + gpa.destroy(writer_context); + abandonNow(io, &child, pid); + return fail.k(.spawn); + }, }; child.stdin = null; // writer_context owns and closes this endpoint - var writer: ?std.Thread = std.Thread.spawn(.{}, writeInput, .{&writer_context}) catch { + var writer: ?std.Thread = std.Thread.spawn(.{}, writeInput, .{writer_context}) catch { writer_context.file.close(io); - child.kill(io); + gpa.free(writer_context.input); + gpa.destroy(writer_context); + abandonNow(io, &child, pid); return fail.k(.spawn); }; - // On every early return kill first, unblocking a command which never read - // stdin, then join the short-lived writer before its borrowed input dies. - defer if (writer) |thread| thread.join(); - defer child.kill(io); - // Before the shell is reaped, while its group id cannot be another's. - defer if (child.id != null) std.posix.kill(-pid, .KILL) catch {}; + // On every early return (a timeout, a ceiling) the answer goes at once: + // the group is killed and the shell left to be reaped elsewhere + // (abandon), and the writer, which owns its input, let go. + defer if (writer) |thread| thread.detach(); + defer if (child.id != null) abandonNow(io, &child, pid); defer if (token != 0) Running.remove(pid); if (token != 0 and !Running.add(token, pid)) return fail.k(.signal); diff --git a/test/selfmount.py b/test/selfmount.py index 5aa9749a..61d32523 100644 --- a/test/selfmount.py +++ b/test/selfmount.py @@ -372,8 +372,41 @@ def run(binary): check(codes == ['124', '0'] and client.read(f'/pane/{edited}/body') == b'aPPle\npear\napple\npear\n', f'an interrupted Edit changes nothing, and the next runs ({codes!r}, {client.read(f"/pane/{edited}/body")!r})') client.write('/exec', b'Msg still here\n') - text = ''.join(cell[0] for cell in client.screen()['cells']) + # The terminal is read as it is waited on: a frame the editor + # cannot write out (its pty full) is a frame /screen does not have. + deadline = time.monotonic() + 8 + while 'still here' not in (text := ''.join(cell[0] for cell in client.screen()['cells'])) and time.monotonic() < deadline: + drain() + time.sleep(.1) check('still here' in text, 'the editor answers after all of that') + # A filter that writes its own pane's ctl through the mount. Both + # sides with `<>` run at once; a `>` or `>>` open on either side + # waits in the kernel for the other write, and the command is + # stopped at its 10 s limit: the Edit's write is answered then, + # never held by a command it cannot kill. + loop_pane = env_pane # the screen has no room for another + client.write(f'/pane/{loop_pane}/body', b'x\n', truncate=True) + inner = Path(f'/tmp/{name}.inner') + timed = Path(f'/tmp/{name}.timed') + for outer_open, inner_open, quick in [('3<>', '1<>', True), ('>', '>', False), ('>>', '>', False), + ('3<>', '>>', False), ('>', '>>', False)]: + # `clean`, which says nothing: a stopped filter's write still + # lands once the Edit's is answered. + inner.write_text(f'echo clean {inner_open} "$PARDES_MOUNT/pane/$winid/ctl"\n') + edit = f'Edit 1 > sh {inner}' + write = (f"exec 3<>{mount}/pane/{loop_pane}/ctl; echo '{edit}' >&3; rc=$?; exec 3>&-" + if outer_open == '3<>' else f"echo '{edit}' {outer_open} {mount}/pane/{loop_pane}/ctl; rc=$?") + ran = run((f"s=$(date +%s%N); {write}; e=$(date +%s%N); " + f"echo $rc $(( (e - s) / 1000000 )) > {timed}\n").encode()) + rc_ms = timed.read_text().split() if timed.exists() else ['?', '-1'] + timed.unlink(missing_ok=True) + ms = int(rc_ms[1]) + ok = ms >= 0 and (ms < 2000 if quick else ms < 13000) + check(ran == '0' and ok, f'outer {outer_open}, filter {inner_open} on its own ctl: answered in {ms} ms (rc {rc_ms[0]})') + inner.unlink(missing_ok=True) + client.write('/exec', b'Msg after the stopped filters\n') + check('msg ' in client.read('/log').decode().split('after the stopped filters')[0][-40:], + 'and the editor answers after them') finally: os.kill(pid, signal.SIGTERM) time.sleep(.2) -- cgit v1.3 From 0073d05329335dbb0eb85f5c3044e42805d144e5 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 23 Oct 2026 17:10:58 -0300 Subject: Callstack lists every caller and pages a tree too big for one query; memory bounds and resident-size fixes Callstack walks while the server answers, counts on the message row past a second, puts test callers last at each level, and ends a tree past one query's memory (memory.limits.arena.lsp_query, a 256 MiB region taken and given back per query) in a row whose Callstack shows the next page. Grep reads whole files. New hard limits: undo text per file, image decode pixels, the LSP snapshot of other open files, one paste. Safe builds no longer fill worst-case buffers with 0xAA (memory.unfilled), big regions are kept off transparent huge pages, and malloc_trim runs after big results. Also: Look's git show pages into a +Diff, and dropped marks in File. Co-Authored-By: Claude Opus 5.5 --- build.zig.zon | 4 +- docs/typ/guide.typ | 14 +- docs/typ/reference.typ | 9 +- src/9p.zig | 24 -- src/9p_io.zig | 121 +++--- src/File.zig | 141 +++++-- src/Registers.zig | 13 + src/ShaderBuild.zig | 23 +- src/builtins.zig | 3 + src/colors.zig | 2 +- src/config.zig | 66 +--- src/crash.zig | 6 + src/detached/server.zig | 29 +- src/dump.zig | 11 - src/edit.zig | 190 ++++++++-- src/exec.zig | 6 + src/fs.zig | 82 ++-- src/gui/gui.zig | 329 ++++++---------- src/gui/pet.zig | 29 +- src/host_io.zig | 103 ++++-- src/hunks.zig | 19 +- src/image.zig | 8 +- src/locations.zig | 135 +++++-- src/look.zig | 61 ++- src/lsp/lsp.zig | 17 +- src/lsp/lsp_client.zig | 761 ++++++++++++++++++++++++++------------ src/macos.zig | 17 +- src/main.zig | 12 +- src/memory.zig | 98 +++++ src/mini.zig | 16 +- src/modal.zig | 139 +++++-- src/mouse.zig | 11 +- src/ninep/addr.zig | 42 ++- src/ninep/ctl.zig | 4 + src/panes.zig | 3 + src/pardes.zig | 42 ++- src/regexp.zig | 11 +- src/sam_edit.zig | 15 + src/selection_pipe.zig | 11 +- src/surface.zig | 5 - src/terminal.zig | 86 +---- src/tty/tty.zig | 51 ++- test/lspbench.zig | 22 +- test/panes.zig | 3 - test/perf-baseline-gui-debug.json | 2 +- test/perf-baseline-tty-debug.json | 2 +- test/perf-baseline-tty-fast.json | 2 +- test/perf.zig | 21 +- 48 files changed, 1843 insertions(+), 978 deletions(-) (limited to 'src/selection_pipe.zig') diff --git a/build.zig.zon b/build.zig.zon index 292bd479..7885fd4e 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -9,8 +9,8 @@ // read-only HTTPS URL is what a manifest can carry. Re-pin with // `zig fetch --save=cloud9 git+https://git.sr.ht/~gbrls/cloud9#`. .cloud9 = .{ - .url = "git+https://git.sr.ht/~gbrls/cloud9#d5c37d9122240fe95d308fbe15874e14dd786c5a", - .hash = "cloud9-0.1.0-yt86qnAvHgB1dEWGakukcJ54GZ7J3mv_lm_kApzAuEOY", + .url = "git+https://git.sr.ht/~gbrls/cloud9#0702e33bcfadf0abd9ccbaa8b681a19a84c332fe", + .hash = "cloud9-0.1.0-yt86qkExHgCRI7_eJsztRECamioWguBj2mTFsJ5N-AOn", }, // Pinned to the exact libvaxis commit ghostty is built against, so it // is compatible with ghostty's uucode configuration (the one we share diff --git a/docs/typ/guide.typ b/docs/typ/guide.typ index a5327a44..e60b4231 100644 --- a/docs/typ/guide.typ +++ b/docs/typ/guide.typ @@ -240,11 +240,19 @@ wrapping. Cross-directory Look paths are shortened when possible. pane. #word("LookNext") / #word("LookPrev") (#keys("SPC j n", "SPC j p")) select locations without opening them. The view follows the language server's static call hierarchy; recursive paths are marked `↻`, and a -large hierarchy invites continuing from another function. +hierarchy deeper than the limit invites continuing from another function. +Every caller is listed: the walk runs as long as the server keeps answering, +says how many callers it has found on the message row once it takes more +than a second, and shows what it found if the server stops answering. +Asking anything else stops it. A tree too big for one page (about 100,000 +rows, the memory one query may use) ends in a row saying so: running +#word("Callstack") in `+Callstack` shows the next page, from where this +one stopped, with that caller's own callers above it again. +At each level, callers whose function or file name contains "test" come +after the rest. #word("CallstackDepth") reports the current caller depth; `CallstackDepth 16` changes it for subsequent queries. The default is 8 -levels, and the accepted range is 1–64. The view also caps collection at -64 callers. +levels, and the accepted range is 1–64. `CallstackLayout stacked` (the default) puts each signature and call snippet below its location, aligned with the location text; `CallstackLayout inline` puts it diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index bb13b011..c14ad659 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -264,7 +264,9 @@ A line written to #file("exec") is #word("Exec"): a declaration; from a use it reaches every file); #word("Callstack") (#key("g c") or #key("SPC l b")) opens and focuses `+Callstack`, with static caller ancestry, tree-sitter signatures and Look locations - for callers and call sites. #word("CallstackDepth") reports or sets the + for callers and call sites, test callers after the rest at each level; run + in `+Callstack` itself, it shows the next page of a tree too big for one. + #word("CallstackDepth") reports or sets the runtime depth (1–64, default 8) for subsequent queries. `CallstackLayout stacked` (the default) aligns snippets below their locations; `CallstackLayout inline` places the snippets beside them. @@ -971,12 +973,11 @@ one a #file("exec") open still holds is never reused. In a command pane, #key("n = Find and Grep #word("Find") matches file names, #word("Grep") the text of lines, both literally and -ignoring ASCII case. #word("Grep") reads at most 256 KiB of a file and stops at 512 +ignoring ASCII case. #word("Grep") reads whole files and stops at 512 hits, #word("Find") at 512 names. The walk stops at 20000 files or 100000 entries, 16 deep, and passes over `.git`, `.jj`, `target`, `node_modules`, `.venv`, `__pycache__`, `.zig-cache` and `zig-out`. A cap hit, or a -directory it could not open, is said at the end: `cut at 512 hits`, `N -files read only in part (first 256 KiB)`, `walk cut at N entries`, `N +directory it could not open, is said at the end: `cut at 512 hits`, `walk cut at N entries`, `N directories skipped: permission denied`. A search that finds nothing and skipped nothing fails, `Grep: text not found`, and leaves the `+Search` as it was. diff --git a/src/9p.zig b/src/9p.zig index 67b3b39a..0979b66a 100644 --- a/src/9p.zig +++ b/src/9p.zig @@ -5,16 +5,10 @@ const cloud9 = @import("cloud9"); pub const fs = cloud9.fs; pub const Server = cloud9.fs.Server; -pub const Error = cloud9.Error; -pub const Type = cloud9.Type; -pub const isT = cloud9.isT; pub const header_len = cloud9.header_len; -pub const qid_len = cloud9.qid_len; -pub const stat_fixed = cloud9.stat_fixed; pub const notag = cloud9.notag; pub const nofid = cloud9.nofid; pub const max_welem = cloud9.max_welem; -pub const iohdrsz = cloud9.iohdrsz; pub const Qid = cloud9.Qid; pub const Stat = cloud9.Stat; pub const Msg = cloud9.Msg; @@ -23,29 +17,11 @@ pub const frameLen = cloud9.frameLen; pub const encode = cloud9.encode; pub const decode = cloud9.decode; pub const qtdir = cloud9.qtdir; -pub const qtappend = cloud9.qtappend; -pub const qtexcl = cloud9.qtexcl; -pub const qtmount = cloud9.qtmount; -pub const qtauth = cloud9.qtauth; -pub const qttmp = cloud9.qttmp; -pub const qtfile = cloud9.qtfile; -pub const dmdir = cloud9.dmdir; -pub const dmappend = cloud9.dmappend; -pub const dmexcl = cloud9.dmexcl; -pub const dmmount = cloud9.dmmount; -pub const dmauth = cloud9.dmauth; -pub const dmtmp = cloud9.dmtmp; -pub const dmperm = cloud9.dmperm; pub const Client = cloud9.Client; -pub const ClientError = cloud9.ClientError; -pub const max_tags = cloud9.max_tags; pub const oread = cloud9.oread; pub const owrite = cloud9.owrite; pub const ordwr = cloud9.ordwr; -pub const oexec = cloud9.oexec; pub const otrunc = cloud9.otrunc; -pub const ocexec = cloud9.ocexec; -pub const orclose = cloud9.orclose; pub const errmax = cloud9.fs.errmax; pub const errString = cloud9.fs.errString; pub const msize_min = cloud9.fs.msize_min; diff --git a/src/9p_io.zig b/src/9p_io.zig index 446c24de..87972b43 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -174,8 +174,15 @@ pub const Listener = struct { io: std.Io, /// The core that answers; `reset` puts a replacement in. core: *pardes.Pardes, - runner: Runner = undefined, + /// cloud9's runner, sized for every connection's buffers and parks (some + /// 136 MiB): pages of its own (`allocRunner`), written only as clients + /// take slots, so an idle session does not hold them resident. + runner: *Runner = undefined, stopping: std.atomic.Value(bool) = .init(false), + /// The watchers of held Edit writes (watchEdit): canceled and awaited + /// by `deinit`, once the turn lets a waiting one through, so none wakes + /// into a freed Listener. + edit_watchers: std.Io.Group = .init, /// A connection wrote the Restore and has yet to answer it. restore_writer: std.atomic.Value(bool) = .init(false), /// Clients turned away with every slot taken, since the log last said so. @@ -354,8 +361,7 @@ pub const Listener = struct { const later: pardes.ctlfs.Reply = .{ .tag = req.tag, .status = .again }; const ticket = conn.hold(&later) orelse return; core.fs.edit_hold = .{ .asker = conn, .slot = ticket.slot, .seq = ticket.seq, .tag = req.tag, .node = req.node, .handle = req.handle, .written = reply.written }; - const watcher = std.Thread.spawn(.{}, watchEdit, .{ l, conn, ticket }) catch return; - watcher.detach(); + l.edit_watchers.concurrent(l.io, watchEdit, .{ l, conn, ticket }) catch {}; return; } const restoring = core.restore_req != null; @@ -457,13 +463,13 @@ pub const Listener = struct { /// flush, so it is looked for, every 100 ms; a park out for a retry is /// not waiting either, so only three looks in a row with no write being /// served count. - fn watchEdit(l: *Listener, conn: *Runner.Conn, ticket: cloud9.fs.Ticket) void { - const io = pardes.turn.io orelse return; + fn watchEdit(l: *Listener, conn: *Runner.Conn, ticket: cloud9.fs.Ticket) error{Canceled}!void { var misses: u8 = 0; while (!l.stopping.load(.acquire)) { - io.sleep(.fromMilliseconds(100), .awake) catch return; + try l.io.sleep(.fromMilliseconds(100), .awake); _ = pardes.turn.take(); defer pardes.turn.give(); + if (l.stopping.load(.acquire)) return; const core = l.core; const h = core.fs.edit_hold orelse return; if (h.slot != ticket.slot or h.seq != ticket.seq or h.asker != @as(*anyopaque, conn)) return; @@ -914,6 +920,7 @@ pub const Listener = struct { // A connection task may be waiting for the turn; it answers // nothing more once stopping, but the wait itself must end. pardes.turn.rest(); + l.edit_watchers.cancel(l.io); // An answer given as the editor quit (a Kill written to /ctl) // may still be on its way out: let it go before the cut. const deadline = nowMs() +| 200; @@ -942,10 +949,26 @@ pub const Listener = struct { z[l.path_len] = 0; _ = libc.unlink(z[0..l.path_len :0]); } + freeRunner(l.runner); gpa.destroy(l); } }; +/// The runner's memory, from the page allocator with no fill: an Allocator's +/// `create` writes `undefined` over all of it in a safe build, which would make +/// every page resident at startup. +fn allocRunner() ?*Runner { + if (comptime !supported) return @constCast(&@as(Runner, {})); + const bytes = std.heap.page_allocator.rawAlloc(@sizeOf(Runner), .of(Runner), @returnAddress()) orelse return null; + @import("memory.zig").sparse(bytes[0..@sizeOf(Runner)]); + return @ptrCast(@alignCast(bytes)); +} + +fn freeRunner(runner: *Runner) void { + if (comptime !supported) return; + std.heap.page_allocator.rawFree(@as([*]u8, @ptrCast(runner))[0..@sizeOf(Runner)], .of(Runner), @returnAddress()); +} + /// The socket this process listens on, kept where a fatal signal's handler /// can reach it without allocating (unlinkOwnSocket); empty when none. var own_socket: [sun_path_len:0]u8 = undefined; @@ -1065,6 +1088,10 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [ } const l = gpa.create(Listener) catch return null; l.* = .{ .io = io, .core = core }; + l.runner = allocRunner() orelse { + gpa.destroy(l); + return null; + }; pardes.turn.start(io); pardes.turn.wake_parked = Listener.wakeParked; pardes.turn.answer_held = Listener.answerHeld; @@ -1551,7 +1578,7 @@ test "a socket path too long for sun_path is no listener, and leaves the editor' defer if (old_copy) |o| { _ = setenv("XDG_RUNTIME_DIR", o, 1); } else { - _ = unsetenv("XDG_RUNTIME_DIR"); + _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); }; const long = &@as([120]u8, @splat('n')); try testing.expect(listen(testing.io, testing.allocator, p, long, "", null, null) == null); @@ -1572,7 +1599,7 @@ test "a listening editor posts itself into the 9P registry and unposts on stop" if (old_runtime) |v| { _ = setenv("XDG_RUNTIME_DIR", v, 1); gpa.free(v); - } else _ = unsetenv("XDG_RUNTIME_DIR"); + } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); } try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); @@ -1633,7 +1660,7 @@ test "Unix TCP and QUIC share one listener through reads writes reconnects and r if (old_runtime) |v| { _ = setenv("XDG_RUNTIME_DIR", v, 1); gpa.free(v); - } else _ = unsetenv("XDG_RUNTIME_DIR"); + } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); } try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); const replacement = try gpa.alloc(u8, 3 * msize + 27); @@ -1754,7 +1781,7 @@ test "a change waits while the editor is out mid-step, a read does not, and the if (old_runtime) |v| { _ = setenv("XDG_RUNTIME_DIR", v, 1); gpa.free(v); - } else _ = unsetenv("XDG_RUNTIME_DIR"); + } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); } try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); @@ -1829,7 +1856,7 @@ test "a held read is answered when the log has news, and a flushed one spends no if (old_runtime) |v| { _ = setenv("XDG_RUNTIME_DIR", v, 1); gpa.free(v); - } else _ = unsetenv("XDG_RUNTIME_DIR"); + } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); } try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); @@ -2001,28 +2028,22 @@ pub fn exportSessionEnv(serial: u32, adopts: bool) void { exportEnv(if (own_socket_len > 0) own_socket[0..own_socket_len] else null, serial, adopts); } +/// Told to children through host_io.ChildEnv, never set into this process's +/// own environment (see ChildEnv.exportVar for why). fn exportEnv(socket: ?[]const u8, serial: u32, adopts: bool) void { - var announced = false; - if (adopts) announcing: { - var buf: [16]u8 = undefined; - const text = std.fmt.bufPrintSentinel(&buf, "{d}", .{@as(u32, @intCast(libc.getpid()))}, 0) catch - break :announcing; - announced = setenv("PARDES_PID", text, 1) == 0; - } - if (!announced) _ = unsetenv("PARDES_PID"); - - if (socket) |own| exporting: { - var sock: [sun_path_len]u8 = undefined; - const path = std.fmt.bufPrintSentinel(&sock, "{s}", .{own}, 0) catch break :exporting; + const ChildEnv = @import("host_io.zig").ChildEnv; + var pid_buf: [16]u8 = undefined; + ChildEnv.exportVar("PARDES_PID", if (adopts) std.fmt.bufPrint(&pid_buf, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch null else null); + if (socket) |own| { var buf: [16]u8 = undefined; - const id = std.fmt.bufPrintSentinel(&buf, "{d}", .{serial}, 0) catch break :exporting; - if (setenv("PARDES_9P", path, 1) != 0) break :exporting; + ChildEnv.exportVar("PARDES_9P", own); exportMount(own); - if (setenv("PARDES_PANE", id, 1) == 0) return; + ChildEnv.exportVar("PARDES_PANE", std.fmt.bufPrint(&buf, "{d}", .{serial}) catch unreachable); + return; } - _ = unsetenv("PARDES_9P"); - _ = unsetenv("PARDES_PANE"); - _ = unsetenv("PARDES_MOUNT"); + ChildEnv.exportVar("PARDES_9P", null); + ChildEnv.exportVar("PARDES_PANE", null); + ChildEnv.exportVar("PARDES_MOUNT", null); } /// $PARDES_MOUNT, the session's own directory, when the environment says @@ -2040,11 +2061,11 @@ fn exportMount(sock_path: []const u8) void { const suffix = ".sock"; if (mntgen and root.len > 0 and std.mem.startsWith(u8, base, sock_prefix) and std.mem.endsWith(u8, base, suffix) and base.len > sock_prefix.len + suffix.len) { var buf: [4096]u8 = undefined; - if (std.fmt.bufPrintSentinel(&buf, "{s}/pardes/{s}", .{ std.mem.trimEnd(u8, root, "/"), base[sock_prefix.len .. base.len - suffix.len] }, 0)) |mount| { - if (setenv("PARDES_MOUNT", mount, 1) == 0) return; + if (std.fmt.bufPrint(&buf, "{s}/pardes/{s}", .{ std.mem.trimEnd(u8, root, "/"), base[sock_prefix.len .. base.len - suffix.len] })) |mount| { + return @import("host_io.zig").ChildEnv.exportVar("PARDES_MOUNT", mount); } else |_| {} } - _ = unsetenv("PARDES_MOUNT"); + @import("host_io.zig").ChildEnv.exportVar("PARDES_MOUNT", null); } test "a pane's PARDES_MOUNT is the session's directory under 9ns --mntgen, and unset when not known" { @@ -2057,16 +2078,16 @@ test "a pane's PARDES_MOUNT is the session's directory under 9ns --mntgen, and u if (value) |old| { _ = setenv(name, old, 1); testing.allocator.free(old); - } else _ = unsetenv(name); + } else _ = @import("host_io.zig").testUnsetenv(name); }; _ = setenv("NINE_MOUNT", "/mnt/9p", 1); _ = setenv("NINE_MNTGEN", "1", 1); exportMount("/run/user/1000/pardes-9p-work.sock"); - try testing.expectEqualStrings("/mnt/9p/pardes/work", std.mem.span(libc.getenv("PARDES_MOUNT").?)); + try testing.expectEqualStrings("/mnt/9p/pardes/work", @import("host_io.zig").ChildEnv.exported("PARDES_MOUNT").?); // A mount of another server (9ns --unix): not this session's. - _ = unsetenv("NINE_MNTGEN"); + _ = @import("host_io.zig").testUnsetenv("NINE_MNTGEN"); exportMount("/run/user/1000/pardes-9p-work.sock"); - try testing.expect(libc.getenv("PARDES_MOUNT") == null); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_MOUNT") == null); } test "9P shell environment states being inside pardes apart from how to reach it" { @@ -2079,16 +2100,16 @@ test "9P shell environment states being inside pardes apart from how to reach it if (value) |old| { _ = setenv(name, old, 1); testing.allocator.free(old); - } else _ = unsetenv(name); + } else _ = @import("host_io.zig").testUnsetenv(name); }; var dir: [sun_path_len:0]u8 = undefined; _ = setenv("XDG_RUNTIME_DIR", "/run/user/1000", 1); try testing.expectEqualStrings("/run/user/1000", socketDir(&dir).?); - _ = unsetenv("XDG_RUNTIME_DIR"); + _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); _ = setenv("HOME", "/home/example", 1); try testing.expectEqualStrings("/home/example/.local/state/pardes", socketDir(&dir).?); - _ = unsetenv("HOME"); + _ = @import("host_io.zig").testUnsetenv("HOME"); try testing.expect(socketDir(&dir) == null); const listener = try testing.allocator.create(Listener); @@ -2104,25 +2125,25 @@ test "9P shell environment states being inside pardes apart from how to reach it // parent, so a pardes started in one of its shells runs a session of // its own instead of handing its argument over. exportPaneEnv(listener, 7, false); - try testing.expectEqualStrings(path, std.mem.span(libc.getenv("PARDES_9P").?)); - try testing.expectEqualStrings("7", std.mem.span(libc.getenv("PARDES_PANE").?)); - try testing.expect(libc.getenv("PARDES_PID") == null); + try testing.expectEqualStrings(path, @import("host_io.zig").ChildEnv.exported("PARDES_9P").?); + try testing.expectEqualStrings("7", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null); exportPaneEnv(listener, 8, true); - try testing.expectEqualStrings("8", std.mem.span(libc.getenv("PARDES_PANE").?)); - try testing.expectEqualStrings(own_pid, std.mem.span(libc.getenv("PARDES_PID").?)); + try testing.expectEqualStrings("8", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?); + try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?); // A session whose listener never came up is still the session this shell // is inside: the pid stands on its own, and only the address is missing. exportPaneEnv(null, 0, true); - try testing.expect(libc.getenv("PARDES_9P") == null); - try testing.expect(libc.getenv("PARDES_PANE") == null); - try testing.expectEqualStrings(own_pid, std.mem.span(libc.getenv("PARDES_PID").?)); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null); + try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?); exportPaneEnv(null, 0, false); - try testing.expect(libc.getenv("PARDES_9P") == null); - try testing.expect(libc.getenv("PARDES_PANE") == null); - try testing.expect(libc.getenv("PARDES_PID") == null); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null); } pub const Client = struct { diff --git a/src/File.zig b/src/File.zig index 59b04e3f..7e05ac9f 100644 --- a/src/File.zig +++ b/src/File.zig @@ -43,14 +43,14 @@ pub const Snapshot = struct { saved_len: ?usize = null, fn deinit(s: Snapshot, gpa: std.mem.Allocator) void { - gpa.free(s.content); + pardes.memory.freeUnfilled(gpa, s.content); } }; /// The pane's text, place and name now, for an undo or redo step. fn snapshotOf(gpa: std.mem.Allocator, pane: *const Pane, f: *const State) ?Snapshot { return .{ - .content = gpa.dupe(u8, f.content) catch return null, + .content = pardes.memory.dupeUnfilled(gpa, f.content) catch return null, .cur_row = pane.body.cur_row, .cur_col = pane.body.cur_col, .vsel = pane.body.vsel, @@ -105,6 +105,8 @@ pub const History = struct { undo_len: usize = 0, redo: [limits.undo_max]Snapshot = undefined, redo_len: usize = 0, + /// The text `undo` and `redo` hold, kept near limits.undo_bytes. + bytes: usize = 0, /// The names the file had, each with the rename generation it ended /// (Snapshot.gen), and the generation now. @@ -115,6 +117,7 @@ pub const History = struct { const history = try gpa.create(History); history.undo_len = 0; history.redo_len = 0; + history.bytes = 0; history.names = .empty; history.gen = 0; return history; @@ -165,6 +168,9 @@ pub const State = struct { /// edit drops them (setContentSpan); shifting them through the splice, /// as pane.zig's shiftOne moves dot, would keep them. pager_spans: []pardes.pager.Span = &.{}, + /// The rows and spans an edit dropped, with the text they were for: an + /// edit (an undo) back to that very text takes them again. + dropped_marks: ?DroppedMarks = null, /// Seconds of the last content change, for the filesystem's stat; zero until edited. mtime: u32 = 0, /// Non-null for a generated output buffer rather than an on-disk file. @@ -457,10 +463,6 @@ test "an invalid byte in a grapheme is one cell, and never reaches vaxis's width try std.testing.expectEqual(@as(usize, 4), fitEnd(text, 0, 10)); } -pub fn byteDisplayWidth(byte: u8) usize { - return if (byte == '\t') config.tab_now else 1; -} - pub fn displayWidth(text: []const u8) usize { var width: usize = 0; var at: usize = 0; @@ -787,18 +789,9 @@ pub fn lineIndex(gpa: std.mem.Allocator, f: *State) ![]const usize { pub const Span = struct { head: usize, tail: usize }; fn editedLineStarts(gpa: std.mem.Allocator, starts: []const usize, old: []const u8, new: []const u8, known: ?Span) ![]usize { - const n = @min(old.len, new.len); - var head: usize = 0; - var tail: usize = 0; - if (known) |span| { - head = span.head; - tail = span.tail; - } else { - while (head + 64 <= n and std.mem.eql(u8, old[head..][0..64], new[head..][0..64])) head += 64; - while (head < n and old[head] == new[head]) head += 1; - while (tail + 64 <= n - head and std.mem.eql(u8, old[old.len - tail - 64 ..][0..64], new[new.len - tail - 64 ..][0..64])) tail += 64; - while (tail < n - head and old[old.len - tail - 1] == new[new.len - tail - 1]) tail += 1; - } + const span = known orelse spanOf(old, new); + const head = span.head; + const tail = span.tail; // A start s follows the newline at s-1: kept while that newline is in // the common head, shifted while it is in the common tail. const kept = std.sort.upperBound(usize, starts, head, struct { @@ -1123,6 +1116,7 @@ pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { if (file.highlights.len > 0) p.tree_sitter_gpa.free(file.highlights); p.gpa.free(file.pager_spans); locations.freeRows(p.gpa, file.location_rows); + if (file.dropped_marks) |m| m.deinit(p.gpa); if (file.context_declarations.len > 0) p.tree_sitter_gpa.free(file.context_declarations); if (file.node_tree) |tree| syntax.freeTree(tree); for (file.node_history.items) |step| p.gpa.free(step.ranges); @@ -1140,6 +1134,18 @@ fn reportEdit(p: *Pardes, f: *State, new: []const u8) void { pardes.ctlfs.events.noteReplace(p, id, false, f.content, new); } +pub const DroppedMarks = struct { + len: usize, + hash: u64, + rows: []locations.Row, + spans: []pardes.pager.Span, + + fn deinit(m: DroppedMarks, gpa: std.mem.Allocator) void { + locations.freeRows(gpa, m.rows); + gpa.free(m.spans); + } +}; + pub fn setContent(p: *Pardes, f: *State, new: []u8) void { setContentSpan(p, f, new, null); } @@ -1160,10 +1166,16 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { moved[i] = .{ .cursor = spliced(@min(at, f.content.len), f.content.len, new.len, span.?), .scroll = spliced(lineStart(p.gpa, tf, tf.scroll), f.content.len, new.len, span.?) }; } const old_starts = f.line_starts.ptr; - locations.freeRows(p.gpa, f.location_rows); - f.location_rows = &.{}; - p.gpa.free(f.pager_spans); - f.pager_spans = &.{}; + if (f.location_rows.len > 0 or f.pager_spans.len > 0) { + if (f.dropped_marks) |m| m.deinit(p.gpa); + f.dropped_marks = .{ .len = f.content.len, .hash = std.hash.Wyhash.hash(0, f.content), .rows = f.location_rows, .spans = f.pager_spans }; + f.location_rows = &.{}; + f.pager_spans = &.{}; + } else if (f.dropped_marks) |m| if (m.len == new.len and m.hash == std.hash.Wyhash.hash(0, new)) { + f.location_rows = m.rows; + f.pager_spans = m.spans; + f.dropped_marks = null; + }; for (p.panes) |slot| if (slot) |pane| { if (pane.file) |*file| if (file == f) { for (0..pane.sel.len) |button| { @@ -1182,7 +1194,9 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { f.saved_hash = std.hash.Wyhash.hash(0, f.content); f.saved_len = f.content.len; } - p.gpa.free(f.content); + // Raw: `free` overwrites a safe build's freed memory, a whole file's + // worth per keystroke. + if (f.content.len > 0) p.gpa.rawFree(f.content, .of(u8), @returnAddress()); f.content = new; f.revision +%= 1; if (f.saved_hash) |h| if ((f.saved_len orelse new.len) == new.len and std.hash.Wyhash.hash(0, new) == h) { @@ -1241,14 +1255,43 @@ fn spliced(at: usize, old_len: usize, new_len: usize, span: Span) usize { } /// What `old` and `new` agree on at either end. -fn spanOf(old: []const u8, new: []const u8) Span { +/// What `new` kept of `old` at either end, compared 32 bytes at a time +/// (a byte at a time was most of a `d` in a 7 MB file). +pub fn spanOf(old: []const u8, new: []const u8) Span { + const block = 32; const n = @min(old.len, new.len); - const head = std.mem.indexOfDiff(u8, old[0..n], new[0..n]) orelse n; + var head: usize = 0; + while (head + block <= n and std.mem.eql(u8, old[head..][0..block], new[head..][0..block])) head += block; + while (head < n and old[head] == new[head]) head += 1; var tail: usize = 0; + while (tail + block <= n - head and std.mem.eql(u8, old[old.len - tail - block ..][0..block], new[new.len - tail - block ..][0..block])) tail += block; while (tail < n - head and old[old.len - 1 - tail] == new[new.len - 1 - tail]) tail += 1; return .{ .head = head, .tail = tail }; } +test "spanOf by blocks finds the byte loop's ends" { + var prng: std.Random.DefaultPrng = .init(5); + const random = prng.random(); + var a: [160]u8 = undefined; + var b: [160]u8 = undefined; + for (0..4000) |_| { + const la = random.uintLessThan(usize, a.len); + const lb = random.uintLessThan(usize, b.len); + for (a[0..la]) |*c| c.* = "ab"[random.uintLessThan(usize, 2)]; + const keep = @min(la, lb); + @memcpy(b[0..keep], a[0..keep]); + for (b[keep..lb]) |*c| c.* = "ab"[random.uintLessThan(usize, 2)]; + if (lb > 0 and random.boolean()) b[random.uintLessThan(usize, lb)] ^= 1; + const old = a[0..la]; + const new = b[0..lb]; + const n = @min(old.len, new.len); + const head = std.mem.indexOfDiff(u8, old[0..n], new[0..n]) orelse n; + var tail: usize = 0; + while (tail < n - head and old[old.len - 1 - tail] == new[new.len - 1 - tail]) tail += 1; + try std.testing.expectEqual(Span{ .head = head, .tail = tail }, spanOf(old, new)); + } +} + fn positionIn(gpa: std.mem.Allocator, f: *State, at: usize) struct { row: usize, col: usize } { const index = lineIndex(gpa, f) catch { const c = modal.positionAt(f.content, at); @@ -1492,14 +1535,18 @@ pub fn restoreSnap(pane: *Pane, f: *State, snap: Snapshot) void { pane.ensureCursorVisible(); } -fn pushHistory(gpa: std.mem.Allocator, slots: []Snapshot, len: *usize, snap: Snapshot) void { - if (len.* == slots.len) { +/// Bounded in steps and in bytes: the oldest go first, though the one +/// pushed always stays. +fn pushHistory(gpa: std.mem.Allocator, history: *History, slots: []Snapshot, len: *usize, snap: Snapshot) void { + while (len.* > 0 and (len.* == slots.len or history.bytes + snap.content.len > limits.undo_bytes)) { + history.bytes -= slots[0].content.len; slots[0].deinit(gpa); - std.mem.copyForwards(Snapshot, slots[0 .. slots.len - 1], slots[1..]); + std.mem.copyForwards(Snapshot, slots[0 .. len.* - 1], slots[1..len.*]); len.* -= 1; } slots[len.*] = snap; len.* += 1; + history.bytes += snap.content.len; } pub fn pushUndo(p: *Pardes, pane: *Pane) void { @@ -1510,9 +1557,12 @@ pub fn pushUndo(p: *Pardes, pane: *Pane) void { if (std.mem.eql(u8, last.content, f.content) and last.gen == history.gen) return; } const snap = snapshotOf(p.gpa, pane, f) orelse return; - pushHistory(p.gpa, &history.undo, &history.undo_len, snap); - for (history.redo[0..history.redo_len]) |item| item.deinit(p.gpa); + for (history.redo[0..history.redo_len]) |item| { + history.bytes -= item.content.len; + item.deinit(p.gpa); + } history.redo_len = 0; + pushHistory(p.gpa, history, &history.undo, &history.undo_len, snap); } pub fn undo(p: *Pardes, pane: *Pane) void { @@ -1520,9 +1570,10 @@ pub fn undo(p: *Pardes, pane: *Pane) void { const history = f.history; if (history.undo_len == 0) return; const current = snapshotOf(p.gpa, pane, f) orelse return; - pushHistory(p.gpa, &history.redo, &history.redo_len, current); + pushHistory(p.gpa, history, &history.redo, &history.redo_len, current); history.undo_len -= 1; const previous = history.undo[history.undo_len]; + history.bytes -= previous.content.len; // the file's text now setContent(p, f, previous.content); restoreName(p, pane, f, previous); restoreSnap(pane, f, previous); @@ -1533,9 +1584,10 @@ pub fn redo(p: *Pardes, pane: *Pane) void { const history = f.history; if (history.redo_len == 0) return; const current = snapshotOf(p.gpa, pane, f) orelse return; - pushHistory(p.gpa, &history.undo, &history.undo_len, current); + pushHistory(p.gpa, history, &history.undo, &history.undo_len, current); history.redo_len -= 1; const next = history.redo[history.redo_len]; + history.bytes -= next.content.len; // the file's text now setContent(p, f, next.content); restoreName(p, pane, f, next); restoreSnap(pane, f, next); @@ -2266,3 +2318,28 @@ pub fn paintWordSelection( } } } + +test "undo keeps its steps within a byte bound, the newest always" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const pane = try p.setTestFile(""); + const f = &pane.file.?; + const step = limits.undo_bytes / 4 + 1; + const text = try gpa.alloc(u8, step); + defer gpa.free(text); + for (0..8) |i| { + @memset(text, 'a' + @as(u8, @intCast(i))); + pushUndo(p, pane); + setContent(p, f, try gpa.dupe(u8, text)); + } + pushUndo(p, pane); + const history = f.history; + try std.testing.expect(history.undo_len > 0 and history.undo_len < 8); + try std.testing.expect(history.bytes <= limits.undo_bytes + step); + var sum: usize = 0; + for (history.undo[0..history.undo_len]) |snap| sum += snap.content.len; + try std.testing.expectEqual(sum, history.bytes); + undo(p, pane); + try std.testing.expectEqualStrings(text[0..1], f.content[0..1]); +} diff --git a/src/Registers.zig b/src/Registers.zig index 3f2486cd..5e23da5b 100644 --- a/src/Registers.zig +++ b/src/Registers.zig @@ -81,6 +81,7 @@ pub fn text(r: *Registers, gpa: std.mem.Allocator, name: u21) ?[]const u8 { const reg = r.get(name) orelse return null; if (reg.joined) |j| return j; var out: std.ArrayList(u8) = .empty; + defer out.deinit(gpa); // what a failed append left; empty once owned for (reg.values, 0..) |v, i| { if (i > 0 and !std.mem.endsWith(u8, reg.values[i - 1], "\n")) out.append(gpa, '\n') catch return null; out.appendSlice(gpa, v) catch return null; @@ -105,3 +106,15 @@ test "a register holds a value per range and joins them for one-text readers" { try std.testing.expectEqualStrings("x", r.text(gpa, 'a').?); try std.testing.expectEqualStrings("one\ntwo", r.text(gpa, default).?); } + +test "joining a register that runs out of memory partway leaks nothing" { + var r: Registers = .{}; + defer r.deinit(std.testing.allocator); + r.put(std.testing.allocator, default, "one", 0, 2, true); + r.put(std.testing.allocator, default, "two", 1, 2, false); + r.put(std.testing.allocator, default, &(@as([4096]u8, @splat('x'))), 1, 2, false); + // The first append allocates; growing for the 4096 bytes fails. + var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 1, .resize_fail_index = 0 }); + try std.testing.expect(r.text(failing.allocator(), default) == null); + try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes); +} diff --git a/src/ShaderBuild.zig b/src/ShaderBuild.zig index b50999a5..e5b6680b 100644 --- a/src/ShaderBuild.zig +++ b/src/ShaderBuild.zig @@ -76,7 +76,8 @@ const Result = union(enum) { spirv: struct { bytes: []u8, source: u64, animated: bool }, }; -/// One compile of the stale files, on its own thread. +/// One compile of the stale files, a task of its own: `deinit` cancels it +/// (glslc's wait is a cancelation point) rather than waiting out a glslc. const Job = struct { io: std.Io, gpa: std.mem.Allocator, @@ -86,7 +87,10 @@ const Job = struct { results: [max]Result = @splat(.none), len: usize = 0, done: std.atomic.Value(bool) = .init(false), - thread: std.Thread = undefined, + /// Set before the cancel: the files after the one it stopped are not + /// compiled either (a cancel is said once, to the wait it lands in). + stop: std.atomic.Value(bool) = .init(false), + future: std.Io.Future(void) = undefined, }; files: [max]File = @splat(.{}), @@ -105,7 +109,7 @@ pub fn sync(b: *Build, gpa: std.mem.Allocator, io: std.Io, core: *pardes.Pardes, var collected = false; if (b.job) |job| { if (!job.done.load(.acquire)) return false; - job.thread.join(); + job.future.await(job.io); b.job = null; b.collect(gpa, core, job); collected = true; @@ -148,7 +152,7 @@ pub fn sync(b: *Build, gpa: std.mem.Allocator, io: std.Io, core: *pardes.Pardes, job.len += 1; file.stale = false; } - job.thread = std.Thread.spawn(.{}, compile, .{job}) catch { + job.future = io.concurrent(compile, .{job}) catch { for (job.paths[0..job.len]) |path| gpa.free(path); gpa.destroy(job); return collected; @@ -174,7 +178,8 @@ pub fn view(b: *const Build, chain: *const Chain, out: *[max]Pass) []const Pass pub fn deinit(b: *Build, gpa: std.mem.Allocator, fd: c_int, watches: *file_watch.Table) void { if (b.job) |job| { - job.thread.join(); + job.stop.store(true, .release); + job.future.cancel(job.io); freeJob(gpa, job); } for (b.files[0..b.len]) |file| free(gpa, file); @@ -245,7 +250,10 @@ fn expand(buf: *[4096]u8, path: []const u8) []const u8 { /// build's own flags (build.zig compileGlsl), source on stdin, SPIR-V on /// stdout. fn compile(job: *Job) void { - for (job.paths[0..job.len], 0..) |path, i| job.results[i] = compileOne(job.gpa, job.io, path, job.known[i]); + for (job.paths[0..job.len], 0..) |path, i| { + if (job.stop.load(.acquire)) break; + job.results[i] = compileOne(job.gpa, job.io, path, job.known[i]); + } job.done.store(true, .release); if (job.wake.call) |call| call(job.wake.ctx); } @@ -378,7 +386,8 @@ test "a save that changes a file compiles it again, a failed one keeps the last fn run(build: *Build, c: *pardes.Pardes, f: c_int, w: *file_watch.Table) void { _ = build.sync(std.testing.allocator, std.testing.io, c, f, w, .{}); while (build.job) |job| { - while (!job.done.load(.acquire)) std.Thread.yield() catch {}; + job.future.await(std.testing.io); + job.done.store(true, .release); _ = build.sync(std.testing.allocator, std.testing.io, c, f, w, .{}); } } diff --git a/src/builtins.zig b/src/builtins.zig index cc7d1d4a..fdce534b 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1971,6 +1971,9 @@ pub const WsDiagnostics = struct { /// Show outermost callers first, with signatures and call sites (gc). pub const Callstack = struct { pub fn run(c: Ctx) void { + // In +Callstack itself: the next page of what it shows. + if (c.pane.file) |f| if (f.output) |o| if (std.meta.eql(o.from, panes.Output.Origin{ .query = .callstack })) + return c.p.callstackMore(c.id); c.p.lspRequest(c.id, .callstack, ""); } }; diff --git a/src/colors.zig b/src/colors.zig index dd0d8938..28136975 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -840,7 +840,7 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8, animate: bo @memcpy(settled[0..request.path.len], request.path); _ = p.theme_file_loaded.set(settled[0..request.path.len]); invalidateThemeDependentRasters(p); - _ = p.scratch.reset(.retain_capacity); + _ = p.scratch.reset(.{ .retain_with_limit = pardes.memory.limits.scratch_kept }); p.sync(); return true; } diff --git a/src/config.zig b/src/config.zig index a92aabb6..5251b1e5 100644 --- a/src/config.zig +++ b/src/config.zig @@ -1230,27 +1230,12 @@ pub const Runtime = struct { if (!state.shell.requested.set(value)) return false; state.shell.pending = true; }, - .tagline_size => { - const text = std.mem.trim(u8, argument orelse return false, " \t\r\n"); - const percent = std.fmt.parseInt(u16, text, 10) catch return false; - if (percent < tagline_percent_min or percent > tagline_percent_max) return false; - state.font.tagline_percent = @intCast(percent); - }, + .tagline_size => state.font.tagline_percent = @intCast(number(u16, argument, " \t\r\n", tagline_percent_min, tagline_percent_max) orelse return false), .window_opacity => { - const text = std.mem.trim(u8, argument orelse return false, " \t\r\n"); - for (text) |byte| if (!std.ascii.isDigit(byte)) return false; - const percent = std.fmt.parseInt(u8, text, 10) catch return false; - if (percent > 100) return false; - state.window_opacity = percent; + state.window_opacity = number(u8, argument, " \t\r\n", 0, 100) orelse return false; state.window_opacity_pending = true; }, - .window_blur => { - const text = std.mem.trim(u8, argument orelse return false, " \t\r\n"); - for (text) |byte| if (!std.ascii.isDigit(byte)) return false; - const percent = std.fmt.parseInt(u8, text, 10) catch return false; - if (percent > 100) return false; - state.window_blur = percent; - }, + .window_blur => state.window_blur = number(u8, argument, " \t\r\n", 0, 100) orelse return false, // Bare, it returns to the default directory. Else an absolute or // `~` path, to a directory there or to be made, that may be // written: a Dump there could never be, refused now. @@ -1264,10 +1249,7 @@ pub const Runtime = struct { if (!state.dump_dir.set(text)) return false; }, .message_ms => |which| { - const text = std.mem.trim(u8, argument orelse return false, " \t\r\n"); - for (text) |byte| if (!std.ascii.isDigit(byte)) return false; - const ms = std.fmt.parseInt(u16, text, 10) catch return false; - if (ms > message_ms_max) return false; + const ms = number(u16, argument, " \t\r\n", 0, message_ms_max) orelse return false; switch (which) { inline else => |w| @field(state, "message_" ++ @tagName(w) ++ "_ms") = ms, } @@ -1330,30 +1312,10 @@ pub const Runtime = struct { std.meta.stringToEnum(LiftStyle, text) orelse return false; }, .motion => state.motion = stepped(animation.Motion.Flavour, state.motion, argument) orelse return false, - .inactive_dim => { - const text = std.mem.trim(u8, argument orelse return false, " \t\r\n%"); - const percent = std.fmt.parseInt(u8, text, 10) catch return false; - if (percent > 100) return false; - state.inactive_dim = percent; - }, - .grip_width => { - const text = std.mem.trim(u8, argument orelse return false, " \t\r\n%"); - const percent = std.fmt.parseInt(u16, text, 10) catch return false; - if (percent < 50 or percent > 300) return false; - state.grip_width = percent; - }, - .callstack_depth => { - const text = std.mem.trim(u8, argument orelse return false, " \t\r\n"); - const depth = std.fmt.parseInt(u8, text, 10) catch return false; - if (depth < 1 or depth > @import("lsp/lsp.zig").callstack_max_depth) return false; - state.callstack_depth = depth; - }, - .tab => { - const text = std.mem.trim(u8, argument orelse return false, " \t\r\n"); - const width = std.fmt.parseInt(u16, text, 10) catch return false; - if (width < 1 or width > 16) return false; - state.tab = width; - }, + .inactive_dim => state.inactive_dim = number(u8, argument, " \t\r\n%", 0, 100) orelse return false, + .grip_width => state.grip_width = number(u16, argument, " \t\r\n%", 50, 300) orelse return false, + .callstack_depth => state.callstack_depth = number(u8, argument, " \t\r\n", 1, @import("lsp/lsp.zig").callstack_max_depth) orelse return false, + .tab => state.tab = number(u16, argument, " \t\r\n", 1, 16) orelse return false, .theme, .font => return false, } return true; @@ -1411,6 +1373,16 @@ pub const Runtime = struct { return all[(at + 1) % all.len]; } + /// A setting's number: digits only (parseInt alone takes a sign and `_`), + /// trimmed of `trim`, within lo..hi. + fn number(comptime T: type, argument: ?[]const u8, trim: []const u8, lo: T, hi: T) ?T { + const text = std.mem.trim(u8, argument orelse return null, trim); + if (text.len == 0) return null; + for (text) |byte| if (!std.ascii.isDigit(byte)) return null; + const n = std.fmt.parseInt(T, text, 10) catch return null; + return if (n < lo or n > hi) null else n; + } + /// What `on`, `off` or nothing after a switch word asks for: null flips /// it, as a bare tagline word does. fn switchedTo(argument: ?[]const u8) error{NotOnOrOff}!?bool { @@ -2523,7 +2495,7 @@ test "CallstackDepth is bounded and keeps the last valid runtime limit" { try std.testing.expect(Runtime.apply(&state, Runtime.find("CallstackDepth").?, value)); try std.testing.expectEqual(try std.fmt.parseInt(u8, value, 10), state.callstack_depth); } - for ([_][]const u8{ "0", "65", "256", "-1", "eight", "" }) |value| { + for ([_][]const u8{ "0", "65", "256", "-1", "+8", "1_6", "eight", "" }) |value| { try std.testing.expect(!Runtime.apply(&state, Runtime.find("CallstackDepth").?, value)); try std.testing.expectEqual(@as(u8, 64), state.callstack_depth); } diff --git a/src/crash.zig b/src/crash.zig index 34dc1b3d..a432fb87 100644 --- a/src/crash.zig +++ b/src/crash.zig @@ -34,6 +34,9 @@ pub const name = "crashes"; /// A panic handler is the one caller that cannot check whether its input is /// still alive, so it does not borrow. var dir_buf: [1024]u8 = undefined; + +/// The crash file's bound (see `record`). +const max_log_bytes = 256 * 1024; var dir_len: usize = 0; /// Called where the launcher sets `Options.config_dir`. Ignores a path too long @@ -109,6 +112,9 @@ pub fn record(msg: []const u8) void { }, @as(std.c.mode_t, 0o600)); if (fd < 0) return; defer _ = std.c.close(fd); + // Bounded: a file past `max_log_bytes` starts over, so it keeps the + // newest records rather than every crash since it was first made. + if (std.c.lseek(fd, 0, std.c.SEEK.END) > max_log_bytes) _ = std.c.ftruncate(fd, 0); var w: std.Io.Writer = .fixed(&scratch); diff --git a/src/detached/server.zig b/src/detached/server.zig index d6a1f553..6c8d4352 100644 --- a/src/detached/server.zig +++ b/src/detached/server.zig @@ -107,17 +107,19 @@ const Mailbox = struct { status: ?[]u8 = null, }; - mutex: std.atomic.Mutex = .unlocked, + /// Taken by the workers and the loop; `io` is the session's (initAsync). + mutex: std.Io.Mutex = .init, + io: std.Io = undefined, batch: Batch = .{}, wake: [2]c_int = .{ -1, -1 }, fn post(box: *Mailbox, completion: Completion) void { - while (!box.mutex.tryLock()) std.atomic.spinLoopHint(); + box.mutex.lockUncancelable(box.io); // Tasks retain their slot until the owner consumes their one completion. std.debug.assert(box.batch.len < capacity); box.batch.items[box.batch.len] = completion; box.batch.len += 1; - box.mutex.unlock(); + box.mutex.unlock(box.io); box.signal(); } @@ -130,8 +132,8 @@ const Mailbox = struct { fn take(box: *Mailbox) Batch { var bytes: [128]u8 = undefined; while (libc.recv(box.wake[0], &bytes, bytes.len, 0) > 0) {} - while (!box.mutex.tryLock()) std.atomic.spinLoopHint(); - defer box.mutex.unlock(); + box.mutex.lockUncancelable(box.io); + defer box.mutex.unlock(box.io); const batch = box.batch; box.batch.len = 0; box.batch.status = null; @@ -184,6 +186,7 @@ pub const Session = struct { accept_paused_ms: i64 = 0, pub fn initAsync(s: *Session) !void { + s.mailbox.io = s.io; var pair: [2]c_int = undefined; if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM, 0, &pair) != 0) return error.SocketFailed; errdefer for (pair) |fd| { @@ -410,7 +413,7 @@ pub const Session = struct { s.core.update(.{ .lsp_resp = .{ .id = request.id, .rows = null } }); return s.core.reportError(request.pane, "lsp", err); }; - const future = s.io.concurrent(lspWorker, .{ s.worker_gpa, job, &s.mailbox }) catch |err| { + const future = s.io.concurrent(lspWorker, .{ s.io, s.worker_gpa, job, &s.mailbox }) catch |err| { job.free(s.worker_gpa); s.core.update(.{ .lsp_resp = .{ .id = request.id, .rows = null } }); return s.core.reportError(request.pane, "lsp", err); @@ -418,8 +421,8 @@ pub const Session = struct { s.lsp_task = .{ .id = request.id, .future = future }; } - fn lspWorker(gpa: std.mem.Allocator, job: *host_io.Lsp.Job, box: *Mailbox) anyerror!void { - host_io.Lsp.work(gpa, job, box, deliverLspRows); + fn lspWorker(io: std.Io, gpa: std.mem.Allocator, job: *host_io.Lsp.Job, box: *Mailbox) anyerror!void { + host_io.Lsp.work(io, gpa, job, box, deliverLspRows); } fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { @@ -430,10 +433,10 @@ pub const Session = struct { fn lspStatus(ctx: ?*anyopaque, text: []const u8) void { const s = of(ctx); const copy = s.worker_gpa.dupe(u8, text) catch return; - while (!s.mailbox.mutex.tryLock()) std.atomic.spinLoopHint(); + s.mailbox.mutex.lockUncancelable(s.io); if (s.mailbox.batch.status) |old| s.worker_gpa.free(old); s.mailbox.batch.status = copy; - s.mailbox.mutex.unlock(); + s.mailbox.mutex.unlock(s.io); s.mailbox.signal(); } @@ -529,7 +532,8 @@ pub const Session = struct { if (pane >= s.ptys.len) return; const pt = &s.ptys[pane]; if (pt.fd < 0) return; - if (pt.out.items.len > pty_backlog) { + // The bound holds with this write in: queued and new together. + if (pt.out.items.len + bytes.len > pty_backlog) { var mbuf: [256]u8 = undefined; const text = std.fmt.bufPrint( &mbuf, @@ -1145,6 +1149,9 @@ pub const Session = struct { s.queue(c, bytes); } + /// A client's queued output is at most `out_backlog + wire.max_payload`: + /// past the backlog it is closed, and one frame lands on top of it first + /// (a frame bigger than the backlog must still go out whole). fn queue(s: *Session, c: *Client, bytes: []const u8) void { if (c.out.items.len > out_backlog) return s.close(c, .backlog); s.account(); diff --git a/src/dump.zig b/src/dump.zig index 76a6de85..67934e0a 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -394,17 +394,6 @@ test "oversized recognized output argument is rejected before restore" { ); } -pub fn writeFile(io: std.Io, gpa: std.mem.Allocator, path: []const u8, state: State) !void { - try validate(state); - var out: std.Io.Writer.Allocating = .init(gpa); - defer out.deinit(); - try std.zon.stringify.serialize(state, .{ .whitespace = true }, &out.writer); - - var file = try std.Io.Dir.createFileAbsolute(io, path, .{}); - defer file.close(io); - try file.writeStreamingAll(io, out.written()); -} - pub const Parsed = struct { value: State, arena: std.heap.ArenaAllocator, diff --git a/src/edit.zig b/src/edit.zig index 466b5cdb..55365692 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -119,7 +119,15 @@ pub fn typeToTty(p: *Pardes, id: usize, pane: *const Pane, text: []const u8) voi for (panes.terminal.encodePaste(pane, cp)) |part| if (part.len > 0) p.emitWrite(id, part); } -pub fn applyPaste(p: *Pardes, bytes: []const u8) void { +pub fn applyPaste(p: *Pardes, whole: []const u8) void { + // Every shell's paste comes through here, bounded: what is past the + // limit is left out, at a character's start, and said. + const limit = @import("memory.zig").limits.max_paste_bytes; + const bytes = if (whole.len > limit) whole[0..modal.graphemeStart(whole, limit)] else whole; + if (whole.len > limit) { + var said: [96]u8 = undefined; + p.setMessage(p.active, std.fmt.bufPrint(&said, "paste cut at its limit, {d} KiB: the rest left out", .{limit / 1024}) catch "paste cut at its limit"); + } // A prompt answering for a header takes the paste, as it takes keys. const asking = if (p.panes[p.active]) |active| active.prompt != .none else false; if (!asking) if (tagline.focusedHeader(p)) |t| { @@ -466,8 +474,10 @@ fn editText(p: *Pardes, t: *Text, lo: i32, hi: i32, col: i32) ?panes.EditText { fn editTextEol(p: *Pardes, t: *Text, b: Bounds) ?panes.EditText { const eb = editText(p, t, b.lo_row, b.hi_row, -1) orelse return null; const r: usize = @intCast(@max(0, b.hi_row - eb.row0)); - if (r + 1 < modal.lineCount(eb.text)) return eb; - if (b.hi_col < @as(i32, @intCast(modal.lineSlice(eb.text, r).len))) return eb; + // A file's own line count and line, from its index. + const lines = if (indexedLine(p, t, eb.text, 0) != null) panes.File.nlines(p.gpa, &t.pane().?.file.?) else modal.lineCount(eb.text); + if (r + 1 < lines) return eb; + if (b.hi_col < @as(i32, @intCast(lineAt(p, t, eb.text, r).line.len))) return eb; return editText(p, t, b.lo_row, b.hi_row + 1, -1); } @@ -476,6 +486,47 @@ pub fn setEditText(p: *Pardes, t: *Text, new: []u8) void { setEditTextEnd(p, t, new, null); } +const LineAt = struct { start: usize, line: []const u8 }; + +/// `text`'s line count: the file's index when `text` is the pane's file. +fn lineCountOf(p: *Pardes, t: *Text, text: []const u8) usize { + if (indexedLine(p, t, text, 0) != null) return panes.File.nlines(p.gpa, &t.pane().?.file.?); + return modal.cursorLineCount(text); +} + +/// The row offset `off` is on: a search of the file's index when `text` is +/// the pane's file, else the newlines before it counted (modal.lineAtOffset). +fn rowOf(p: *Pardes, t: *Text, text: []const u8, off: usize) usize { + if (indexedLine(p, t, text, 0) != null) { + const index = panes.File.lineIndex(p.gpa, &t.pane().?.file.?) catch return modal.lineAtOffset(text, off); + return std.sort.upperBound(usize, index, @min(off, text.len), struct { + fn order(key: usize, item: usize) std.math.Order { + return std.math.order(key, item); + } + }.order) -| 1; + } + return modal.lineAtOffset(text, off); +} + +/// Line `row` of `text` and where it starts: through the file's line index +/// when `text` is the pane's file (a lookup), else counted from the top. +/// Finding line 94000 by counting was most of a keystroke in a 7 MB file. +fn lineAt(p: *Pardes, t: *Text, text: []const u8, row: usize) LineAt { + return indexedLine(p, t, text, row) orelse .{ .start = modal.lineStartOffset(text, row), .line = modal.lineSlice(text, row) }; +} + +/// lineAt's lookup, null when `text` is not the pane's file or `row` is past its last line. +fn indexedLine(p: *Pardes, t: *Text, text: []const u8, row: usize) ?LineAt { + if (t.what != .body) return null; + const pane = t.pane() orelse return null; + const f = if (pane.file) |*f| f else return null; + if (f.content.ptr != text.ptr or f.content.len != text.len) return null; + if (row >= panes.File.nlines(p.gpa, f)) return null; + const start = @min(panes.File.lineStart(p.gpa, f, row), text.len); + const end = std.mem.indexOfScalarPos(u8, text, start, '\n') orelse text.len; + return .{ .start = start, .line = text[start..end] }; +} + /// Where text `s` put in at `c` ends: `g.`'s place after typing it. fn insertedEnd(c: modal.Cursor, s: []const u8) modal.Cursor { const nl = std.mem.lastIndexOfScalar(u8, s, '\n') orelse return .{ .row = c.row, .col = c.col + s.len }; @@ -486,6 +537,13 @@ fn insertedEnd(c: modal.Cursor, s: []const u8) modal.Cursor { /// rows) when the caller knows it, as typing does: then nothing scans the /// whole text to find it. Null, it is found by comparing the texts. pub fn setEditTextEnd(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor) void { + setEditTextSpan(p, t, new, known_end, null); +} + +/// `setEditTextEnd` with what a file's splice left alone at either end, when +/// the caller knows it (typing): the line index is then not found again by +/// comparing the whole old and new texts, which a keystroke in a 7 MB file did. +fn setEditTextSpan(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor, span: ?panes.File.Span) void { if (t.what == .tag) { // A tag is edited as it is shown, but only what follows its // prefix is its own: an edit that changed the prefix is refused. @@ -511,16 +569,13 @@ pub fn setEditTextEnd(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor) const pane = t.pane() orelse return retireEditText(p, new); // what changed ended where the texts agree again from their ends const old: []const u8, const row0: i32 = if (pane.file) |f| .{ f.content, 0 } else if (pane.ovl) |o| .{ o.text, o.row } else .{ "", 0 }; - const end = known_end orelse found: { - // ponytail: O(file), for the edits that do not say where they ended - // (paste, a pipe, s): typing passes its end and never comes here. - const front = std.mem.indexOfDiff(u8, old, new) orelse @min(old.len, new.len); - var back: usize = 0; - while (back < @min(old.len, new.len) - front and old[old.len - 1 - back] == new[new.len - 1 - back]) back += 1; - break :found modal.positionAt(new, new.len - back); - }; + // ponytail: O(file), for the edits that do not say what they changed + // (paste, a pipe, d, s): compared once, block by block, here; the same + // span then spares the line index a second comparison. + const found = span orelse if (known_end == null or pane.file != null) panes.File.spanOf(old, new) else null; + const end = known_end orelse modal.positionAt(new, new.len - found.?.tail); t.last_edit = .{ .row = @as(i32, @intCast(end.row)) + row0, .col = @intCast(end.col) }; - if (pane.file) |*f| return panes.File.setContent(p, f, new); + if (pane.file) |*f| return panes.File.setContentSpan(p, f, new, found); panes.terminal.setEditText(p, pane, new); } @@ -815,8 +870,10 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) void { const text = eb.text; const c = modal.Cursor{ .row = @intCast(@max(0, t.cur_row - eb.row0)), .col = @intCast(@max(0, t.cur_col)) }; if (key.text.len > 0 and !key.ctrl and !key.alt) { - const new = modal.insertAt(p.gpa, text, c, key.text) catch return; - setEditTextEnd(p, t, new, insertedEnd(c, key.text)); + // insertAt's offset (col clamped to the line), the line found once. + const at = if (indexedLine(p, t, text, c.row)) |l| l.start + @min(c.col, l.line.len) else modal.insertOffset(text, c); + const new = modal.spliceAlloc(p.gpa, text, at, at, key.text) catch return; + setEditTextSpan(p, t, new, insertedEnd(c, key.text), .{ .head = at, .tail = text.len - at }); for (key.text) |ch| { if (ch == '\n') { t.cur_row += 1; @@ -918,7 +975,8 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) void { }, Key.backspace => { if (t.cur_col > 0) { - const line = modal.lineSlice(text, c.row); + const found = lineAt(p, t, text, c.row); + const line = found.line; var prev = modal.prevGrapheme(line, c.col); // in leading blanks, back to the previous indent stop (helix // dedent): a whole unit when on one, else the part past it @@ -931,8 +989,12 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) void { prev = c.col; while (drop > 0 and prev > 0 and line[prev - 1] == ' ') : (drop -= 1) prev -= 1; } - const new = modal.deleteSpan(p.gpa, text, .{ .row = c.row, .col = prev }, c) catch return; - setEditTextEnd(p, t, new, .{ .row = c.row, .col = prev }); + // deleteSpan's splice, the line found once. + const s = found.start + @min(prev, line.len); + const e = found.start + @min(c.col, line.len); + const new = if (e > s) modal.spliceAlloc(p.gpa, text, s, e, "") catch return else p.gpa.dupe(u8, text) catch return; + const span: panes.File.Span = if (e > s) .{ .head = s, .tail = text.len - e } else .{ .head = text.len, .tail = 0 }; + setEditTextSpan(p, t, new, .{ .row = c.row, .col = prev }, span); t.cur_col = @intCast(prev); t.cur_pinned = true; t.show(); @@ -1479,26 +1541,33 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void { const text = eb.text; var rs: [Text.max_selections]modal.Selection = undefined; const got = t.ranges(text, eb.row0, &rs); - const nlines = modal.cursorLineCount(text); + // Lines, their count and rows from the file's index when it has one: + // counted from the top, a J in a 7 MB file was 8 ms. + const nlines = lineCountOf(p, t, text); const arena = p.scratch.allocator(); - const chs = arena.alloc(TextChange, nlines) catch return; - var chs_len: usize = 0; + var changes: std.ArrayList(TextChange) = .empty; var next: usize = 0; // lines below this one are joined already for (rs[0..got.n]) |r| { - const span = Text.rangeLineSpan(text, r); + // Text.rangeLineSpan, through rowOf + const lo = @min(r.anchor, r.head); + const hi = @max(r.anchor, r.head); + const span = .{ .start = rowOf(p, t, text, lo), .end = rowOf(p, t, text, if (lo == hi) hi else @max(modal.prevGrapheme(text, hi), lo)) }; var end = span.end; if (span.start == end) end = @min(end + 1, nlines - 1); var l = @max(span.start, next); while (l < end) : (l += 1) { - const from = modal.lineEndOffset(text, l); - var to = if (l + 1 >= nlines) text.len else modal.lineStartOffset(text, l + 1); + const here = lineAt(p, t, text, l); + const from = here.start + here.line.len; + var to = if (l + 1 >= nlines) text.len else lineAt(p, t, text, l + 1).start; while (to < text.len and (text[to] == ' ' or text[to] == '\t')) to += 1; - const sep: []const u8 = if (to == modal.lineEndOffset(text, @min(l + 1, nlines - 1))) "" else " "; - chs[chs_len] = .{ .from = from, .to = to, .ins = sep }; - chs_len += 1; + const last = lineAt(p, t, text, @min(l + 1, nlines - 1)); + const sep: []const u8 = if (to == last.start + last.line.len) "" else " "; + changes.append(arena, .{ .from = from, .to = to, .ins = sep }) catch return; } next = @max(next, end); } + const chs = changes.items; + const chs_len = chs.len; if (chs_len == 0) return; pushUndo(p, t); const new = applyChanges(p, text, chs[0..chs_len]) catch return; @@ -2459,3 +2528,72 @@ test "a paste into a terminal is xterm's: bracketed when the program asked, cont p.update(.{ .paste = "x\x1b[201~y\n" }); try std.testing.expectEqualStrings("\x1b[200~x [201~y\n\x1b[201~", try take(p, &out)); } + +test "a paste past the limit is cut at it, and says so" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const pane = try p.setTestFile(""); + pane.body.mode = .insert; + const limit = @import("memory.zig").limits.max_paste_bytes; + const big = try gpa.alloc(u8, limit + 100); + defer gpa.free(big); + @memset(big, 'x'); + applyPaste(p, big); + try std.testing.expect(pane.file.?.content.len <= limit); + try std.testing.expect(pane.file.?.content.len > 0); + try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "paste cut") != null); +} + +test "typing and backspace through the line index keep the text and the index as a rebuild has them" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("one\n two\n\nthree four\n"); + pane.body.mode = .insert; + var prng: std.Random.DefaultPrng = .init(11); + const random = prng.random(); + for (0..300) |_| { + _ = panes.File.lineIndex(gpa, &pane.file.?) catch unreachable; + const f = &pane.file.?; + const row = random.uintLessThan(usize, panes.File.nlines(gpa, f)); + const line = modal.lineSlice(f.content, row); + pane.body.cur_row = @intCast(row); + pane.body.cur_col = @intCast(random.uintLessThan(usize, line.len + 1)); + pane.body.cur_pinned = true; + if (random.boolean()) { + insertKey(p, &pane.body, .{ .cp = 'x', .text = "x" }); + } else { + insertKey(p, &pane.body, .{ .cp = Key.backspace }); + } + // the index the edits kept, against one built from the text + const kept = try gpa.dupe(usize, try panes.File.lineIndex(gpa, &pane.file.?)); + defer gpa.free(kept); + gpa.free(pane.file.?.line_starts); + pane.file.?.line_starts = &.{}; + try std.testing.expectEqualSlices(usize, try panes.File.lineIndex(gpa, &pane.file.?), kept); + } +} + +test "J joins a line with the next, its indent dropped, a space between unless the next is empty" { + const gpa = std.testing.allocator; + const cases = [_]struct { text: []const u8, row: i32, rows: i32 = 0, want: []const u8 }{ + .{ .text = "a\n b\nc\n", .row = 0, .want = "a b\nc\n" }, + .{ .text = "a\n\nc\n", .row = 0, .want = "a\nc\n" }, + .{ .text = "a\nb", .row = 1, .want = "a\nb" }, + .{ .text = "one\n\ttwo\nthree\nfour\n", .row = 0, .rows = 2, .want = "one two three\nfour\n" }, + }; + for (cases) |case| { + const p = try Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile(case.text); + pane.body.mode = .normal; + pane.body.cur_row = case.row; + pane.body.cur_col = 0; + if (case.rows > 0) pane.body.vsel = .{ .active = true, .row = case.row + case.rows, .col = 0, .explicit = true }; + normalJoin(p, &pane.body, false); + try std.testing.expectEqualStrings(case.want, pane.file.?.content); + } +} diff --git a/src/exec.zig b/src/exec.zig index d03a4a48..c9f304b0 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -913,6 +913,12 @@ pub fn commandDone(p: *Pardes, id: usize, status: ?u8) void { pane.repl = null; pane.command_status = status; pane.body.mode = .normal; + // Look's `git show`: its output went to a +Pager, the +Diff now; only a + // failure is worth the pane. + if (pane.command_pages) { + pane.command_pages = false; + if (status == 0) return p.removePane(id, null) catch {}; + } var buf: [16]u8 = undefined; const code = if (status) |n| std.fmt.bufPrint(&buf, "{d}", .{n}) catch "?" else "?"; var said: [32]u8 = undefined; diff --git a/src/fs.zig b/src/fs.zig index 061d4225..032ab9c0 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -1087,17 +1087,17 @@ pub const platform_has_fs = !pardes.isolated and switch (pardes.platform) { const find_max_hits = 512; /// What a Find or Grep walk passed over, said under its hits: directories -/// it might not open, files read only in part, and caps it stopped at. +/// it might not open, and caps it stopped at. pub const WalkNotes = struct { skipped: usize = 0, - /// Files longer than grep_max_bytes, searched in their first part only. - partial: usize = 0, hits_cut: bool = false, + /// A pane search's matching lines it stopped at, its rows filling + /// look.search_text_output_bytes; 0 if none. + rows_cut: usize = 0, /// The cap on entries walked it stopped at, 0 if none. walk_cut: usize = 0, pub const max_hits = find_max_hits; - pub const max_file_bytes = grep_max_bytes; pub const max_files = grep_max_files; pub const max_depth = find_max_depth; @@ -1105,7 +1105,7 @@ pub const WalkNotes = struct { pub fn write(n: WalkNotes, out: []u8) usize { var w = std.Io.Writer.fixed(out); if (n.hits_cut) w.print("cut at {d} hits\n", .{max_hits}) catch {}; - if (n.partial > 0) w.print("{d} file{s} read only in part (first {d} KiB)\n", .{ n.partial, if (n.partial == 1) "" else "s", max_file_bytes / 1024 }) catch {}; + if (n.rows_cut > 0) w.print("cut at {d} lines\n", .{n.rows_cut}) catch {}; if (n.walk_cut > 0) w.print("walk cut at {d} entries\n", .{n.walk_cut}) catch {}; if (n.skipped > 0) w.print("{d} director{s} skipped: permission denied\n", .{ n.skipped, if (n.skipped == 1) "y" else "ies" }) catch {}; return w.buffered().len; @@ -1124,7 +1124,8 @@ pub const WalkNotes = struct { }; const find_max_depth = 16; const find_max_steps = 100_000; -pub const search_max_output_bytes = pardes.MAX_PANES * find_max_hits * (4096 + 320); +/// One root's Find or Grep rows at most: its hits, each a path and a line. +pub const search_root_output_bytes = find_max_hits * (4096 + 320); const find_skip = [_][]const u8{ ".git", ".jj", "target", "node_modules", @@ -1194,9 +1195,10 @@ const grep_max_files = 20_000; const GrepResult = struct { bytes: usize, hits: usize }; -fn grepText(path: []const u8, text: []const u8, pat: []const u8, out: []u8, budget: usize) GrepResult { +/// `text` starts at line `first_line` + 1 of its file. +fn grepText(path: []const u8, text: []const u8, pat: []const u8, out: []u8, budget: usize, first_line: usize) GrepResult { var result: GrepResult = .{ .bytes = 0, .hits = 0 }; - var line: usize = 0; + var line: usize = first_line; var it = std.mem.splitScalar(u8, text, '\n'); while (it.next()) |raw| { line += 1; @@ -1220,7 +1222,7 @@ pub fn grep(arena: std.mem.Allocator, gpa: std.mem.Allocator, dir: []const u8, b if (!platform_has_fs) { for (sources) |s| { if (hits >= find_max_hits or written == out.len) break; - const result = grepText(s.path, s.contents, pat, out[written..], find_max_hits - hits); + const result = grepText(s.path, s.contents, pat, out[written..], find_max_hits - hits, 0); hits += result.hits; written += result.bytes; } @@ -1276,30 +1278,39 @@ pub fn grep(arena: std.mem.Allocator, gpa: std.mem.Allocator, dir: []const u8, b const path_z = std.fmt.bufPrintSentinel(&pathbuf, "{s}", .{path}, 0) catch return error.PathTooLong; const fd = libc.open(path_z, .{ .ACCMODE = .RDONLY, .CLOEXEC = true, .NONBLOCK = true }); if (fd < 0) continue; + defer _ = libc.close(fd); + // The whole file, a buffer at a time: each searched through its last + // whole line, the rest carried into the next. + const shown = lsp.rel(home, path); var len: usize = 0; - var readable = true; - while (len < buf.len) { - const n = libc.read(fd, buf[len..].ptr, buf.len - len); - if (n < 0) { - if (libc.errno(n) == .INTR) continue; - readable = false; - break; + var lines_before: usize = 0; + var first = true; + file: while (true) { + var eof = false; + while (len < buf.len) { + const n = libc.read(fd, buf[len..].ptr, buf.len - len); + if (n < 0) { + if (libc.errno(n) == .INTR) continue; + break :file; + } + if (n == 0) { + eof = true; + break; + } + len += @intCast(n); } - if (n == 0) break; - len += @intCast(n); + if (first and std.mem.indexOfScalar(u8, buf[0..@min(len, 1024)], 0) != null) break; + first = false; + // A line longer than the buffer is searched in buffer-sized parts. + const end = if (eof) len else if (std.mem.lastIndexOfScalar(u8, buf[0..len], '\n')) |nl| nl + 1 else len; + const result = grepText(shown, buf[0..end], pat, out[written..], find_max_hits - hits, lines_before); + hits += result.hits; + written += result.bytes; + if (eof or hits >= find_max_hits or written == out.len) break; + lines_before += std.mem.count(u8, buf[0..end], "\n"); + std.mem.copyForwards(u8, buf[0 .. len - end], buf[end..len]); + len -= end; } - // Read to its cap with more behind it: searched in part, and said. - var more: [1]u8 = undefined; - const longer = readable and len == buf.len and libc.read(fd, &more, 1) > 0; - _ = libc.close(fd); - if (!readable) continue; - const text = buf[0..len]; - if (std.mem.indexOfScalar(u8, text[0..@min(len, 1024)], 0) != null) continue; - if (longer) notes.partial += 1; - const shown = lsp.rel(home, path); - const result = grepText(shown, text, pat, out[written..], find_max_hits - hits); - hits += result.hits; - written += result.bytes; if (hits >= find_max_hits) notes.hits_cut = true; } return written; @@ -1366,14 +1377,14 @@ test "a directory the walk may not open is skipped and counted: Grep and Find ke try std.testing.expectEqual(@as(usize, 1), notes.skipped); } -test "Grep says when a cap hid what it might have found: a file read in part, the hits cut" { +test "Grep reads a long file to its end, and says when its hits were cut" { if (!platform_has_fs) return; const gpa = std.testing.allocator; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var base_buf: [std.fs.max_path_bytes]u8 = undefined; const dir = base_buf[0..try tmp.dir.realPath(std.testing.io, &base_buf)]; - // The match past the first 256 KiB of a long file. + // A match past the first buffer of a long file, on its own line number. const long = try gpa.alloc(u8, grep_max_bytes + 64); defer gpa.free(long); @memset(long, 'x'); @@ -1386,8 +1397,9 @@ test "Grep says when a cap hid what it might have found: a file read in part, th defer gpa.free(out); var notes: WalkNotes = .{}; var n = try grep(arena.allocator(), gpa, dir, dir, "needle", out, ¬es); - try std.testing.expectEqual(@as(usize, 0), n); - try std.testing.expectEqual(@as(usize, 1), notes.partial); + var want_buf: [64]u8 = undefined; + const want = try std.fmt.bufPrint(&want_buf, "long.txt:{d}:", .{std.mem.count(u8, long, "\n")}); + try std.testing.expect(std.mem.startsWith(u8, out[0..n], want)); // More hits than the cap. var many: std.ArrayList(u8) = .empty; defer many.deinit(gpa); @@ -1398,7 +1410,7 @@ test "Grep says when a cap hid what it might have found: a file read in part, th try std.testing.expect(notes.hits_cut); var said: [256]u8 = undefined; const lines = said[0..notes.write(&said)]; - try std.testing.expectEqualStrings("cut at 512 hits\n1 file read only in part (first 256 KiB)\n", lines); + try std.testing.expectEqualStrings("cut at 512 hits\n", lines); try std.testing.expect(WalkNotes.isNote("cut at 512 hits") and WalkNotes.isNote("2 directories skipped: permission denied") and !WalkNotes.isNote("src/2 directories.txt")); } diff --git a/src/gui/gui.zig b/src/gui/gui.zig index f9921111..9cd37b6b 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -828,18 +828,13 @@ fn emitTouchClick(t: *Touch, in: *Input, point: TouchNormPoint, button: pardes.M in.post(.{ .mouse = .{ .button = button, .kind = .release, .col = cell.col, .row = cell.row, .tag_hit = cell.tag_hit, .body_hit = cell.body_hit } }); } -fn normCell(norm: f32, win: f32, cell: f32) u16 { - const px = std.math.clamp(norm, 0.0, 1.0) * win; - return @intFromFloat(@max(0, @floor(px / cell))); -} - const Pty = struct { fd: c_int, pid: libc.pid_t, serial: u32, kill_at: i64 = 0, - reader: ?std.Thread = null, - stop: [2]c_int = .{ -1, -1 }, + /// Its reader, blocked in an Io read: `closePty` cancels it. + reader: ?std.Io.Future(void) = null, /// A command pane's child, watched to its exit (host_io.watchExit); /// the pty stays open until both its end of file and its exit. cmd: host_io.CommandWatch = .{}, @@ -876,31 +871,6 @@ const Msg = union(enum) { } }; -const LspWorkers = struct { - const capacity = 16; - active: std.atomic.Value(usize) = .init(0), - - fn start(workers: *LspWorkers) bool { - var count = workers.active.load(.monotonic); - while (count < capacity) - count = workers.active.cmpxchgWeak(count, count + 1, .monotonic, .monotonic) orelse return true; - return false; - } - - fn finish(workers: *LspWorkers) void { - const previous = workers.active.fetchSub(1, .release); - std.debug.assert(previous > 0); - } - - fn wait(workers: *LspWorkers) void { - var spins: u8 = 0; - while (workers.active.load(.acquire) != 0) { - spins +%= 1; - if (spins == 0) std.Thread.yield() catch {} else std.atomic.spinLoopHint(); - } - } -}; - const PipeTasks = selection_pipe.Tasks; const queue_capacity = 512; @@ -918,10 +888,13 @@ const MessageBatch = struct { const Queue = struct { gpa: std.mem.Allocator, lsp_allocator: std.mem.Allocator, - lsp_workers: *LspWorkers, + /// The query in flight; the UI thread's alone (spawnLsp, teardown). + lsp_task: ?host_io.Lsp.Task = null, sdl_wake: bool, // wake a blocking SDL_WaitEventTimeout on cross-thread push - mutex: libc.pthread_mutex_t = .{}, - space: libc.pthread_cond_t = .{}, + /// The process's Io: its terminal readers wait for space on `space`. + io: std.Io, + mutex: std.Io.Mutex = .init, + space: std.Io.Condition = .init, items: [queue_capacity]Msg = undefined, head: usize = 0, len: usize = 0, @@ -934,13 +907,11 @@ const Queue = struct { completion_len: usize = 0, fn lock(q: *Queue) void { - // unreachable failures: pthread calls on this queue's own initialised, non-recursive - // mutex and condition fail only on misuse (EINVAL, EDEADLK), never on input. - std.debug.assert(libc.pthread_mutex_lock(&q.mutex) == .SUCCESS); + q.mutex.lockUncancelable(q.io); } fn unlock(q: *Queue) void { - std.debug.assert(libc.pthread_mutex_unlock(&q.mutex) == .SUCCESS); + q.mutex.unlock(q.io); } fn wake(q: *Queue) void { @@ -973,14 +944,14 @@ const Queue = struct { }; if (matches) q.removeAt(i).deinit(q.gpa, q.lsp_allocator) else i += 1; } - std.debug.assert(libc.pthread_cond_broadcast(&q.space) == .SUCCESS); + q.space.broadcast(q.io); } fn pushOutput(q: *Queue, pane: u8, gen: u32, bytes: []u8) bool { q.lock(); while (!q.closed and q.readers[pane] == gen and q.len >= output_capacity) { q.waiting += 1; - std.debug.assert(libc.pthread_cond_wait(&q.space, &q.mutex) == .SUCCESS); + q.space.waitUncancelable(q.io, &q.mutex); q.waiting -= 1; } if (q.closed or q.readers[pane] != gen) { @@ -1088,7 +1059,7 @@ const Queue = struct { @memcpy(batch.items[batch.len..][0..q.completion_len], q.completions[0..q.completion_len]); batch.len += q.completion_len; q.completion_len = 0; - std.debug.assert(libc.pthread_cond_broadcast(&q.space) == .SUCCESS); + q.space.broadcast(q.io); } fn discardCompletions(q: *Queue) void { @@ -1104,7 +1075,7 @@ const Queue = struct { q.closed = true; q.files_changed = false; q.readers = @splat(null); - std.debug.assert(libc.pthread_cond_broadcast(&q.space) == .SUCCESS); + q.space.broadcast(q.io); q.unlock(); q.discardCompletions(); q.lock(); @@ -1121,15 +1092,12 @@ const Queue = struct { fn deinit(q: *Queue) void { q.close(); std.debug.assert(q.waiting == 0); - std.debug.assert(libc.pthread_cond_destroy(&q.space) == .SUCCESS); - std.debug.assert(libc.pthread_mutex_destroy(&q.mutex) == .SUCCESS); } }; test "GUI completion survives a full output queue" { const gpa = std.testing.allocator; - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false, .lsp_id = 7 }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .lsp_id = 7 }; defer queue.deinit(); queue.acceptReader(0, 1); for (0..output_capacity) |_| queue.push(.{ .output = .{ @@ -1185,8 +1153,7 @@ const PtyTests = struct { test "GUI PTY backpressure retains byte order and tail before EOF" { const gpa = std.testing.allocator; - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); queue.acceptReader(0, 1); for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "before") } }); @@ -1220,8 +1187,7 @@ test "GUI PTY backpressure retains byte order and tail before EOF" { test "GUI PTY backpressure cancellation and close release owned producers" { const gpa = std.testing.allocator; for ([_]bool{ false, true }) |close| { - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); queue.acceptReader(0, 1); for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "before") } }); @@ -1252,8 +1218,7 @@ test "GUI PTY backpressure cancellation and close release owned producers" { test "GUI PTY EOF reserve cannot evict terminal output" { const gpa = std.testing.allocator; - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); for (0..pardes.MAX_PANES) |pane| queue.acceptReader(@intCast(pane), 1); for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "before") } }); @@ -1269,8 +1234,7 @@ test "GUI PTY reader delivers a real shell tail before EOF and joins on close" { const gpa = std.testing.allocator; const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); defer core.deinit(); - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); var ptys: [pardes.MAX_PANES]?Pty = @splat(null); var gens: [pardes.MAX_PANES]u32 = @splat(1); @@ -1320,8 +1284,7 @@ test "GUI PTY Restore joins a real reader waiting for queue space" { var core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); defer core.deinit(); try dump.dumpState(core); - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); queue.acceptReader(1, 1); for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = 1, .gen = 1, .bytes = try gpa.dupe(u8, "other pane") } }); @@ -1365,8 +1328,7 @@ test "GUI PTY deletion joins an idle reader and retains only its owned child for const gpa = std.testing.allocator; const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); defer core.deinit(); - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); var ptys: [pardes.MAX_PANES]?Pty = @splat(null); var gens: [pardes.MAX_PANES]u32 = @splat(1); @@ -1397,8 +1359,7 @@ test "GUI PTY deletion joins an idle reader and retains only its owned child for test "GUI PTY file watcher stops and joins without closing its watched descriptor" { if (!file_watch.supported) return error.SkipZigTest; const gpa = std.testing.allocator; - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); const fd = file_watch.init(true); if (fd < 0) return error.WatchInitFailed; @@ -1422,8 +1383,7 @@ test "GUI completion failure survives backlog and leaves the document unchanged" pane.body.cur_col = 1; core.lspRequest(core.active, .completion, ""); const id = core.lsp_wait.?.id; - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false, .lsp_id = id }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .lsp_id = id }; defer queue.deinit(); queue.acceptReader(0, 1); for (0..output_capacity) |_| queue.push(.{ .output = .{ @@ -1452,8 +1412,7 @@ test "GUI completion failure survives backlog and leaves the document unchanged" test "GUI completions retain their arrival order across LSP and pipes" { const gpa = std.testing.allocator; - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false, .lsp_id = 2 }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .lsp_id = 2 }; defer queue.deinit(); queue.push(.{ .pipe = .{ .id = 1, .success = true, .outputs = &.{} } }); queue.push(.{ .lsp = .{ .id = 2, .rows = try gpa.dupe(u8, "second") } }); @@ -1469,8 +1428,7 @@ test "GUI completions retain their arrival order across LSP and pipes" { test "GUI completion reset frees queued payloads and rejects late LSP workers" { const gpa = std.testing.allocator; - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false, .lsp_id = 9 }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .lsp_id = 9 }; defer queue.deinit(); queue.acceptReader(0, 1); queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "retained") } }); @@ -1523,29 +1481,22 @@ fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void { q.push(.{ .lsp_status = copy }); } -fn readPtyThread(gpa: std.mem.Allocator, fd: c_int, stop: c_int, pane: u8, gen: u32, q: *Queue) void { +fn readPty(io: std.Io, gpa: std.mem.Allocator, fd: c_int, pane: u8, gen: u32, q: *Queue) void { + const pty: std.Io.File = .{ .handle = fd, .flags = .{ .nonblocking = false } }; var buf: [0x10000]u8 = undefined; var failure: ?anyerror = null; while (true) { - var fds = [_]libc.pollfd{ - .{ .fd = stop, .events = libc.POLL.IN, .revents = 0 }, - .{ .fd = fd, .events = libc.POLL.IN, .revents = 0 }, + const n = pty.readStreaming(io, &.{&buf}) catch |err| switch (err) { + // A pty whose child has gone answers EIO: its end of file. + error.EndOfStream, error.InputOutput => break, + error.Canceled => return, + else => { + failure = err; + break; + }, }; - if (libc.poll(&fds, fds.len, -1) < 0) { - if (libc.errno(-1) == .INTR) continue; - failure = error.PollFailed; - break; - } - if (fds[0].revents != 0) return; - if (fds[1].revents == 0) continue; - const n = libc.read(fd, &buf, buf.len); - if (n < 0) { - if (libc.errno(n) == .INTR) continue; - if (libc.errno(n) != .IO) failure = error.ReadFailed; - break; - } - if (n == 0) break; - const bytes = gpa.dupe(u8, buf[0..@intCast(n)]) catch |err| { + if (n == 0) continue; + const bytes = gpa.dupe(u8, buf[0..n]) catch |err| { failure = err; break; }; @@ -1566,14 +1517,9 @@ fn stopPipe() ![2]c_int { fn spawnReader(gpa: std.mem.Allocator, pt: *Pty, pane: u8, gen: u32, q: *Queue) !void { std.debug.assert(pt.reader == null and pt.fd >= 0); - const stop = try stopPipe(); - errdefer for (stop) |fd| { - _ = libc.close(fd); - }; q.acceptReader(pane, gen); errdefer q.cancelReader(pane, gen); - pt.reader = try std.Thread.spawn(.{}, readPtyThread, .{ gpa, pt.fd, stop[0], pane, gen, q }); - pt.stop = stop; + pt.reader = try q.io.concurrent(readPty, .{ q.io, gpa, pt.fd, pane, gen, q }); } fn watchThread(fd: c_int, stop: c_int, q: *Queue) void { @@ -1592,9 +1538,8 @@ fn watchThread(fd: c_int, stop: c_int, q: *Queue) void { } } -fn lspThread(lsp_allocator: std.mem.Allocator, workers: *LspWorkers, job: *host_io.Lsp.Job, q: *Queue) void { - defer workers.finish(); - host_io.Lsp.work(lsp_allocator, job, q, pushLspRows); +fn lspThread(io: std.Io, lsp_allocator: std.mem.Allocator, job: *host_io.Lsp.Job, q: *Queue) anyerror!void { + host_io.Lsp.work(io, lsp_allocator, job, q, pushLspRows); } fn pushLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { @@ -1602,107 +1547,52 @@ fn pushLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { q.push(.{ .lsp = .{ .id = id, .rows = rows } }); } -fn spawnLsp(core: *pardes.Pardes, q: *Queue, e: host_io.Lsp.Request) void { +fn spawnLsp(core: *pardes.Pardes, io: std.Io, q: *Queue, e: host_io.Lsp.Request) void { const lsp_allocator = q.lsp_allocator; q.lock(); q.lsp_id = e.id; q.discardLsp(); q.unlock(); - if (!q.lsp_workers.start()) { - core.update(.{ .lsp_resp = .{ .id = e.id, .rows = null } }); - return core.reportError(e.pane, "lsp", error.WorkersBusy); + // Its waits are cancelation points: this join is short, and what it + // still delivers carries an id the queue now rejects. + if (q.lsp_task) |*old| { + old.future.cancel(io) catch {}; + q.lsp_task = null; } const job = host_io.Lsp.snapshot(lsp_allocator, core, e) catch |err| { - q.lsp_workers.finish(); core.update(.{ .lsp_resp = .{ .id = e.id, .rows = null } }); return core.reportError(e.pane, "lsp", err); }; - const th = std.Thread.spawn(.{}, lspThread, .{ lsp_allocator, q.lsp_workers, job, q }) catch |err| { - q.lsp_workers.finish(); + const future = io.concurrent(lspThread, .{ io, lsp_allocator, job, q }) catch |err| { job.free(lsp_allocator); core.update(.{ .lsp_resp = .{ .id = e.id, .rows = null } }); return core.reportError(e.pane, "lsp", err); }; - th.detach(); + q.lsp_task = .{ .id = e.id, .future = future }; } -test "GUI LSP worker limit rejects before allocation and recovers after owned workers exit" { +test "GUI LSP query answers, and a newer one cancels it" { const gpa = std.testing.allocator; + const io = std.testing.io; const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); defer core.deinit(); - const pane = try core.setTestFile("abc"); - pane.body.cur_col = 1; - var workers: LspWorkers = .{}; - var gate: std.Io.Event = .unset; - var threads: [LspWorkers.capacity]?std.Thread = @splat(null); - defer { - gate.set(std.testing.io); - for (threads) |thread| if (thread) |owned| owned.join(); - workers.wait(); - } - for (&threads) |*thread| { - try std.testing.expect(workers.start()); - thread.* = std.Thread.spawn(.{}, struct { - fn run(active: *LspWorkers, ready: *std.Io.Event) void { - defer active.finish(); - ready.waitUncancelable(std.testing.io); - } - }.run, .{ &workers, &gate }) catch |err| { - workers.finish(); - return err; - }; - } - try std.testing.expectEqual(@as(usize, LspWorkers.capacity), workers.active.load(.monotonic)); - try std.testing.expect(!workers.start()); - var failing = std.testing.FailingAllocator.init(gpa, .{ .fail_index = 0 }); - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = failing.allocator(), .lsp_workers = &workers, .sdl_wake = false }; + _ = try core.setTestFile("abc"); + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); - core.lspRequest(core.active, .completion, ""); - const rejected = core.lsp_wait.?.id; - queue.lsp_id = rejected -% 1; - queue.push(.{ .lsp = .{ .id = queue.lsp_id.?, .rows = try gpa.dupe(u8, "obsolete queued result") } }); - spawnLsp(core, &queue, .{ - .id = rejected, - .kind = .completion, - .pane = @intCast(core.active), - .offset = 1, - .arg = "", - }); - try std.testing.expect(core.lsp_wait == null); - try std.testing.expectEqualStrings("abc", pane.file.?.content); - try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); - try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "workers busy") != null); - try std.testing.expectEqual(@as(usize, 0), failing.alloc_index); - try std.testing.expect(!failing.has_induced_failure); - try std.testing.expectEqual(rejected, queue.lsp_id.?); - try std.testing.expectEqual(@as(usize, 0), queue.completion_len); - queue.push(.{ .lsp = .{ .id = rejected -% 1, .rows = try gpa.dupe(u8, "obsolete late result") } }); - try std.testing.expectEqual(@as(usize, 0), queue.completion_len); - try std.testing.expectEqual(@as(usize, LspWorkers.capacity), workers.active.load(.monotonic)); - - gate.set(std.testing.io); - workers.wait(); - try std.testing.expectEqual(@as(usize, 0), workers.active.load(.monotonic)); - queue.lsp_allocator = gpa; - core.lspRequest(core.active, .status, ""); - const accepted = core.lsp_wait.?.id; - spawnLsp(core, &queue, .{ - .id = accepted, - .kind = .status, - .pane = @intCast(core.active), - .offset = 0, - .arg = "", - }); - workers.wait(); + for (0..2) |_| { + core.lspRequest(core.active, .status, ""); + spawnLsp(core, io, &queue, .{ .id = core.lsp_wait.?.id, .kind = .status, .pane = @intCast(core.active), .offset = 0, .arg = "" }); + } + const last = core.lsp_wait.?.id; + try queue.lsp_task.?.future.await(io); + queue.lsp_task = null; var batch = queue.take(); defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); try std.testing.expectEqual(@as(usize, 1), batch.len); - try std.testing.expectEqual(accepted, batch.items[0].lsp.id); + try std.testing.expectEqual(last, batch.items[0].lsp.id); try std.testing.expect(batch.items[0].lsp.rows != null); - core.update(.{ .lsp_resp = .{ .id = accepted, .rows = batch.items[0].lsp.rows } }); + core.update(.{ .lsp_resp = .{ .id = last, .rows = batch.items[0].lsp.rows } }); try std.testing.expect(core.lsp_wait == null); - try std.testing.expectEqual(@as(usize, 0), workers.active.load(.monotonic)); - try std.testing.expectEqualStrings("abc", pane.file.?.content); } fn pipeThread(io: std.Io, gpa: std.mem.Allocator, job: *selection_pipe.Job, q: *Queue) anyerror!void { @@ -1748,11 +1638,10 @@ test "GUI worker setup failures finish matching LSP and pipe requests" { var failing = std.testing.FailingAllocator.init(gpa, .{ .fail_index = if (failure == 0) 0 else std.math.maxInt(usize), }); - var workers: LspWorkers = .{}; var queue: Queue = .{ + .io = std.testing.io, .gpa = gpa, .lsp_allocator = failing.allocator(), - .lsp_workers = &workers, .sdl_wake = false, }; defer queue.deinit(); @@ -1768,7 +1657,7 @@ test "GUI worker setup failures finish matching LSP and pipe requests" { if (failure == 0) { queue.lsp_id = req.id -% 1; queue.push(.{ .lsp = .{ .id = queue.lsp_id.?, .rows = try gpa.dupe(u8, "old queued result") } }); - spawnLsp(core, &queue, req); + spawnLsp(core, std.testing.io, &queue, req); try std.testing.expectEqual(req.id, queue.lsp_id.?); try std.testing.expectEqual(@as(usize, 0), queue.completion_len); queue.push(.{ .lsp = .{ .id = req.id -% 1, .rows = try gpa.dupe(u8, "late old result") } }); @@ -1791,7 +1680,7 @@ test "GUI worker setup failures finish matching LSP and pipe requests" { spawnPipe(core, failing_io, failing.allocator(), &queue, &tasks, pipe_id); try std.testing.expect(core.pipe.wait == null); try std.testing.expectEqualStrings("abc", pane.file.?.content); - try std.testing.expectEqual(@as(usize, 0), workers.active.load(.monotonic)); + try std.testing.expect(queue.lsp_task == null); try std.testing.expectEqual(@as(usize, 0), tasks.len); try std.testing.expectEqual(@as(usize, 0), queue.len); } @@ -2071,22 +1960,6 @@ fn fixedCellLayout(g: *const Gui) CellLayout { return .{ .w = @floatFromInt(g.cell_w), .h = @floatFromInt(g.cell_h), .x_off = 0, .y_off = 0 }; } -fn compactTaglineLayout(g: *const Gui, origin_col: f32) CellLayout { - const body_w: f32 = @floatFromInt(g.cell_w); - const tag_w: f32 = @floatFromInt(g.tagline_width); - return .{ - .w = tag_w, - .h = @floatFromInt(g.cell_h), - .x_off = origin_col * (body_w - tag_w), - .y_off = 0, - }; -} - -fn taglineLayoutForCell(g: *const Gui, col: u16) CellLayout { - // Canonical tag cells are physical grips; text is drawn by TagLayer. - return compactTaglineLayout(g, @floatFromInt(col)); -} - const boxContains = pardes.animation.Box.contains; /// What one tier draws, or one track in tiers 2 and 3 (docs/render-pipeline.md @@ -2663,15 +2536,14 @@ fn localSession( var ptys: [pardes.MAX_PANES]?Pty = @splat(null); var gens: [pardes.MAX_PANES]u32 = @splat(0); - var lsp_workers: LspWorkers = .{}; var queue: Queue = .{ + .io = io, .gpa = gpa, .lsp_allocator = allocs.lsp, - .lsp_workers = &lsp_workers, .sdl_wake = true, }; defer { - lsp_workers.wait(); + if (queue.lsp_task) |*task| task.future.cancel(io) catch {}; queue.deinit(); } var pipe_tasks: PipeTasks = .{}; @@ -3035,15 +2907,14 @@ fn runGrid(init: std.process.Init, opts_in: pardes.Options) !void { var ptys: [pardes.MAX_PANES]?Pty = @splat(null); var gens: [pardes.MAX_PANES]u32 = @splat(0); - var lsp_workers: LspWorkers = .{}; var queue: Queue = .{ + .io = io, .gpa = gpa, .lsp_allocator = allocs.lsp, - .lsp_workers = &lsp_workers, .sdl_wake = false, }; defer { - lsp_workers.wait(); + if (queue.lsp_task) |*task| task.future.cancel(io) catch {}; queue.deinit(); } var pipe_tasks: PipeTasks = .{}; @@ -4195,13 +4066,12 @@ const Shell = struct { fn closePty(s: *Shell, pane: u8) void { const pt = if (s.ptys[pane]) |*pt| pt else return; if (pt.fd < 0) return; + // Out of the queue first, which lets go a reader waiting for room + // in it; then its read is canceled (or, at its end, it is joined). s.queue.cancelReader(pane, s.gens[pane]); - if (pt.reader) |thread| { - _ = host_io.writeFd(pt.stop[1], "x"); - thread.join(); + if (pt.reader) |*reader| { + reader.cancel(s.queue.io); // the Io it was started on (spawnReader) pt.reader = null; - for (pt.stop) |fd| _ = libc.close(fd); - pt.stop = .{ -1, -1 }; } _ = libc.close(pt.fd); pt.fd = -1; @@ -4848,11 +4718,10 @@ test "the headless grid host round-trips a yank back as a paste" { var ptys: [pardes.MAX_PANES]?Pty = @splat(null); var gens: [pardes.MAX_PANES]u32 = @splat(0); - var lsp_workers: LspWorkers = .{}; var queue: Queue = .{ + .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, - .lsp_workers = &lsp_workers, .sdl_wake = false, }; defer queue.deinit(); @@ -4933,7 +4802,7 @@ fn openLink(ctx: ?*anyopaque, url: []const u8) void { fn lsp(ctx: ?*anyopaque, req: host_io.Lsp.Request) void { const s = shellOf(ctx); - if (s.threads_ok) return spawnLsp(s.core, s.queue, req); + if (s.threads_ok) return spawnLsp(s.core, s.io, s.queue, req); s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); s.core.reportError(req.pane, "lsp", error.WorkersUnavailable); } @@ -5198,6 +5067,12 @@ const Glide = struct { below: []pardes.Cell = &.{}, nabove: u16 = 0, nbelow: u16 = 0, + /// The core's lines a scroll moved, not yet measured; and what the + /// glide in flight has moved, in lines and in the picture's rows. A + /// wrapped line is several rows, and the thumb counts lines. + pending_lines: f32 = 0, + lines_moved: f32 = 0, + rows_moved: f32 = 0, /// The Motion flavour the last loop step read. motion: pardes.animation.Motion = .{}, @@ -5211,6 +5086,16 @@ const Glide = struct { glide.pending = false; glide.nabove = 0; glide.nbelow = 0; + glide.pending_lines = 0; + glide.lines_moved = 0; + glide.rows_moved = 0; + } + + /// The glide's lag in the core's lines: its rows, scaled by the lines + /// they carried. + fn lagLines(glide: *const Glide) f32 { + if (glide.rows_moved == 0) return glide.offset; + return glide.offset * glide.lines_moved / glide.rows_moved; } fn deinit(glide: *Glide, gpa: std.mem.Allocator) void { @@ -5270,6 +5155,9 @@ const Glide = struct { break :found kk; } else 0; if (shift == 0) return glide.stop(); + glide.rows_moved += @floatFromInt(shift); + glide.lines_moved += glide.pending_lines; + glide.pending_lines = 0; // The rows that left: past the top going down, the bottom going up. if (!glide.ensure(gpa)) return glide.stop(); const keep = if (glide.dir > 0) &glide.above else &glide.below; @@ -5326,6 +5214,8 @@ fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { glide.offset = 0; glide.nabove = 0; glide.nbelow = 0; + glide.lines_moved = 0; + glide.rows_moved = 0; } } const notch = g.scroll_notch; @@ -5357,6 +5247,8 @@ fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { } var left = notch; var moved = false; + const first = pane.scroll(); + defer glide.pending_lines += @floatFromInt(@abs(pane.scroll() - first)); while (left != 0) { const down = left > 0; left += if (down) -1 else 1; @@ -9347,7 +9239,7 @@ fn paneDecor(g: *Gui, gpa: std.mem.Allocator, regions: []const pardes.Region, gr // In pixels, not rows, and with the picture while it glides // or lags behind the core's rows (both in rows of the body). const lag: f32 = if (g.glide.offset != 0 and g.glide.pane == rail.owner) - g.glide.offset + g.glide.lagLines() else if (g.scroll_pane != null and g.scroll_pane.? == rail.owner) g.scroll_lag else 0; const track_px = body_y1 - y0; const size = @min(track_px, @max(layout.h, rail.thumb_span * track_px)); @@ -9983,6 +9875,12 @@ test "the scrollbar thumb moves by the pixel and follows a glide" { g.glide.pane = core.active; g.glide.offset = -1; try std.testing.expectApproxEqAbs(at10, try thumb.top(&g, core), 1e-3); + // A wrapped line: the picture moved two rows for the one line, and the + // thumb lags the line, not two. + g.glide.lines_moved = 1; + g.glide.rows_moved = 2; + g.glide.offset = -2; + try std.testing.expectApproxEqAbs(at10, try thumb.top(&g, core), 1e-3); } test "the rule between stacked panes stays in the tag band's slack, at any tagline size" { @@ -10581,12 +10479,11 @@ fn appendWorkspacePet(g: *Gui, core: ?*const pardes.Pardes, surface: *const pard return; } // Read only the rendered global row. No pane/column tags, status text or - // mouse targets are changed, and editing the workspace parks the pet. + // mouse targets are changed; editing the workspace tag leaves it be. var occupied: usize = 0; for (surface.cells[0..surface.cols], 0..) |*cell, i| { if (cellCodepoint(cell) != ' ') occupied = i + 1; } - const editing = p.header_focus and p.header_column == null; const band_top: f32 = @floatFromInt(taglineBandOffset(0, builder.win_h, g.cell_h, g.tagline_height)); const region = if (occupied >= surface.cols) null else pet.lane( occupied, @@ -10594,7 +10491,6 @@ fn appendWorkspacePet(g: *Gui, core: ?*const pardes.Pardes, surface: *const pard builder.win_w, band_top, @floatFromInt(g.tagline_height), - editing, ); // SDL's regular pump already wakes at least every 16 ms. Quantization in // sample keeps this independent of input, render rate and scene effects, @@ -10602,16 +10498,24 @@ fn appendWorkspacePet(g: *Gui, core: ?*const pardes.Pardes, surface: *const pard const pose = g.pet_state.sample(kind, c.SDL_GetTicks(), region) orelse return; const ink = overlayRgb(p.chromeTheme().tag_fg); const eye = overlayRgb(p.chromeTheme().tag_bg); + // Behind the cursor: the overlay is over the cells, so the pixels a + // cursor covers are left out. + var boxes: [max_cursors]CursorBox = undefined; + const cursors = boxes[0..cursorBoxes(g, surface, fixedCellLayout(g), builder.win_h, &boxes)]; for (0..pet.height) |y| for (0..pet.width) |x| { const color = pet.pixel(kind, pose, x, y); if (color == 0) continue; const x0 = pose.x + @as(f32, @floatFromInt(x)) * pose.scale; const y0 = pose.y + @as(f32, @floatFromInt(y)) * pose.scale; + const covered = for (cursors) |box| { + if (x0 < box.x1 and x0 + pose.scale > box.x0 and y0 < box.y1 and y0 + pose.scale > box.y0) break true; + } else false; + if (covered) continue; builder.addRect(x0, y0, x0 + pose.scale, y0 + pose.scale, if (color == 2) eye else ink); }; } -test "pet overlay stays beyond workspace text and vanishes while its tag is edited" { +test "pet overlay stays beyond workspace text and keeps going while its tag is edited" { std.testing.refAllDecls(pet); const core = try pardes.Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); defer core.deinit(); @@ -10638,10 +10542,13 @@ test "pet overlay stays beyond workspace text and vanishes while its tag is edit try std.testing.expect(x >= 20 * 8 - 0.01); try std.testing.expect(y >= 0 and y < 20); } + const before = g.pet_state; pardes.tagline.enterHeader(core, null); builder.len = 0; appendWorkspacePet(&g, core, &surface, &builder); - try std.testing.expectEqual(@as(usize, 0), builder.len); + try std.testing.expect(builder.len > 0); + try std.testing.expectEqual(before.tick, g.pet_state.tick); + try std.testing.expectEqual(before.x, g.pet_state.x); } fn cursorColor(chrome: *const pardes.Chrome, cell: *const pardes.Cell, role: pardes.FontRole) [3]u8 { diff --git a/src/gui/pet.zig b/src/gui/pet.zig index a249d70f..7a13ca91 100644 --- a/src/gui/pet.zig +++ b/src/gui/pet.zig @@ -13,8 +13,8 @@ const hop_ticks = 8; /// Only the trailing blank part of the global tag is eligible. The caller /// supplies its last occupied cell, including any wide-grapheme continuation. -pub fn lane(occupied: usize, cell_w: f32, screen_w: f32, top: f32, band_h: f32, editing: bool) ?Lane { - if (editing or cell_w <= 0 or band_h < height + 2) return null; +pub fn lane(occupied: usize, cell_w: f32, screen_w: f32, top: f32, band_h: f32) ?Lane { + if (cell_w <= 0 or band_h < height + 2) return null; const scale = @min(3, @floor((band_h - 2) / height)); const left = @as(f32, @floatFromInt(occupied + 2)) * cell_w; const right = screen_w - 2 * cell_w - width * scale; @@ -32,11 +32,10 @@ pub const State = struct { frame: u8 = 0, rng: std.Random.DefaultPrng = undefined, + /// No lane (the tag's text has filled it) hides the pet but keeps + /// where it was and what it was doing. pub fn sample(s: *State, kind: Kind, now_ms: u64, area: ?Lane) ?Pose { - const region = area orelse { - s.* = .{}; - return null; - }; + const region = area orelse return null; if (kind == .off) { s.* = .{}; return null; @@ -142,11 +141,10 @@ pub fn pixel(kind: Kind, pose: Pose, x_in: usize, y: usize) u2 { }; } -test "pet stays in trailing workspace blanks and disappears during editing" { - try std.testing.expect(lane(20, 8, 200, 0, 16, false) == null); - try std.testing.expect(lane(3, 8, 200, 0, 16, true) == null); - try std.testing.expect(lane(3, 8, 200, 0, 10, false) == null); - const region = lane(3, 8, 200, 2, 20, false).?; +test "pet stays in trailing workspace blanks, and a lost lane hides it without a reset" { + try std.testing.expect(lane(20, 8, 200, 0, 16) == null); + try std.testing.expect(lane(3, 8, 200, 0, 10) == null); + const region = lane(3, 8, 200, 2, 20).?; try std.testing.expect(region.left >= 5 * 8); var state: State = .{}; var moved = false; @@ -159,12 +157,15 @@ test "pet stays in trailing workspace blanks and disappears during editing" { reversed = reversed or pose.left; } try std.testing.expect(moved and reversed); + const kept = state; try std.testing.expect(state.sample(.cat, 300000, null) == null); + try std.testing.expectEqual(kept.x, state.x); + try std.testing.expectEqual(kept.tick, state.tick); try std.testing.expect(state.sample(.off, 300100, region) == null); } test "pet clock is fixed rate and sprites bounded in both directions" { - const region = lane(1, 8, 400, 0, 16, false).?; + const region = lane(1, 8, 400, 0, 16).?; var state: State = .{}; const start = state.sample(.frog, 1000, region).?; try std.testing.expectEqualDeep(start, state.sample(.frog, 1099, region).?); @@ -180,7 +181,7 @@ test "pet clock is fixed rate and sprites bounded in both directions" { } test "pet rests at random spots and the frog travels only in the air" { - const region = lane(1, 8, 800, 0, 16, false).?; + const region = lane(1, 8, 800, 0, 16).?; var state: State = .{}; var last = state.sample(.frog, 0, region).?; var rested_midway = false; @@ -195,7 +196,7 @@ test "pet rests at random spots and the frog travels only in the air" { } test "pet initial idle visibly animates without walking or leaving its tile" { - const region = lane(1, 8, 400, 0, 16, false).?; + const region = lane(1, 8, 400, 0, 16).?; for ([_]Kind{ .cat, .frog }) |kind| { var state: State = .{}; const initial = state.sample(kind, 0, region).?; diff --git a/src/host_io.zig b/src/host_io.zig index c593551b..2c736e20 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -203,11 +203,14 @@ pub const Lsp = struct { } others.deinit(gpa); } + var copied: usize = 0; for (core.panes, 0..) |slot, i| { const other = slot orelse continue; if (i == req.pane) continue; const f = other.file orelse continue; if (f.output != null) continue; + if (copied + f.content.len > pardes.memory.limits.lsp_snapshot_bytes) continue; + copied += f.content.len; const local = filesystem.localPath(f.path) orelse continue; const other_path = try gpa.dupe(u8, local); const other_source = gpa.dupeSentinel(u8, f.content, 0) catch |err| { @@ -224,13 +227,19 @@ pub const Lsp = struct { // Null is failure; the receiver frees non-null rows with the job's allocator. pub const Deliver = *const fn (ctx: ?*anyopaque, id: u32, rows: ?[]u8) void; - pub fn work(gpa: std.mem.Allocator, job: *Job, ctx: ?*anyopaque, deliver: Deliver) void { + pub fn work(io: std.Io, gpa: std.mem.Allocator, job: *Job, ctx: ?*anyopaque, deliver: Deliver) void { defer job.free(gpa); - var arena: std.heap.ArenaAllocator = .init(gpa); - defer arena.deinit(); + // The query's memory is the fixed region memory.zig reserved at + // startup: a hard bound, reused by each query in turn. + const region = pardes.memory.takeLspQuery() orelse { + deliver(ctx, job.id, null); + return; + }; + var arena: std.heap.FixedBufferAllocator = .init(region); + defer pardes.memory.giveLspQuery(arena.end_index); var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); - pardes.lsp.query(gpa, arena.allocator(), .{ + pardes.lsp.query(io, gpa, arena.allocator(), .{ .kind = job.kind, .path = job.path, .source = job.source, @@ -240,6 +249,7 @@ pub const Lsp = struct { .others = job.others, .callstack_depth = job.callstack_depth, .callstack_corners = job.callstack_corners, + .budget = &arena, }, &out.writer) catch { deliver(ctx, job.id, null); return; @@ -367,7 +377,7 @@ pub const Lsp = struct { .offset = 0, .arg = "", }); - work(gpa, job, null, Sink.take); + work(std.testing.io, gpa, job, null, Sink.take); // `status` is the one kind that answers with no file and no cursor, which // is what makes it assertable here without a language server on the box. @@ -416,7 +426,7 @@ pub const Lsp = struct { .arg = "renamed", }); if (fail) failing.fail_index = failing.alloc_index; - work(failing.allocator(), job, &sink, Sink.take); + work(std.testing.io, failing.allocator(), job, &sink, Sink.take); try std.testing.expectEqual(fail, sink.failed); try std.testing.expect(core.lsp_wait == null); if (fail) { @@ -482,7 +492,7 @@ pub const Lsp = struct { defer core.deinit(); const job = try snapshot(gpa, core, .{ .id = 37, .kind = .status, .pane = @intCast(core.active), .offset = 0, .arg = "" }); var sink: Sink = .{}; - work(gpa, job, &sink, Sink.take); + work(std.testing.io, gpa, job, &sink, Sink.take); if (sink.rows) |rows| gpa.free(rows); try std.testing.expect(allocator.failed); try std.testing.expectEqual(@as(usize, 1), sink.calls); @@ -1179,7 +1189,7 @@ pub const Shell = struct { if (builtin.os.tag == .windows) return; var saved_buf: [std.fs.max_path_bytes:0]u8 = @splat(0); const saved: ?[:0]const u8 = if (std.c.getenv("XDG_RUNTIME_DIR")) |v| std.fmt.bufPrintSentinel(&saved_buf, "{s}", .{std.mem.span(v)}, 0) catch null else null; - defer _ = if (saved) |v| setenv("XDG_RUNTIME_DIR", v.ptr, 1) else unsetenv("XDG_RUNTIME_DIR"); + defer _ = if (saved) |v| setenv("XDG_RUNTIME_DIR", v.ptr, 1) else testUnsetenv("XDG_RUNTIME_DIR"); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); @@ -1213,7 +1223,7 @@ pub const Shell = struct { try std.testing.expectEqualStrings(bash_rc, readSmall(c.bashPath().?, &buf) orelse return error.ReadFailed); // No runtime dir: private files, one per owner, gone at teardown. - _ = unsetenv("XDG_RUNTIME_DIR"); + _ = testUnsetenv("XDG_RUNTIME_DIR"); var d = PromptFiles.init(); var e = PromptFiles.init(); defer e.deinit(); @@ -1294,6 +1304,44 @@ pub const ChildEnv = struct { "TERM_PROGRAM=" ++ pardes.config.child_term_program, }; + /// What this editor tells its children of itself (exportPaneEnv, `$winid`), + /// kept here rather than set into this process's environment: std's Io read + /// that block at startup, and setenv/unsetenv rewrite it in place under it + /// (a spawn then scans a null inside it). `build` puts each exported one in + /// place of an inherited variable of its name; a cleared one hides it; one + /// never exported is inherited as it is. + pub const exported_names = [_][]const u8{ "PARDES_PID", "PARDES_9P", "PARDES_PANE", "PARDES_MOUNT", "winid" }; + var exported_state: [exported_names.len]enum { inherited, cleared, set } = @splat(.inherited); + var exported_line: [exported_names.len][pardes.memory.limits.host_path_cap + 64:0]u8 = undefined; + + /// Set (or, null, clear) what children are told for `var_name`. + pub fn exportVar(var_name: []const u8, value: ?[]const u8) void { + const i = exportedIndex(var_name).?; + const v = value orelse { + exported_state[i] = .cleared; + return; + }; + const line = std.fmt.bufPrintSentinel(&exported_line[i], "{s}={s}", .{ var_name, v }, 0) catch { + exported_state[i] = .cleared; + return; + }; + exported_state[i] = .set; + _ = line; + } + + /// What children are told for `var_name`, as `exportVar` left it; null + /// when cleared or never exported. + pub fn exported(var_name: []const u8) ?[]const u8 { + const i = exportedIndex(var_name) orelse return null; + if (exported_state[i] != .set) return null; + return std.mem.span(@as([*:0]const u8, &exported_line[i]))[var_name.len + 1 ..]; + } + + fn exportedIndex(var_name: []const u8) ?usize { + for (exported_names, 0..) |n, i| if (std.mem.eql(u8, n, var_name)) return i; + return null; + } + /// Wide enough for any real environment; `build` answers null rather than /// truncate, because a child missing half its variables is a worse bug /// than one missing TERM, and the caller then execs with `environ` intact. @@ -1318,7 +1366,8 @@ pub const ChildEnv = struct { if (std.mem.eql(u8, inherited, name(mine))) break true; } else false; if (shadowed) continue; - if (n + own.len + 1 > slots.len) return null; + if (exportedIndex(inherited)) |e| if (exported_state[e] != .inherited) continue; + if (n + own.len + exported_names.len + 1 > slots.len) return null; slots[n] = entry; n += 1; } @@ -1336,10 +1385,14 @@ pub const ChildEnv = struct { if (std.mem.eql(u8, name(entry.?), name(default))) break true; } else false; if (set) continue; - if (n + own.len + 4 > slots.len) return null; + if (n + own.len + exported_names.len + 4 > slots.len) return null; slots[n] = default; n += 1; } + for (&exported_line, exported_state) |*line, state| if (state == .set) { + slots[n] = line; + n += 1; + }; for (own) |mine| { slots[n] = mine; n += 1; @@ -1411,14 +1464,14 @@ test "the child environment replaces the launcher's terminal identity exactly on defer if (restore) |t| { _ = Shell.setenv("TERM", t.ptr, 1); } else { - _ = unsetenv("TERM"); + _ = testUnsetenv("TERM"); }; // A launcher TERM must be shadowed rather than duplicated, and the rest of // the environment must arrive untouched. try std.testing.expectEqual(@as(c_int, 0), Shell.setenv("TERM", "dumb", 1)); try std.testing.expectEqual(@as(c_int, 0), Shell.setenv("PARDES_CHILD_ENV_PROBE", "1", 1)); - defer _ = unsetenv("PARDES_CHILD_ENV_PROBE"); + defer _ = testUnsetenv("PARDES_CHILD_ENV_PROBE"); var slots: ChildEnv.Slots = undefined; // Where the user named no pager: a terminal's shell pages into a @@ -1481,6 +1534,16 @@ test "the child environment replaces the launcher's terminal identity exactly on extern "c" fn unsetenv(name: [*:0]const u8) c_int; +/// For tests: unset `name` without compacting the environment block std +/// captured at startup (std.testing.environ and Io's spawns read it later). A +/// new variable first moves glibc's `environ` to a heap copy, which unsetenv +/// then compacts instead. Runtime code never changes this process's +/// environment (ChildEnv.exportVar). +pub fn testUnsetenv(name: [*:0]const u8) c_int { + _ = Shell.setenv("PARDES_TEST_ENVIRON_COPIED", "1", 1); + return unsetenv(name); +} + /// The environment a pane's command is given (exportPaneEnv, `$winid`, /// ChildEnv), as `NAME=value` lines in `arena`, for a command that is not a /// pane's: an Edit's filters, run from pane `serial`. @@ -1497,10 +1560,7 @@ pub fn commandEnv(arena: std.mem.Allocator, serial: u32, adopts: bool) ![]const fn exportWinid(serial: u32) void { var buf: [16]u8 = undefined; - if (serial != 0) if (std.fmt.bufPrintSentinel(&buf, "{d}", .{serial}, 0)) |text| { - if (Shell.setenv("winid", text, 1) == 0) return; - } else |_| {}; - _ = unsetenv("winid"); + ChildEnv.exportVar("winid", if (serial == 0) null else std.fmt.bufPrint(&buf, "{d}", .{serial}) catch null); } /// The longest line a command pane runs (exec.zig refuses a longer one). @@ -1587,17 +1647,18 @@ pub fn forkShell( if (helper) |path| { const c = core.?; const pn = c.panes[pane].?; - const socket = libc.getenv("PARDES_9P") orelse return error.SessionSocketRequired; + const socket = ChildEnv.exported("PARDES_9P") orelse return error.SessionSocketRequired; var command: std.Io.Writer.Allocating = .init(c.gpa); defer command.deinit(); - try @import("linux/v9fs.zig").writeLaunchCommand(&command.writer, path, std.mem.span(socket), &spawn.argv); + try @import("linux/v9fs.zig").writeLaunchCommand(&command.writer, path, socket, &spawn.argv); if (!try pardes.panes.terminal.queuePendingCommand(pn, command.written())) return error.ShellAlreadyStarted; } // Built here and not after the fork: between fork and exec the child may // not call setenv, whose malloc can deadlock against a pty reader thread // that held the heap when the fork took its snapshot. var env_slots: ChildEnv.Slots = undefined; - const paging: ChildEnv.Paging = if (one_line != null) + const pages = if (core) |c| if (c.panes[pane]) |pn| pn.command_pages else false else false; + const paging: ChildEnv.Paging = if (one_line != null and !pages) .command else if (core) |c| (if (c.settings.pager == .off) .terminal_off else .terminal) else .terminal; const envp = ChildEnv.build(&env_slots, paging); @@ -1695,7 +1756,7 @@ test "a forked shell is told which terminal it is talking to" { defer if (restore) |t| { _ = Shell.setenv("TERM", t.ptr, 1); } else { - _ = unsetenv("TERM"); + _ = testUnsetenv("TERM"); }; _ = Shell.setenv("TERM", "dumb", 1); diff --git a/src/hunks.zig b/src/hunks.zig index 73c0654a..24aaf63d 100644 --- a/src/hunks.zig +++ b/src/hunks.zig @@ -368,21 +368,34 @@ const max_line_distance = 0.6; /// rather than aligned at a quadratic cost (delta aligns them all). const max_run = 256; const max_cells = 1 << 16; +/// Pairs one hunk compares (each a table of up to `max_cells`): past it, the +/// rest of its lines are shown unemphasised. And the scratch each pair is +/// aligned in, reused by the next: its table and the ranges it marks. +const max_pairs = 4096; +const pair_scratch_bytes = 8 << 20; /// Pair each removed line with the first added line after the last one /// paired that is near enough to it, in order, and emphasise in both the /// tokens one has and the other has not. fn inferEdits(arena: std.mem.Allocator, minus: []HunkLine, plus: []HunkLine) !void { if (minus.len == 0 or plus.len == 0 or minus.len > max_run or plus.len > max_run) return; + const plus_tokens = try arena.alloc([]const []const u8, plus.len); + for (plus, plus_tokens) |line, *tokens| tokens.* = try tokenize(arena, line.code); + var scratch: std.heap.FixedBufferAllocator = .init(try @import("memory.zig").unfilled(arena, pair_scratch_bytes)); + var pairs: usize = 0; var plus_index: usize = 0; for (minus) |*m| { const m_tokens = try tokenize(arena, m.code); var j = plus_index; while (j < plus.len) : (j += 1) { - const p_tokens = try tokenize(arena, plus[j].code); + const p_tokens = plus_tokens[j]; if ((m_tokens.len + 1) * (p_tokens.len + 1) > max_cells) continue; - var alignment = try Alignment.init(arena, m_tokens, p_tokens); - const marks = try annotate(arena, &alignment, m.code, plus[j].code); + if (pairs == max_pairs) return; + pairs += 1; + scratch.reset(); + const pair = scratch.allocator(); + var alignment = Alignment.init(pair, m_tokens, p_tokens) catch continue; + const marks = annotate(pair, &alignment, m.code, plus[j].code) catch continue; if (marks.distance > max_line_distance) continue; for (marks.minus) |r| for (m.styles[r[0]..r[1]]) |*b| { b.* |= syntax.diff_emph; diff --git a/src/image.zig b/src/image.zig index 8bf9de08..5797ce9e 100644 --- a/src/image.zig +++ b/src/image.zig @@ -768,7 +768,13 @@ pub fn readable(bytes: []const u8) bool { /// decode + downscale to RGBA, gpa-owned. Returns null on any failure — the /// pane then simply shows a blank body. pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?struct { rgba: []u8, w: usize, h: usize } { - if (bytes.len == 0) return null; + if (bytes.len == 0 or bytes.len > std.math.maxInt(c_int)) return null; + // Bounded before decoding: a few KB of PNG can name gigapixels. + var iw: c_int = 0; + var ih: c_int = 0; + var ic: c_int = 0; + if (stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &iw, &ih, &ic) != 1) return null; + if (@as(usize, @intCast(@max(0, iw))) * @as(usize, @intCast(@max(0, ih))) > @import("memory.zig").limits.image_decode_pixels) return null; var img = zstbi.Image.loadFromMemory(bytes, 4) catch return null; // 4 = RGBA defer img.deinit(); var scaled: ?zstbi.Image = null; diff --git a/src/locations.zig b/src/locations.zig index 2828566d..1e4a20cf 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -217,6 +217,98 @@ pub fn treeContinuation(line: []const u8, buffer: []u8, max_cells: usize) []cons return out.buffered()[0..end]; } +const Resolved = struct { code: []const u8 = "", colors: []const u8 = &.{}, source_row: usize = 0 }; + +/// Each Callstack row's code (its call site's line, or a caller's header) and +/// colors, by input line. A page spans thousands of files, so they are taken +/// a file at a time: grouped by path, each file read, line-indexed and +/// analyzed once, its rows' code and colors copied out, and the file let go +/// before the next. Row by row, the cache compared the whole source each +/// time and past its 64 entries parsed it again, every lookup walked the +/// file from its top, and every source stayed in the arena to the end. +fn resolveCallstack(p: *pardes.Pardes, arena: std.mem.Allocator, dir: []const u8, input: []const u8) ![]Resolved { + const mark = @import("lsp/lsp.zig").callstack_match; + var all: std.ArrayList([]const u8) = .empty; + var by_path: std.StringArrayHashMapUnmanaged(std.ArrayList(u32)) = .empty; + var lines = std.mem.splitScalar(u8, std.mem.trimEnd(u8, input, "\n"), '\n'); + while (lines.next()) |raw| { + const line = if (std.mem.startsWith(u8, raw, mark)) raw[mark.len..] else raw; + const index: u32 = @intCast(all.items.len); + try all.append(arena, line); + const target = parseRaw(line[treePrefix(line).end..]); + if (target.at.line == 0) continue; + const entry = try by_path.getOrPut(arena, target.path); + if (!entry.found_existing) entry.value_ptr.* = .empty; + try entry.value_ptr.append(arena, index); + } + const resolved = try arena.alloc(Resolved, all.items.len); + @memset(resolved, .{}); + var file_arena: std.heap.ArenaAllocator = .init(p.gpa); + defer file_arena.deinit(); + for (by_path.keys(), by_path.values()) |path, indexes| { + _ = file_arena.reset(.retain_capacity); + const fa = file_arena.allocator(); + const source = sourceText(p, fa, dir, path); + var cached: Cache.Hit = .{ .analysis = .{} }; + defer cached.deinit(p.tree_sitter_gpa); + // Where each line starts, the file's length closing the last. + var starts: std.ArrayList(usize) = .empty; + if (source) |text| { + cached = p.locations_cache.get(p.tree_sitter_gpa, text.path, text.bytes, true, true) catch .{ .analysis = .{} }; + try starts.append(fa, 0); + var at: usize = 0; + while (std.mem.indexOfScalarPos(u8, text.bytes, at, '\n')) |nl| : (at = nl + 1) try starts.append(fa, nl + 1); + } + for (indexes.items) |index| { + const line = all.items[index]; + const prefix = treePrefix(line); + const target = parseRaw(line[prefix.end..]); + var code = line[prefix.end + target.code_start ..]; + var colors: []const u8 = &.{}; + var source_row = target.at.line - 1; + if (source) |text| { + const row = target.at.line - 1; + if (prefix.branch) { + // Choose the innermost header containing the server's name range. + var chosen: ?syntax.ContextDeclaration = null; + for (cached.analysis.declarations) |declaration| { + if (declaration.start_line <= row and row <= declaration.header_end_line) { + if (chosen == null or declaration.end_byte - declaration.start_byte < chosen.?.end_byte - chosen.?.start_byte) + chosen = declaration; + } + } + if (chosen) |header| { + code = text.bytes[header.start_byte..header.header_end_byte]; + source_row = header.start_line; + } else { + const fallback = indexedLine(text.bytes, starts.items, row); + if (fallback.len > 0) code = fallback; + } + } else if (code.len == 0) { + // A call site the language worker sent bare (it reads no + // sources for Callstack): its line, from the source here. + code = indexedLine(text.bytes, starts.items, row); + } + const offset = if (source_row < starts.items.len) starts.items[source_row] else text.bytes.len; + if (offset + code.len <= cached.analysis.colors.len and + std.mem.eql(u8, code, text.bytes[offset..][0..code.len])) + colors = cached.analysis.colors[offset..][0..code.len]; + code = try arena.dupe(u8, code); + colors = try arena.dupe(u8, colors); + } + resolved[index] = .{ .code = code, .colors = colors, .source_row = source_row }; + } + } + return resolved; +} + +/// modal.lineSlice through a line index (`starts`, as resolveCallstack builds it). +fn indexedLine(text: []const u8, starts: []const usize, row: usize) []const u8 { + if (row >= starts.len or starts[row] >= text.len) return ""; + const end = if (row + 1 < starts.len) starts[row + 1] - 1 else text.len; + return text[starts[row]..end]; +} + /// Enrich caller nodes on the UI thread, using the same source/declaration cache /// as LocationsContext. The LSP worker only supplies relationships and addresses. pub fn formatCallstack(p: *pardes.Pardes, dir: []const u8, input: []const u8) !Result { @@ -232,9 +324,10 @@ pub fn formatCallstack(p: *pardes.Pardes, dir: []const u8, input: []const u8) !R rows.deinit(p.gpa); } const stacked = p.settings.callstack_layout == .stacked; - var sources: std.StringHashMap(?Source) = .init(arena); + const resolved = try resolveCallstack(p, arena, dir, input); var lines = std.mem.splitScalar(u8, std.mem.trimEnd(u8, input, "\n"), '\n'); - while (lines.next()) |raw| { + var index: usize = 0; + while (lines.next()) |raw| : (index += 1) { const mark = @import("lsp/lsp.zig").callstack_match; const focus = std.mem.startsWith(u8, raw, mark); const line = if (focus) raw[mark.len..] else raw; @@ -245,41 +338,9 @@ pub fn formatCallstack(p: *pardes.Pardes, dir: []const u8, input: []const u8) !R try appendMetadata(p.gpa, &rows, .{ .path = "", .at = .{}, .label = "", .code = "", .original = null }, .context, 0, line.len, false); continue; } - const entry = try sources.getOrPut(target.path); - if (!entry.found_existing) entry.value_ptr.* = sourceText(p, arena, dir, target.path); - const source = entry.value_ptr.*; - var cached: Cache.Hit = .{ .analysis = .{} }; - defer cached.deinit(p.tree_sitter_gpa); - if (source) |text| cached = p.locations_cache.get(p.tree_sitter_gpa, text.path, text.bytes, true, true) catch .{ .analysis = .{} }; - var code = line[prefix.end + target.code_start ..]; - var colors: []const u8 = &.{}; - var source_row = target.at.line - 1; - if (source) |text| { - const row = target.at.line - 1; - if (prefix.branch) { - // Choose the innermost header containing the server's name range. - var chosen: ?syntax.ContextDeclaration = null; - for (cached.analysis.declarations) |declaration| { - if (declaration.start_line <= row and row <= declaration.header_end_line) { - if (chosen == null or declaration.end_byte - declaration.start_byte < chosen.?.end_byte - chosen.?.start_byte) - chosen = declaration; - } - } - if (chosen) |header| { - code = text.bytes[header.start_byte..header.header_end_byte]; - source_row = header.start_line; - } else { - const fallback = @import("modal.zig").lineSlice(text.bytes, row); - if (fallback.len > 0) code = fallback; - } - } - { - const offset = @import("modal.zig").lineStartOffset(text.bytes, source_row); - if (offset + code.len <= cached.analysis.colors.len and - std.mem.eql(u8, code, text.bytes[offset..][0..code.len])) - colors = cached.analysis.colors[offset..][0..code.len]; - } - } + const code = resolved[index].code; + const colors = resolved[index].colors; + var source_row = resolved[index].source_row; const recursion = prefix.branch and std.mem.endsWith(u8, line, " ↻ recursion"); const first_line = code[0 .. std.mem.indexOfScalar(u8, code, '\n') orelse code.len]; const source_indent = if (stacked) first_line.len - std.mem.trimStart(u8, first_line, " \t").len else 0; diff --git a/src/look.zig b/src/look.zig index f4fbe52e..02754fff 100644 --- a/src/look.zig +++ b/src/look.zig @@ -587,6 +587,10 @@ fn lookFirstHit(p: *Pardes, id: usize) void { /// Fills the results buffer, and answers how many rows a text or PDF /// search found (0 for Find and Grep, which do not count). With /// `open_empty` false a text search that found nothing opens no buffer. +/// A search of one pane's text (or its PDF) writes at most this many bytes +/// of rows: past it, the rows stop and say where. +pub const search_text_output_bytes = 16 << 20; + pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start: SearchStart, open_empty: bool) !usize { const pane = p.panes[id] orelse return 0; // Blanks at either end are the pattern's (a look of an indented line); @@ -598,7 +602,9 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start // root, and the directory the results buffer is named in. From a // column's or the workspace's tag, the session's. const dir = if (p.exec_header) p.sessionDir() else Pardes.paneDir(pane); - const out = try arena.alloc(u8, filesystem.search_max_output_bytes); + // As much as the search can write and no more: a root's hits for each + // root Grep walks, one for Find, `search_text_output_bytes` in a pane. + var out: []u8 = &.{}; var out_len: usize = 0; var nrows: usize = 0; var anchor: ?usize = null; @@ -631,6 +637,7 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start roots[nroots] = d; nroots += 1; } + out = try @import("memory.zig").unfilled(arena, @max(1, nroots) * filesystem.search_root_output_bytes); // A pane's directory that is not there (a scratch named into one // not made yet, a peer's) has nothing to grep: passed over, not the // search's failure. @@ -641,9 +648,11 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start }; nrows = std.mem.count(u8, out[0..out_len], "\n"); } else if (kind == .find) { + out = try @import("memory.zig").unfilled(arena, filesystem.search_root_output_bytes); out_len = try filesystem.find(arena, dir, pat, out, ¬es); nrows = std.mem.count(u8, out[0..out_len], "\n"); } else if (pane.hasPdf()) { + out = try @import("memory.zig").unfilled(arena, search_text_output_bytes); if (comptime pdf_enabled) { const found = try panes.Pdf.searchRows( &pane.pdf.?, @@ -658,6 +667,7 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start anchor = found.anchor; } } else { + out = try @import("memory.zig").unfilled(arena, search_text_output_bytes); const lines = try edit.paneCursorLines(p, &pane.body); // a real file names itself; a terminal or an output buffer has no path const has_path = if (pane.file) |f| f.output == null else false; @@ -675,7 +685,10 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start const row = try std.fmt.allocPrint(arena, "{s}:{d}:{d}{c}{d} {s}\n", .{ loc, i + 1, at + 1, config.range_sep, at + pat.len, std.mem.trimEnd(u8, ln, " \t"), }); - if (row.len > out.len - out_len) break; + if (row.len > out.len - out_len) { + notes.rows_cut = nrows; + break; + } if (start == .cursor and (i < cl or (i == cl and at <= cc))) anchor = nrows; @memcpy(out[out_len..][0..row.len], row); out_len += row.len; @@ -1451,6 +1464,7 @@ fn lookUnresolved(p: *Pardes, id: usize, pane: *Pane, trimmed: []const u8, text: // 9P is no keyboard input, and searches whatever the key mode. if (pane.image != null or (pane.body.mode == .tty and !p.fs.serving)) return; if (text.len == 0) return; + if (lookCommit(p, id, pane, trimmed)) return; if (p.settings.look_word == .search and pane.file != null) return lookWord(p, id, pane, text); p.word_look = true; defer p.word_look = false; @@ -1471,6 +1485,49 @@ fn lookUnresolved(p: *Pardes, id: usize, pane: *Pane, trimmed: []const u8, text: } } +/// A commit hash (7 to 40 hex digits, a letter and a digit among them) +/// looked at inside a git repository: `git show` it, paged into a +Pager, +/// which stacks its hunks in a +Diff (ctl.fillPager); the command pane +/// goes when it succeeds (exec.commandDone). +// ponytail: a hex word that names no commit leaves git's error in a command pane, not a search; check the object first if that bites. +fn lookCommit(p: *Pardes, id: usize, pane: *Pane, word: []const u8) bool { + if (comptime !pardes.hosted) return false; + if (!isCommitHash(word)) return false; + var dir = filesystem.localPath(Pardes.paneDir(pane)) orelse return false; + while (true) { + var buf: [4096]u8 = undefined; + const dot_git = std.fmt.bufPrint(&buf, "{s}/.git", .{dir}) catch return false; + if (exec.kindOf(dot_git) != null) break; + dir = std.fs.path.dirname(dir) orelse return false; + } + var line: [64]u8 = undefined; + const dst = exec.execute(p, id, std.fmt.bufPrint(&line, "git show {s}", .{word}) catch return false) orelse return true; + if (p.panes[dst]) |c| c.command_pages = true; + return true; +} + +fn isCommitHash(word: []const u8) bool { + if (word.len < 7 or word.len > 40) return false; + var digit = false; + var letter = false; + for (word) |c| switch (c) { + '0'...'9' => digit = true, + 'a'...'f' => letter = true, + else => return false, + }; + return digit and letter; +} + +test "a commit hash is 7 to 40 lowercase hex digits with a letter and a digit" { + try std.testing.expect(isCommitHash("9bffa7041c")); + try std.testing.expect(isCommitHash("9bffa7041cd2e8a0b6b1f7c3e4d5a6b7c8d9e0f1")); + try std.testing.expect(!isCommitHash("9bffa7")); + try std.testing.expect(!isCommitHash("defaced")); + try std.testing.expect(!isCommitHash("1234567")); + try std.testing.expect(!isCommitHash("0x9bffa7041c")); + try std.testing.expect(!isCommitHash("9BFFA7041C")); +} + /// Opens (or finds) `path` as Look would, `id`'s own pane when it is empty, /// and selects what `expr` addresses there from its dot. An address that /// does not evaluate moves nothing and says why (acme: no jump). diff --git a/src/lsp/lsp.zig b/src/lsp/lsp.zig index 4a70efe5..1c1b57a2 100644 --- a/src/lsp/lsp.zig +++ b/src/lsp/lsp.zig @@ -26,6 +26,10 @@ pub const Kind = enum { }; pub const callstack_match = "@callstack-match "; +/// The last row of a Callstack page that stopped full: ` `, +/// the entry indices from the top the next page starts at (`3.0.17`), and +/// how many callers this page holds. +pub const callstack_more = "@callstack-more "; pub const CallstackCorners = enum { rounded, square }; pub const callstack_default_depth: u8 = 8; pub const callstack_max_depth: u8 = 64; @@ -46,6 +50,9 @@ pub const Req = struct { /// For `explain`: the query it narrates, read from `arg` (` /// `) by `explaining`; a definition when none was asked before. explains: Kind = .definition, + /// The fixed region `arena` allocates from, when it is one: a walk that + /// can grow (Callstack) reads how much is left and stops in time. + budget: ?*const std.heap.FixedBufferAllocator = null, }; pub const Doc = struct { path: []const u8, source: [:0]const u8 }; @@ -102,18 +109,18 @@ pub fn speaks(path: []const u8) bool { return false; } -pub fn query(gpa: std.mem.Allocator, arena: std.mem.Allocator, req: Req, out: *std.Io.Writer) !void { +pub fn query(io: std.Io, gpa: std.mem.Allocator, arena: std.mem.Allocator, req: Req, out: *std.Io.Writer) !void { if (req.kind == .status) { try out.print("backend: {s}\n", .{backend_name}); - inline for (backends) |b| try b.query(gpa, arena, req, out); + inline for (backends) |b| try b.query(io, gpa, arena, req, out); return; } inline for (backends) |b| { if (b.speaks(req.path) and b.supports.contains(req.kind)) - return b.query(gpa, arena, req, out); + return b.query(io, gpa, arena, req, out); } if (req.kind == .explain and backends.len > 0) - return backends[0].query(gpa, arena, req, out); + return backends[0].query(io, gpa, arena, req, out); // Nothing asked: said why, where an empty answer would read as nothing // found. inline for (backends) |b| if (b.speaks(req.path)) @@ -217,6 +224,6 @@ test "a question no backend answers for a file says why rather than find nothing var buf: [256]u8 = undefined; var out: std.Io.Writer = .fixed(&buf); // A file no server speaks for is answered, not left empty. - try query(std.testing.allocator, arena.allocator(), .{ .kind = .incoming_calls, .path = "/x/notes.txt", .source = "", .offset = 0 }, &out); + try query(std.testing.io, std.testing.allocator, arena.allocator(), .{ .kind = .incoming_calls, .path = "/x/notes.txt", .source = "", .offset = 0 }, &out); try std.testing.expectStringStartsWith(out.buffered(), "@none "); } diff --git a/src/lsp/lsp_client.zig b/src/lsp/lsp_client.zig index b396879b..e356c979 100644 --- a/src/lsp/lsp_client.zig +++ b/src/lsp/lsp_client.zig @@ -8,9 +8,10 @@ //! worker thread, and a language server costs tens of milliseconds to start //! and MINUTES to index a large workspace. So a server is spawned lazily on //! the first query that needs it, kept for the life of the editor, and each -//! spec gets at most one — `conns[i]` guards itself with a pthread mutex -//! because the gui shell detaches its workers and a superseded query can still -//! be inside `run` when the next arrives. +//! spec gets at most one — `conns[i]` guards itself with an `Io.Mutex` +//! because a canceled query can still be inside `run` when the next arrives. +//! Every wait is an `Io` cancelation point: a shell cancels the worker of a +//! query it no longer wants, and the worker stops there. //! //! ONE READER THREAD PER SERVER, and it is not optional. The first design //! pumped the socket only while a query waited, which works for a server that @@ -28,7 +29,8 @@ //! under a thread still polling it. //! //! Nothing here may wedge the editor: -//! - every write and every mailbox wait is bounded by a deadline, +//! - every write is bounded by a deadline, and every mailbox wait by a +//! deadline or, while the server narrates progress, by cancelation, //! - a query the server does not answer in time returns no rows and sends //! `$/cancelRequest`; the server is NOT killed for being busy (an indexing //! server is busy for minutes and the status row says so) — but three @@ -58,20 +60,10 @@ extern "c" fn access(path: [*:0]const u8, mode: c_int) c_int; extern "c" fn getenv(name: [*:0]const u8) ?[*:0]const u8; extern "c" fn usleep(usec: c_uint) c_int; extern "c" fn atexit(f: *const fn () callconv(.c) void) c_int; -extern "c" fn realpath(path: [*:0]const u8, resolved: [*]u8) ?[*:0]u8; // test-only libc (the seam's own tests build a real directory tree) extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8; extern "c" fn mkdir(path: [*:0]const u8, mode: libc.mode_t) c_int; extern "c" fn system(cmd: [*:0]const u8) c_int; -// pthread condattr surface, absent from std.c: what makes the mailbox waits -// tick on CLOCK_MONOTONIC (linux) or a relative timeout (darwin). -const pthread_condattr_t = extern struct { data: [8]u8 align(@alignOf(usize)) = @splat(0) }; -extern "c" fn pthread_condattr_init(attr: *pthread_condattr_t) c_int; -extern "c" fn pthread_condattr_setclock(attr: *pthread_condattr_t, clock: c_int) c_int; -extern "c" fn pthread_condattr_destroy(attr: *pthread_condattr_t) c_int; -extern "c" fn pthread_cond_init(cond: *libc.pthread_cond_t, attr: ?*const pthread_condattr_t) c_int; -extern "c" fn pthread_cond_timedwait_relative_np(cond: *libc.pthread_cond_t, mutex: *libc.pthread_mutex_t, reltime: *const libc.timespec) c_int; -const CLOCK_MONOTONIC: c_int = 1; // linux ABI; the setclock call is linux-only const X_OK: c_int = 1; const POLLIN: i16 = 0x001; @@ -182,21 +174,35 @@ fn specFor(path: []const u8) ?usize { return null; } -// Deadlines. Requests are bounded because tty.zig cancels-and-joins the -// previous worker on a new keypress, so the worst UI stall a wedged wait can -// cause is one req_ms. Timeouts do NOT kill the server — an indexing -// rust-analyzer legitimately sits on a `gd` for longer than anyone will wait, -// and the message row is already narrating why. +// Deadlines. A shell cancels-and-joins the previous worker on a new query, +// and its waits are cancelation points: so a request needs no deadline to +// keep the editor responsive, and waits for its answer. Past req_ms the +// message row says the server is still at it; only a server silent and idle +// (no $/progress) for wedge_ms has failed to answer. Timeouts do NOT kill the +// server — an indexing rust-analyzer legitimately sits on a `gd` for minutes. +// The handshake runs protected from cancelation, so it keeps its deadline. const init_ms = 8_000; // spawn + initialize handshake -const req_ms = 4_000; // one request/response round trip +const req_ms = 4_000; // a request past this is said to be still working +const wedge_ms = 300_000; // silent and idle this long: not answering (rust-analyzer takes ~1 min on `default`'s callers in agave) const diag_ms = 1_200; // wait for a publishDiagnostics push after didChange const reply_ms = 2_000; // our answers to server-to-client requests +const reply_queue_bytes = 64 << 10; // replies waiting for the socket (Conn.replies) +const parse_kept = 1 << 20; // what the reader's parse arena keeps between messages +const max_diag_files = 256; // files a server's pushed diagnostics are kept for +const max_diag_bytes = 32 << 20; // and their bytes const wedged_strikes = 3; // consecutive timeouts before a restart -const max_rows = 2000; +/// Rows one answer may hold, and its writer's bytes: carved from the query's +/// fixed region (memory.limits.arena.lsp_query) with the rest of its memory. +const max_rows = 100_000; +/// What a Callstack walk leaves of its query's region for rendering the +/// tree it found: past this it stops and says so. +const callstack_reserve = 32 << 20; +/// What rendering leaves of the answer's writer for its closing line. +const row_room = 4096; const max_doc_bytes = 8 << 20; -const Err = error{ Dead, Timeout, Protocol, OutOfMemory, NoServer, Backoff, WriteFailed }; +const Err = error{ Dead, Timeout, Protocol, OutOfMemory, NoServer, Backoff, WriteFailed, Canceled }; /// Long-lived state outlives every query arena and cannot borrow the caller's /// gpa (a different one shows up in the harness than in the shell), so @@ -251,8 +257,22 @@ const State = enum(u8) { }; const Conn = struct { - mu: libc.pthread_mutex_t = .{}, - cond: libc.pthread_cond_t = .{}, + mu: std.Io.Mutex = .init, + cond: std.Io.Condition = .init, + /// Frames go out whole: a query's requests and the reader's replies + /// share the socket, and interleaved halves are a parse error that ends + /// the server. + write_mu: std.Io.Mutex = .init, + /// Replies the reader made while a query's write held the socket, sent + /// by that writer once its own message is out (releaseWrite): the reader + /// never waits to write, since a server blocked on its output reads + /// nothing more of ours. + /// Two halves: the reader appends to `replies[filling]`, and the writer + /// flips them and sends the other, with no copy and no wait. + replies: [2][reply_queue_bytes]u8 = undefined, + replies_len: [2]usize = .{ 0, 0 }, + filling: u1 = 0, + replies_mu: std.Io.Mutex = .init, state: State = .off, /// bumped per spawn. A reader thread that sees a different gen than its /// own is reading a corpse and exits; a waiter that sees one stops waiting. @@ -264,9 +284,6 @@ const Conn = struct { /// monotonic ms when the last handshake completed — a crash shortly /// after "ready" counts as a handshake failure for backoff purposes ready_at_ms: i64 = 0, - /// linux: the zeroed condvar defaults to REALTIME; re-initialized with a - /// monotonic condattr before its first wait (under the mutex) - cond_monotonic: bool = false, timeouts: u8 = 0, pid: libc.pid_t = -1, sock: c_int = -1, @@ -278,6 +295,8 @@ const Conn = struct { extra_roots: std.ArrayList([]u8) = .empty, docs: std.ArrayList(Doc) = .empty, diags: std.ArrayList(DiagSet) = .empty, + /// The bytes `diags` holds, kept under max_diag_bytes. + diag_bytes: usize = 0, /// the response mailbox: one request outstanding per connection want_id: u32 = 0, resp: ?[]u8 = null, @@ -285,13 +304,12 @@ const Conn = struct { progress: i32 = 0, title: [48]u8 = @splat(0), title_len: u8 = 0, + /// The server says it is still loading its workspace (rust-analyzer's + /// serverStatus, not quiescent): an empty answer now is not one. + loading: bool = false, + /// its index in `conns`, for what it posts + si: usize = 0, - fn lock(c: *Conn) void { - _ = libc.pthread_mutex_lock(&c.mu); - } - fn unlock(c: *Conn) void { - _ = libc.pthread_mutex_unlock(&c.mu); - } fn alive(c: *const Conn) bool { return c.state == .starting or c.state == .ready; } @@ -406,10 +424,10 @@ const Trace = struct { // ------------------------------------------------------------------- query -pub fn query(gpa: std.mem.Allocator, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io.Writer) !void { +pub fn query(io: std.Io, gpa: std.mem.Allocator, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io.Writer) !void { _ = gpa; const req = lsp.explaining(asked); - if (req.kind == .status) return status(req, out); + if (req.kind == .status) return status(io, req, out); var tr: Trace = .{ .on = req.kind == .explain }; const si = specFor(req.path) orelse { @@ -421,13 +439,14 @@ pub fn query(gpa: std.mem.Allocator, arena: std.mem.Allocator, asked: lsp.Req, o }; tr.note("file {s} -> {s} (languageId {s})", .{ std.fs.path.basename(req.path), specs[si].name, specs[si].lang }); - const scratch_buf = try arena.alloc(u8, max_rows * 512); + // Raw: `alloc` fills a safe build's new memory, all 51 MB of it, per query. + const scratch_buf = (arena.rawAlloc(max_rows * 512, .of(u8), @returnAddress()) orelse return error.OutOfMemory)[0 .. max_rows * 512]; var scratch: std.Io.Writer = .fixed(scratch_buf); const t0 = nowUs(); var err_name: []const u8 = ""; var failure: ?anyerror = null; - answer(arena, si, req, &scratch, &tr) catch |e| { + answer(io, arena, si, req, &scratch, &tr) catch |e| { failure = e; err_name = @errorName(e); tr.note("ERROR: {s}", .{err_name}); @@ -443,9 +462,9 @@ pub fn query(gpa: std.mem.Allocator, arena: std.mem.Allocator, asked: lsp.Req, o // again, not that it is not there. if (failure) |err| if (err == error.Backoff) { const c = &conns[si]; - c.lock(); + c.mu.lockUncancelable(io); const left_ms = c.retry_after_ms -| nowMs(); - c.unlock(); + c.mu.unlock(io); return lsp.none(out, "{s} failed recently; retry in {d}s", .{ specs[si].name, @divFloor(left_ms + 999, 1000) }); }; if (failure) |err| return err; @@ -464,17 +483,34 @@ fn traceOut(tr: *const Trace, req: lsp.Req, out: *std.Io.Writer, rows: usize, us try out.print("\n{d} row(s) in {d}us\n", .{ rows, us }); } -fn answer(arena: std.mem.Allocator, si: usize, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void { +fn answer(io: std.Io, arena: std.mem.Allocator, si: usize, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void { const c = &conns[si]; - c.lock(); - defer c.unlock(); - const g = try ensure(c, si, arena, req, tr); - run(c, si, arena, req, out, tr) catch |e| { + try c.mu.lock(io); + defer c.mu.unlock(io); + const g = try ensure(c, io, si, arena, req, tr); + const before = out.end; + try attempt(c, io, si, arena, req, out, tr, g); + // A server still loading its workspace answers at once, with nothing + // (rust-analyzer before its index): that is no answer. Wait until it says + // it is loaded, the message row saying so, and ask again. + const answered = out.buffered()[before..]; + if (c.loading and c.gen == g and (answered.len == 0 or std.mem.startsWith(u8, answered, "@none "))) { + tr.note("{s} is still loading: waiting for it, then asking again", .{specs[si].name}); + post(si, .always, "{s} is still loading the workspace; the answer comes when it is done\u{2026}", .{specs[si].name}); + while (c.loading and c.gen == g and c.alive()) _ = try timedWait(c, io, nowMs() + 500); + out.end = before; + try attempt(c, io, si, arena, req, out, tr, g); + } +} + +/// One run of a query, and what its failure says of the server. +fn attempt(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io.Writer, tr: *Trace, g: u32) Err!void { + run(c, io, si, arena, req, out, tr) catch |e| { switch (e) { // transport-level: this server is gone; forget it, and the next // query respawns. Gen-checked so a respawn that happened while we // waited is not the one we kill. - error.Dead, error.Protocol => shutdownIf(c, g), + error.Dead, error.Protocol => shutdownIf(c, io, g), error.Timeout => { // Slow is only wedged when the server is IDLE. One with // active `$/progress` work (rust-analyzer running cargo @@ -487,18 +523,19 @@ fn answer(arena: std.mem.Allocator, si: usize, req: lsp.Req, out: *std.Io.Writer c.timeouts +|= 1; if (c.timeouts >= wedged_strikes and c.gen == g) { post(si, .always, "{s} not answering — restarting", .{specs[si].name}); - shutdownIf(c, g); + shutdownIf(c, io, g); } } }, - error.OutOfMemory, error.NoServer, error.Backoff, error.WriteFailed => {}, + // Canceled: the shell asked something newer, not a wedged server. + error.OutOfMemory, error.NoServer, error.Backoff, error.WriteFailed, error.Canceled => {}, } return e; }; c.timeouts = 0; } -fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void { +fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void { // `explain` narrates the last query asked (a definition, `gd`, when // none was) — dispatch below on the effective kind so the trace // follows the code that query really runs. @@ -510,18 +547,18 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io var uri: std.ArrayList(u8) = .empty; try uriOf(&uri, arena, req.path); - try syncDoc(c, si, arena, uri.items, req.source); + try syncDoc(c, io, si, arena, uri.items, req.source); // Every other open file this server answers for, before a query whose // answer reaches them: zls renames only in the documents it has. if (lsp.reachesOtherFiles(kind)) for (req.others) |doc| if (specFor(doc.path) == si and !std.mem.eql(u8, doc.path, req.path)) { var other: std.ArrayList(u8) = .empty; try uriOf(&other, arena, doc.path); - try syncDoc(c, si, arena, other.items, doc.source); + try syncDoc(c, io, si, arena, other.items, doc.source); tr.note("synced {s} first", .{std.fs.path.basename(doc.path)}); }; const pos = posOf(req.source, req.offset, c.caps.enc); - var cx: Cx = .{ .arena = arena, .base = req.root, .cur_path = req.path, .cur_src = req.source, .out = out, .relative_parents = req.kind == .callstack, .callstack_corners = req.callstack_corners }; + var cx: Cx = .{ .arena = arena, .base = req.root, .cur_path = req.path, .cur_src = req.source, .out = out, .fixed_out = true, .relative_parents = req.kind == .callstack, .callstack_corners = req.callstack_corners }; for (req.others) |doc| try cx.files.append(arena, .{ .path = doc.path, .text = doc.source }); tr.note("server {s} (pid {d}), root {s}, {s} columns", .{ @tagName(c.state), c.pid, c.root, @tagName(c.caps.enc), @@ -540,7 +577,7 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io var b = try atPos(arena, uri.items, pos); if (kind == .references) try app(&b, arena, ",\"context\":{\"includeDeclaration\":true}"); tr.note("-> {s} @ {d}:{d}", .{ method, pos.line + 1, pos.ch + 1 }); - const result = (try call(c, arena, method, b.items, deadline)) orelse { + const result = (try call(c, io, arena, method, b.items, deadline)) orelse { tr.note("<- empty (no result, an error response, or null)", .{}); return; }; @@ -549,12 +586,12 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io }, .select_refs => { const b = try atPos(arena, uri.items, pos); - const result = (try call(c, arena, "textDocument/documentHighlight", b.items, deadline)) orelse return; + const result = (try call(c, io, arena, "textDocument/documentHighlight", b.items, deadline)) orelse return; for (items(result)) |h| try emitRange(&cx, c.caps.enc, uri.items, get(h, "range"), ""); }, .hover => { const b = try atPos(arena, uri.items, pos); - const result = (try call(c, arena, "textDocument/hover", b.items, deadline)) orelse return; + const result = (try call(c, io, arena, "textDocument/hover", b.items, deadline)) orelse return; const contents = get(result, "contents") orelse return; const text = str(contents) orelse str(get(contents, "value")) orelse blk: { for (items(contents)) |m| if (str(m) orelse str(get(m, "value"))) |t| break :blk t; @@ -569,26 +606,26 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io try app(&b, arena, "\"textDocument\":{\"uri\":"); try jstr(&b, arena, uri.items); try app(&b, arena, "}"); - const result = (try call(c, arena, "textDocument/documentSymbol", b.items, deadline)) orelse return; + const result = (try call(c, io, arena, "textDocument/documentSymbol", b.items, deadline)) orelse return; try walkSymbols(&cx, c.caps.enc, uri.items, result, 0); }, .workspace_symbols => { var b: std.ArrayList(u8) = .empty; try app(&b, arena, "\"query\":"); try jstr(&b, arena, req.arg); - const result = (try call(c, arena, "workspace/symbol", b.items, deadline)) orelse return; + const result = (try call(c, io, arena, "workspace/symbol", b.items, deadline)) orelse return; for (items(result)) |sym| { const loc = get(sym, "location") orelse continue; try emitRange(&cx, c.caps.enc, str(get(loc, "uri")) orelse continue, get(loc, "range"), str(get(sym, "name")) orelse ""); } }, - .diagnostics => try diagnostics(c, arena, &cx, uri.items, deadline), - .workspace_diagnostics => try workspaceDiagnostics(c, arena, &cx, deadline), + .diagnostics => try diagnostics(c, io, arena, &cx, uri.items, deadline), + .workspace_diagnostics => try workspaceDiagnostics(c, io, arena, &cx, deadline), .rename => { var b = try atPos(arena, uri.items, pos); try app(&b, arena, ",\"newName\":"); try jstr(&b, arena, req.arg); - const result = (try call(c, arena, "textDocument/rename", b.items, deadline)) orelse return; + const result = (try call(c, io, arena, "textDocument/rename", b.items, deadline)) orelse return; // Other open files that say the old name, should the server // answer for this file alone (zls renaming at a declaration does, // whatever it was told): applied here, they would no longer build. @@ -603,7 +640,7 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io try app(&b, arena, "\"textDocument\":{\"uri\":"); try jstr(&b, arena, uri.items); try app(&b, arena, "},\"options\":{\"tabSize\":4,\"insertSpaces\":true}"); - const result = (try call(c, arena, "textDocument/formatting", b.items, deadline)) orelse return; + const result = (try call(c, io, arena, "textDocument/formatting", b.items, deadline)) orelse return; try formatEdits(&cx, c.caps.enc, result, req.source); }, .code_action => { @@ -611,7 +648,7 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io try app(&b, arena, "\"textDocument\":{\"uri\":"); try jstr(&b, arena, uri.items); try b.print(arena, "}},\"range\":{{\"start\":{{\"line\":{d},\"character\":{d}}},\"end\":{{\"line\":{d},\"character\":{d}}}}},\"context\":{{\"diagnostics\":[]}}", .{ pos.line, pos.ch, pos.line, pos.ch }); - const result = (try call(c, arena, "textDocument/codeAction", b.items, deadline)) orelse return; + const result = (try call(c, io, arena, "textDocument/codeAction", b.items, deadline)) orelse return; for (items(result)) |ca| { const title = str(get(ca, "title")) orelse continue; try lsp.row(cx.out, lsp.rel(cx.base, cx.cur_path), pos.line, 0, flat(arena, title)); @@ -621,7 +658,7 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io .completion => { var b = try atPos(arena, uri.items, pos); try app(&b, arena, ",\"context\":{\"triggerKind\":1}"); - const result = (try call(c, arena, "textDocument/completion", b.items, deadline)) orelse return; + const result = (try call(c, io, arena, "textDocument/completion", b.items, deadline)) orelse return; const list = if (get(result, "items")) |it| items(it) else items(result); const here = lsp.rel(cx.base, cx.cur_path); var n: usize = 0; @@ -643,11 +680,11 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io n += 1; } }, - .callstack => try callstack(c, si, arena, &cx, uri.items, pos, deadline, tr, req.callstack_depth), - .incoming_calls => try hierarchy(c, si, arena, &cx, uri.items, pos, deadline, .incoming, tr), - .outgoing_calls => try hierarchy(c, si, arena, &cx, uri.items, pos, deadline, .outgoing, tr), - .supertypes => try hierarchy(c, si, arena, &cx, uri.items, pos, deadline, .supers, tr), - .subtypes => try hierarchy(c, si, arena, &cx, uri.items, pos, deadline, .subs, tr), + .callstack => try callstack(c, io, si, arena, &cx, uri.items, pos, deadline, tr, req.callstack_depth, req.budget, req.arg), + .incoming_calls => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .incoming, tr), + .outgoing_calls => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .outgoing, tr), + .supertypes => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .supers, tr), + .subtypes => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .subs, tr), .status, .explain => unreachable, } } @@ -687,13 +724,13 @@ fn walkSymbols(cx: *Cx, enc: Enc, uri: []const u8, node: std.json.Value, depth: /// Pull when the server does (LSP 3.17), the push store otherwise. The store /// path waits briefly for a publish that postdates the didChange we just /// sent, so `]d` right after an edit sees the new truth, not the old one. -fn diagnostics(c: *Conn, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, deadline: i64) Err!void { +fn diagnostics(c: *Conn, io: std.Io, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, deadline: i64) Err!void { if (c.caps.pull) { var b: std.ArrayList(u8) = .empty; try app(&b, arena, "\"textDocument\":{\"uri\":"); try jstr(&b, arena, uri); try app(&b, arena, "}"); - const result = (try call(c, arena, "textDocument/diagnostic", b.items, deadline)) orelse return; + const result = (try call(c, io, arena, "textDocument/diagnostic", b.items, deadline)) orelse return; for (items(get(result, "items"))) |dg| try emitDiag(cx, c.caps.enc, uri, dg, arena); return; } @@ -702,13 +739,13 @@ fn diagnostics(c: *Conn, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, dea stale = d.stale; break; }; - if (stale) waitFresh(c, uri, nowMs() + diag_ms); + if (stale) try waitFresh(c, io, uri, nowMs() + diag_ms); try renderStore(c, arena, cx, uri); } -fn workspaceDiagnostics(c: *Conn, arena: std.mem.Allocator, cx: *Cx, deadline: i64) Err!void { +fn workspaceDiagnostics(c: *Conn, io: std.Io, arena: std.mem.Allocator, cx: *Cx, deadline: i64) Err!void { if (c.caps.pull_workspace) { - const result = (try call(c, arena, "workspace/diagnostic", "\"previousResultIds\":[]", deadline)) orelse return; + const result = (try call(c, io, arena, "workspace/diagnostic", "\"previousResultIds\":[]", deadline)) orelse return; for (items(get(result, "items"))) |per| { const uri = str(get(per, "uri")) orelse continue; for (items(get(per, "items"))) |dg| try emitDiag(cx, c.caps.enc, uri, dg, arena); @@ -741,7 +778,7 @@ fn renderStore(c: *Conn, arena: std.mem.Allocator, cx: *Cx, only_uri: ?[]const u /// Block (mutex released) until the reader marks `uri` fresh or the deadline /// passes. The reader broadcasts on every publishDiagnostics. -fn waitFresh(c: *Conn, uri: []const u8, deadline: i64) void { +fn waitFresh(c: *Conn, io: std.Io, uri: []const u8, deadline: i64) error{Canceled}!void { const g = c.gen; while (c.gen == g and c.alive()) { var fresh = false; @@ -750,7 +787,7 @@ fn waitFresh(c: *Conn, uri: []const u8, deadline: i64) void { break; }; if (fresh) return; - if (!timedWait(c, deadline)) return; + if (!try timedWait(c, io, deadline)) return; } } @@ -873,18 +910,21 @@ fn sortEdits(edits: []PutEdit) void { /// A bounded static hierarchy. Reuse the prepared item verbatim (including /// server data), and retain every call-site range rather than just a declaration. -fn hierarchyResult(c: *Conn, arena: std.mem.Allocator, item: std.json.Value, method: []const u8, deadline: i64) Err!?std.json.Value { +fn hierarchyResult(c: *Conn, io: std.Io, arena: std.mem.Allocator, item: std.json.Value, method: []const u8, deadline: i64) Err!?std.json.Value { if (jsonDepth(item, 0) > 96) return null; var body: std.ArrayList(u8) = .empty; try app(&body, arena, "\"item\":"); try app(&body, arena, std.json.Stringify.valueAlloc(arena, item, .{}) catch return error.OutOfMemory); - return call(c, arena, method, body.items, deadline); + return call(c, io, arena, method, body.items, deadline); } const CallTree = struct { - const Site = struct { range: std.json.Value, focus: bool }; + /// What a node keeps of the server's item: its own copies, so the + /// response it came in is freed once its subtree is walked. + const Call = struct { uri: []const u8, name: []const u8, at: Range }; + const Site = struct { range: Range, focus: bool }; const Node = struct { - item: std.json.Value, + call: Call, ranges: std.ArrayList(Site) = .empty, children: std.ArrayList(usize) = .empty, cycle: bool = false, @@ -893,10 +933,39 @@ const CallTree = struct { roots: std.ArrayList(usize) = .empty, path: [lsp.callstack_max_depth]std.json.Value = undefined, depth_limit: usize = lsp.callstack_default_depth, - visited: usize = 0, limited: bool = false, + out_of_time: bool = false, + out_of_memory: bool = false, + budget: ?*const std.heap.FixedBufferAllocator = null, + si: usize = 0, + collected: usize = 0, + started_ms: i64 = 0, + narrated: bool = false, siblings: [lsp.callstack_max_depth]bool = @splat(false), rendered: usize = 0, + /// Paging. A page starts at `start`, a path of entry indices from the + /// top (empty: the start), skipping what is before it at each level; + /// `rows` and `bytes` count what rendering takes, and the walk stops + /// before their caps at `cut`, which the next page starts at (`more`). + /// The ancestors on the path are shown again on it, as context. + start: [lsp.callstack_max_depth]u32 = undefined, + start_len: usize = 0, + at: [lsp.callstack_max_depth]u32 = undefined, + cut: [lsp.callstack_max_depth]u32 = undefined, + cut_len: usize = 0, + more: bool = false, + rows: usize = 0, + row_cap: usize = max_rows - 16, + /// The bytes those rows take, over-estimated (the uri for the path it + /// becomes), kept under the answer writer's room. + bytes: usize = 0, + byte_cap: usize = std.math.maxInt(usize), + + /// A row of a caller at `depth`: its tree columns, the uri the path is + /// made from, the name, and room for the address. + fn rowBytes(depth: usize, caller: Call) usize { + return 5 * (depth + 2) + caller.uri.len + caller.name.len + 40; + } /// Incoming paths arrive nearest-first. Insert them outermost-first, /// sharing common caller prefixes and retaining all their call sites. @@ -909,11 +978,14 @@ const CallTree = struct { const item = get(entry, "from").?; const children = if (parent) |id| tree.nodes.items[id].children.items else tree.roots.items; const found = for (children) |id| { - if (sameCall(tree.nodes.items[id].item, item)) break id; + if (sameCall(tree.nodes.items[id].call, item)) break id; } else null; + const depth = path.len - 1 - at; const id = found orelse blk: { const id = tree.nodes.items.len; - try tree.nodes.append(arena, .{ .item = item }); + try tree.nodes.append(arena, .{ .call = try callOf(arena, item) }); + tree.rows += 1; + tree.bytes += rowBytes(depth, tree.nodes.items[id].call); if (parent) |pid| { try tree.nodes.items[pid].children.append(arena, id); } else try tree.roots.append(arena, id); @@ -924,23 +996,39 @@ const CallTree = struct { for (items(get(entry, "fromRanges"))) |range| { const parsed = rangeOf(range) orelse continue; for (node.ranges.items) |*other| { - if (std.meta.eql(parsed, rangeOf(other.range).?)) { + if (std.meta.eql(parsed, other.range)) { other.focus = other.focus or at == 0; break; } - } else try node.ranges.append(arena, .{ .range = range, .focus = at == 0 }); + } else { + try node.ranges.append(arena, .{ .range = parsed, .focus = at == 0 }); + tree.rows += 1; + tree.bytes += rowBytes(depth + 1, .{ .uri = node.call.uri, .name = "", .at = parsed }); + } } parent = id; } } }; -fn sameCall(a: std.json.Value, b: std.json.Value) bool { - const au = str(get(a, "uri")) orelse return false; +fn callOf(arena: std.mem.Allocator, item: std.json.Value) Err!CallTree.Call { + return .{ + .uri = try arena.dupe(u8, str(get(item, "uri")).?), + .name = try arena.dupe(u8, str(get(item, "name")) orelse "(unnamed)"), + .at = rangeOf(get(item, "selectionRange") orelse get(item, "range")).?, + }; +} + +fn sameCall(a: CallTree.Call, b: std.json.Value) bool { const bu = str(get(b, "uri")) orelse return false; - const ar = rangeOf(get(a, "selectionRange") orelse get(a, "range")) orelse return false; const br = rangeOf(get(b, "selectionRange") orelse get(b, "range")) orelse return false; - return std.mem.eql(u8, au, bu) and std.meta.eql(ar, br); + return std.mem.eql(u8, a.uri, bu) and std.meta.eql(a.at, br); +} + +fn sameItem(a: std.json.Value, b: std.json.Value) bool { + const au = str(get(a, "uri")) orelse return false; + const ar = rangeOf(get(a, "selectionRange") orelse get(a, "range")) orelse return false; + return sameCall(.{ .uri = au, .name = "", .at = ar }, b); } fn callPrefix(out: *std.Io.Writer, tree: *const CallTree, depth: usize) Err!void { @@ -954,44 +1042,115 @@ fn validCaller(entry: std.json.Value) bool { rangeOf(get(from, "selectionRange") orelse get(from, "range")) != null; } -fn callstack(c: *Conn, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, tr: *Trace, max_depth: u8) Err!void { +fn callstack(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, tr: *Trace, max_depth: u8, budget: ?*const std.heap.FixedBufferAllocator, arg: []const u8) Err!void { if (!c.caps.call_hier) return lsp.none(cx.out, "{s} does not answer call hierarchy (no callHierarchyProvider)", .{specs[si].name}); const b = try atPos(arena, uri, pos); - const prepared = (try call(c, arena, "textDocument/prepareCallHierarchy", b.items, deadline)) orelse return; + const prepared = (try call(c, io, arena, "textDocument/prepareCallHierarchy", b.items, deadline)) orelse return; if (items(prepared).len == 0) return lsp.none(cx.out, "no function at this position", .{}); + // The rows come from the tree alone: the core reads the sources. + cx.no_text = true; + // Each response is parsed into its own part of the region, freed once + // its subtree is walked; only the path being walked is live JSON. + // Raw, as query()'s scratch: `alloc` would fill all ~100 MB of it. + const parse_len = if (budget) |fixed| (fixed.buffer.len - fixed.end_index) / 2 else 0; + var parse_fba: std.heap.FixedBufferAllocator = .init(if (budget != null) + (arena.rawAlloc(parse_len, .of(u8), @returnAddress()) orelse return error.OutOfMemory)[0..parse_len] + else + &.{}); + const parse = if (budget != null) parse_fba.allocator() else arena; for (items(prepared)) |item| { - var tree: CallTree = .{ .depth_limit = std.math.clamp(max_depth, 1, lsp.callstack_max_depth) }; - const incoming = try hierarchyResult(c, arena, item, "callHierarchy/incomingCalls", deadline); - const count = try collectCallers(c, arena, item, incoming, deadline, &tree, 0); - if (count == 0) try cx.out.writeAll("No callers reported\n"); + var tree: CallTree = .{ + .depth_limit = std.math.clamp(max_depth, 1, lsp.callstack_max_depth), + .si = si, + .started_ms = nowMs(), + .budget = budget, + // The page ends while the answer's writer has room for the rest. + .byte_cap = if (cx.fixed_out) (cx.out.buffer.len - cx.out.end) -| (1 << 20) else std.math.maxInt(usize), + }; + var steps = std.mem.tokenizeScalar(u8, std.mem.trim(u8, arg, " \n"), '.'); + while (steps.next()) |step| { + if (tree.start_len == tree.start.len) break; + tree.start[tree.start_len] = std.fmt.parseInt(u32, step, 10) catch break; + tree.start_len += 1; + } + const incoming = try hierarchyResult(c, io, parse, item, "callHierarchy/incomingCalls", deadline); + const count = try collectCallers(c, io, arena, if (budget != null) &parse_fba else null, item, incoming, &tree, 0, tree.start_len > 0); + if (tree.narrated) post(si, .always, "Callstack: {d} callers", .{tree.collected}); + if (count == 0 and tree.start_len == 0) try cx.out.writeAll("No callers reported\n"); try renderCallers(cx, c.caps.enc, &tree, tree.roots.items, 0); - if (tree.limited) try cx.out.writeAll("… continue from a function above with Callstack\n"); + if (tree.more) { + try cx.out.writeAll(lsp.callstack_more); + for (tree.cut[0..tree.cut_len], 0..) |index, level| try cx.out.print("{s}{d}", .{ if (level == 0) "" else ".", index }); + try cx.out.print(" {d}\n", .{tree.collected}); + } else if (tree.out_of_time) { + try cx.out.writeAll("… the language server ran out of time; continue from a function above with Callstack\n"); + } else if (tree.out_of_memory) { + try cx.out.writeAll("… stopped at the query's memory limit; continue from a function above with Callstack\n"); + } else if (tree.limited) try cx.out.writeAll("… continue from a function above with Callstack\n"); tr.note("callstack: {d} callers", .{tree.rendered}); } } -fn collectCallers(c: *Conn, arena: std.mem.Allocator, focus: std.json.Value, incoming: ?std.json.Value, deadline: i64, tree: *CallTree, depth: usize) Err!usize { +/// No caller count and no overall deadline: the walk narrates itself on the +/// message row after a second, and it stops when a request goes unanswered +/// (what was collected is shown), when the page is full (the rest is the +/// next page), or when the shell cancels it for a newer query. +fn collectCallers(c: *Conn, io: std.Io, arena: std.mem.Allocator, parse: ?*std.heap.FixedBufferAllocator, focus: std.json.Value, incoming: ?std.json.Value, tree: *CallTree, depth: usize, on_path: bool) Err!usize { var count: usize = 0; - for (items(incoming)) |entry| { - if (tree.visited >= 64) { - tree.limited = true; - break; + for (items(incoming), 0..) |entry, index| { + // A later page skips what the pages before it walked. + const resuming = on_path and depth < tree.start_len; + if (resuming and index < tree.start[depth]) continue; + tree.at[depth] = @intCast(index); + if (tree.budget) |b| if (b.buffer.len - b.end_index < callstack_reserve) { + tree.out_of_memory = true; + }; + if (!tree.more and (tree.rows >= tree.row_cap or tree.bytes >= tree.byte_cap or tree.out_of_memory)) { + // The page is full: the next goes on here. Never where this + // one began, which would page in place: past that entry. + tree.more = true; + tree.cut_len = depth + 1; + @memcpy(tree.cut[0..tree.cut_len], tree.at[0..tree.cut_len]); + if (tree.cut_len == tree.start_len and std.mem.eql(u32, tree.cut[0..tree.cut_len], tree.start[0..tree.start_len])) + tree.cut[depth] += 1; } + if (tree.out_of_time or tree.more) break; if (!validCaller(entry)) continue; const from = get(entry, "from").?; - const cycle = sameCall(focus, from) or for (tree.path[0..depth]) |ancestor| { - if (sameCall(get(ancestor, "from").?, from)) break true; + const cycle = sameItem(focus, from) or for (tree.path[0..depth]) |ancestor| { + if (sameItem(get(ancestor, "from").?, from)) break true; } else false; tree.path[depth] = entry; - tree.visited += 1; count += 1; + tree.collected += 1; + if (nowMs() - tree.started_ms >= 1000) { + tree.narrated = true; + post(tree.si, .chatty, "Callstack: {d} callers\u{2026}", .{tree.collected}); + } var children: usize = 0; if (!cycle) { - if (depth + 1 == tree.depth_limit or tree.visited >= 64) { + if (depth + 1 == tree.depth_limit) { tree.limited = true; } else { - const ancestors = try hierarchyResult(c, arena, from, "callHierarchy/incomingCalls", deadline); - children = try collectCallers(c, arena, focus, ancestors, deadline, tree, depth + 1); + const mark = if (parse) |fixed| fixed.end_index else 0; + defer if (parse) |fixed| { + fixed.end_index = mark; + }; + const ancestors = hierarchyResult(c, io, if (parse) |fixed| fixed.allocator() else arena, from, "callHierarchy/incomingCalls", nowMs() + req_ms) catch |e| switch (e) { + error.Timeout => blk: { + tree.out_of_time = true; + break :blk null; + }, + // A response bigger than the parse region: this caller + // stops here, the page with it. + error.OutOfMemory => blk: { + tree.out_of_memory = true; + break :blk null; + }, + else => return e, + }; + const deeper = resuming and index == tree.start[depth] and depth + 1 < tree.start_len; + if (!tree.out_of_time and !tree.out_of_memory) children = try collectCallers(c, io, arena, parse, focus, ancestors, tree, depth + 1, deeper); } } if (children == 0) try tree.addPath(arena, tree.path[0 .. depth + 1], cycle); @@ -999,51 +1158,61 @@ fn collectCallers(c: *Conn, arena: std.mem.Allocator, focus: std.json.Value, inc return count; } +/// Test callers sink below the rest; each group keeps the usual order. +// ponytail: substring heuristic, "latest.zig" or "contest" count as tests too +fn testCaller(caller: CallTree.Call) bool { + return std.ascii.findIgnoreCase(caller.name, "test") != null or + std.ascii.findIgnoreCase(std.fs.path.basenamePosix(caller.uri), "test") != null; +} + fn callerLess(tree: *const CallTree, a: usize, b: usize) bool { - const left = tree.nodes.items[a].item; - const right = tree.nodes.items[b].item; - const uri_order = std.mem.order(u8, str(get(left, "uri")).?, str(get(right, "uri")).?); + const left = tree.nodes.items[a].call; + const right = tree.nodes.items[b].call; + if (testCaller(left) != testCaller(right)) return testCaller(right); + const uri_order = std.mem.order(u8, left.uri, right.uri); if (uri_order != .eq) return uri_order == .lt; - return callRangeLess({}, get(left, "selectionRange") orelse get(left, "range").?, get(right, "selectionRange") orelse get(right, "range").?); + return rangeLess(left.at, right.at); } -fn callRangeLess(_: void, a: std.json.Value, b: std.json.Value) bool { - const left = rangeOf(a).?; - const right = rangeOf(b).?; +fn rangeLess(left: Range, right: Range) bool { return left.sl < right.sl or (left.sl == right.sl and left.sc < right.sc); } fn renderCallers(cx: *Cx, enc: Enc, tree: *CallTree, children: []usize, depth: usize) Err!void { std.mem.sort(usize, children, @as(*const CallTree, tree), callerLess); for (children, 0..) |id, index| { - if (tree.rendered >= 64) { + // emitRange's row ceiling and the writer's room, met here before a + // prefix is half written + if (cx.rows >= max_rows or (cx.fixed_out and cx.out.buffer.len - cx.out.end < row_room)) { tree.limited = true; return; } const node = &tree.nodes.items[id]; - const item = node.item; - const uri = str(get(item, "uri")).?; const more = index + 1 < children.len; try callPrefix(cx.out, tree, depth); const has_children = node.children.items.len > 0 or node.ranges.items.len > 0; const rounded = cx.callstack_corners == .rounded; try cx.out.writeAll(if (more) "├" else if (rounded) "╰" else "└"); try cx.out.writeAll(if (has_children) "──┬ " else "─── "); - const name = flat(cx.arena, str(get(item, "name")) orelse "(unnamed)"); + const name = flat(cx.arena, node.call.name); const label = if (node.cycle) try std.fmt.allocPrint(cx.arena, "{s} ↻ recursion", .{name}) else name; - try emitRange(cx, enc, uri, get(item, "selectionRange") orelse get(item, "range"), label); + try emitAt(cx, enc, node.call.uri, node.call.at, label); tree.rendered += 1; tree.siblings[depth] = more; std.mem.sort(CallTree.Site, node.ranges.items, {}, struct { fn less(_: void, a: CallTree.Site, b: CallTree.Site) bool { - return callRangeLess({}, a.range, b.range); + return rangeLess(a.range, b.range); } }.less); for (node.ranges.items, 0..) |site, site_index| { + if (cx.rows >= max_rows or (cx.fixed_out and cx.out.buffer.len - cx.out.end < row_room)) { + tree.limited = true; + return; + } if (site.focus) try cx.out.writeAll(lsp.callstack_match); try callPrefix(cx.out, tree, depth + 1); try cx.out.writeAll(if (node.children.items.len > 0 or site_index + 1 < node.ranges.items.len) "├─ " else if (rounded) "╰─ " else "└─ "); - try emitRange(cx, enc, uri, site.range, ""); + try emitAt(cx, enc, node.call.uri, site.range, ""); } try renderCallers(cx, enc, tree, node.children.items, depth + 1); } @@ -1054,7 +1223,7 @@ const Hier = enum { incoming, outgoing, supers, subs }; /// The two-step hierarchy kinds: prepare at the cursor, then follow every /// item the server returned (usually one). Gated on the server capability so /// an old server costs zero round trips. -fn hierarchy(c: *Conn, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, h: Hier, tr: *Trace) Err!void { +fn hierarchy(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, h: Hier, tr: *Trace) Err!void { const call_side = h == .incoming or h == .outgoing; if (call_side and !c.caps.call_hier) { tr.note("STOP: {s} does not advertise callHierarchyProvider", .{specs[si].name}); @@ -1072,9 +1241,9 @@ fn hierarchy(c: *Conn, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []cons .subs => "typeHierarchy/subtypes", }; const b = try atPos(arena, uri, pos); - const prepared = (try call(c, arena, prepare, b.items, deadline)) orelse return; + const prepared = (try call(c, io, arena, prepare, b.items, deadline)) orelse return; for (items(prepared)) |item| { - const result = (try hierarchyResult(c, arena, item, follow, deadline)) orelse return; + const result = (try hierarchyResult(c, io, arena, item, follow, deadline)) orelse return; for (items(result)) |entry| switch (h) { .incoming => { // one row per CALL SITE, under the caller's name — that is @@ -1134,6 +1303,10 @@ const Cx = struct { cur_path: []const u8, cur_src: []const u8, out: *std.Io.Writer, + /// `out` is the query's fixed answer buffer, whose room is checked. + fixed_out: bool = false, + /// Rows without their source text (Callstack: the core reads it). + no_text: bool = false, rows: usize = 0, relative_parents: bool = false, callstack_corners: lsp.CallstackCorners = .rounded, @@ -1169,11 +1342,16 @@ const Cx = struct { /// columns, and a single-line range becomes the `path:LINE:COL-ENDCOL` form a /// look SELECTS. `note` overrides the source line as the row's text. fn emitRange(cx: *Cx, enc: Enc, uri: []const u8, range: ?std.json.Value, note: []const u8) Err!void { + return emitAt(cx, enc, uri, rangeOf(range) orelse return, note); +} + +fn emitAt(cx: *Cx, enc: Enc, uri: []const u8, r: Range, note: []const u8) Err!void { if (cx.rows >= max_rows) return; const path = pathOf(cx.arena, uri) orelse return; if (path.len == 0 or path[0] != '/') return; // rows promise absolute-or-rel-from-base - const r = rangeOf(range) orelse return; - const src = cx.text(path); + // Without sources (Callstack) the asking file is still known: its rows' + // columns convert from utf-16 as ever. + const src = if (cx.no_text and !std.mem.eql(u8, path, cx.cur_path)) null else cx.text(path); var bol: usize = 0; var eol: usize = 0; @@ -1323,7 +1501,8 @@ fn transportPair(sv: *[2]libc.fd_t) bool { /// Spawn-or-return, and tell an existing server about a new project root. /// Returns the generation the caller's whole query is bound to. -fn ensure(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trace) Err!u32 { +fn ensure(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trace) Err!u32 { + c.si = si; if (c.state == .disabled) { tr.note("STOP: {s} is disabled for this session (no binary; set {s})", .{ specs[si].name, specs[si].env }); return error.NoServer; @@ -1341,7 +1520,7 @@ fn ensure(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trac try app(&b, arena, "{\"jsonrpc\":\"2.0\",\"method\":\"workspace/didChangeWorkspaceFolders\",\"params\":{\"event\":{\"added\":[{\"name\":\"root\",\"uri\":\""); try uriOf(&b, arena, root); try app(&b, arena, "\"}],\"removed\":[]}}}"); - try frame(c.sock, b.items, nowMs() + reply_ms); + try send(c, io, b.items, nowMs() + wedge_ms); const owned = sa.dupe(u8, root) catch return error.OutOfMemory; c.extra_roots.append(sa, owned) catch sa.free(owned); } @@ -1430,12 +1609,13 @@ fn ensure(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trac if (c.resp) |r| sa.free(r); c.resp = null; c.progress = 0; + c.loading = false; c.timeouts = 0; forgetDocs(c); if (c.root.len > 0) sa.free(c.root); c.root = owned_root; - const th = std.Thread.spawn(.{}, reader, .{ si, g, sv[0] }) catch { + const th = std.Thread.spawn(.{}, reader, .{ io, si, g, sv[0] }) catch { // no reader means nobody would ever close the fd our way; do it here, // before anything else can see the conn _ = libc.close(sv[0]); @@ -1450,8 +1630,14 @@ fn ensure(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trac post(si, .always, "{s} starting — {s}", .{ specs[si].name, root }); tr.note("spawned {s} (pid {d}) at {s}", .{ specs[si].name, pid, root }); - handshake(c, arena, root) catch |e| { - shutdownIf(c, g); + // A start runs to the end however soon the query that began it is + // canceled: killing a half-started server would only make the next + // query start it again. The cancel lands at the next wait instead. + const protection = io.swapCancelProtection(.blocked); + const started = handshake(c, io, arena, root); + _ = io.swapCancelProtection(protection); + started catch |e| { + shutdownIf(c, io, g); // NOT a session disable. A handshake that misses the deadline is // routinely environmental — a rustup shim deciding to download the // project's pinned toolchain before launching the real server was @@ -1477,7 +1663,7 @@ fn ensure(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trac return g; } -fn handshake(c: *Conn, arena: std.mem.Allocator, root: []const u8) Err!void { +fn handshake(c: *Conn, io: std.Io, arena: std.mem.Allocator, root: []const u8) Err!void { var b: std.ArrayList(u8) = .empty; try b.print(arena, "\"processId\":{d},\"clientInfo\":{{\"name\":\"pardes\"}},\"rootUri\":\"", .{@as(u32, @bitCast(libc.getpid()))}); try uriOf(&b, arena, root); @@ -1509,7 +1695,7 @@ fn handshake(c: *Conn, arena: std.mem.Allocator, root: []const u8) Err!void { for (b.items) |ch| if (ch != '\n') try body.append(arena, ch); const deadline = nowMs() + init_ms; - const reply = (try call(c, arena, "initialize", body.items, deadline)) orelse return error.Protocol; + const reply = (try call(c, io, arena, "initialize", body.items, deadline)) orelse return error.Protocol; const caps = get(reply, "capabilities"); c.caps.enc = if (std.mem.eql(u8, str(get(caps, "positionEncoding")) orelse "utf-16", "utf-8")) .utf8 else .utf16; @@ -1521,7 +1707,7 @@ fn handshake(c: *Conn, arena: std.mem.Allocator, root: []const u8) Err!void { c.caps.type_hier = provider(get(caps, "typeHierarchyProvider")); c.caps.folders = if (get(get(get(caps, "workspace"), "workspaceFolders"), "supported")) |s| s == .bool and s.bool else false; - try frame(c.sock, "{\"jsonrpc\":\"2.0\",\"method\":\"initialized\",\"params\":{}}", deadline); + try send(c, io, "{\"jsonrpc\":\"2.0\",\"method\":\"initialized\",\"params\":{}}", deadline); } /// A server capability that may be `true`, an options object, or absent. @@ -1536,7 +1722,7 @@ fn provider(v: ?std.json.Value) bool { /// didOpen the first time a file is seen, didChange when its bytes moved, /// nothing when they did not — the common case between two presses of gd. -fn syncDoc(c: *Conn, si: usize, arena: std.mem.Allocator, uri: []const u8, src: []const u8) Err!void { +fn syncDoc(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, uri: []const u8, src: []const u8) Err!void { const hash = std.hash.Wyhash.hash(0, src); var doc: ?*Doc = null; for (c.docs.items) |*d| if (std.mem.eql(u8, d.uri, uri)) { @@ -1568,13 +1754,16 @@ fn syncDoc(c: *Conn, si: usize, arena: std.mem.Allocator, uri: []const u8, src: try jstr(&b, arena, src); try app(&b, arena, "}}}"); } - try frame(c.sock, b.items, nowMs() + req_ms); + // A server busy with what it was sent last reads no more meanwhile + // (clangd parses its compile database at a first open): this waits as + // an answer does, for a cancel or a server that has stopped. + try send(c, io, b.items, nowMs() + wedge_ms); } /// Send a request and wait on the mailbox. Returns the parsed `result`, or /// null for an error response / null result — both are legal "no answer". /// Runs with the conn mutex held; the mutex is released inside the wait. -fn call(c: *Conn, arena: std.mem.Allocator, method: []const u8, params: []const u8, deadline: i64) Err!?std.json.Value { +fn call(c: *Conn, io: std.Io, arena: std.mem.Allocator, method: []const u8, params: []const u8, deadline: i64) Err!?std.json.Value { const g = c.gen; const id = c.next_id; c.next_id +%= 1; @@ -1589,17 +1778,36 @@ fn call(c: *Conn, arena: std.mem.Allocator, method: []const u8, params: []const c.want_id = id; defer c.want_id = 0; - try frame(c.sock, b.items, deadline); + try send(c, io, b.items, if (c.state == .ready) nowMs() + wedge_ms else deadline); + // The handshake (still `.starting`) has its deadline. A ready server is + // waited for until it answers or the shell cancels this query for a + // newer one, so the first query after a start is answered, not lost: + // past `deadline` the message row says it is still working, and only + // silent and idle for wedge_ms is it not answering. + var said = false; + var busy_at = nowMs(); while (c.resp == null and c.gen == g and c.alive()) { - if (!timedWait(c, deadline)) break; + const ready = c.state == .ready; + const waiting = timedWait(c, io, if (ready) nowMs() + 500 else deadline) catch |e| { + cancelRequest(c, io, arena, id); + return e; + }; + if (!ready) { + if (waiting) continue; + break; + } + if (c.progress > 0) busy_at = nowMs(); + if (!said and nowMs() >= deadline) { + said = true; + post(c.si, .always, "{s} is still working on it\u{2026}", .{specs[c.si].name}); + } + if (nowMs() - busy_at >= wedge_ms) break; } + if (said and c.resp != null) post(c.si, .always, "{s} answered", .{specs[c.si].name}); if (c.gen != g or !c.alive()) return error.Dead; const raw = c.resp orelse { - // give the server leave to abandon the work we stopped waiting for - var cb: std.ArrayList(u8) = .empty; - cb.print(arena, "{{\"jsonrpc\":\"2.0\",\"method\":\"$/cancelRequest\",\"params\":{{\"id\":{d}}}}}", .{id}) catch return error.Timeout; - frame(c.sock, cb.items, nowMs() + reply_ms) catch {}; + cancelRequest(c, io, arena, id); return error.Timeout; }; c.resp = null; @@ -1609,49 +1817,23 @@ fn call(c: *Conn, arena: std.mem.Allocator, method: []const u8, params: []const if (result == .null) return null; return result; } -/// One bounded cond wait. False when the deadline has passed. -/// -/// The deadline arithmetic everywhere here is CLOCK_MONOTONIC, so the wait -/// must be too: a REALTIME abstime plus an NTP step backwards would park a -/// worker far past `req_ms`, violating the seam's bounded-wait promise. On -/// linux the condvar is lazily initialized with a monotonic condattr; darwin -/// has no setclock but has a RELATIVE wait, which never consults the wall -/// clock at all. -fn timedWait(c: *Conn, deadline: i64) bool { +/// Give the server leave to abandon the work we stopped waiting for. +fn cancelRequest(c: *Conn, io: std.Io, arena: std.mem.Allocator, id: u32) void { + var cb: std.ArrayList(u8) = .empty; + cb.print(arena, "{{\"jsonrpc\":\"2.0\",\"method\":\"$/cancelRequest\",\"params\":{{\"id\":{d}}}}}", .{id}) catch return; + send(c, io, cb.items, nowMs() + reply_ms) catch {}; +} + +/// One bounded cond wait, and a cancelation point: a worker the shell +/// cancels for a newer query stops here. False when the deadline has passed. +fn timedWait(c: *Conn, io: std.Io, deadline: i64) error{Canceled}!bool { const left = deadline - nowMs(); if (left <= 0) return false; const wait_ms = @min(left, 500); // tick so gen/state changes are noticed - if (comptime builtin.os.tag.isDarwin()) { - const rel: libc.timespec = .{ - .sec = @divTrunc(wait_ms, 1000), - .nsec = @rem(wait_ms, 1000) * 1_000_000, - }; - _ = pthread_cond_timedwait_relative_np(&c.cond, &c.mu, &rel); - return true; - } - if (!c.cond_monotonic) { - // lazily re-initialize the zeroed (REALTIME) condvar with a - // monotonic clock; under the mutex, and only before the first wait, - // so no thread can be parked on the old one - var attr: pthread_condattr_t = undefined; - if (pthread_condattr_init(&attr) == 0) { - _ = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); - _ = pthread_cond_init(&c.cond, &attr); - _ = pthread_condattr_destroy(&attr); - } - c.cond_monotonic = true; - } - var now: libc.timespec = undefined; - _ = libc.clock_gettime(.MONOTONIC, &now); - var abs: libc.timespec = .{ - .sec = now.sec + @divTrunc(wait_ms, 1000), - .nsec = now.nsec + @rem(wait_ms, 1000) * 1_000_000, + c.cond.waitTimeout(io, &c.mu, .{ .duration = .{ .raw = .fromMilliseconds(wait_ms), .clock = .awake } }) catch |e| switch (e) { + error.Timeout => {}, + error.Canceled => return error.Canceled, }; - if (abs.nsec >= 1_000_000_000) { - abs.sec += 1; - abs.nsec -= 1_000_000_000; - } - _ = libc.pthread_cond_timedwait(&c.cond, &c.mu, &abs); return true; } @@ -1659,7 +1841,7 @@ fn timedWait(c: *Conn, deadline: i64) bool { /// respawn that happened during the caller's wait must not be killed for its /// predecessor's crimes. Mutex held. The reader closes the fd; this only /// shuts the transport down and reaps the child. -fn shutdownIf(c: *Conn, g: u32) void { +fn shutdownIf(c: *Conn, io: std.Io, g: u32) void { if (c.gen != g or c.sock < 0) return; _ = libc.shutdown(c.sock, 2); // SHUT_RDWR: the reader sees EOF and closes reap(c.pid); @@ -1669,7 +1851,7 @@ fn shutdownIf(c: *Conn, g: u32) void { forgetDocs(c); if (c.resp) |r| sa.free(r); c.resp = null; - _ = libc.pthread_cond_broadcast(&c.cond); + c.cond.broadcast(io); } /// TERM, a bounded grace, then KILL, to the server's whole process group @@ -1715,6 +1897,7 @@ fn forgetDocs(c: *Conn) void { sa.free(d.body); } c.diags.clearRetainingCapacity(); + c.diag_bytes = 0; } // ------------------------------------------------------------ reader thread @@ -1722,20 +1905,22 @@ fn forgetDocs(c: *Conn) void { /// Owns the read side of one socket for one generation, and is the only /// closer of that fd. Routes responses to the mailbox, answers server /// requests, feeds the diagnostics store, narrates progress. -fn reader(si: usize, g: u32, sock: c_int) void { +fn reader(io: std.Io, si: usize, g: u32, sock: c_int) void { const c = &conns[si]; var buf = sa.alloc(u8, 256 * 1024) catch { - readerExit(c, si, g, sock); + readerExit(c, io, si, g, sock); return; }; defer sa.free(buf); var len: usize = 0; + var parse_arena: std.heap.ArenaAllocator = .init(sa); + defer parse_arena.deinit(); outer: while (true) { // drain every complete frame already buffered drain: while (true) switch (frameNext(buf[0..len])) { .frame => |m| { - dispatch(c, si, g, sock, buf[m.body_start..m.body_end]); + dispatch(c, io, si, g, sock, buf[m.body_start..m.body_end], &parse_arena); std.mem.copyForwards(u8, buf[0 .. len - m.consumed], buf[m.consumed..len]); len -= m.consumed; }, @@ -1761,9 +1946,9 @@ fn reader(si: usize, g: u32, sock: c_int) void { if (pr == 0) { // tick: a respawn while the server was silent leaves this thread // reading a corpse; notice and go - c.lock(); + c.mu.lockUncancelable(io); const stale = c.gen != g; - c.unlock(); + c.mu.unlock(io); if (stale) break :outer; continue; } @@ -1776,11 +1961,11 @@ fn reader(si: usize, g: u32, sock: c_int) void { if (got == 0) break :outer; // the server exited or shutdownIf spoke len += @intCast(got); } - readerExit(c, si, g, sock); + readerExit(c, io, si, g, sock); } -fn readerExit(c: *Conn, si: usize, g: u32, sock: c_int) void { - c.lock(); +fn readerExit(c: *Conn, io: std.Io, si: usize, g: u32, sock: c_int) void { + c.mu.lockUncancelable(io); if (c.gen == g and c.alive()) { // the server died on its own — shutdownIf never saw it, so the corpse // is ours to reap and the conn's transport fields are ours to clear @@ -1800,10 +1985,14 @@ fn readerExit(c: *Conn, si: usize, g: u32, sock: c_int) void { } else { post(si, .always, "{s} exited — restarts on the next query", .{specs[si].name}); } - _ = libc.pthread_cond_broadcast(&c.cond); + c.cond.broadcast(io); } - c.unlock(); + c.mu.unlock(io); + // A query writes with the mutex let go (send): the fd stays its until + // that write is done, and shutdown(2) has already made it fail fast. + c.write_mu.lockUncancelable(io); _ = libc.close(sock); + c.write_mu.unlock(io); } /// 10s doubling to 2min: the fork-per-keystroke guard's schedule. @@ -1845,32 +2034,33 @@ fn frameNext(data: []const u8) FrameStep { /// One message from the server. Parses into a throwaway arena; touches conn /// state only under the mutex; replies never hold it (bounded writes must not /// stall the mailbox). -fn dispatch(c: *Conn, si: usize, g: u32, sock: c_int, raw: []const u8) void { - var arena_state: std.heap.ArenaAllocator = .init(sa); - defer arena_state.deinit(); +fn dispatch(c: *Conn, io: std.Io, si: usize, g: u32, sock: c_int, raw: []const u8, arena_state: *std.heap.ArenaAllocator) void { + // The reader's one parse arena, reset per message: no mapping per + // `$/progress` report, and no more than parse_kept kept between them. + defer _ = arena_state.reset(.{ .retain_with_limit = parse_kept }); const arena = arena_state.allocator(); const v = std.json.parseFromSliceLeaky(std.json.Value, arena, raw, .{}) catch return; if (v != .object) return; if (str(get(v, "method"))) |method| { - if (get(v, "id")) |id| return serverRequest(arena, sock, method, id, get(v, "params")); - return notification(c, si, g, raw, method, get(v, "params")); + if (get(v, "id")) |id| return serverRequest(c, io, arena, sock, method, id, get(v, "params")); + return notification(c, io, si, g, raw, method, get(v, "params")); } // a response: deliver to the one waiting query, if it is still waiting const id = num(get(v, "id")) orelse return; - c.lock(); - defer c.unlock(); + c.mu.lockUncancelable(io); + defer c.mu.unlock(io); if (c.gen == g and c.want_id != 0 and id == c.want_id and c.resp == null) { c.resp = sa.dupe(u8, raw) catch null; - _ = libc.pthread_cond_broadcast(&c.cond); + c.cond.broadcast(io); } } /// The server asked US something. Everything optional was declined in the /// handshake, so a null-ish answer is always legal — but it must ARRIVE, or /// the server blocks forever on its own question. -fn serverRequest(arena: std.mem.Allocator, sock: c_int, method: []const u8, id: std.json.Value, params: ?std.json.Value) void { +fn serverRequest(c: *Conn, io: std.Io, arena: std.mem.Allocator, sock: c_int, method: []const u8, id: std.json.Value, params: ?std.json.Value) void { var b: std.ArrayList(u8) = .empty; app(&b, arena, "{\"jsonrpc\":\"2.0\",\"id\":") catch return; switch (id) { @@ -1890,17 +2080,17 @@ fn serverRequest(arena: std.mem.Allocator, sock: c_int, method: []const u8, id: } else { app(&b, arena, ",\"result\":null}") catch return; } - frame(sock, b.items, nowMs() + reply_ms) catch {}; + answerServer(c, io, sock, b.items); } -fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8, params: ?std.json.Value) void { +fn notification(c: *Conn, io: std.Io, si: usize, g: u32, raw: []const u8, method: []const u8, params: ?std.json.Value) void { if (std.mem.eql(u8, method, "textDocument/publishDiagnostics")) { const uri = str(get(params, "uri")) orelse return; // keep the raw MESSAGE (params included); re-parsing it per query // beats owning a parsed tree past this arena's life const body = sa.dupe(u8, raw) catch return; - c.lock(); - defer c.unlock(); + c.mu.lockUncancelable(io); + defer c.mu.unlock(io); if (c.gen != g) { sa.free(body); return; @@ -1914,10 +2104,25 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8 slot = d; break; }; + // Bounded in files and bytes: a new set that does not fit pushes + // out the oldest files' (the server sends a file's again when it + // changes); one bigger than the whole store is not kept. + if (body.len > max_diag_bytes) { + sa.free(body); + return; + } if (slot) |d| { + c.diag_bytes -= d.body.len; sa.free(d.body); d.body = body; - } else if (c.diags.items.len < 256) { + c.diag_bytes += body.len; + } else { + while (c.diags.items.len > 0 and (c.diags.items.len >= max_diag_files or c.diag_bytes + body.len > max_diag_bytes)) { + const old = c.diags.orderedRemove(0); + c.diag_bytes -= old.body.len; + sa.free(old.uri); + sa.free(old.body); + } const owned = sa.dupe(u8, uri) catch { sa.free(body); return; @@ -1927,17 +2132,18 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8 sa.free(body); return; }; - } else sa.free(body); - _ = libc.pthread_cond_broadcast(&c.cond); // waitFresh watches this + c.diag_bytes += body.len; + } + c.cond.broadcast(io); // waitFresh watches this return; } if (std.mem.eql(u8, method, "$/progress")) { const value = get(params, "value") orelse return; const pkind = str(get(value, "kind")) orelse return; const msg = str(get(value, "message")) orelse ""; - c.lock(); + c.mu.lockUncancelable(io); if (c.gen != g) { - c.unlock(); + c.mu.unlock(io); return; } if (std.mem.eql(u8, pkind, "begin")) { @@ -1948,7 +2154,7 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8 var tbuf: [48]u8 = undefined; const t = tbuf[0..c.title_len]; @memcpy(t, c.title[0..c.title_len]); - c.unlock(); + c.mu.unlock(io); post(si, .chatty, "{s}: {s}\u{2026} {s}", .{ specs[si].name, t, msg }); return; } @@ -1957,7 +2163,7 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8 const t = tbuf[0..c.title_len]; @memcpy(t, c.title[0..c.title_len]); const pct = num(get(value, "percentage")); - c.unlock(); + c.mu.unlock(io); if (pct) |p| post(si, .chatty, "{s}: {s} {d}% {s}", .{ specs[si].name, t, p, msg }) else @@ -1968,7 +2174,7 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8 c.progress -= 1; const idle = c.progress <= 0; if (idle) c.progress = 0; - c.unlock(); + c.mu.unlock(io); // the setpoint every burst of progress ends on; `.always` + the // dedupe above means it lands exactly once however the reports raced if (idle) post(si, .always, "{s}: ready", .{specs[si].name}); @@ -1985,6 +2191,12 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8 if (std.mem.eql(u8, method, "experimental/serverStatus")) { const health = str(get(params, "health")) orelse "ok"; const quiescent = if (get(params, "quiescent")) |q| q == .bool and q.bool else false; + c.mu.lockUncancelable(io); + if (c.gen == g) { + c.loading = !quiescent; + c.cond.broadcast(io); + } + c.mu.unlock(io); if (!std.mem.eql(u8, health, "ok")) post(si, .always, "{s}: {s} — {s}", .{ specs[si].name, health, str(get(params, "message")) orelse "" }) else if (quiescent) @@ -1995,12 +2207,12 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8 // ------------------------------------------------------------- introspection -fn status(req: lsp.Req, out: *std.Io.Writer) !void { +fn status(io: std.Io, req: lsp.Req, out: *std.Io.Writer) !void { _ = req; try out.print("\nprotocol servers (lsp-client):\n", .{}); for (&specs, 0..) |*s, i| { const c = &conns[i]; - c.lock(); + c.mu.lockUncancelable(io); const state = c.state; var rootbuf: [256]u8 = undefined; const rootlen = @min(c.root.len, rootbuf.len); @@ -2012,7 +2224,7 @@ fn status(req: lsp.Req, out: *std.Io.Writer) !void { const pull = c.caps.pull; const chier = c.caps.call_hier; const thier = c.caps.type_hier; - c.unlock(); + c.mu.unlock(io); const env_v = if (getenv(s.env)) |v| std.mem.span(v) else null; // Never spawned is not off: the first query of its files starts it. try out.print(" {s:<28} {s}", .{ s.name, if (state == .off) "not started yet (its first query starts it)" else @tagName(state) }); @@ -2069,21 +2281,80 @@ fn atPos(arena: std.mem.Allocator, uri: []const u8, pos: Pos) Err!std.ArrayList( return b; } -fn frame(sock: c_int, body: []const u8, deadline: i64) Err!void { +/// A message to the server from a query, the conn's mutex let go while it is +/// written: the reader goes on taking the server's output meanwhile, which a +/// server may be waiting to write before it reads more of ours. +fn send(c: *Conn, io: std.Io, body: []const u8, deadline: i64) Err!void { + const sock = c.sock; + c.mu.unlock(io); + defer c.mu.lockUncancelable(io); + return frame(c, io, sock, body, deadline); +} + +fn frame(c: *Conn, io: std.Io, sock: c_int, body: []const u8, deadline: i64) Err!void { if (sock < 0) return error.Dead; + c.write_mu.lockUncancelable(io); + defer releaseWrite(c, io, sock); var hdr: [64]u8 = undefined; const h = std.fmt.bufPrint(&hdr, "Content-Length: {d}\r\n\r\n", .{body.len}) catch return error.Protocol; - try sockWrite(sock, h, deadline); - try sockWrite(sock, body, deadline); + var started = false; + try sockWrite(io, sock, h, deadline, &started); + try sockWrite(io, sock, body, deadline, &started); +} + +/// The reader's answer to a server request: written now if the socket is +/// free, else queued for the query writing (bounded; one that does not fit is +/// dropped, and the server's request goes unanswered). +fn answerServer(c: *Conn, io: std.Io, sock: c_int, body: []const u8) void { + var hdr: [64]u8 = undefined; + const h = std.fmt.bufPrint(&hdr, "Content-Length: {d}\r\n\r\n", .{body.len}) catch return; + c.replies_mu.lockUncancelable(io); + const half = &c.replies[c.filling]; + const len = &c.replies_len[c.filling]; + if (len.* + h.len + body.len <= half.len) { + @memcpy(half[len.*..][0..h.len], h); + @memcpy(half[len.* + h.len ..][0..body.len], body); + len.* += h.len + body.len; + } + c.replies_mu.unlock(io); + if (c.write_mu.tryLock()) releaseWrite(c, io, sock); +} + +/// Lets the socket go, first sending what the reader queued meanwhile, and +/// again for what it queues while that goes out. +fn releaseWrite(c: *Conn, io: std.Io, sock: c_int) void { + while (true) { + c.replies_mu.lockUncancelable(io); + const sent = c.filling; + const n = c.replies_len[sent]; + if (n > 0) c.filling ^= 1; + c.replies_mu.unlock(io); + if (n > 0) { + var started = false; + sockWrite(null, sock, c.replies[sent][0..n], nowMs() + reply_ms, &started) catch {}; + c.replies_len[sent] = 0; + } + c.write_mu.unlock(io); + c.replies_mu.lockUncancelable(io); + const more = c.replies_len[c.filling] > 0; + c.replies_mu.unlock(io); + if (!more or !c.write_mu.tryLock()) return; + } } -fn sockWrite(sock: c_int, bytes: []const u8, deadline: i64) Err!void { +/// Writes `bytes`, `started` once any byte of their message is out. Before +/// then, the deadline and a cancel stop it; after, the message is finished +/// however long the server takes to read it (bounded by wedge_ms), since +/// half of one ends the server's reading of every message after it. +fn sockWrite(io: ?std.Io, sock: c_int, bytes: []const u8, deadline: i64, started: *bool) Err!void { var off: usize = 0; while (off < bytes.len) { - const left = deadline - nowMs(); - if (left <= 0) return error.Timeout; + if (!started.*) { + if (deadline - nowMs() <= 0) return error.Timeout; + if (io) |cancel_io| cancel_io.checkCancel() catch return error.Canceled; + } else if (nowMs() - deadline > wedge_ms) return error.Dead; var pfd = [_]libc.pollfd{.{ .fd = sock, .events = POLLOUT, .revents = 0 }}; - const pr = libc.poll(&pfd, 1, @intCast(@min(left, 1000))); + const pr = libc.poll(&pfd, 1, 500); if (pr < 0) { if (libc.errno(pr) == .INTR) continue; return error.Dead; @@ -2096,6 +2367,7 @@ fn sockWrite(sock: c_int, bytes: []const u8, deadline: i64) Err!void { return error.Dead; } if (n == 0) return error.Dead; + started.* = true; off += @intCast(n); } } @@ -2318,7 +2590,7 @@ test "caller trees reverse incoming paths and merge shared callers and sites" { try std.testing.expectEqual(@as(usize, 1), tree.roots.items.len); try std.testing.expectEqual(@as(usize, 4), tree.nodes.items.len); const root = tree.nodes.items[tree.roots.items[0]]; - try std.testing.expectEqualStrings("main", str(get(root.item, "name")).?); + try std.testing.expectEqualStrings("main", root.call.name); try std.testing.expectEqual(@as(usize, 1), root.ranges.items.len); const page = tree.nodes.items[root.children.items[0]]; try std.testing.expectEqual(@as(usize, 2), page.children.items.len); @@ -2355,6 +2627,32 @@ test "caller trees reverse incoming paths and merge shared callers and sites" { try std.testing.expect(recursive.nodes.items[1].cycle); } +test "test callers sort after the rest, each group by place" { + var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena_state.deinit(); + const arena = arena_state.allocator(); + const fixture = try std.json.parseFromSliceLeaky(std.json.Value, arena, "[{\"name\":\"test b\",\"uri\":\"file:///a.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}},{\"name\":\"z\",\"uri\":\"file:///b.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}},{\"name\":\"x\",\"uri\":\"file:///a_test.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}},{\"name\":\"y\",\"uri\":\"file:///a.zig\",\"selectionRange\":{\"start\":{\"line\":5,\"character\":0},\"end\":{\"line\":5,\"character\":1}}}]", .{}); + var tree: CallTree = .{}; + for (items(fixture)) |item| try tree.nodes.append(arena, .{ .call = try callOf(arena, item) }); + var order = [_]usize{ 0, 1, 2, 3 }; + std.mem.sort(usize, &order, @as(*const CallTree, &tree), callerLess); + try std.testing.expectEqualSlices(usize, &.{ 3, 1, 0, 2 }, &order); +} + +test "a mailbox wait is a cancelation point: a canceled worker stops waiting" { + const io = std.testing.io; + var c: Conn = .{}; + const Waiter = struct { + fn run(conn: *Conn, wio: std.Io) error{Canceled}!void { + conn.mu.lockUncancelable(wio); + defer conn.mu.unlock(wio); + while (true) _ = try timedWait(conn, wio, nowMs() + 60_000); + } + }; + var future = try io.concurrent(Waiter.run, .{ &c, io }); + try std.testing.expectError(error.Canceled, future.cancel(io)); +} + test "callstack cross-crate paths remain resolvable without absolute path clutter" { var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena_state.deinit(); @@ -2584,37 +2882,38 @@ test "a language with no server to start says so, not that it did not answer" { defer arena.deinit(); var buf: [256]u8 = undefined; var out: std.Io.Writer = .fixed(&buf); - try query(std.testing.allocator, arena.allocator(), .{ .kind = .hover, .path = "/x/a.py", .source = "", .offset = 0 }, &out); + try query(std.testing.io, std.testing.allocator, arena.allocator(), .{ .kind = .hover, .path = "/x/a.py", .source = "", .offset = 0 }, &out); try std.testing.expectEqualStrings("@none language server for .py not found\n", out.buffered()); } test "Lspinfo before any query says a server is not started yet, not off" { var out: std.Io.Writer.Allocating = .init(std.testing.allocator); defer out.deinit(); - try status(.{ .kind = .status, .path = "", .source = "", .offset = 0 }, &out.writer); + try status(std.testing.io, .{ .kind = .status, .path = "", .source = "", .offset = 0 }, &out.writer); try std.testing.expect(std.mem.indexOf(u8, out.written(), "not started yet (its first query starts it)") != null); } test "a server that failed a moment ago is said to have failed, and when it is tried again, not to be missing" { if (getenv("PARDES_LSP_ZIG") != null) return error.SkipZigTest; + const io = std.testing.io; const si = specFor("/x/a.zig").?; const c = &conns[si]; - c.lock(); + c.mu.lockUncancelable(io); const was = .{ c.state, c.retry_after_ms }; c.state = .dead; c.retry_after_ms = nowMs() + 4500; - c.unlock(); + c.mu.unlock(io); defer { - c.lock(); + c.mu.lockUncancelable(io); c.state = was[0]; c.retry_after_ms = was[1]; - c.unlock(); + c.mu.unlock(io); } var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); var out: std.Io.Writer.Allocating = .init(std.testing.allocator); defer out.deinit(); - try query(std.testing.allocator, arena.allocator(), .{ .kind = .hover, .path = "/x/a.zig", .source = "", .offset = 0 }, &out.writer); + try query(std.testing.io, std.testing.allocator, arena.allocator(), .{ .kind = .hover, .path = "/x/a.zig", .source = "", .offset = 0 }, &out.writer); try std.testing.expect(std.mem.indexOf(u8, out.written(), "zls failed recently; retry in 5s") != null); } diff --git a/src/macos.zig b/src/macos.zig index 50fa04fa..5d3dfec4 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -44,7 +44,11 @@ fn hostWatchFile(pane: u8, generation: u32, path: ?[*]const u8, path_len: usize) const TIOCSWINSZ: c_int = @bitCast(@as(u32, if (@hasDecl(posix.T, "IOCSWINSZ")) posix.T.IOCSWINSZ else 0x80087467)); -pub const std_options: std.Options = .{ .logFn = logFn }; +// Every thread's alternate signal stack is a threadlocal that libc zeroes as +// the thread starts, so std's 256 KiB made each 9P connection's thread cost +// that much resident. A stack-overflow segfault on a thread still printed its +// whole symbolized trace from 32 KiB up (16 KiB double-faulted): 64 KiB. +pub const std_options: std.Options = .{ .logFn = logFn, .signal_stack_size = 64 * 1024 }; fn logFn( comptime level: std.log.Level, @@ -2419,7 +2423,7 @@ fn pipeWorker(st: *State, job: *selection_pipe.Job) anyerror!void { } fn lspWorker(st: *State, job: *host_io.Lsp.Job) anyerror!void { - host_io.Lsp.work(st.gpa, job, st, deliverLspRows); + host_io.Lsp.work(st.io, st.gpa, job, st, deliverLspRows); } fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { @@ -2582,12 +2586,11 @@ fn reap(st: *State, pane: u8) void { } fn readPty(st: *State, io: std.Io, pty: std.Io.File, id: u8, gen: u32) anyerror!void { - var read_buf: [0x10000]u8 = undefined; - var reader = pty.readerStreaming(io, &read_buf); + // One buffer, read into directly: a Reader around it only added a second + // (and in a safe build, filled both as `undefined` on every start). + var buf: [0x10000]u8 = undefined; while (true) { - var buf: [0x10000]u8 = undefined; - var vec = [_][]u8{&buf}; - const n = reader.interface.readVec(&vec) catch break; + const n = pty.readStreaming(io, &.{&buf}) catch break; if (n == 0) break; const bytes = st.gpa.dupe(u8, buf[0..n]) catch break; st.inbox.pushOutput(st.gpa, io, .{ .pane = id, .gen = gen, .bytes = bytes }) catch break; diff --git a/src/main.zig b/src/main.zig index 64614daa..ea2db917 100644 --- a/src/main.zig +++ b/src/main.zig @@ -16,7 +16,11 @@ pub const std_options_debug_io: std.Io = if (is_emscripten) else std_options_debug_threaded_io.?.io(); -pub const std_options: std.Options = .{ .logFn = logFn }; +// Every thread's alternate signal stack is a threadlocal that libc zeroes as +// the thread starts, so std's 256 KiB made each 9P connection's thread cost +// that much resident. A stack-overflow segfault on a thread still printed its +// whole symbolized trace from 32 KiB up (16 KiB double-faulted): 64 KiB. +pub const std_options: std.Options = .{ .logFn = logFn, .signal_stack_size = 64 * 1024 }; fn logFn( comptime level: std.log.Level, @@ -328,7 +332,11 @@ fn webMain() !void { // malloc otherwise. Kept on purpose: glibc's malloc was measured at least as // fast as std.heap.smp_allocator on every scenario and faster on MuPDF's // page-sized rasters. macos.zig makes the same choice for itself. -fn nativeMain(init: std.process.Init) !void { +fn nativeMain(std_init: std.process.Init) !void { + var init = std_init; + // Zig 0.17's std.start gives a safe build a SafeAllocator over the page + // allocator, not libc's malloc: hand the shells malloc, as above. + if (builtin.mode != .debug) init.gpa = std.heap.c_allocator; var stderr_probe: [64]u8 = undefined; _ = std.debug.lockStderr(&stderr_probe); std.debug.unlockStderr(); diff --git a/src/memory.zig b/src/memory.zig index 48403c55..853d641a 100644 --- a/src/memory.zig +++ b/src/memory.zig @@ -16,6 +16,10 @@ pub const limits = struct { pub const pending_write_cap: usize = if (board) 0 else 4 * MiB; pub const wrap_rows = if (board) 128 else 256; pub const undo_max = if (board) 16 else 256; + /// The text a file's undo and redo steps hold together, kept by dropping + /// the oldest; the newest step always stays, so the bound per file is + /// this plus one snapshot (at most max_file_bytes). + pub const undo_bytes: usize = if (board) 64 * KiB else 64 * MiB; pub const message_log = if (board) 16 else 128; /// ranges one selection holds; helix has no limit, and a `%s` over a /// file's words wants hundreds @@ -23,6 +27,19 @@ pub const limits = struct { /// /log's ring, which records whether or not anyone reads it. pub const log_bytes: usize = if (board) 4 * KiB else 64 * KiB; pub const max_tag_tail: usize = if (board) 512 else 4096; + /// Pixels one image may decode to (RGBA, 4 bytes each): checked from its + /// header before stb_image allocates them. + pub const image_decode_pixels: usize = if (board) 0 else 64 * 1024 * 1024; + /// What the core's per-step scratch arena and the frame arena keep + /// between steps: a step that needed more (a huge Callstack's sources) + /// gives the rest back instead of holding its high-water mark. + pub const scratch_kept: usize = if (board) 4 * KiB else 16 * MiB; + pub const frame_kept: usize = if (board) 4 * KiB else 16 * MiB; + /// The other open files one language query copies for its server, in + /// bytes: past it the rest are left out (the asked file always goes). + pub const lsp_snapshot_bytes: usize = if (board) 0 else 64 * MiB; + /// One paste, from any shell: past it, the rest is left out and said. + pub const max_paste_bytes: usize = if (board) 16 * KiB else 4 * MiB; pub const host_path_cap: usize = if (board) 0 else 4095; /// -Dembed-sources: the editor serves its own sources under /src. pub const embedded_sources = config.embed_sources; @@ -34,6 +51,10 @@ pub const limits = struct { pub const tree_sitter = if (board) 0 else if (reduced_target) 4 * MiB else 16 * MiB; pub const image = if (board) 0 else if (reduced_target) 64 * KiB else 32 * MiB; pub const pdf = if (board) 0 else if (reduced_target or !config.mupdf) 64 * KiB else 64 * MiB; + /// One language query's working memory (its parsed answers, the rows + /// it writes): a hard bound, nothing spills. Callstack walks until it + /// would leave less than its rendering reserve. + pub const lsp_query = if (board or reduced_target) 0 else 256 * MiB; }; }; @@ -96,6 +117,82 @@ var frame_buffer: [limits.arena.frame]u8 = @splat(0); var tree_sitter_buffer: [limits.arena.tree_sitter]u8 = @splat(0); var image_buffer: [limits.arena.image]u8 = @splat(0); var pdf_buffer: [limits.arena.pdf]u8 = @splat(0); +var lsp_query_buffer: [limits.arena.lsp_query]u8 align(std.heap.page_size_max) = @splat(0); +var lsp_query_busy: std.atomic.Value(bool) = .init(false); +/// What a query may leave resident of the region (giveLspQuery). +const lsp_query_kept = 8 * MiB; + +/// The language query region, for the one query running; null while another +/// holds it (the shells cancel and join a query before starting the next). +pub fn takeLspQuery() ?[]u8 { + if (lsp_query_busy.swap(true, .acquire)) return null; + return &lsp_query_buffer; +} + +/// Gives the region back after a query. A big one's touched pages go back to +/// the system (zeroed again on their next touch, as .bss is), so one huge +/// answer does not stay resident; a small one's stay, for the next query to +/// reuse without faulting them in again. +pub fn giveLspQuery(used: usize) void { + if (comptime builtin.os.tag == .linux) if (used > lsp_query_kept) { + const page = std.heap.pageSize(); + const len = std.mem.alignForward(usize, @min(used, lsp_query_buffer.len), page); + std.posix.madvise(@as([*]align(std.heap.page_size_min) u8, @ptrCast(&lsp_query_buffer)), len, std.posix.MADV.DONTNEED) catch {}; + }; + lsp_query_busy.store(false, .release); +} + +/// `n` bytes as they come: `Allocator.alloc` fills a safe build's new memory +/// as `undefined` (0xAA), which writes, and makes resident, all of a buffer +/// sized for the worst case: every language query wrote its 51 MB answer +/// room, every search its 16 MiB. For buffers written before they are read. +pub fn unfilled(allocator: Allocator, n: usize) Allocator.Error![]u8 { + if (n == 0) return allocator.alloc(u8, 0); + const ptr = allocator.rawAlloc(n, .of(u8), @returnAddress()) orelse return error.OutOfMemory; + return ptr[0..n]; +} + +/// `bytes` copied into memory `unfilled` takes: a text-sized copy (an undo +/// snapshot) is written once, not filled first. +pub fn dupeUnfilled(allocator: Allocator, bytes: []const u8) Allocator.Error![]u8 { + const out = try unfilled(allocator, bytes.len); + @memcpy(out, bytes); + return out; +} + +/// Frees without the safe-build overwrite `free` does, for text-sized buffers +/// freed on every edit. +pub fn freeUnfilled(allocator: Allocator, bytes: []const u8) void { + if (bytes.len > 0) allocator.rawFree(@constCast(bytes), .of(u8), @returnAddress()); +} + +/// Gives libc's free memory back to the system, inside its heap too: after a +/// big result is formatted or a big pane torn down, glibc keeps what was freed +/// (a V8 Callstack page left 21 MiB free in the heap; this returned 20). +/// Walks the heap, so for rare, large events only. +pub fn trim() void { + if (comptime builtin.os.tag != .linux or builtin.abi.isMusl() or !builtin.link_libc) return; + const glibc = struct { + extern "c" fn malloc_trim(pad: usize) c_int; + }; + _ = glibc.malloc_trim(0); +} + +/// Marks a big region that is touched sparsely as not for huge pages. With +/// transparent huge pages on `always` (a common default), touching one byte +/// of a 2 MiB-aligned stretch makes all 2 MiB resident: the 9P runner's +/// slots and these arenas' first pages held 45 MiB of an idle session's +/// 71 MiB that way. Only the region given, never the whole process (a prctl +/// would reach every shell and server pardes starts). +pub fn sparse(region: []u8) void { + if (comptime builtin.os.tag != .linux) return; + const page = std.heap.pageSize(); + const start = std.mem.alignForward(usize, @intFromPtr(region.ptr), page); + const end = std.mem.alignBackward(usize, @intFromPtr(region.ptr) + region.len, page); + if (end <= start) return; + std.posix.madvise(@ptrFromInt(start), end - start, std.posix.MADV.NOHUGEPAGE) catch {}; +} + var pardes_fallback: SharedStackFallback = .{ .buffer = &pardes_buffer }; var frame_fallback: SharedStackFallback = .{ .buffer = &frame_buffer }; var tree_sitter_fallback: SharedStackFallback = .{ .buffer = &tree_sitter_buffer }; @@ -118,6 +215,7 @@ pub fn init(fallback: Allocator) Allocators { // a Debug build `fallback` is std.process.Init's gpa, a DebugAllocator // already, and a second one checked every block twice. const backing = if (debug_enabled) std.heap.page_allocator else fallback; + inline for (.{ &pardes_buffer, &frame_buffer, &tree_sitter_buffer, &image_buffer, &pdf_buffer, &lsp_query_buffer }) |buffer| sparse(buffer); pardes_fallback.fallback_allocator = backing; frame_fallback.fallback_allocator = backing; tree_sitter_fallback.fallback_allocator = backing; diff --git a/src/mini.zig b/src/mini.zig index ce291c99..8d8ea0b2 100644 --- a/src/mini.zig +++ b/src/mini.zig @@ -313,7 +313,21 @@ test "Mini publishes only complete snapshots and content replacement drops metad const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; var path_buf: [4096]u8 = undefined; const path = try std.fmt.bufPrint(&path_buf, "{s}/mini.txt", .{dir}); - try std.testing.checkAllAllocationFailures(std.testing.allocator, Case.run, .{path}); + // Growth that never resizes in place: whether a resize fits where it is + // depends on the heap's layout, and with it how many allocations a run + // makes, which checkAllAllocationFailures needs to be the same each run. + const NoResize = struct { + fn resize(_: *anyopaque, _: []u8, _: std.mem.Alignment, _: usize, _: usize) bool { + return false; + } + fn remap(_: *anyopaque, _: []u8, _: std.mem.Alignment, _: usize, _: usize) ?[*]u8 { + return null; + } + }; + const testing_vtable = std.testing.allocator.vtable; + const no_resize_vtable: std.mem.Allocator.VTable = .{ .alloc = testing_vtable.alloc, .resize = NoResize.resize, .remap = NoResize.remap, .free = testing_vtable.free }; + const backing: std.mem.Allocator = .{ .ptr = std.testing.allocator.ptr, .vtable = &no_resize_vtable }; + try std.testing.checkAllAllocationFailures(backing, Case.run, .{path}); } test "Mini refuses a file that is no UTF-8, before its decoding could panic" { diff --git a/src/modal.zig b/src/modal.zig index 82f1a3a0..ad06833f 100644 --- a/src/modal.zig +++ b/src/modal.zig @@ -989,13 +989,77 @@ pub fn lineCount(content: []const u8) usize { // byte offset of the start of line `row` (0-based). row may == lineCount() // (== content.len, the end). pub fn lineStartOffset(content: []const u8, row: usize) usize { + // Newlines counted 32 bytes at a time, the last block byte by byte: one + // indexOfScalarPos per line was a call per line, 7000 of them to find + // line 7000 of a source, for every cursor move, selection and language + // request there. + const block = 32; + const Block = @Vector(block, u8); var off: usize = 0; - var r: usize = 0; - while (r < row) : (r += 1) { - const nl = std.mem.indexOfScalarPos(u8, content, off, '\n') orelse return content.len; - off = nl + 1; + var left = row; + if (left == 0) return 0; + while (off + block <= content.len) : (off += block) { + const chunk: Block = content[off..][0..block].*; + const found: usize = @popCount(@as(u32, @bitCast(chunk == @as(Block, @splat('\n'))))); + if (found >= left) break; + left -= found; + } + while (off < content.len) : (off += 1) if (content[off] == '\n') { + left -= 1; + if (left == 0) return off + 1; + }; + return content.len; +} + +test "lineStartOffset by blocks and offsetAt agree with a line at a time" { + var prng: std.Random.DefaultPrng = .init(0x9e3779b9); + const random = prng.random(); + var buf: [300]u8 = undefined; + for (0..2000) |_| { + const len = random.uintLessThan(usize, buf.len); + for (buf[0..len]) |*b| b.* = if (random.uintLessThan(u8, 4) == 0) '\n' else 'a'; + const text = buf[0..len]; + for (0..len + 3) |row| { + var off: usize = 0; + var r: usize = 0; + const want = while (r < row) : (r += 1) { + off = (std.mem.indexOfScalarPos(u8, text, off, '\n') orelse break text.len) + 1; + } else off; + try std.testing.expectEqual(want, lineStartOffset(text, row)); + const cursor: Cursor = .{ .row = row, .col = random.uintLessThan(usize, 6) }; + const clamped = lineStartOffset(text, @min(row, cursorLineCount(text) - 1)); + const end = std.mem.indexOfScalarPos(u8, text, clamped, '\n') orelse text.len; + try std.testing.expectEqual(clamped + graphemeStart(text[clamped..end], @min(cursor.col, end - clamped)), offsetAt(text, cursor)); + // lineSpan, as it was: a line at a time, null past the last. + const span: ?LineSpan = span: { + if (text.len == 0) break :span null; + var start: usize = 0; + for (0..row) |_| start = (std.mem.indexOfScalarPos(u8, text, start, '\n') orelse break :span null) + 1; + if (start == text.len and !(row == 0 or text[text.len - 1] == '\n')) break :span null; + break :span .{ .start = start, .end = std.mem.indexOfScalarPos(u8, text, start, '\n') orelse text.len }; + }; + try std.testing.expectEqual(span, lineSpan(text, row)); + // rangeBytes, as it was: each line looked for from the top, twice. + const other: Cursor = .{ .row = random.uintLessThan(usize, len + 3), .col = random.uintLessThan(usize, 6) }; + var lo = cursor; + var hi = other; + if (hi.row < lo.row or (hi.row == lo.row and hi.col < lo.col)) std.mem.swap(Cursor, &lo, &hi); + const lo_line = lineSlice(text, lo.row); + const hi_line = lineSlice(text, hi.row); + const s0 = lineStartOffset(text, lo.row) + graphemeStart(lo_line, @min(lo.col, lo_line.len)); + var e0 = lineStartOffset(text, hi.row) + graphemeStart(hi_line, @min(hi.col, hi_line.len)); + if (e0 < text.len) e0 = nextGrapheme(text, e0); + const got = rangeBytes(text, cursor, other); + try std.testing.expectEqual(s0, got.s); + try std.testing.expectEqual(@max(s0, e0), got.e); + } } - return off; +} + +/// The line that starts at `start` (lineSlice's, once its start is known). +fn lineFrom(content: []const u8, start: usize) []const u8 { + if (start >= content.len) return ""; + return content[start .. std.mem.indexOfScalarPos(u8, content, start, '\n') orelse content.len]; } // the text of line `row` (no terminator), a slice into `content`. @@ -1011,23 +1075,29 @@ pub fn lineSlice(content: []const u8, row: usize) []const u8 { /// A bounded scan of one line; null if the row does not exist. pub const LineSpan = struct { start: usize, end: usize }; +/// Just past the `row`-th newline; null if the text has fewer. +pub fn lineStartExact(content: []const u8, row: usize) ?usize { + const at = lineStartOffset(content, row); + // At the end: either the row-th newline was the last byte, or there were fewer. + if (at == content.len and std.mem.count(u8, content, "\n") < row) return null; + return at; +} + pub fn lineSpan(content: []const u8, row: usize) ?LineSpan { // Match lineCount: empty content has no lines; a trailing newline adds one. if (content.len == 0) return null; - var start: usize = 0; - var r: usize = 0; - while (r < row) : (r += 1) { - const nl = std.mem.indexOfScalarPos(u8, content, start, '\n') orelse return null; - start = nl + 1; - } - if (start > content.len) return null; + const start = lineStartExact(content, row) orelse return null; if (start == content.len and !(row == 0 or content.len == 0 or content[content.len - 1] == '\n')) return null; const end = std.mem.indexOfScalarPos(u8, content, start, '\n') orelse content.len; return .{ .start = start, .end = end }; } -fn spliceAlloc(alloc: std.mem.Allocator, content: []const u8, start: usize, end: usize, replacement: []const u8) ![]u8 { - const out = try alloc.alloc(u8, content.len - (end - start) + replacement.len); +pub fn spliceAlloc(alloc: std.mem.Allocator, content: []const u8, start: usize, end: usize, replacement: []const u8) ![]u8 { + // Raw: every byte is written below, where `alloc` fills a safe build's + // new memory first, a whole file's worth per keystroke. + const len = content.len - (end - start) + replacement.len; + if (len == 0) return alloc.alloc(u8, 0); + const out = (alloc.rawAlloc(len, .of(u8), @returnAddress()) orelse return error.OutOfMemory)[0..len]; @memcpy(out[0..start], content[0..start]); @memcpy(out[start..][0..replacement.len], replacement); @memcpy(out[start + replacement.len ..], content[end..]); @@ -1036,13 +1106,18 @@ fn spliceAlloc(alloc: std.mem.Allocator, content: []const u8, start: usize, end: /// insert `text` at (row, col). col is clamped to the line length. pub fn insertAt(alloc: std.mem.Allocator, content: []const u8, c: Cursor, text: []const u8) ![]u8 { + const off = insertOffset(content, c); + return spliceAlloc(alloc, content, off, off, text); +} + +/// Where insertAt puts its text: col clamped to the line, no further. +pub fn insertOffset(content: []const u8, c: Cursor) usize { // Only an out-of-range row requires counting the whole document. const span = lineSpan(content, c.row) orelse blk: { const last = lineCount(content) -| 1; break :blk lineSpan(content, last) orelse LineSpan{ .start = content.len, .end = content.len }; }; - const off = span.start + @min(c.col, span.end - span.start); - return spliceAlloc(alloc, content, off, off, text); + return span.start + @min(c.col, span.end - span.start); } // delete the character at (row, col). no-op if col is past the line end. @@ -1105,12 +1180,15 @@ fn rangeBytes(content: []const u8, a: Cursor, b: Cursor) struct { s: usize, e: u lo = b; hi = a; } - const lo_line = lineSlice(content, lo.row); - const hi_line = lineSlice(content, hi.row); + // Each line found once, the second from the first. + const lo_start = lineStartOffset(content, lo.row); + const hi_start = if (hi.row == lo.row) lo_start else lo_start + lineStartOffset(content[lo_start..], hi.row - lo.row); + const lo_line = lineFrom(content, lo_start); + const hi_line = lineFrom(content, hi_start); const lo_col = graphemeStart(lo_line, @min(lo.col, lo_line.len)); const hi_col = graphemeStart(hi_line, @min(hi.col, hi_line.len)); - const s = lineStartOffset(content, lo.row) + lo_col; - var e = lineStartOffset(content, hi.row) + hi_col; + const s = lo_start + lo_col; + var e = hi_start + hi_col; if (e < content.len) e = nextGrapheme(content, e); // include the grapheme under the head return .{ .s = s, .e = @max(s, e) }; } @@ -1138,12 +1216,12 @@ pub fn clearLine(alloc: std.mem.Allocator, content: []const u8, row: usize) ![]u // paste `text` as a new line BELOW `row`. Multiline `text` becomes several lines. pub fn pasteLineBelow(alloc: std.mem.Allocator, content: []const u8, row: usize, text: []const u8) ![]u8 { - const n = lineCount(content); - const off = if (row + 1 < n) lineStartOffset(content, row + 1) else content.len; + // The next line's start, or the end: no whole-text line count to know which. + const off = lineStartExact(content, row + 1) orelse content.len; const suffix_newline = off < content.len or (content.len > 0 and content[content.len - 1] == '\n'); const prefix_newline = !suffix_newline and off > 0; const extra: usize = @intFromBool(suffix_newline or prefix_newline); - const out = try alloc.alloc(u8, content.len + text.len + extra); + const out = try @import("memory.zig").unfilled(alloc, content.len + text.len + extra); var write: usize = 0; @memcpy(out[write..][0..off], content[0..off]); write += off; @@ -1393,8 +1471,10 @@ pub fn positionAt(text: []const u8, off: usize) Cursor { /// (row, col) -> clamped gap offset; col == line length lands ON the '\n' pub fn offsetAt(text: []const u8, c: Cursor) usize { - const row = @min(c.row, cursorLineCount(text) - 1); - const s = lineStartOffset(text, row); + // A row past the last is the last: found back from the end, where + // counting every line of the text to clamp it cost a pass over all of it. + var s = lineStartOffset(text, c.row); + if (s == text.len) s = if (std.mem.lastIndexOfScalar(u8, text, '\n')) |nl| nl + 1 else 0; // lineEndOffset(text, row) inlined: it starts by walking to `row` again, // and we are already standing there const e = std.mem.indexOfScalarPos(u8, text, s, '\n') orelse text.len; @@ -1783,15 +1863,6 @@ test "selection textobjects distinguish words and paragraph boundaries" { try std.testing.expectEqual(@as(usize, 7), r.head); } -/// leading-whitespace visual width (tab -> next multiple of INDENT_W) -pub fn indentWidth(line: []const u8) usize { - var w: usize = 0; - for (line) |ch| { - if (ch == ' ') w += 1 else if (ch == '\t') w = (w / INDENT_W + 1) * INDENT_W else break; - } - return w; -} - /// A line's indent as it is, tabs and spaces both, as acme's autoindent /// copies it: what o/O/insert-newline give the new line. pub fn indentText(line: []const u8) []const u8 { diff --git a/src/mouse.zig b/src/mouse.zig index 76c65a16..eeb987b2 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -1114,7 +1114,9 @@ fn mirrorTtySelection(p: *Pardes, pane: *Pane) void { /// second selects what edit.doubleClickRange says, from the caret the click /// left. A plain click still puts the caret on its cell. fn doubleClick(p: *Pardes, id: usize, t: *panes.Text, on_tag: bool, row: i32, col: i32) void { - const now = clockMs(); + // The host's clock, which every host keeps (a libc clock is no clock on + // the web or the board) and a test's virtual one replaces. + const now: i64 = @intCast(p.now_ns / std.time.ns_per_ms); const prev = p.last_click; p.last_click = .{ .id = id, .on_tag = on_tag, .row = row, .col = col, .ms = now }; const c = prev orelse return; @@ -1160,13 +1162,6 @@ fn sweepPoint(p: *Pardes, pane: *panes.Pane, r: i32, c: i32) ?modal.Cursor { return .{ .row = @intCast(w.line), .col = @intCast(@max(0, col)) }; } -fn clockMs() i64 { - if (comptime !pardes.hosted) return 0; - var ts: std.c.timespec = undefined; - if (std.c.clock_gettime(.MONOTONIC, &ts) != 0) return 0; - return @as(i64, ts.sec) * 1000 + @divTrunc(@as(i64, ts.nsec), 1_000_000); -} - fn dragRelease(p: *Pardes, button: Mouse.Button) void { // A different button's release belongs to a chord or another host // stream; only the button which began this gesture may end it. diff --git a/src/ninep/addr.zig b/src/ninep/addr.zig index 86d2d49e..7753c94d 100644 --- a/src/ninep/addr.zig +++ b/src/ninep/addr.zig @@ -239,21 +239,15 @@ pub const Addr = struct { k = @intFromBool(a.text[p] == '\n'); p += 1; } - while (k < n) { - if (p >= nc) { + if (k < n) { + p = nthLineAfter(a.text, p, n - k) orelse { a.err = e_range; return null; - } - if (a.text[p] == '\n') k += 1; - p += 1; + }; } p1 = p; } - while (p < nc) { - const ch = a.text[p]; - p += 1; - if (ch == '\n') break; - } + p = if (std.mem.indexOfScalarPos(u8, a.text, p, '\n')) |nl| nl + 1 else nc; return .{ .q0 = clip(p1), .q1 = clip(p) }; } @@ -678,3 +672,31 @@ test "a line just past the last newline is an empty one there, as sam's lineaddr try testing.expectEqual(Status.ok, th.wr(p, Node.of(th.serialOf(p), .ctl), "Edit 1i/header/\n").reply.status); try testing.expectEqualStrings("header", p.panes[0].?.file.?.content); } + +/// Just past the `count`-th newline at or after `from`; null if the text has +/// fewer. Counted a block at a time (modal.lineStartOffset), where a byte at +/// a time made `echo 7000 > addr` walk every byte above line 7000. +fn nthLineAfter(text: []const u8, from: usize, count: usize) ?usize { + return from + (modal.lineStartExact(text[from..], count) orelse return null); +} + +test "nthLineAfter finds what a byte at a time finds" { + var prng: std.Random.DefaultPrng = .init(0x51ab); + const random = prng.random(); + var buf: [200]u8 = undefined; + for (0..3000) |_| { + const len = random.uintLessThan(usize, buf.len); + for (buf[0..len]) |*b| b.* = if (random.uintLessThan(u8, 3) == 0) '\n' else 'x'; + const text = buf[0..len]; + const from = random.uintLessThan(usize, len + 1); + const count = 1 + random.uintLessThan(usize, 40); + var p = from; + var k: usize = 0; + const want: ?usize = while (k < count) { + if (p >= text.len) break null; + if (text[p] == '\n') k += 1; + p += 1; + } else p; + try std.testing.expectEqual(want, nthLineAfter(text, from, count)); + } +} diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 733d9c8e..480c1dc7 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -4684,6 +4684,10 @@ test "what a /pager open writes after its directory is the text: plain in the bo // An edit drops the colours (pager.zig's ponytail). try testing.expectEqual(Status.ok, call(p, .{ .tag = 6, .op = .write, .node = Node.of(pn.serial, .body), .data = "more\n" }).reply.status); try testing.expectEqual(@as(usize, 0), f.pager_spans.len); + // Undone back to the paged text: its colours come back with it. + panes.File.undo(p, pn); + try testing.expectEqualStrings("red /a\nNext\n", f.content); + try testing.expectEqual(@as(usize, 2), f.pager_spans.len); // PagerColor off, and an open closed with no read: the same text, plain, // in at the editor's next step (a close through a mount never waits). diff --git a/src/panes.zig b/src/panes.zig index 7ce85419..b535993e 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -133,6 +133,9 @@ pub const Pane = struct { /// Its pty is still open: a job the command left in the background may /// be printing to it, and a reuse would hang it up. command_pty: bool = false, + /// Its child pages as a terminal's does (`pardes -`, a +Pager), not + /// through cat: Look's `git show` of a commit (look.lookCommit). + command_pages: bool = false, /// The shell a terminal runs when it is not the configured one (`Tty /// fish`), owned. shell: ?[]u8 = null, diff --git a/src/pardes.zig b/src/pardes.zig index aaa9d177..c2a3ba21 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -478,6 +478,7 @@ pub const Loc = struct { }; test { + _ = @import("modal.zig"); _ = @import("tagline.zig"); _ = @import("hunks.zig"); _ = @import("builtins_json.zig"); @@ -4645,6 +4646,10 @@ pub const Pardes = struct { /// The last query asked (Lspwhy explains it): its kind, argument, and /// the pane and place it was asked at. lsp_last: ?struct { kind: lsp.Kind, arg: Effect.Buf(128), serial: u32, offset: u32 } = null, + /// The Callstack page `+Callstack` shows stopped short: its file's pane, + /// the place it was asked at, and the direct caller the next page starts + /// at. Callstack run in `+Callstack` asks for it. + callstack_more: ?struct { serial: u32, offset: u32, next: Effect.Buf(128) } = null, lsp_wait: ?struct { id: u32, kind: lsp.Kind, @@ -5168,10 +5173,13 @@ pub const Pardes = struct { } fn reapPanes(p: *Pardes) void { + var reaped = false; for (&p.retired_panes.previous) |*slot| if (slot.*) |pane| { p.teardownPane(pane); slot.* = null; + reaped = true; }; + if (reaped) memory.trim(); p.retired_panes.previous = p.retired_panes.current; p.retired_panes.current = @splat(null); } @@ -5544,7 +5552,7 @@ pub const Pardes = struct { p.invalidate(); look.invalidateLookHover(p, id); const applied = p.applyWatchedFileChanged(id, bytes); - _ = p.scratch.reset(.retain_capacity); + _ = p.scratch.reset(.{ .retain_with_limit = memory.limits.scratch_kept }); p.sync(); return applied; } @@ -6000,7 +6008,7 @@ pub const Pardes = struct { // A pty streaming into an unwatched session spent most of its time // drawing what nobody looked at. if (v.watched) |f| if (!f(h.ctx)) return; - _ = p.frame_arena.reset(.retain_capacity); + _ = p.frame_arena.reset(.{ .retain_with_limit = memory.limits.frame_kept }); const surface = try p.render(p.frame_arena.allocator()); if (v.present) |f| f(h.ctx, surface); // Settled before post_present: a shell ticks there, and what that @@ -6202,7 +6210,7 @@ pub const Pardes = struct { look.advanceLookHover(p); }, } - _ = p.scratch.reset(.retain_capacity); + _ = p.scratch.reset(.{ .retain_with_limit = memory.limits.scratch_kept }); p.sync(); p.fsReport(); ctlfs.events.announce(p); @@ -6777,6 +6785,19 @@ pub const Pardes = struct { pub const SavePath = Effect.Buf(effect_path_cap); pub fn lspRequest(p: *Pardes, asked: usize, kind: lsp.Kind, arg_in: []const u8) void { + p.lspRequestAt(asked, kind, arg_in, null); + } + + /// The next page of the Callstack `+Callstack` shows, asked where the + /// first was. + pub fn callstackMore(p: *Pardes, from: usize) void { + const more = p.callstack_more orelse return p.reportFailure(from, "Callstack: this is the last page; run it in a file to start again"); + const id = p.paneBySerial(more.serial) orelse return p.reportFailure(from, "Callstack: the file it was asked in is closed"); + p.lspRequestAt(id, .callstack, more.next.slice(), more.offset); + } + + /// `lspRequest` at byte `at` of the pane's file rather than its cursor. + fn lspRequestAt(p: *Pardes, asked: usize, kind: lsp.Kind, arg_in: []const u8, at_byte: ?u32) void { var arg = arg_in; if (!normal.multiOnce(p)) return; // one question per keystroke, from the primary // Lspwhy narrates the last query where it was asked -- that pane's @@ -6800,7 +6821,7 @@ pub const Pardes = struct { (!zig_buf and std.mem.indexOfAny(u8, arg, " \t\r\n\"\\") != null)) return p.reportFailure(id, std.fmt.bufPrint(&said, "Rename: {s} is no name", .{arg}) catch "Rename: no name"); } - var off = if (pane.file) |f| modal.offsetAt(f.content, .{ + var off = if (at_byte) |at| @min(at, if (pane.file) |f| f.content.len else 0) else if (pane.file) |f| modal.offsetAt(f.content, .{ .row = @intCast(@max(0, pane.body.cur_row)), .col = @intCast(@max(0, pane.body.cur_col)), }) else 0; @@ -7089,13 +7110,24 @@ pub const Pardes = struct { } const dir = if (pane.file) |f| panes.Dir.ofName(f.path) else pane.cwdSlice(); - const content = p.gpa.dupe(u8, rows) catch return; + // A Callstack page that stops short ends in its marker: said as a + // row naming the word that shows the next page, and kept. + if (w.kind == .callstack) p.callstack_more = null; + const marker = if (w.kind == .callstack) std.mem.lastIndexOf(u8, rows, lsp.callstack_more) else null; + const content = if (marker) |at| more: { + var fields = std.mem.tokenizeAny(u8, rows[at + lsp.callstack_more.len ..], " \n"); + const next = fields.next() orelse ""; + const shown = fields.next() orelse "?"; + if (p.lsp_last) |last| p.callstack_more = .{ .serial = w.serial, .offset = last.offset, .next = .from(next) }; + break :more std.fmt.allocPrint(p.gpa, "{s}… {s} callers on this page, more after them: Callstack here shows the next page\n", .{ rows[0..at], shown }) catch return; + } else p.gpa.dupe(u8, rows) catch return; const serial_before = p.next_serial; p.fs.refilled = null; panes.Output.fillResults(p, w.pane, dir, from, w.arg.slice(), content, null) catch |err| { p.reportError(w.pane, "language response", err); return; }; + if (rows.len >= 1 << 20) memory.trim(); // What a 9P write that asked it answers: the pane the rows are in // (a multi-file Rename's preview, Lspinfo's +Lsp), made or filled // again, not the pane it was asked from. diff --git a/src/regexp.zig b/src/regexp.zig index 4cbca57e..6d281859 100644 --- a/src/regexp.zig +++ b/src/regexp.zig @@ -283,7 +283,10 @@ pub const Regex = struct { /// its top level and every branch starts with `^`; null when it is not /// one, or no branch does. fn anchoredAlternation(pat: []const u8, out: *[5 * max_ops + 2]u8) error{ Bad, Mixed }!?[]const u8 { - var bars: [16]usize = undefined; + // No rewrite is longer than `out`, so neither is any pattern it + // takes, nor its count of bars. + if (pat.len > out.len) return error.Bad; + var bars: [5 * max_ops + 2]u16 = undefined; var n: usize = 0; var depth: usize = 0; var in_class = false; @@ -301,8 +304,7 @@ pub const Regex = struct { } else if (c == ')') { depth -|= 1; } else if (c == '|' and depth == 0) { - if (n == bars.len) return error.Bad; - bars[n] = i; + bars[n] = @intCast(i); n += 1; } } @@ -518,6 +520,9 @@ test "a ^ after | anchors that branch: ^def|^ finds a line that starts either w try std.testing.expectError(error.Mixed, Regex.compile("^def|x")); var plain = try Regex.compile("a|b"); try std.testing.expectEqual(@as(usize, 7), (try plain.find(text, 0, text.len, text.len)).?.start); + // Seventeen branches and more are a pattern like any other. + var many = try Regex.compile("q|r|s|t|u|v|w|x|y|z|k|l|m|n|o|p|j|b"); + try std.testing.expectEqual(@as(usize, 0), (try many.find(text, 0, text.len, text.len)).?.start); } test "a quoted backslash before n is no newline, and a class ends where mvzr ends it" { diff --git a/src/sam_edit.zig b/src/sam_edit.zig index f6253c89..94b76816 100644 --- a/src/sam_edit.zig +++ b/src/sam_edit.zig @@ -259,6 +259,10 @@ fn fail(why: *Why, comptime fmt: []const u8, args: anytype) Failure { // ---- parsing: edit.c ---- +/// Commands one Edit may nest, loops (`x`, `y`, `g`, `v`, `X`, `Y`) and +/// braces together: each level is a stack frame holding a compiled regex. +pub const max_nest = 32; + const Parser = struct { arena: std.mem.Allocator, src: []const u8, @@ -271,6 +275,9 @@ const Parser = struct { open_text: bool = false, /// The address being read had a `,` after a `"re"`. file_comma: bool = false, + /// Commands open around the one being read, braces and loops alike: + /// each is a frame of `parse` and later of `execIn`, so it is bounded. + depth: u8 = 0, why: *Why, fn getch(ps: *Parser) ?u8 { @@ -460,6 +467,9 @@ const Parser = struct { /// edit.c:467, parsecmd. fn parse(ps: *Parser, nest: u32) Failure!?*Cmd { + if (ps.depth >= max_nest) return fail(ps.why, "commands nested more than {d} deep", .{max_nest}); + ps.depth += 1; + defer ps.depth -= 1; var file_re: ?[]const u8 = null; ps.file_comma = false; const a = try ps.address(&file_re); @@ -1163,6 +1173,11 @@ test "an Edit that fails halfway changes nothing, and says why in acme's words" .{ "e /tmp/x", "can't open /tmp/x: no files here" }, .{ "\"zz\" p", "no file matches \"zz\"" }, .{ ",s/(a)/\\1/", "no \\1: mvzr keeps no submatches" }, + .{ comptime deep: { + var loops: []const u8 = ""; + for (0..40) |_| loops = loops ++ ",x/a/"; + break :deep loops ++ "d"; + }, "commands nested more than 32 deep" }, .{ "1 m 1,2", "move overlaps itself" }, .{ ",x/(^|\\n)foo/d", "bad regular expression: in a pattern with \\n, ^ can only come first and $ only just before a \\n" }, }) |c| { diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index c3e5e3a6..229b3d67 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -401,7 +401,7 @@ pub fn runIn( }, }; child.stdin = null; // writer_context owns and closes this endpoint - var writer: ?std.Thread = std.Thread.spawn(.{}, writeInput, .{writer_context}) catch { + var writer: ?std.Io.Future(void) = io.concurrent(writeInput, .{writer_context}) catch { writer_context.file.close(io); gpa.free(writer_context.input); gpa.destroy(writer_context); @@ -410,8 +410,9 @@ pub fn runIn( }; // On every early return (a timeout, a ceiling) the answer goes at once: // the group is killed and the shell left to be reaped elsewhere - // (abandon), and the writer, which owns its input, let go. - defer if (writer) |thread| thread.detach(); + // (abandon), and the writer's write canceled (it closes and frees what + // it owns as it ends). + defer if (writer) |*w| w.cancel(io); defer if (child.id != null) abandonNow(io, &child, pid); defer if (token != 0) Running.remove(pid); if (token != 0 and !Running.add(token, pid)) return fail.k(.signal); @@ -443,7 +444,9 @@ pub fn runIn( multi_reader.checkAnyError() catch return fail.k(.io); if (token != 0) Running.remove(pid); const term = child.wait(io) catch return fail.k(.io); - writer.?.join(); + // A cancel of this job reaches the write too: a grandchild that keeps + // stdin open and never reads holds no worker. + writer.?.await(io); writer = null; const stdout = multi_reader.toOwnedSlice(0) catch return fail.k(.io); diff --git a/src/surface.zig b/src/surface.zig index e67cf21c..d6ec016d 100644 --- a/src/surface.zig +++ b/src/surface.zig @@ -533,11 +533,6 @@ pub const Surface = struct { return s.cell_diffs[@as(usize, y) * s.cols + x].changed(); } - pub fn panelCellDiff(s: *const Surface, x: u16, y: u16) PanelCellDiff { - if (!s.hasPanelDiff() or x >= s.cols or y >= s.rows) return .unchanged; - return s.cell_diffs[@as(usize, y) * s.cols + x]; - } - pub fn at(s: *Surface, x: u16, y: u16) *Cell { std.debug.assert(x < s.cols and y < s.rows); return &s.cells[@as(usize, y) * s.cols + x]; diff --git a/src/terminal.zig b/src/terminal.zig index 33ad22e6..05054521 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -1,4 +1,4 @@ -//! A terminal pane: its VT emulator and replay, prompt rows, the edit overlay +//! A terminal pane: its VT emulator, prompt rows, the edit overlay //! typed over shell rows, and the Filter that recolours output to the theme. const panes = @import("panes.zig"); const std = @import("std"); @@ -141,16 +141,9 @@ pub const MarkedHandler = if (enabled) struct { } } else void; -pub const Replay = struct { - bytes: [1024 * 1024]u8 = undefined, - head: usize = 0, - len: usize = 0, -}; - pub const State = struct { vt: VtSlot, stream: StreamSlot, - replay: Replay, reply: [256]u8 = undefined, reply_len: u16 = 0, /// The grid cell the program last heard the mouse at, so motion is @@ -613,8 +606,6 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { .kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0, }); state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa }); - state.replay.head = 0; - state.replay.len = 0; state.reply_len = 0; state.mouse_cell = null; state.images = if (images_enabled) .{} else {}; @@ -678,43 +669,12 @@ pub fn restore(p: *Pardes, src: dump.Pane, live: bool) !*Pane { return pane; } -/// Feed the emulator and retain the bounded suffix a dump can replay. Live -/// output and restoration share this byte path, then apply different views. +/// Feed the emulator. Live output and restoration share this byte path, +/// then apply different views. fn ingest(pane: *Pane, bytes: []const u8) void { - const state = pane.terminal.?; - const replay = &state.replay; - if (bytes.len >= replay.bytes.len) { - const kept = bytes[bytes.len - replay.bytes.len ..]; - @memcpy(&replay.bytes, kept); - replay.head = 0; - replay.len = replay.bytes.len; - } else { - const overflow = bytes.len -| (replay.bytes.len - replay.len); - replay.head = (replay.head + overflow) % replay.bytes.len; - replay.len -= overflow; - const tail = (replay.head + replay.len) % replay.bytes.len; - const first = @min(bytes.len, replay.bytes.len - tail); - @memcpy(replay.bytes[tail..][0..first], bytes[0..first]); - @memcpy(replay.bytes[0 .. bytes.len - first], bytes[first..]); - replay.len += bytes.len; - } - state.stream.nextSlice(bytes); + pane.terminal.?.stream.nextSlice(bytes); } -/// Return the replay ring in chronological order. Wrapped records are copied -/// into `allocator`; contiguous records remain a borrowed slice of the pane. -fn replayBytes(pane: *const Pane, allocator: std.mem.Allocator) ![]const u8 { - const state = pane.terminal orelse return &.{}; - const replay = &state.replay; - if (replay.len == 0) return &.{}; - if (replay.head + replay.len <= replay.bytes.len) - return replay.bytes[replay.head..][0..replay.len]; - const out = try allocator.alloc(u8, replay.len); - const first = replay.bytes.len - replay.head; - @memcpy(out[0..first], replay.bytes[replay.head..]); - @memcpy(out[first..], replay.bytes[0 .. replay.len - first]); - return out; -} test "replay ownership is terminal-only and construction rolls back on allocation failure" { const Case = struct { @@ -753,7 +713,6 @@ test "document construction allocates only the pane and has inert terminal defau try std.testing.expectEqual(GridCursor{}, gridCursor(pane)); try std.testing.expectEqual(Scrollbar{}, scrollbar(pane)); try std.testing.expectEqualStrings("", try screenTextAlloc(pane, allocator)); - try std.testing.expectEqualStrings("", try replayBytes(pane, allocator)); try std.testing.expect(!failing.has_induced_failure); } @@ -779,46 +738,18 @@ test "terminal state keeps parser fragments and sends emulator replies through i } try std.testing.expectEqual(@as(usize, 1), replies); try std.testing.expectEqual(@as(u16, 0), state.reply_len); - try std.testing.expectEqualStrings("\x1b[6n", try replayBytes(pane, std.testing.allocator)); } -test "replay keeps a bounded chronological suffix across large writes and wrapping" { - if (comptime !enabled) return error.SkipZigTest; - const gpa = std.testing.allocator; - const pane = try create(gpa, 40, 12); - defer gpa.destroy(pane); - defer deinitEmulator(pane, gpa); - try std.testing.expectEqualStrings("", try replayBytes(pane, gpa)); - ingest(pane, "hello"); - const small = try replayBytes(pane, gpa); - try std.testing.expectEqualStrings("hello", small); - try std.testing.expectEqual(@intFromPtr(&pane.terminal.?.replay.bytes), @intFromPtr(small.ptr)); - - const capacity = pane.terminal.?.replay.bytes.len; - const input = try gpa.alloc(u8, capacity + 7); - defer gpa.free(input); - @memset(input, 0); - const ending = "\x1b[31mred\x1b[0m\r\n"; - @memcpy(input[input.len - ending.len ..], ending); - ingest(pane, input); - try std.testing.expectEqualSlices(u8, input[7..], try replayBytes(pane, gpa)); - ingest(pane, "next\r\n"); - const wrapped = try replayBytes(pane, gpa); - defer gpa.free(wrapped); - try std.testing.expectEqual(capacity, wrapped.len); - try std.testing.expectEqualSlices(u8, input[7 + "next\r\n".len ..], wrapped[0 .. capacity - "next\r\n".len]); - try std.testing.expectEqualStrings("next\r\n", wrapped[capacity - "next\r\n".len ..]); -} - -test "replay restores terminal cells from the retained stream" { +test "a terminal stream restores the cells it drew" { if (comptime !enabled) return error.SkipZigTest; const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); defer p.deinit(); while (p.nextEffect()) |_| {} const pane = p.panes[0].?; - feedOutput(p, pane, "\x1b[31mred\x1b[0m\r\ncafé\r\n"); - const encoded = try dump.encodeBytes(gpa, try replayBytes(pane, gpa)); + const drawn = "\x1b[31mred\x1b[0m\r\ncafé\r\n"; + feedOutput(p, pane, drawn); + const encoded = try dump.encodeBytes(gpa, drawn); defer gpa.free(encoded); const restored = try restore(p, .{ .kind = .terminal, @@ -835,7 +766,6 @@ test "replay restores terminal cells from the retained stream" { const after = try screenTextAlloc(restored, gpa); defer gpa.free(after); try std.testing.expectEqualStrings(before, after); - try std.testing.expectEqualSlices(u8, try replayBytes(pane, gpa), try replayBytes(restored, gpa)); } test "a restored command pane comes back finished, its command not run again" { diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 4cd1bc9a..afc0dbf0 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -34,6 +34,8 @@ var kitty_shm = std.atomic.Value(bool).init(false); var kitty_shm_probing = std.atomic.Value(bool).init(false); const kitty_shm_probe_id = 0x7fff_fff1; var kitty_shm_serial: u32 = 0; +/// Transmissions an unread shm segment outlives before it is removed. +const kitty_shm_window = 64; fn kittyShmName(buf: []u8, n: u32) ![:0]const u8 { return std.fmt.bufPrintSentinel(buf, "/pardes-{d}-{d}", .{ libc.getpid(), n }, 0); @@ -1787,6 +1789,13 @@ const Shell = struct { if (!s.kitty.contains(key) and vx.caps.kitty_graphics and kitty_shm.load(.acquire)) shm: { var buf: [64]u8 = undefined; kitty_shm_serial +%= 1; + // Bounded: a segment this many transmissions old was never + // read (a terminal reads one as its command arrives), and a + // tmpfs holds it in memory until it goes. + if (kitty_shm_serial > kitty_shm_window) { + var old_buf: [64]u8 = undefined; + if (kittyShmName(&old_buf, kitty_shm_serial - kitty_shm_window)) |old| _ = libc.shm_unlink(old) else |_| {} + } const name = kittyShmName(&buf, kitty_shm_serial) catch break :shm; if (!kittyShmWrite(name, place.rgba)) break :shm; if (vx.transmitLocalImagePath(s.gpa, s.tty.writer(), name, @intCast(place.iw), @intCast(place.ih), .shared_mem, .rgba) catch null) |handle| @@ -2010,7 +2019,7 @@ const Shell = struct { old.future.cancel(s.io) catch {}; s.lsp_task = null; } - const future = s.io.concurrent(lspWorker, .{ s.lsp_gpa, job, s.loop }) catch |err| { + const future = s.io.concurrent(lspWorker, .{ s.io, s.lsp_gpa, job, s.loop }) catch |err| { job.free(s.lsp_gpa); s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); return s.core.reportError(req.pane, "lsp", err); @@ -2144,9 +2153,9 @@ fn requestClipboard(vx: *vaxis.Vaxis, tty: *vaxis.Tty) void { vx.requestSystemClipboard(tty.writer()) catch {}; } -fn lspWorker(allocator: std.mem.Allocator, job: *host_io.Lsp.Job, loop: *Loop) anyerror!void { +fn lspWorker(io: std.Io, allocator: std.mem.Allocator, job: *host_io.Lsp.Job, loop: *Loop) anyerror!void { var sink: LspRowSink = .{ .allocator = allocator, .loop = loop }; - host_io.Lsp.work(allocator, job, &sink, LspRowSink.take); + host_io.Lsp.work(io, allocator, job, &sink, LspRowSink.take); } const LspRowSink = struct { @@ -2185,23 +2194,30 @@ fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void { return; } const file: std.Io.File = .{ .handle = fd, .flags = .{ .nonblocking = false } }; - var read_buf: [4096]u8 = undefined; - var reader = file.readerStreaming(io, &read_buf); + // One buffer, read into directly: a Reader around it only added a second + // (and in a safe build, filled both as `undefined` on every start). + var buf: [4096]u8 = undefined; while (true) { - var buf: [4096]u8 = undefined; - var vec = [_][]u8{&buf}; - const n = reader.interface.readVec(&vec) catch break; + const n = file.readStreaming(io, &.{&buf}) catch break; if (n == 0) break; loop.postEvent(.files_changed) catch break; } } fn winchWatch(loop: *Loop, vx: *vaxis.Vaxis, tty: *vaxis.Tty) void { + winchWatchUntil(loop, vx, tty, null); +} + +/// A SIGWINCH watcher that ends: `stop` set, then a SIGWINCH sent to its +/// thread, wakes its sigwait and it returns, so whoever owns `loop` can join +/// it before the loop goes (attachSession's loop is on its stack). +fn winchWatchUntil(loop: *Loop, vx: *vaxis.Vaxis, tty: *vaxis.Tty, stop: ?*const std.atomic.Value(bool)) void { var set = posix.sigemptyset(); posix.sigaddset(&set, posix.SIG.WINCH); while (true) { var sig: c_int = 0; if (libc.sigwait(&set, &sig) != 0) continue; + if (stop) |flag| if (flag.load(.acquire)) return; if (vx.state.in_band_resize) continue; // terminal reports via CSI 48 const ws = tty.getWinsize() catch continue; loop.postEvent(.{ .winsize = ws }) catch {}; @@ -2285,12 +2301,11 @@ fn wakeFs(ctx: ?*anyopaque) void { } fn readPty(io: std.Io, gpa: std.mem.Allocator, pty: std.Io.File, id: usize, gen: u32, loop: *Loop) anyerror!void { - var read_buf: [0x10000]u8 = undefined; - var reader = pty.readerStreaming(io, &read_buf); + // One buffer, read into directly: a Reader around it only added a second + // (and in a safe build, filled both as `undefined` on every start). + var buf: [0x10000]u8 = undefined; while (true) { - var buf: [0x10000]u8 = undefined; - var vec = [_][]u8{&buf}; - const n = reader.interface.readVec(&vec) catch break; + const n = pty.readStreaming(io, &.{&buf}) catch break; if (n == 0) break; const bytes = try gpa.dupe(u8, buf[0..n]); loop.postEvent(.{ .pty_read = .{ .id = id, .gen = gen, .bytes = bytes } }) catch { @@ -2301,7 +2316,7 @@ fn readPty(io: std.Io, gpa: std.mem.Allocator, pty: std.Io.File, id: usize, gen: _ = loop.tryPostEvent(.{ .pty_eof = .{ .id = id, .gen = gen } }) catch {}; } -const max_paste_bytes: usize = 4 << 20; +const max_paste_bytes: usize = pardes.memory.limits.max_paste_bytes; fn pasteBytes(key: vaxis.Key) []const u8 { const text = key.text orelse ""; @@ -3023,7 +3038,13 @@ fn attachSession(init: std.process.Init, name: []const u8, tty: *vaxis.Tty, vx: startInput(&loop, &input_cache) catch |err| return .{ .lost = err }; defer stopInput(&loop); - (std.Thread.spawn(.{}, winchWatch, .{ &loop, vx, tty }) catch |err| return .{ .lost = err }).detach(); + var winch_stop: std.atomic.Value(bool) = .init(false); + const winch = std.Thread.spawn(.{}, winchWatchUntil, .{ &loop, vx, tty, &winch_stop }) catch |err| return .{ .lost = err }; + defer { + winch_stop.store(true, .release); + _ = libc.pthread_kill(winch.getHandle(), posix.SIG.WINCH); + winch.join(); + } vx.queryTerminalSend(tty.writer()) catch {}; return attachLoop(&a); diff --git a/test/lspbench.zig b/test/lspbench.zig index d0e25aee..08df9f4c 100644 --- a/test/lspbench.zig +++ b/test/lspbench.zig @@ -110,8 +110,13 @@ test "lspbench options reject unknown flags and multiple roots before probing" { try std.testing.expectError(error.MultipleRoots, Options.parse(&.{ "first", "--check", "second" })); } +/// The process's Io, for the queries: their waits are woken by the client's +/// reader threads, which a single-threaded Io cannot be. +var query_io: std.Io = undefined; + pub fn main(init: std.process.Init) !void { const gpa = std.heap.page_allocator; + query_io = init.io; const args = try init.minimal.args.toSlice(init.arena.allocator()); if (args.len > 1 and std.mem.eql(u8, args[1], "probe")) return probeCommand(init); @@ -171,13 +176,19 @@ fn checkResults(results: []const Result, required: usize) !void { for (results) |r| if (r.empty or !r.correct) return error.IncorrectProbe; } -fn runOnce(gpa: std.mem.Allocator, req: lsp.Req, expected: []const u8, r: *Result) !u64 { - var arena: std.heap.ArenaAllocator = .init(gpa); - defer arena.deinit(); +fn runOnce(gpa: std.mem.Allocator, req_in: lsp.Req, expected: []const u8, r: *Result) !u64 { + // The editor's own path (host_io.Lsp.work): the query in the fixed + // region reserved at startup, not a heap arena that maps its memory anew. + const memory = @import("pardes").memory; + const region = memory.takeLspQuery() orelse return error.QueryRegionBusy; + var arena: std.heap.FixedBufferAllocator = .init(region); + defer memory.giveLspQuery(arena.end_index); + var req = req_in; + req.budget = &arena; var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); const t0 = nowNs(); - try lsp.query(gpa, arena.allocator(), req, &out.writer); + try lsp.query(query_io, gpa, arena.allocator(), req, &out.writer); const ns = nowNs() -| t0; r.accept(out.written(), expected); return ns / 1000; @@ -341,6 +352,7 @@ extern "c" fn usleep(usec: c_uint) c_int; /// [--reps N] [--warm S]`, rows on stdout, the timing on stderr. fn probeCommand(init: std.process.Init) !void { const gpa = std.heap.page_allocator; + query_io = init.io; const arena = init.arena.allocator(); const io = std.Io.Threaded.global_single_threaded.io(); @@ -431,7 +443,7 @@ fn probeCommand(init: std.process.Init) !void { var scratch: std.heap.ArenaAllocator = .init(gpa); defer scratch.deinit(); const t0 = nowUs(); - try lsp.query(gpa, scratch.allocator(), .{ + try lsp.query(query_io, gpa, scratch.allocator(), .{ .kind = kind, .path = path, .source = source, diff --git a/test/panes.zig b/test/panes.zig index e98e3a54..1460b355 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -1913,9 +1913,6 @@ test "terminal output keeps following new rows after scrollback eviction" { try std.testing.expectEqualStrings("f", effect.write.bytes.slice()); try std.testing.expect(p.nextEffect() == null); } - const replay = &pane.terminal.?.replay; - try std.testing.expectEqual(replay.bytes.len, replay.len); - try std.testing.expect(replay.head > 0); } test "terminal edit stays on its surviving row when old scrollback is evicted" { diff --git a/test/perf-baseline-gui-debug.json b/test/perf-baseline-gui-debug.json index ffcd678f..a41c92b2 100644 --- a/test/perf-baseline-gui-debug.json +++ b/test/perf-baseline-gui-debug.json @@ -1 +1 @@ -{"harness":"15bee9f100e5ef64","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"gui","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":1744,"med_us":1832,"p90_us":2468,"max_us":2524},{"op":"render","fixture":"medium","min_us":88,"med_us":97,"p90_us":114,"max_us":115},{"op":"key-down","fixture":"medium","min_us":95,"med_us":98,"p90_us":104,"max_us":105},{"op":"key-right","fixture":"medium","min_us":92,"med_us":96,"p90_us":98,"max_us":105},{"op":"page-down","fixture":"medium","min_us":91,"med_us":95,"p90_us":97,"max_us":105},{"op":"wheel","fixture":"medium","min_us":87,"med_us":93,"p90_us":99,"max_us":100},{"op":"edit-char","fixture":"medium","min_us":1241,"med_us":1459,"p90_us":1691,"max_us":1724},{"op":"edit-twin","fixture":"medium","min_us":2592,"med_us":3003,"p90_us":3481,"max_us":3503},{"op":"body-2m","fixture":"medium","min_us":4576,"med_us":8262,"p90_us":15424,"max_us":17259},{"op":"body-appends","fixture":"medium","min_us":152003,"med_us":227268,"p90_us":272872,"max_us":326510},{"op":"deep-sel","fixture":"medium","min_us":82,"med_us":83,"p90_us":90,"max_us":93}]} +{"harness":"442f274a5da9136c","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"gui","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2253,"med_us":2626,"p90_us":3065,"max_us":3090},{"op":"render","fixture":"medium","min_us":100,"med_us":103,"p90_us":103,"max_us":110},{"op":"key-down","fixture":"medium","min_us":102,"med_us":103,"p90_us":105,"max_us":113},{"op":"key-right","fixture":"medium","min_us":98,"med_us":104,"p90_us":108,"max_us":113},{"op":"page-down","fixture":"medium","min_us":103,"med_us":104,"p90_us":111,"max_us":114},{"op":"wheel","fixture":"medium","min_us":98,"med_us":100,"p90_us":102,"max_us":108},{"op":"edit-char","fixture":"medium","min_us":1251,"med_us":1940,"p90_us":2652,"max_us":3041},{"op":"edit-twin","fixture":"medium","min_us":2779,"med_us":3177,"p90_us":3366,"max_us":3657},{"op":"body-2m","fixture":"medium","min_us":5812,"med_us":11423,"p90_us":13681,"max_us":13758},{"op":"body-appends","fixture":"medium","min_us":153898,"med_us":241075,"p90_us":284518,"max_us":305761},{"op":"deep-sel","fixture":"medium","min_us":86,"med_us":91,"p90_us":94,"max_us":122}]} diff --git a/test/perf-baseline-tty-debug.json b/test/perf-baseline-tty-debug.json index b8e534c4..dc37e033 100644 --- a/test/perf-baseline-tty-debug.json +++ b/test/perf-baseline-tty-debug.json @@ -1 +1 @@ -{"harness":"15bee9f100e5ef64","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":1747,"med_us":1912,"p90_us":2150,"max_us":2247},{"op":"render","fixture":"medium","min_us":89,"med_us":95,"p90_us":102,"max_us":104},{"op":"key-down","fixture":"medium","min_us":97,"med_us":101,"p90_us":107,"max_us":133},{"op":"key-right","fixture":"medium","min_us":93,"med_us":104,"p90_us":109,"max_us":112},{"op":"page-down","fixture":"medium","min_us":92,"med_us":96,"p90_us":106,"max_us":108},{"op":"wheel","fixture":"medium","min_us":90,"med_us":92,"p90_us":96,"max_us":96},{"op":"edit-char","fixture":"medium","min_us":1167,"med_us":1345,"p90_us":1483,"max_us":1627},{"op":"edit-twin","fixture":"medium","min_us":2319,"med_us":2921,"p90_us":3425,"max_us":3539},{"op":"body-2m","fixture":"medium","min_us":4333,"med_us":7752,"p90_us":15941,"max_us":17192},{"op":"body-appends","fixture":"medium","min_us":149149,"med_us":234177,"p90_us":285302,"max_us":295516},{"op":"deep-sel","fixture":"medium","min_us":83,"med_us":88,"p90_us":99,"max_us":119}]} +{"harness":"442f274a5da9136c","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2181,"med_us":2495,"p90_us":2621,"max_us":2660},{"op":"render","fixture":"medium","min_us":96,"med_us":98,"p90_us":102,"max_us":112},{"op":"key-down","fixture":"medium","min_us":101,"med_us":103,"p90_us":107,"max_us":108},{"op":"key-right","fixture":"medium","min_us":100,"med_us":112,"p90_us":118,"max_us":119},{"op":"page-down","fixture":"medium","min_us":97,"med_us":101,"p90_us":105,"max_us":107},{"op":"wheel","fixture":"medium","min_us":98,"med_us":100,"p90_us":108,"max_us":110},{"op":"edit-char","fixture":"medium","min_us":1377,"med_us":1842,"p90_us":2336,"max_us":2418},{"op":"edit-twin","fixture":"medium","min_us":2655,"med_us":3200,"p90_us":3321,"max_us":3660},{"op":"body-2m","fixture":"medium","min_us":5420,"med_us":10753,"p90_us":12826,"max_us":15385},{"op":"body-appends","fixture":"medium","min_us":153569,"med_us":232519,"p90_us":281625,"max_us":309036},{"op":"deep-sel","fixture":"medium","min_us":91,"med_us":98,"p90_us":111,"max_us":111}]} diff --git a/test/perf-baseline-tty-fast.json b/test/perf-baseline-tty-fast.json index 46125349..bd8f32dc 100644 --- a/test/perf-baseline-tty-fast.json +++ b/test/perf-baseline-tty-fast.json @@ -1 +1 @@ -{"harness":"15bee9f100e5ef64","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"fast","c_optimize":"fast","terminal_optimize":"fast","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":1904,"med_us":2040,"p90_us":2290,"max_us":2600},{"op":"render","fixture":"medium","min_us":80,"med_us":83,"p90_us":88,"max_us":93},{"op":"key-down","fixture":"medium","min_us":81,"med_us":87,"p90_us":93,"max_us":109},{"op":"key-right","fixture":"medium","min_us":79,"med_us":82,"p90_us":86,"max_us":96},{"op":"page-down","fixture":"medium","min_us":79,"med_us":83,"p90_us":89,"max_us":93},{"op":"wheel","fixture":"medium","min_us":77,"med_us":79,"p90_us":82,"max_us":83},{"op":"edit-char","fixture":"medium","min_us":1139,"med_us":1338,"p90_us":1453,"max_us":1578},{"op":"edit-twin","fixture":"medium","min_us":2515,"med_us":2776,"p90_us":2879,"max_us":2907},{"op":"body-2m","fixture":"medium","min_us":4832,"med_us":7650,"p90_us":15076,"max_us":15371},{"op":"body-appends","fixture":"medium","min_us":165796,"med_us":240881,"p90_us":284919,"max_us":292573},{"op":"deep-sel","fixture":"medium","min_us":73,"med_us":75,"p90_us":83,"max_us":93}]} +{"harness":"442f274a5da9136c","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"fast","c_optimize":"fast","terminal_optimize":"fast","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2283,"med_us":2611,"p90_us":2812,"max_us":2917},{"op":"render","fixture":"medium","min_us":89,"med_us":97,"p90_us":105,"max_us":119},{"op":"key-down","fixture":"medium","min_us":88,"med_us":95,"p90_us":100,"max_us":116},{"op":"key-right","fixture":"medium","min_us":90,"med_us":99,"p90_us":103,"max_us":106},{"op":"page-down","fixture":"medium","min_us":86,"med_us":90,"p90_us":96,"max_us":97},{"op":"wheel","fixture":"medium","min_us":85,"med_us":91,"p90_us":94,"max_us":101},{"op":"edit-char","fixture":"medium","min_us":1229,"med_us":1823,"p90_us":1874,"max_us":2101},{"op":"edit-twin","fixture":"medium","min_us":2196,"med_us":2745,"p90_us":2928,"max_us":2972},{"op":"body-2m","fixture":"medium","min_us":5606,"med_us":10369,"p90_us":14063,"max_us":14406},{"op":"body-appends","fixture":"medium","min_us":155867,"med_us":237195,"p90_us":289128,"max_us":306283},{"op":"deep-sel","fixture":"medium","min_us":82,"med_us":84,"p90_us":86,"max_us":91}]} diff --git a/test/perf.zig b/test/perf.zig index 7662d028..ab284739 100644 --- a/test/perf.zig +++ b/test/perf.zig @@ -665,8 +665,6 @@ fn measureTerm(op: TermOp, fx: TermFixture, reps: usize) !Cell { .row = pane.body.cur_row, .col = pane.body.cur_col, .edit_len = if (pane.ovl) |overlay| overlay.text.len else 0, - .replay_head = state.replay.head, - .replay_len = state.replay.len, .cols = pane.cols, .rows = pane.rows, }; @@ -689,22 +687,11 @@ fn measureTerm(op: TermOp, fx: TermFixture, reps: usize) !Cell { _ = try frame(core); const dt = nowNs() -| t0; switch (op) { - .render => if (pane.body.cur_row != before.row or pane.body.cur_col != before.col or - state.replay.head != before.replay_head or state.replay.len != before.replay_len) + .render => if (pane.body.cur_row != before.row or pane.body.cur_col != before.col) return error.RenderChangedTerminal, - .output => { - const capacity = state.replay.bytes.len; - if (term_chunk.len == 0 or term_chunk.len >= capacity) return error.InvalidOutputFixture; - const overflow = term_chunk.len -| (capacity - before.replay_len); - if (state.replay.len != @min(capacity, before.replay_len + term_chunk.len) or - state.replay.head != (before.replay_head + overflow) % capacity) - return error.OutputWasNotRecorded; - const start = (state.replay.head + state.replay.len - term_chunk.len) % capacity; - const first = @min(term_chunk.len, capacity - start); - if (!std.mem.eql(u8, term_chunk[0..first], state.replay.bytes[start..][0..first]) or - !std.mem.eql(u8, term_chunk[first..], state.replay.bytes[0 .. term_chunk.len - first])) - return error.WrongTerminalOutput; - }, + // What the output did to the screen is the snapshot suite's to + // check; here it is timed. + .output => if (term_chunk.len == 0) return error.InvalidOutputFixture, .resize_rows => if (pane.rows == before.rows or pane.cols != before.cols or state.vt.screens.active.pages.rows != pane.rows) return error.TerminalDidNotResize, .resize_cols => if (pane.cols == before.cols or pane.rows != before.rows or -- cgit v1.3 From b8f076b63d6fcbb01539d5541520a32bccc0e219 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 23 Oct 2026 11:00:49 -0300 Subject: Audit: the hosts' wire on std.Io Reader/Writer, Lsp and selection_pipe deliver through a sink, std.Io clock and sleep wire.zig reads and writes through std.Io.Reader/Writer instead of its own framing helpers; host_io.Lsp.start and selection_pipe.start take a callback sink, so each shell keeps no copy of the worker plumbing. Co-Authored-By: Claude Opus 5.5 --- src/detached/client.zig | 6 +- src/detached/server.zig | 103 ++---- src/detached/wire.zig | 906 ++++++++++++++++++++++-------------------------- src/gui/gui.zig | 84 +---- src/host_io.zig | 54 +-- src/macos.zig | 68 +--- src/selection_pipe.zig | 38 +- src/tty/tty.zig | 96 ++--- 8 files changed, 556 insertions(+), 799 deletions(-) (limited to 'src/selection_pipe.zig') diff --git a/src/detached/client.zig b/src/detached/client.zig index 60950208..cd17871e 100644 --- a/src/detached/client.zig +++ b/src/detached/client.zig @@ -165,7 +165,7 @@ pub const Client = struct { _ = libc.close(fd); return error.NoSession; } - server.setNonblock(fd); + _ = server.setNonblock(fd); var c: Client = .{ .gpa = gpa, .fd = fd }; errdefer c.deinit(); try c.send(.{ .hello = .{ @@ -225,7 +225,7 @@ pub const Client = struct { // next flush: the session decodes it as a message, refuses it, and // drops a frontend whose only mistake was a message this protocol // cannot carry (an `Event.command` past 64 KiB is `Overlong`, and a - // caller that mis-sized the queue is `NoSpace`). + // caller that mis-sized the queue is `WriteFailed`). c.out.items.len = at; return err; }; @@ -1201,7 +1201,7 @@ test "detached session: a frontend is never asked to fork, write, or watch" { for (0..256) |i| { const tag: u8 = @intCast(i); // Payloads are deliberately empty: what is asked is whether the TAG is - // known, and every known tag fails later (`Truncated`) or succeeds, but + // known, and every known tag fails later (`EndOfStream`) or succeeds, but // never with `BadTag`. if (wire.decodeServer(tag, &.{})) |_| accepted += 1 else |err| switch (err) { error.BadTag => continue, diff --git a/src/detached/server.zig b/src/detached/server.zig index 6c8d4352..989bdc0f 100644 --- a/src/detached/server.zig +++ b/src/detached/server.zig @@ -194,12 +194,7 @@ pub const Session = struct { }; for (pair) |fd| { if (libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1)) < 0) return error.SocketOptionFailed; - const flags = libc.fcntl(fd, libc.F.GETFL, @as(c_int, 0)); - if (flags < 0) return error.SocketOptionFailed; - var options: libc.O = @bitCast(@as(u32, @bitCast(flags))); - options.NONBLOCK = true; - if (libc.fcntl(fd, libc.F.SETFL, @as(c_int, @bitCast(@as(u32, @bitCast(options))))) < 0) - return error.SocketOptionFailed; + if (!setNonblock(fd)) return error.SocketOptionFailed; if (comptime darwin) { const on: c_int = 1; if (libc.setsockopt(fd, libc.SOL.SOCKET, libc.SO.NOSIGPIPE, &on, @sizeOf(c_int)) != 0) @@ -213,7 +208,7 @@ pub const Session = struct { fn cancelWorkers(s: *Session) void { if (s.lsp_task) |*task| { - task.future.cancel(s.io) catch {}; + task.future.cancel(s.io); s.lsp_task = null; } s.pipe_tasks.cancelAll(s.io); @@ -229,7 +224,7 @@ pub const Session = struct { .lsp => |result| { s.core.update(.{ .lsp_resp = .{ .id = result.id, .rows = result.rows } }); if (s.lsp_task) |*task| if (task.id == result.id) { - task.future.await(s.io) catch {}; + task.future.await(s.io); s.lsp_task = null; }; }, @@ -345,7 +340,7 @@ pub const Session = struct { _ = libc.close(fd); return false; } - setNonblock(fd); + _ = setNonblock(fd); s.listener = fd; s.path_len = path.len; return true; @@ -371,7 +366,7 @@ pub const Session = struct { fn clockNow(ctx: ?*anyopaque) u64 { const s = of(ctx); - return s.test_clock orelse host_io.monotonicNs(); + return s.test_clock orelse @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); } const vtable: host_io.Host.VTable = .{ @@ -404,25 +399,14 @@ pub const Session = struct { fn lspRequest(ctx: ?*anyopaque, request: host_io.Lsp.Request) void { const s = of(ctx); _ = s.drainCompletions(true); + // Canceled here, not in `start`: what it delivered before it went + // is drained first, which leaves its slot in the mailbox free. if (s.lsp_task) |*task| { - task.future.cancel(s.io) catch {}; + task.future.cancel(s.io); s.lsp_task = null; _ = s.drainCompletions(true); } - const job = host_io.Lsp.snapshot(s.worker_gpa, s.core, request) catch |err| { - s.core.update(.{ .lsp_resp = .{ .id = request.id, .rows = null } }); - return s.core.reportError(request.pane, "lsp", err); - }; - const future = s.io.concurrent(lspWorker, .{ s.io, s.worker_gpa, job, &s.mailbox }) catch |err| { - job.free(s.worker_gpa); - s.core.update(.{ .lsp_resp = .{ .id = request.id, .rows = null } }); - return s.core.reportError(request.pane, "lsp", err); - }; - s.lsp_task = .{ .id = request.id, .future = future }; - } - - fn lspWorker(io: std.Io, gpa: std.mem.Allocator, job: *host_io.Lsp.Job, box: *Mailbox) anyerror!void { - host_io.Lsp.work(io, gpa, job, box, deliverLspRows); + host_io.Lsp.start(s.io, s.worker_gpa, s.core, &s.lsp_task, request, &s.mailbox, deliverLspRows); } fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { @@ -443,26 +427,12 @@ pub const Session = struct { fn pipeRequest(ctx: ?*anyopaque, id: u32) void { const s = of(ctx); _ = s.drainCompletions(true); - if (s.pipe_tasks.full()) { - s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return; - } - const request = s.core.pipe.pipeRequest(id) orelse return; - const job = selection_pipe.Job.copy(s.worker_gpa, request) catch |err| { - s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return s.core.reportError(s.core.active, "pipe", err); - }; - const future = s.io.concurrent(pipeWorker, .{ s.io, s.worker_gpa, job, &s.mailbox }) catch |err| { - job.deinit(s.worker_gpa); - s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return s.core.reportError(s.core.active, "pipe", err); - }; - std.debug.assert(s.pipe_tasks.add(.{ .id = id, .future = future })); + selection_pipe.start(s.io, s.worker_gpa, s.core, &s.pipe_tasks, id, &s.mailbox, deliverPipe); } - fn pipeWorker(io: std.Io, gpa: std.mem.Allocator, job: *selection_pipe.Job, box: *Mailbox) anyerror!void { - defer job.deinit(gpa); - box.post(.{ .pipe = selection_pipe.runJob(gpa, io, job) }); + fn deliverPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void { + const box: *Mailbox = @ptrCast(@alignCast(ctx.?)); + box.post(.{ .pipe = response }); } fn primary(s: *Session) ?*Client { @@ -519,7 +489,7 @@ pub const Session = struct { } const command = if (s.core.panes[pane]) |pn| pn.command != null else false; s.ptys[pane] = .{ .fd = child.file.handle, .pid = child.pid, .cmd = .{ .watched = command and host_io.watchExit(child.pid), .fd = child.file.handle } }; - setNonblock(child.file.handle); + _ = setNonblock(child.file.handle); var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; // Only a shell given no directory is asked where it is: one given a // directory is named by it already (newShell), and asked now, before @@ -682,7 +652,7 @@ pub const Session = struct { pt.out = .empty; if (pt.pid == 0) return; _ = libc.kill(pt.pid, libc.SIG.HUP); - pt.kill_at = monotonicMs() + 100; + pt.kill_at = std.Io.Clock.awake.now(s.io).toMilliseconds() + 100; s.harvest(); if (pt.pid == 0) return; for (&s.retired_shells) |*shell| if (shell.pid == 0) { @@ -757,7 +727,7 @@ pub const Session = struct { } fn harvest(s: *Session) void { - const now = monotonicMs(); + const now = std.Io.Clock.awake.now(s.io).toMilliseconds(); for (&s.retired_shells) |*shell| host_io.reapRetired(&shell.pid, &shell.kill_at, now); for (&s.ptys) |*pty| if (pty.fd < 0) host_io.reapRetired(&pty.pid, &pty.kill_at, now); } @@ -840,7 +810,7 @@ pub const Session = struct { s.core.unattended = s.primary() == null; pardes.Messages.expireUnattended(s.core); - const now = monotonicMs(); + const now = std.Io.Clock.awake.now(s.io).toMilliseconds(); var fds: [poll_slots]libc.pollfd = undefined; var src: [poll_slots]Source = undefined; var n: usize = 0; @@ -894,7 +864,7 @@ pub const Session = struct { // The wait is the 9P connections' turn with the core. if (n == 0) { pardes.turn.rest(); - nap(if (timeout_ms == 0) 16 else timeout_ms); + s.io.sleep(.fromMilliseconds(if (timeout_ms == 0) 16 else timeout_ms), .awake) catch {}; pardes.turn.wake(); if (timeout_ms != 0) if (s.test_clock) |*virtual| { virtual.* = @max(virtual.*, s.core.nextWake() orelse virtual.*); @@ -909,12 +879,12 @@ pub const Session = struct { pardes.turn.wake(); if (ready > 0) s.dispatch(fds[0..n], src[0..n]); // The core's wake ran out; the pump advances it from `now`. - if (timeout_ms != 0 and monotonicMs() -| now >= timeout_ms) if (s.test_clock) |*virtual| { + if (timeout_ms != 0 and std.Io.Clock.awake.now(s.io).toMilliseconds() -| now >= timeout_ms) if (s.test_clock) |*virtual| { virtual.* = @max(virtual.*, s.core.nextWake() orelse virtual.*); }; _ = s.drainCompletions(true); s.takeExits(); - s.expire(monotonicMs()); + s.expire(std.Io.Clock.awake.now(s.io).toMilliseconds()); s.harvest(); s.reloadWatched(); _ = s.reconcile(); @@ -951,7 +921,6 @@ pub const Session = struct { } fn nextWake(s: *const Session, now: i64) ?c_int { - if (now == 0) return null; // no clock; see `monotonicMs` var due: ?i64 = null; for (s.retired_shells) |shell| if (shell.pid != 0) { due = now + 10; @@ -998,13 +967,13 @@ pub const Session = struct { switch (libc.errno(fd)) { .AGAIN, .INTR, .CONNABORTED => return, else => { - s.accept_paused_ms = monotonicMs() + accept_pause_ms; + s.accept_paused_ms = std.Io.Clock.awake.now(s.io).toMilliseconds() + accept_pause_ms; return; }, } } ninep_io.setCloexec(fd); - setNonblock(fd); + _ = setNonblock(fd); if (comptime darwin) { const on: c_int = 1; _ = libc.setsockopt(fd, libc.SOL.SOCKET, libc.SO.NOSIGPIPE, &on, @sizeOf(c_int)); @@ -1016,7 +985,7 @@ pub const Session = struct { _ = libc.close(fd); continue; }; - s.clients[slot] = .{ .fd = fd, .accepted_ms = monotonicMs() }; + s.clients[slot] = .{ .fd = fd, .accepted_ms = std.Io.Clock.awake.now(s.io).toMilliseconds() }; } } @@ -1432,8 +1401,8 @@ test "a detached session's terminal pane takes its shell with it when it closes" const host = s.host(); host.vtable.close_pty.?(host.ctx, 3); try std.testing.expect(s.ptys[3].fd < 0); - const deadline = monotonicMs() + 2000; - while (monotonicMs() < deadline) { + const deadline = std.Io.Clock.awake.now(std.testing.io).toMilliseconds() + 2000; + while (std.Io.Clock.awake.now(std.testing.io).toMilliseconds() < deadline) { s.harvest(); const running = s.ptys[3].pid != 0 or for (s.retired_shells) |shell| { if (shell.pid == pid) break true; @@ -1482,12 +1451,12 @@ test "detached worker setup failure completes requests without changing document pub const setCloexec = ninep_io.setCloexec; -pub fn setNonblock(fd: c_int) void { +pub fn setNonblock(fd: c_int) bool { const flags = libc.fcntl(fd, libc.F.GETFL, @as(c_int, 0)); - if (flags < 0) return; + if (flags < 0) return false; var o: libc.O = @bitCast(@as(u32, @bitCast(flags))); o.NONBLOCK = true; - _ = libc.fcntl(fd, libc.F.SETFL, @as(c_int, @bitCast(@as(u32, @bitCast(o))))); + return libc.fcntl(fd, libc.F.SETFL, @as(c_int, @bitCast(@as(u32, @bitCast(o))))) >= 0; } pub const nosignal: u32 = if (darwin) 0 else libc.MSG.NOSIGNAL; @@ -1503,20 +1472,6 @@ fn retire(gpa: std.mem.Allocator, list: *std.ArrayListUnmanaged(u8)) void { list.clearAndFree(gpa); } -pub fn monotonicMs() i64 { - var ts: libc.timespec = undefined; - if (libc.clock_gettime(.MONOTONIC, &ts) != 0) return 0; - return @as(i64, ts.sec) * std.time.ms_per_s + @divTrunc(ts.nsec, std.time.ns_per_ms); -} - -fn nap(ms: u32) void { - var ts: libc.timespec = .{ - .sec = @intCast(ms / 1000), - .nsec = @intCast((ms % 1000) * std.time.ns_per_ms), - }; - _ = libc.nanosleep(&ts, null); -} - pub fn socketPath(buf: *[sun_path_len]u8, dir: []const u8, name: []const u8) ?[:0]const u8 { if (name.len == 0) return null; if (std.mem.indexOfAny(u8, name, "/\x00") != null) return null; @@ -1560,7 +1515,7 @@ fn alive(path: [:0]const u8) bool { if (fd < 0) return true; defer _ = libc.close(fd); ninep_io.setCloexec(fd); - setNonblock(fd); + _ = setNonblock(fd); const rc = libc.connect(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))); if (rc == 0) return true; return libc.errno(rc) != .CONNREFUSED; diff --git a/src/detached/wire.zig b/src/detached/wire.zig index d57a28be..2252ce3b 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -28,7 +28,10 @@ comptime { if (hash.final() != chrome_layout) @compileError(std.fmt.comptimePrint("pardes.Chrome changed (layout 0x{x}): bump wire.version, then chrome_layout", .{hash.final()})); } -pub const Error = error{ Truncated, Overlong, BadTag, BadValue, Trailing, NoSpace }; +/// EndOfStream: the message ends early; WriteFailed: no room for it. +/// ReadFailed is in only because std.Io.Reader says so: a fixed one never +/// fails a read. +pub const Error = error{ EndOfStream, ReadFailed, Overlong, BadTag, BadValue, Trailing, WriteFailed }; pub const max_cols: u16 = 512; pub const max_rows: u16 = 128; @@ -159,8 +162,8 @@ pub const Hello = struct { /// An error rather than a zero on a payload shorter than two bytes, so a /// truncated hello stays diagnosable too instead of reading as version 0. pub fn helloVersion(payload: []const u8) Error!u16 { - var r: Reader = .init(payload); - return r.getU16(); + var r: std.Io.Reader = .fixed(payload); + return r.takeInt(u16, .little); } pub const Welcome = struct { @@ -214,39 +217,39 @@ pub const Frame = struct { chrome.* = .{}; } if (f.layers.len == 0) return; - var r: Reader = .init(f.layers); - const count = try r.getU16(); + var r: std.Io.Reader = .fixed(f.layers); + const count = try r.takeInt(u16, .little); if (count > layers.len) return error.Overlong; for (layers[0..count]) |*layer| { layer.kind = .body; - layer.id = try r.getU16(); - layer.serial = try r.getU32(); - layer.viewport = .{ .x = try r.getU16(), .y = try r.getU16(), .w = try r.getU16(), .h = try r.getU16() }; - layer.cols = try r.getU16(); - layer.rows = try r.getU16(); - layer.context_rows = try r.getU16(); + layer.id = try r.takeInt(u16, .little); + layer.serial = try r.takeInt(u32, .little); + layer.viewport = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .w = try r.takeInt(u16, .little), .h = try r.takeInt(u16, .little) }; + layer.cols = try r.takeInt(u16, .little); + layer.rows = try r.takeInt(u16, .little); + layer.context_rows = try r.takeInt(u16, .little); if (layer.id >= pardes.MAX_PANES or layer.cols == 0 or layer.cols > f.cols or layer.rows == 0 or layer.rows > 2 * max_rows or layer.context_rows > layer.rows or layer.context_rows > limits.wrap_rows or layer.viewport.x > f.cols or layer.viewport.w > f.cols - layer.viewport.x or layer.viewport.y > f.rows or layer.viewport.h > f.rows - layer.viewport.y) return error.BadValue; for (0..layer.context_rows) |row| { - if (try r.getBool()) layer.context_separators.set(row); + if (try getBool(&r)) layer.context_separators.set(row); } const cursor = try getCursor(&r, layer.cols, layer.rows); layer.cursor = if (cursor) |c| .{ .x = c.x, .y = c.y, .bar = c.bar } else null; layer.cells = try gpa.alloc(pardes.Cell, @as(usize, layer.cols) * layer.rows); for (layer.cells) |*cell| cell.* = try decodeCell(&r); } - const tag_count = try r.getU16(); + const tag_count = try r.takeInt(u16, .little); if (tag_count > tags.len) return error.Overlong; for (tags[0..tag_count]) |*layer| { - layer.kind = try r.getTag(pardes.TagKind); - layer.id = try r.getU16(); - layer.serial = try r.getU32(); - layer.viewport = .{ .x = try r.getU16(), .y = try r.getU16(), .w = try r.getU16(), .h = try r.getU16() }; - layer.cols = try r.getU16(); - layer.rows = try r.getU16(); - for (&layer.bg) |*channel| channel.* = try r.getByte(); + layer.kind = try getTag(&r, pardes.TagKind); + layer.id = try r.takeInt(u16, .little); + layer.serial = try r.takeInt(u32, .little); + layer.viewport = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .w = try r.takeInt(u16, .little), .h = try r.takeInt(u16, .little) }; + layer.cols = try r.takeInt(u16, .little); + layer.rows = try r.takeInt(u16, .little); + for (&layer.bg) |*channel| channel.* = try r.takeByte(); if (layer.cols == 0 or layer.viewport.w == 0 or layer.rows == 0 or layer.rows > pardes.MAX_TAG_ROWS or layer.viewport.h != layer.rows or layer.viewport.x > f.cols or layer.viewport.w > f.cols - layer.viewport.x or @@ -261,24 +264,24 @@ pub const Frame = struct { layer.cells = try gpa.alloc(pardes.Cell, @as(usize, layer.cols) * layer.rows); for (layer.cells) |*cell| cell.* = try decodeCell(&r); } - const region_count = try r.getU16(); + const region_count = try r.takeInt(u16, .little); if (region_count > pardes.MAX_REGIONS) return error.Overlong; try regions.ensureTotalCapacity(gpa, region_count); for (0..region_count) |_| { - var region: pardes.Region = .{ .kind = try r.getTag(pardes.Region.Kind), .rect = undefined }; - region.owner = try r.getByte(); - region.serial = try r.getU32(); - region.rect = .{ .x = try r.getU16(), .y = try r.getU16(), .w = try r.getU16(), .h = try r.getU16() }; - const flags = try r.getByte(); + var region: pardes.Region = .{ .kind = try getTag(&r, pardes.Region.Kind), .rect = undefined }; + region.owner = try r.takeByte(); + region.serial = try r.takeInt(u32, .little); + region.rect = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .w = try r.takeInt(u16, .little), .h = try r.takeInt(u16, .little) }; + const flags = try r.takeByte(); if (flags > 3) return error.BadValue; region.active = flags & 1 != 0; region.dirty = flags & 2 != 0; - region.thumb_y = try r.getU16(); - region.thumb_h = try r.getU16(); - region.notice = try r.getByte(); - region.clip = try r.getU16(); - region.lift = @bitCast(try r.getU32()); - region.line = @bitCast(try r.getU32()); + region.thumb_y = try r.takeInt(u16, .little); + region.thumb_h = try r.takeInt(u16, .little); + region.notice = try r.takeByte(); + region.clip = try r.takeInt(u16, .little); + region.lift = @bitCast(try r.takeInt(u32, .little)); + region.line = @bitCast(try r.takeInt(u32, .little)); // A springy flavour overshoots, and a wind-up dips below. if (!(region.lift >= -1 and region.lift <= 2)) return error.BadValue; if (region.rect.x > f.cols or region.rect.w > f.cols - region.rect.x or @@ -290,25 +293,25 @@ pub const Frame = struct { inline for (info.field_names, info.field_types) |name, T| { const rgb = &@field(chrome, name); if (T == ?[3]u8) { - if (try r.getBool()) { + if (try getBool(&r)) { rgb.* = .{ 0, 0, 0 }; - for (&rgb.*.?) |*channel| channel.* = try r.getByte(); + for (&rgb.*.?) |*channel| channel.* = try r.takeByte(); } } else if (T == u8) { - rgb.* = try r.getByte(); + rgb.* = try r.takeByte(); } else if (T == u32) { - rgb.* = try r.getU32(); + rgb.* = try r.takeInt(u32, .little); } else if (T == f32) { - rgb.* = @bitCast(try r.getU32()); + rgb.* = @bitCast(try r.takeInt(u32, .little)); if (!std.math.isFinite(rgb.*)) return error.BadValue; } else if (T == [8]f32) { for (rgb) |*value| { - value.* = @bitCast(try r.getU32()); + value.* = @bitCast(try r.takeInt(u32, .little)); if (!std.math.isFinite(value.*)) return error.BadValue; } - } else for (rgb) |*channel| channel.* = try r.getByte(); + } else for (rgb) |*channel| channel.* = try r.takeByte(); } - try r.end(); + try end(&r); } /// Paint this frame into `grid`, which must be exactly `cols * rows` cells @@ -320,12 +323,12 @@ pub const Frame = struct { pub fn apply(f: Frame, grid: []pardes.Cell) Error!void { if (grid.len != @as(usize, f.cols) * @as(usize, f.rows)) return error.BadValue; if (f.kind == .full) @memset(grid, .{}); - var r: Reader = .init(f.runs); + var r: std.Io.Reader = .fixed(f.runs); var i: u32 = 0; while (i < f.nruns) : (i += 1) { - const start = try r.getU32(); - const count = try r.getU16(); - // A zero-length run is not something `encodeFrame` emits, and + const start = try r.takeInt(u32, .little); + const count = try r.takeInt(u16, .little); + // A zero-length run is not something `encodeFrameLayers` emits, and // accepting one would let a peer spend the run budget saying // nothing. if (count == 0) return error.BadValue; @@ -335,7 +338,7 @@ pub const Frame = struct { if (start > grid.len or count > grid.len - start) return error.Overlong; for (grid[start..][0..count]) |*c| c.* = try decodeCell(&r); } - try r.end(); + try end(&r); } }; @@ -384,169 +387,93 @@ pub const Post = struct { // primitives // --------------------------------------------------------------------------- -pub const Writer = struct { - buf: []u8, - n: usize = 0, - - pub fn init(buf: []u8) Writer { - return .{ .buf = buf }; - } - - pub fn written(w: *const Writer) []const u8 { - return w.buf[0..w.n]; - } - - fn room(w: *Writer, k: usize) Error![]u8 { - if (w.buf.len - w.n < k) return error.NoSpace; - defer w.n += k; - return w.buf[w.n..][0..k]; - } - - fn putByte(w: *Writer, v: u8) Error!void { - (try w.room(1))[0] = v; - } - - fn putU16(w: *Writer, v: u16) Error!void { - std.mem.writeInt(u16, (try w.room(2))[0..2], v, .little); - } - - fn putU32(w: *Writer, v: u32) Error!void { - std.mem.writeInt(u32, (try w.room(4))[0..4], v, .little); - } - - /// One byte, 0 or 1, never "nonzero". `getBool` refuses anything else. - fn putBool(w: *Writer, v: bool) Error!void { - try w.putByte(@intFromBool(v)); - } - - /// IEEE-754 binary32, as its bit pattern in an explicit u32. A scalar - /// bitcast and not a struct blit: the format is the one thing a riscv32 - /// and an x86_64 do agree about. - fn putF32(w: *Writer, v: f32) Error!void { - try w.putU32(@bitCast(v)); - } - - fn putBytes(w: *Writer, v: []const u8) Error!void { - @memcpy(try w.room(v.len), v); - } - - /// Length-prefixed, u16: paths, command lines, a key's text. Nothing here - /// is legitimately longer than 64 KiB and a u16 says so on the wire. - fn putSlice16(w: *Writer, v: []const u8) Error!void { - if (v.len > std.math.maxInt(u16)) return error.Overlong; - try w.putU16(@intCast(v.len)); - try w.putBytes(v); - } - - /// ...and u32 for the ones that are: pty output, a paste, a file. - fn putSlice32(w: *Writer, v: []const u8) Error!void { - if (v.len > max_payload) return error.Overlong; - try w.putU32(@intCast(v.len)); - try w.putBytes(v); - } -}; - -pub const Reader = struct { - bytes: []const u8, - i: usize = 0, - - pub fn init(bytes: []const u8) Reader { - return .{ .bytes = bytes }; - } - - /// `i <= bytes.len` is the invariant every getter below preserves, which is - /// what makes the subtraction here safe. - fn take(r: *Reader, n: usize) Error![]const u8 { - if (r.bytes.len - r.i < n) return error.Truncated; - defer r.i += n; - return r.bytes[r.i..][0..n]; - } - - fn getByte(r: *Reader) Error!u8 { - return (try r.take(1))[0]; - } - - fn getU16(r: *Reader) Error!u16 { - return std.mem.readInt(u16, (try r.take(2))[0..2], .little); - } - - fn getU32(r: *Reader) Error!u32 { - return std.mem.readInt(u32, (try r.take(4))[0..4], .little); - } +/// Length-prefixed, u16: paths, command lines, a key's text. Nothing here +/// is legitimately longer than 64 KiB and a u16 says so on the wire. +fn putSlice16(w: *std.Io.Writer, v: []const u8) Error!void { + if (v.len > std.math.maxInt(u16)) return error.Overlong; + try w.writeInt(u16, @intCast(v.len), .little); + try w.writeAll(v); +} - fn getBool(r: *Reader) Error!bool { - return switch (try r.getByte()) { - 0 => false, - 1 => true, - else => error.BadValue, - }; - } +/// ...and u32 for the ones that are: pty output, a paste, a file. +fn putSlice32(w: *std.Io.Writer, v: []const u8) Error!void { + if (v.len > max_payload) return error.Overlong; + try w.writeInt(u32, @intCast(v.len), .little); + try w.writeAll(v); +} - fn getF32(r: *Reader) Error!f32 { - const v: f32 = @bitCast(try r.getU32()); - // A NaN or an infinity here is not a scroll distance. `pinch` in - // particular multiplies into a zoom factor the pane keeps, so one bad - // value poisons that pane for the rest of the session. - if (!std.math.isFinite(v)) return error.BadValue; - return v; - } +/// One byte, 0 or 1, never "nonzero": the writer's `@intFromBool`. +fn getBool(r: *std.Io.Reader) Error!bool { + return switch (try r.takeByte()) { + 0 => false, + 1 => true, + else => error.BadValue, + }; +} - fn getSlice16(r: *Reader) Error![]const u8 { - return r.take(try r.getU16()); - } +/// IEEE-754 binary32, as its bit pattern in an explicit u32. A scalar +/// bitcast and not a struct blit: the format is the one thing a riscv32 +/// and an x86_64 do agree about. +fn getF32(r: *std.Io.Reader) Error!f32 { + const v: f32 = @bitCast(try r.takeInt(u32, .little)); + // A NaN or an infinity here is not a scroll distance. `pinch` in + // particular multiplies into a zoom factor the pane keeps, so one bad + // value poisons that pane for the rest of the session. + if (!std.math.isFinite(v)) return error.BadValue; + return v; +} - fn getSlice32(r: *Reader) Error![]const u8 { - const n = try r.getU32(); - if (n > max_payload) return error.Overlong; - return r.take(n); - } +fn getSlice32(r: *std.Io.Reader) Error![]const u8 { + const n = try r.takeInt(u32, .little); + if (n > max_payload) return error.Overlong; + return r.take(n); +} - /// A pane id the core can actually index. `Event.output{ .pane = 200 }` - /// would reach `p.panes[200]`. - fn getPane(r: *Reader) Error!u8 { - const pane = try r.getByte(); - if (pane >= pardes.MAX_PANES) return error.BadValue; - return pane; - } +/// A pane id the core can actually index. `Event.output{ .pane = 200 }` +/// would reach `p.panes[200]`. +fn getPane(r: *std.Io.Reader) Error!u8 { + const pane = try r.takeByte(); + if (pane >= pardes.MAX_PANES) return error.BadValue; + return pane; +} - /// A grid the core can render into. Zero is refused rather than clamped: a - /// frontend that has not been sized yet must not be allowed to collapse a - /// shared session to nothing (see server.zig `geometry`). - fn getCols(r: *Reader) Error!u16 { - const v = try r.getU16(); - if (v == 0 or v > max_cols) return error.BadValue; - return v; - } +/// A grid the core can render into. Zero is refused rather than clamped: a +/// frontend that has not been sized yet must not be allowed to collapse a +/// shared session to nothing (see server.zig `geometry`). +fn getCols(r: *std.Io.Reader) Error!u16 { + const v = try r.takeInt(u16, .little); + if (v == 0 or v > max_cols) return error.BadValue; + return v; +} - fn getRows(r: *Reader) Error!u16 { - const v = try r.getU16(); - if (v == 0 or v > max_rows) return error.BadValue; - return v; - } +fn getRows(r: *std.Io.Reader) Error!u16 { + const v = try r.takeInt(u16, .little); + if (v == 0 or v > max_rows) return error.BadValue; + return v; +} - fn getTag(r: *Reader, comptime T: type) Error!T { - return std.enums.fromInt(T, try r.getByte()) orelse error.BadTag; - } +fn getTag(r: *std.Io.Reader, comptime T: type) Error!T { + return std.enums.fromInt(T, try r.takeByte()) orelse error.BadTag; +} - fn end(r: *Reader) Error!void { - if (r.i != r.bytes.len) return error.Trailing; - } -}; +fn end(r: *std.Io.Reader) Error!void { + if (r.seek != r.end) return error.Trailing; +} /// Reserve a message header; `finishMessage` back-patches the length. Two /// passes would mean walking a frame's runs twice to find out how long they /// are, and the walk is the expensive half. -fn beginMessage(w: *Writer, tag: u8) Error!usize { - const at = w.n; - try w.putByte(tag); - try w.putU32(0); +fn beginMessage(w: *std.Io.Writer, tag: u8) Error!usize { + const at = w.end; + try w.writeByte(tag); + try w.writeInt(u32, 0, .little); return at; } -fn finishMessage(w: *Writer, at: usize) Error!void { - const len = w.n - at - header_len; +fn finishMessage(w: *std.Io.Writer, at: usize) Error!void { + const len = w.end - at - header_len; if (len > max_payload) return error.Overlong; - std.mem.writeInt(u32, w.buf[at + 1 ..][0..4], @intCast(len), .little); + std.mem.writeInt(u32, w.buffer[at + 1 ..][0..4], @intCast(len), .little); } /// One complete message at the front of a stream buffer, or null when the rest @@ -567,24 +494,24 @@ pub fn framed(buf: []const u8) Error!?Framed { // cells // --------------------------------------------------------------------------- -fn putColor(w: *Writer, c: pardes.Color) Error!void { +fn putColor(w: *std.Io.Writer, c: pardes.Color) Error!void { switch (c) { - .default => try w.putByte(@backingInt(ColorTag.default)), + .default => try w.writeByte(@backingInt(ColorTag.default)), .index => |i| { - try w.putByte(@backingInt(ColorTag.index)); - try w.putByte(i); + try w.writeByte(@backingInt(ColorTag.index)); + try w.writeByte(i); }, .rgb => |v| { - try w.putByte(@backingInt(ColorTag.rgb)); - try w.putBytes(&v); + try w.writeByte(@backingInt(ColorTag.rgb)); + try w.writeAll(&v); }, } } -fn getColor(r: *Reader) Error!pardes.Color { - return switch (try r.getTag(ColorTag)) { +fn getColor(r: *std.Io.Reader) Error!pardes.Color { + return switch (try getTag(r, ColorTag)) { .default => .default, - .index => .{ .index = try r.getByte() }, + .index => .{ .index = try r.takeByte() }, .rgb => .{ .rgb = (try r.take(3))[0..3].* }, }; } @@ -604,31 +531,31 @@ const Ul = @FieldType(pardes.CellStyle, "ul"); /// existed for, in a form that cannot fall out of step. A variant that has to /// travel under a DIFFERENT name than the core's gets an arm of its own before /// the `inline else`, exactly as `clientTag` keeps the machine-local reports. -fn putUl(w: *Writer, u: Ul) Error!void { - try w.putByte(switch (u) { +fn putUl(w: *std.Io.Writer, u: Ul) Error!void { + try w.writeByte(switch (u) { inline else => |t| @backingInt(@field(UlTag, @tagName(t))), }); } -fn getUl(r: *Reader) Error!Ul { - return switch (try r.getTag(UlTag)) { +fn getUl(r: *std.Io.Reader) Error!Ul { + return switch (try getTag(r, UlTag)) { inline else => |t| @field(Ul, @tagName(t)), }; } -fn putFont(w: *Writer, f: pardes.FontRole) Error!void { - try w.putByte(switch (f) { +fn putFont(w: *std.Io.Writer, f: pardes.FontRole) Error!void { + try w.writeByte(switch (f) { inline else => |t| @backingInt(@field(FontTag, @tagName(t))), }); } -fn getFont(r: *Reader) Error!pardes.FontRole { - return switch (try r.getTag(FontTag)) { +fn getFont(r: *std.Io.Reader) Error!pardes.FontRole { + return switch (try getTag(r, FontTag)) { inline else => |t| @field(pardes.FontRole, @tagName(t)), }; } -/// How many bytes `putCell` will write. Exact, because `encodeFrame` weighs it +/// How many bytes `putCell` will write. Exact, because `encodeFrameLayers` weighs it /// against `run_header` to decide whether to coalesce a run across it. fn cellSize(c: *const pardes.Cell) usize { if (c.default) return 1; @@ -646,11 +573,11 @@ fn colorSize(c: pardes.Color) usize { /// An unpainted cell is ONE byte: `default` means "the shell renders the /// terminal's default cell" (surface.zig `Cell`), so its text and style are not /// merely equal to the defaults, they are not part of the frame at all. -fn putCell(w: *Writer, c: *const pardes.Cell) Error!void { - try w.putBool(c.default); +fn putCell(w: *std.Io.Writer, c: *const pardes.Cell) Error!void { + try w.writeByte(@intFromBool(c.default)); if (c.default) return; - try w.putByte(c.len); - try w.putBytes(c.grapheme()); + try w.writeByte(c.len); + try w.writeAll(c.grapheme()); const s = c.style; try putColor(w, s.fg); try putColor(w, s.bg); @@ -662,15 +589,15 @@ fn putCell(w: *Writer, c: *const pardes.Cell) Error!void { if (s.reverse) attrs |= attr_reverse; if (s.invisible) attrs |= attr_invisible; if (s.strikethrough) attrs |= attr_strikethrough; - try w.putByte(attrs); + try w.writeByte(attrs); try putUl(w, s.ul); try putFont(w, s.font_role); } -fn decodeCell(r: *Reader) Error!pardes.Cell { - if (try r.getBool()) return .{}; +fn decodeCell(r: *std.Io.Reader) Error!pardes.Cell { + if (try getBool(r)) return .{}; var c: pardes.Cell = .{ .default = false }; - const len = try r.getByte(); + const len = try r.takeByte(); // `Cell.text` is 7 bytes and `grapheme()` slices to `len`. A zero-length // grapheme is a cell with nothing to draw and no way to advance a column. if (len == 0 or len > c.text.len) return error.BadValue; @@ -678,7 +605,7 @@ fn decodeCell(r: *Reader) Error!pardes.Cell { @memcpy(c.text[0..len], try r.take(len)); c.style.fg = try getColor(r); c.style.bg = try getColor(r); - const attrs = try r.getByte(); + const attrs = try r.takeByte(); if (attrs & attr_reserved != 0) return error.BadValue; c.style.bold = attrs & attr_bold != 0; c.style.dim = attrs & attr_dim != 0; @@ -696,24 +623,24 @@ fn decodeCell(r: *Reader) Error!pardes.Cell { // frames // --------------------------------------------------------------------------- -fn putCursor(w: *Writer, cursor: ?Cursor, cols: u16, rows: u16) Error!void { +fn putCursor(w: *std.Io.Writer, cursor: ?Cursor, cols: u16, rows: u16) Error!void { // Fixed six bytes whether or not there is a cursor. An optional field // would save five bytes on a message that is already hundreds, and cost a // branch on both sides of the wire. - try w.putBool(cursor != null); + try w.writeByte(@intFromBool(cursor != null)); const c = cursor orelse Cursor{ .x = 0, .y = 0, .bar = false }; // Refused here as well as on decode, so this side cannot build a frame its // own decoder rejects: a dropped frame (sendFrame logs it) leaves a stale // screen, and a refused one takes the frontend's connection with it. if (c.x >= cols or c.y >= rows) return error.BadValue; - try w.putU16(c.x); - try w.putU16(c.y); - try w.putBool(c.bar); + try w.writeInt(u16, c.x, .little); + try w.writeInt(u16, c.y, .little); + try w.writeByte(@intFromBool(c.bar)); } -fn getCursor(r: *Reader, cols: u16, rows: u16) Error!?Cursor { - const present = try r.getBool(); - const c: Cursor = .{ .x = try r.getU16(), .y = try r.getU16(), .bar = try r.getBool() }; +fn getCursor(r: *std.Io.Reader, cols: u16, rows: u16) Error!?Cursor { + const present = try getBool(r); + const c: Cursor = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .bar = try getBool(r) }; // THE cursor bounds check. Every other field of a frame is checked against // the grid and these two were not, and they are the two a frontend indexes // with directly — `Surface.at(cursor.x, cursor.y)` on a grid this frame @@ -725,6 +652,22 @@ fn getCursor(r: *Reader, cols: u16, rows: u16) Error!?Cursor { return if (present) c else null; } +pub fn layersBound(layers: []const pardes.Layer, tags: []const pardes.Layer, regions: []const pardes.Region) usize { + var n: usize = 6; + for (layers) |layer| if (layer.rows != 0) { + n += 26 + layer.context_rows + layer.cells.len * cell_max; + }; + for (tags) |layer| if (layer.rows != 0) { + n += 28 + layer.cells.len * cell_max; + }; + return n + regions.len * region_len + chrome_len; +} + +const region_len = 1 + 1 + 4 + 8 + 1 + 2 + 2 + 1 + 2 + 4 + 4; +/// The palette: each colour three bytes, a theme's optional ones a flag +/// before them. +const chrome_len = @typeInfo(pardes.Chrome).@"struct".field_names.len * 4 + 8 * 4; + /// Encode one frame of `cells`, as a DIFF against `prev` when `prev` is the /// same grid this receiver last acknowledged, and as a FULL frame otherwise. /// @@ -746,45 +689,6 @@ fn getCursor(r: *Reader, cols: u16, rows: u16) Error!?Cursor { /// own 56x14 grid with uncoloured cells, where it is exact: 6298 against 56. /// This transport exists for a frontend on the far end of a slow link, and /// shipping the Surface every frame would be 3 MiB/s at the animation tick. -pub fn encodeFrame( - out: []u8, - cols: u16, - rows: u16, - cursor: ?Cursor, - cells: []const pardes.Cell, - prev: []const pardes.Cell, -) Error![]const u8 { - return encodeFramePointer(out, cols, rows, cursor, .arrow, cells, prev); -} - -pub fn encodeFramePointer( - out: []u8, - cols: u16, - rows: u16, - cursor: ?Cursor, - pointer_shape: pardes.Surface.PointerShape, - cells: []const pardes.Cell, - prev: []const pardes.Cell, -) Error![]const u8 { - return encodeFrameLayers(out, cols, rows, cursor, pointer_shape, cells, prev, &.{}, &.{}, &.{}, .{}); -} - -pub fn layersBound(layers: []const pardes.Layer, tags: []const pardes.Layer, regions: []const pardes.Region) usize { - var n: usize = 6; - for (layers) |layer| if (layer.rows != 0) { - n += 26 + layer.context_rows + layer.cells.len * cell_max; - }; - for (tags) |layer| if (layer.rows != 0) { - n += 28 + layer.cells.len * cell_max; - }; - return n + regions.len * region_len + chrome_len; -} - -const region_len = 1 + 1 + 4 + 8 + 1 + 2 + 2 + 1 + 2 + 4 + 4; -/// The palette: each colour three bytes, a theme's optional ones a flag -/// before them. -const chrome_len = @typeInfo(pardes.Chrome).@"struct".field_names.len * 4 + 8 * 4; - pub fn encodeFrameLayers(out: []u8, cols: u16, rows: u16, cursor: ?Cursor, pointer_shape: pardes.Surface.PointerShape, cells: []const pardes.Cell, prev: []const pardes.Cell, layers: []const pardes.Layer, tags: []const pardes.Layer, regions: []const pardes.Region, chrome: pardes.Chrome) Error![]const u8 { // The protocol's ceiling, enforced by the ENCODER too, and BEFORE the // assert below so a caller can be told rather than tripped. `max_payload` @@ -794,38 +698,38 @@ pub fn encodeFrameLayers(out: []u8, cols: u16, rows: u16, cursor: ?Cursor, point if (cols == 0 or cols > max_cols or rows == 0 or rows > max_rows) return error.Overlong; std.debug.assert(cells.len == @as(usize, cols) * @as(usize, rows)); const full = prev.len != cells.len; - var w: Writer = .init(out); + var w: std.Io.Writer = .fixed(out); const at = try beginMessage(&w, @backingInt(ServerTag.frame)); const header: FrameHeader = if (pointer_shape == .link) (if (full) .full_link else .diff_link) else (if (full) .full else .diff); - try w.putByte(@backingInt(header)); - try w.putU16(cols); - try w.putU16(rows); + try w.writeByte(@backingInt(header)); + try w.writeInt(u16, cols, .little); + try w.writeInt(u16, rows, .little); try putCursor(&w, cursor, cols, rows); - const layers_len_at = w.n; - try w.putU32(0); - const layers_start = w.n; + const layers_len_at = w.end; + try w.writeInt(u32, 0, .little); + const layers_start = w.end; if (layers.len != 0 or tags.len != 0 or regions.len != 0) { var count: u16 = 0; for (layers) |layer| if (layer.rows != 0) { count += 1; }; - try w.putU16(count); + try w.writeInt(u16, count, .little); for (layers) |layer| { if (layer.rows == 0) continue; if (layer.context_rows > limits.wrap_rows or layer.context_rows > layer.rows) return error.BadValue; - try w.putU16(layer.id); - try w.putU32(layer.serial); - try w.putU16(layer.viewport.x); - try w.putU16(layer.viewport.y); - try w.putU16(layer.viewport.w); - try w.putU16(layer.viewport.h); - try w.putU16(layer.cols); - try w.putU16(layer.rows); - try w.putU16(layer.context_rows); - for (0..layer.context_rows) |row| try w.putBool(layer.context_separators.isSet(row)); + try w.writeInt(u16, layer.id, .little); + try w.writeInt(u32, layer.serial, .little); + try w.writeInt(u16, layer.viewport.x, .little); + try w.writeInt(u16, layer.viewport.y, .little); + try w.writeInt(u16, layer.viewport.w, .little); + try w.writeInt(u16, layer.viewport.h, .little); + try w.writeInt(u16, layer.cols, .little); + try w.writeInt(u16, layer.rows, .little); + try w.writeInt(u16, layer.context_rows, .little); + for (0..layer.context_rows) |row| try w.writeByte(@intFromBool(layer.context_separators.isSet(row))); try putCursor(&w, if (layer.cursor) |c| .{ .x = c.x, .y = c.y, .bar = c.bar } else null, layer.cols, layer.rows); for (layer.cells) |*cell| try putCell(&w, cell); } @@ -833,60 +737,60 @@ pub fn encodeFrameLayers(out: []u8, cols: u16, rows: u16, cursor: ?Cursor, point for (tags) |layer| if (layer.rows != 0) { tag_count += 1; }; - try w.putU16(tag_count); + try w.writeInt(u16, tag_count, .little); for (tags) |layer| { if (layer.rows == 0) continue; if (layer.cells.len != @as(usize, layer.cols) * layer.rows) return error.BadValue; - try w.putByte(@backingInt(layer.kind)); - try w.putU16(layer.id); - try w.putU32(layer.serial); - try w.putU16(layer.viewport.x); - try w.putU16(layer.viewport.y); - try w.putU16(layer.viewport.w); - try w.putU16(layer.viewport.h); - try w.putU16(layer.cols); - try w.putU16(layer.rows); - for (layer.bg) |channel| try w.putByte(channel); + try w.writeByte(@backingInt(layer.kind)); + try w.writeInt(u16, layer.id, .little); + try w.writeInt(u32, layer.serial, .little); + try w.writeInt(u16, layer.viewport.x, .little); + try w.writeInt(u16, layer.viewport.y, .little); + try w.writeInt(u16, layer.viewport.w, .little); + try w.writeInt(u16, layer.viewport.h, .little); + try w.writeInt(u16, layer.cols, .little); + try w.writeInt(u16, layer.rows, .little); + for (layer.bg) |channel| try w.writeByte(channel); try putCursor(&w, if (layer.cursor) |c| .{ .x = c.x, .y = c.y, .bar = c.bar } else null, layer.cols, layer.rows); for (layer.cells) |*cell| try putCell(&w, cell); } - try w.putU16(@intCast(regions.len)); + try w.writeInt(u16, @intCast(regions.len), .little); for (regions) |region| { - try w.putByte(@backingInt(region.kind)); - try w.putByte(region.owner); - try w.putU32(region.serial); - try w.putU16(region.rect.x); - try w.putU16(region.rect.y); - try w.putU16(region.rect.w); - try w.putU16(region.rect.h); - try w.putByte(@as(u8, @intFromBool(region.active)) | @as(u8, @intFromBool(region.dirty)) << 1); - try w.putU16(region.thumb_y); - try w.putU16(region.thumb_h); - try w.putByte(region.notice); - try w.putU16(region.clip); - try w.putU32(@bitCast(region.lift)); - try w.putU32(@bitCast(region.line)); + try w.writeByte(@backingInt(region.kind)); + try w.writeByte(region.owner); + try w.writeInt(u32, region.serial, .little); + try w.writeInt(u16, region.rect.x, .little); + try w.writeInt(u16, region.rect.y, .little); + try w.writeInt(u16, region.rect.w, .little); + try w.writeInt(u16, region.rect.h, .little); + try w.writeByte(@as(u8, @intFromBool(region.active)) | @as(u8, @intFromBool(region.dirty)) << 1); + try w.writeInt(u16, region.thumb_y, .little); + try w.writeInt(u16, region.thumb_h, .little); + try w.writeByte(region.notice); + try w.writeInt(u16, region.clip, .little); + try w.writeInt(u32, @bitCast(region.lift), .little); + try w.writeInt(u32, @bitCast(region.line), .little); } const info = @typeInfo(pardes.Chrome).@"struct"; inline for (info.field_names, info.field_types) |name, T| { const rgb = @field(chrome, name); if (T == ?[3]u8) { - try w.putBool(rgb != null); - if (rgb) |value| for (value) |channel| try w.putByte(channel); + try w.writeByte(@intFromBool(rgb != null)); + if (rgb) |value| for (value) |channel| try w.writeByte(channel); } else if (T == u8) { - try w.putByte(rgb); + try w.writeByte(rgb); } else if (T == u32) { - try w.putU32(rgb); + try w.writeInt(u32, rgb, .little); } else if (T == f32) { - try w.putU32(@bitCast(rgb)); + try w.writeInt(u32, @bitCast(rgb), .little); } else if (T == [8]f32) { - for (rgb) |value| try w.putU32(@bitCast(value)); - } else for (rgb) |channel| try w.putByte(channel); + for (rgb) |value| try w.writeInt(u32, @bitCast(value), .little); + } else for (rgb) |channel| try w.writeByte(channel); } } - std.mem.writeInt(u32, w.buf[layers_len_at..][0..4], @intCast(w.n - layers_start), .little); - const nruns_at = w.n; - try w.putU32(0); + std.mem.writeInt(u32, w.buffer[layers_len_at..][0..4], @intCast(w.end - layers_start), .little); + const nruns_at = w.end; + try w.writeInt(u32, 0, .little); var nruns: u32 = 0; var i: usize = 0; @@ -923,15 +827,15 @@ pub fn encodeFrameLayers(out: []u8, cols: u16, rows: u16, cursor: ?Cursor, point if (j >= cells.len or gap >= run_header) break; i = j; } - try w.putU32(@intCast(start)); - try w.putU16(@intCast(run_end - start)); + try w.writeInt(u32, @intCast(start), .little); + try w.writeInt(u16, @intCast(run_end - start), .little); for (cells[start..run_end]) |*c| try putCell(&w, c); nruns += 1; i = run_end; } - std.mem.writeInt(u32, w.buf[nruns_at..][0..4], nruns, .little); + std.mem.writeInt(u32, w.buffer[nruns_at..][0..4], nruns, .little); try finishMessage(&w, at); - return w.written(); + return w.buffered(); } fn sendCell(cells: []const pardes.Cell, prev: []const pardes.Cell, full: bool, i: usize) bool { @@ -964,64 +868,64 @@ fn clientTag(msg: ClientMsg) ClientTag { /// WIRE is not. const has_cell_pixels = @hasField(pardes.CellPixels, "w"); -fn putMetrics(w: *Writer, metrics: ?pardes.RowMetrics) Error!void { - try w.putBool(metrics != null); +fn putMetrics(w: *std.Io.Writer, metrics: ?pardes.RowMetrics) Error!void { + try w.writeByte(@intFromBool(metrics != null)); if (metrics) |m| { - try w.putU16(m.body_w); - try w.putU16(m.body_h); - try w.putU16(m.tagline_w); - try w.putU16(m.tagline_h); + try w.writeInt(u16, m.body_w, .little); + try w.writeInt(u16, m.body_h, .little); + try w.writeInt(u16, m.tagline_w, .little); + try w.writeInt(u16, m.tagline_h, .little); } } -fn getMetrics(r: *Reader) Error!?pardes.RowMetrics { - if (!try r.getBool()) return null; - const m: pardes.RowMetrics = .{ .body_w = try r.getU16(), .body_h = try r.getU16(), .tagline_w = try r.getU16(), .tagline_h = try r.getU16() }; +fn getMetrics(r: *std.Io.Reader) Error!?pardes.RowMetrics { + if (!try getBool(r)) return null; + const m: pardes.RowMetrics = .{ .body_w = try r.takeInt(u16, .little), .body_h = try r.takeInt(u16, .little), .tagline_w = try r.takeInt(u16, .little), .tagline_h = try r.takeInt(u16, .little) }; if (m.body_w == 0 or m.body_h == 0 or m.tagline_w == 0 or m.tagline_h == 0) return error.BadValue; return m; } -fn putBodyHit(w: *Writer, hit: ?pardes.Mouse.BodyHit) Error!void { - try w.putBool(hit != null); +fn putBodyHit(w: *std.Io.Writer, hit: ?pardes.Mouse.BodyHit) Error!void { + try w.writeByte(@intFromBool(hit != null)); if (hit) |h| { - try w.putU32(h.serial); - try w.putU16(h.row); - try w.putU16(h.col); - try w.putF32(h.pixel_x); - try w.putF32(h.pixel_y); + try w.writeInt(u32, h.serial, .little); + try w.writeInt(u16, h.row, .little); + try w.writeInt(u16, h.col, .little); + try w.writeInt(u32, @bitCast(h.pixel_x), .little); + try w.writeInt(u32, @bitCast(h.pixel_y), .little); try putMetrics(w, h.metrics); } } -fn getBodyHit(r: *Reader) Error!?pardes.Mouse.BodyHit { - if (!try r.getBool()) return null; - const hit: pardes.Mouse.BodyHit = .{ .serial = try r.getU32(), .row = try r.getU16(), .col = try r.getU16(), .pixel_x = try r.getF32(), .pixel_y = try r.getF32(), .metrics = (try getMetrics(r)) orelse return error.BadValue }; +fn getBodyHit(r: *std.Io.Reader) Error!?pardes.Mouse.BodyHit { + if (!try getBool(r)) return null; + const hit: pardes.Mouse.BodyHit = .{ .serial = try r.takeInt(u32, .little), .row = try r.takeInt(u16, .little), .col = try r.takeInt(u16, .little), .pixel_x = try getF32(r), .pixel_y = try getF32(r), .metrics = (try getMetrics(r)) orelse return error.BadValue }; if (!std.math.isFinite(hit.pixel_x) or !std.math.isFinite(hit.pixel_y)) return error.BadValue; return hit; } -fn putTagHit(w: *Writer, hit: ?pardes.TagHit) Error!void { - try w.putBool(hit != null); +fn putTagHit(w: *std.Io.Writer, hit: ?pardes.TagHit) Error!void { + try w.writeByte(@intFromBool(hit != null)); if (hit) |h| { - try w.putByte(@backingInt(h.kind)); - try w.putU16(h.id); - try w.putU32(h.serial); - try w.putU16(h.col); - try w.putF32(h.pixel_x); - try w.putF32(h.pixel_y); + try w.writeByte(@backingInt(h.kind)); + try w.writeInt(u16, h.id, .little); + try w.writeInt(u32, h.serial, .little); + try w.writeInt(u16, h.col, .little); + try w.writeInt(u32, @bitCast(h.pixel_x), .little); + try w.writeInt(u32, @bitCast(h.pixel_y), .little); try putMetrics(w, h.metrics); } } -fn getTagHit(r: *Reader) Error!?pardes.TagHit { - if (!try r.getBool()) return null; +fn getTagHit(r: *std.Io.Reader) Error!?pardes.TagHit { + if (!try getBool(r)) return null; const hit: pardes.TagHit = .{ - .kind = try r.getTag(pardes.TagKind), - .id = try r.getU16(), - .serial = try r.getU32(), - .col = try r.getU16(), - .pixel_x = try r.getF32(), - .pixel_y = try r.getF32(), + .kind = try getTag(r, pardes.TagKind), + .id = try r.takeInt(u16, .little), + .serial = try r.takeInt(u32, .little), + .col = try r.takeInt(u16, .little), + .pixel_x = try getF32(r), + .pixel_y = try getF32(r), .metrics = (try getMetrics(r)) orelse return error.BadValue, }; if (!std.math.isFinite(hit.pixel_x) or !std.math.isFinite(hit.pixel_y)) return error.BadValue; @@ -1029,123 +933,123 @@ fn getTagHit(r: *Reader) Error!?pardes.TagHit { } pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 { - var w: Writer = .init(out); + var w: std.Io.Writer = .fixed(out); const at = try beginMessage(&w, @backingInt(clientTag(msg))); switch (msg) { .hello => |h| { - try w.putU16(h.version); - try w.putU16(h.cols); - try w.putU16(h.rows); + try w.writeInt(u16, h.version, .little); + try w.writeInt(u16, h.cols, .little); + try w.writeInt(u16, h.rows, .little); }, .bye => {}, .event => |ev| switch (ev) { .key => |k| { - try w.putU32(k.cp); - try w.putSlice16(k.text); - try w.putBool(k.ctrl); - try w.putBool(k.alt); - try w.putBool(k.shift); + try w.writeInt(u32, k.cp, .little); + try putSlice16(&w, k.text); + try w.writeByte(@intFromBool(k.ctrl)); + try w.writeByte(@intFromBool(k.alt)); + try w.writeByte(@intFromBool(k.shift)); }, .mouse => |m| { - try w.putByte(switch (m.button) { + try w.writeByte(switch (m.button) { inline else => |t| @backingInt(@field(ButtonTag, @tagName(t))), }); - try w.putByte(switch (m.kind) { + try w.writeByte(switch (m.kind) { inline else => |t| @backingInt(@field(KindTag, @tagName(t))), }); - try w.putU16(m.col); - try w.putU16(m.row); - try w.putBool(m.ctrl); - try w.putBool(m.shift); - try w.putBool(m.alt); + try w.writeInt(u16, m.col, .little); + try w.writeInt(u16, m.row, .little); + try w.writeByte(@intFromBool(m.ctrl)); + try w.writeByte(@intFromBool(m.shift)); + try w.writeByte(@intFromBool(m.alt)); try putBodyHit(&w, m.body_hit); try putTagHit(&w, m.tag_hit); }, .resize => |rs| { - try w.putU16(rs.cols); - try w.putU16(rs.rows); + try w.writeInt(u16, rs.cols, .little); + try w.writeInt(u16, rs.rows, .little); // The conventional 1:2 cell aspect when this build has no // pixels of its own, which is the same default the field // carries where it exists. - try w.putU16(if (comptime has_cell_pixels) rs.cell_pixels.w else 8); - try w.putU16(if (comptime has_cell_pixels) rs.cell_pixels.h else 16); + try w.writeInt(u16, if (comptime has_cell_pixels) rs.cell_pixels.w else 8, .little); + try w.writeInt(u16, if (comptime has_cell_pixels) rs.cell_pixels.h else 16, .little); try putMetrics(&w, rs.row_metrics); }, - .paste => |b| try w.putSlice32(b), - .command => |line| try w.putSlice16(line), + .paste => |b| try putSlice32(&w, b), + .command => |line| try putSlice16(&w, line), .pdf_scroll => |s| { - try w.putByte(s.pane); - try w.putF32(s.delta_pixels); + try w.writeByte(s.pane); + try w.writeInt(u32, @bitCast(s.delta_pixels), .little); }, - .pinch => |v| try w.putF32(v), - .touch_scroll => |v| try w.putF32(v), + .pinch => |v| try w.writeInt(u32, @bitCast(v), .little), + .touch_scroll => |v| try w.writeInt(u32, @bitCast(v), .little), .pointer_leave => {}, - .focus => |focused| try w.putBool(focused), + .focus => |focused| try w.writeByte(@intFromBool(focused)), // See `clientTag`: no tag, so nothing to encode. .output, .eof, .exited, .lsp_resp, .pipe_resp, .file_changed, .tick => unreachable, }, } try finishMessage(&w, at); - return w.written(); + return w.buffered(); } pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { - var r: Reader = .init(payload); + var r: std.Io.Reader = .fixed(payload); const msg: ClientMsg = switch (std.enums.fromInt(ClientTag, tag) orelse return error.BadTag) { .hello => .{ .hello = .{ - .version = try r.getU16(), - .cols = try r.getCols(), - .rows = try r.getRows(), + .version = try r.takeInt(u16, .little), + .cols = try getCols(&r), + .rows = try getRows(&r), } }, .bye => .bye, .key => blk: { - const cp = try r.getU32(); + const cp = try r.takeInt(u32, .little); // `Key.cp` is a u21, and the specials live in the private-use // plane below this bound. A larger number is not a codepoint and // @intCast of it would panic in a release build's own decoder. if (cp > 0x10FFFF) return error.BadValue; break :blk .{ .event = .{ .key = .{ .cp = @intCast(cp), - .text = try r.getSlice16(), - .ctrl = try r.getBool(), - .alt = try r.getBool(), - .shift = try r.getBool(), + .text = try r.take(try r.takeInt(u16, .little)), + .ctrl = try getBool(&r), + .alt = try getBool(&r), + .shift = try getBool(&r), } } }; }, .mouse => .{ .event = .{ .mouse = .{ - .button = switch (try r.getTag(ButtonTag)) { + .button = switch (try getTag(&r, ButtonTag)) { inline else => |t| @field(pardes.Mouse.Button, @tagName(t)), }, - .kind = switch (try r.getTag(KindTag)) { + .kind = switch (try getTag(&r, KindTag)) { inline else => |t| @field(pardes.Mouse.Kind, @tagName(t)), }, - .col = try r.getU16(), - .row = try r.getU16(), - .ctrl = try r.getBool(), - .shift = try r.getBool(), - .alt = try r.getBool(), + .col = try r.takeInt(u16, .little), + .row = try r.takeInt(u16, .little), + .ctrl = try getBool(&r), + .shift = try getBool(&r), + .alt = try getBool(&r), .body_hit = try getBodyHit(&r), .tag_hit = try getTagHit(&r), } } }, .resize => blk: { - var ev: pardes.Event = .{ .resize = .{ .cols = try r.getCols(), .rows = try r.getRows() } }; - const px_w = try r.getU16(); - const px_h = try r.getU16(); + var ev: pardes.Event = .{ .resize = .{ .cols = try getCols(&r), .rows = try getRows(&r) } }; + const px_w = try r.takeInt(u16, .little); + const px_h = try r.takeInt(u16, .little); // Read either way — the bytes are on the wire — and kept only by a // build that has somewhere to keep them. if (comptime has_cell_pixels) ev.resize.cell_pixels = .{ .w = px_w, .h = px_h }; ev.resize.row_metrics = try getMetrics(&r); break :blk .{ .event = ev }; }, - .paste => .{ .event = .{ .paste = try r.getSlice32() } }, - .command => .{ .event = .{ .command = try r.getSlice16() } }, - .pdf_scroll => .{ .event = .{ .pdf_scroll = .{ .pane = try r.getPane(), .delta_pixels = try r.getF32() } } }, - .pinch => .{ .event = .{ .pinch = try r.getF32() } }, - .touch_scroll => .{ .event = .{ .touch_scroll = try r.getF32() } }, + .paste => .{ .event = .{ .paste = try getSlice32(&r) } }, + .command => .{ .event = .{ .command = try r.take(try r.takeInt(u16, .little)) } }, + .pdf_scroll => .{ .event = .{ .pdf_scroll = .{ .pane = try getPane(&r), .delta_pixels = try getF32(&r) } } }, + .pinch => .{ .event = .{ .pinch = try getF32(&r) } }, + .touch_scroll => .{ .event = .{ .touch_scroll = try getF32(&r) } }, .pointer_leave => .{ .event = .pointer_leave }, - .focus => .{ .event = .{ .focus = try r.getBool() } }, + .focus => .{ .event = .{ .focus = try getBool(&r) } }, }; - try r.end(); + try end(&r); return msg; } @@ -1161,42 +1065,42 @@ fn serverTag(msg: ServerMsg) ServerTag { } /// Every server message EXCEPT a frame, which has its own encoder because its -/// payload is a walk of two grids rather than a value (see `encodeFrame`). +/// payload is a walk of two grids rather than a value (see `encodeFrameLayers`). pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { - var w: Writer = .init(out); + var w: std.Io.Writer = .fixed(out); const at = try beginMessage(&w, @backingInt(serverTag(msg))); switch (msg) { .welcome => |v| { - try w.putU16(v.version); - try w.putByte(v.slot); - try w.putU16(v.cols); - try w.putU16(v.rows); + try w.writeInt(u16, v.version, .little); + try w.writeByte(v.slot); + try w.writeInt(u16, v.cols, .little); + try w.writeInt(u16, v.rows, .little); }, - .refuse => |why| try w.putByte(@backingInt(why)), + .refuse => |why| try w.writeByte(@backingInt(why)), // A frame's runs are not a value this union can hold; the server calls - // encodeFrame directly and this arm exists so the switch stays + // encodeFrameLayers directly and this arm exists so the switch stays // exhaustive over ServerMsg. .frame => return error.BadValue, .quit, .detach => {}, - .set_clipboard => |t| try w.putSlice32(t), + .set_clipboard => |t| try putSlice32(&w, t), .read_clipboard => {}, - .open_link => |u| try w.putSlice16(u), + .open_link => |u| try putSlice16(&w, u), .post => |p| { - try w.putByte(@backingInt(p.animation)); - try w.putByte(p.len); + try w.writeByte(@backingInt(p.animation)); + try w.writeByte(p.len); for (p.list()) |pass| { // 0 a file, else a bundled scene and one. - try w.putByte(if (pass.scene) |scene| @as(u8, @backingInt(scene)) + 1 else 0); - try w.putByte(pass.level); - try w.putByte(@intFromBool(pass.animated)); - try w.putU32(pass.revision); - try w.putSlice16(pass.path); - try w.putSlice32(pass.spirv); + try w.writeByte(if (pass.scene) |scene| @as(u8, @backingInt(scene)) + 1 else 0); + try w.writeByte(pass.level); + try w.writeByte(@intFromBool(pass.animated)); + try w.writeInt(u32, pass.revision, .little); + try putSlice16(&w, pass.path); + try putSlice32(&w, pass.spirv); } }, } try finishMessage(&w, at); - return w.written(); + return w.buffered(); } /// Slack over a message's variable payload, covering every fixed field any @@ -1239,33 +1143,33 @@ pub fn clientBound(msg: ClientMsg) usize { } pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { - var r: Reader = .init(payload); + var r: std.Io.Reader = .fixed(payload); const msg: ServerMsg = switch (std.enums.fromInt(ServerTag, tag) orelse return error.BadTag) { .welcome => .{ .welcome = .{ - .version = try r.getU16(), - .slot = try r.getByte(), - .cols = try r.getCols(), - .rows = try r.getRows(), + .version = try r.takeInt(u16, .little), + .slot = try r.takeByte(), + .cols = try getCols(&r), + .rows = try getRows(&r), } }, - .refuse => .{ .refuse = try r.getTag(Refusal) }, + .refuse => .{ .refuse = try getTag(&r, Refusal) }, .frame => blk: { - const header = try r.getTag(FrameHeader); + const header = try getTag(&r, FrameHeader); const kind: FrameKind = switch (header) { .full, .full_link => .full, .diff, .diff_link => .diff, }; - const cols = try r.getCols(); - const rows = try r.getRows(); + const cols = try getCols(&r); + const rows = try getRows(&r); const cursor = try getCursor(&r, cols, rows); - const layers = try r.getSlice32(); - const nruns = try r.getU32(); + const layers = try getSlice32(&r); + const nruns = try r.takeInt(u32, .little); // One run per cell is the worst an encoder emits, so a bigger // count cannot be describing this grid. Checked HERE rather than // in `apply`'s loop so the number is refused before it is used to // bound anything. if (nruns > @as(u32, cols) * @as(u32, rows)) return error.Overlong; - const runs = r.bytes[r.i..]; - r.i = r.bytes.len; + const runs = r.buffered(); + r.tossBuffered(); break :blk .{ .frame = .{ .layers = layers, .kind = kind, @@ -1279,32 +1183,32 @@ pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { }, .quit => .quit, .detach => .detach, - .set_clipboard => .{ .set_clipboard = try r.getSlice32() }, + .set_clipboard => .{ .set_clipboard = try getSlice32(&r) }, .read_clipboard => .read_clipboard, - .open_link => .{ .open_link = try r.getSlice16() }, + .open_link => .{ .open_link = try r.take(try r.takeInt(u16, .little)) }, .post => blk: { - var p: Post = .{ .animation = try r.getTag(ShaderAnimation) }; - p.len = try r.getByte(); + var p: Post = .{ .animation = try getTag(&r, ShaderAnimation) }; + p.len = try r.takeByte(); if (p.len > ShaderBuild.max) return error.Overlong; for (p.passes[0..p.len]) |*pass| { - const kind = try r.getByte(); + const kind = try r.takeByte(); pass.* = .{ .scene = if (kind == 0) null else std.enums.fromInt(Scene, kind - 1) orelse return error.BadValue, - .level = try r.getByte(), - .animated = switch (try r.getByte()) { + .level = try r.takeByte(), + .animated = switch (try r.takeByte()) { 0 => false, 1 => true, else => return error.BadValue, }, - .revision = try r.getU32(), - .path = try r.getSlice16(), - .spirv = try r.getSlice32(), + .revision = try r.takeInt(u32, .little), + .path = try r.take(try r.takeInt(u16, .little)), + .spirv = try getSlice32(&r), }; } break :blk .{ .post = p }; }, }; - try r.end(); + try end(&r); return msg; } @@ -1498,7 +1402,7 @@ test "detached wire: a session is never told to do a frontend's remembering" { for (0..256) |i| { const tag: u8 = @intCast(i); // Empty payloads on purpose: what is asked is whether the TAG is known. - // A known one fails later (`Truncated`) or succeeds, never with + // A known one fails later (`EndOfStream`) or succeeds, never with // `BadTag`. if (decodeClient(tag, &.{})) |_| accepted += 1 else |err| switch (err) { error.BadTag => continue, @@ -1578,7 +1482,7 @@ test "detached wire: a full frame carries the grid, a diff carries the change" { // FULL: nothing on the far side is comparable, which is the late joiner // and the resize both. - const full_bytes = try encodeFrame(buf, cols, rows, .{ .x = 3, .y = 4, .bar = true }, cells, &.{}); + const full_bytes = try encodeFrameLayers(buf, cols, rows, .{ .x = 3, .y = 4, .bar = true }, .arrow, cells, &.{}, &.{}, &.{}, &.{}, .{}); { const f = (try framed(full_bytes)).?; const msg = (try decodeServer(f.tag, f.payload)).frame; @@ -1598,7 +1502,7 @@ test "detached wire: a full frame carries the grid, a diff carries the change" { defer gpa.free(before); cells[100] = .{ .text = "q".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; cells[0] = .{}; // ...and one goes back to unpainted, which a diff must say - const diff_bytes = try encodeFrame(buf, cols, rows, null, cells, before); + const diff_bytes = try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, before, &.{}, &.{}, &.{}, .{}); { const f = (try framed(diff_bytes)).?; const msg = (try decodeServer(f.tag, f.payload)).frame; @@ -1612,7 +1516,7 @@ test "detached wire: a full frame carries the grid, a diff carries the change" { // An unchanged frame is the head and nothing else: no runs, and the // receiver keeps what it has. This is what makes an idle session silent. { - const idle = try encodeFrame(buf, cols, rows, null, cells, cells); + const idle = try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, cells, &.{}, &.{}, &.{}, .{}); try testing.expectEqual(@as(usize, header_len + frame_head), idle.len); const f = (try framed(idle)).?; const msg = (try decodeServer(f.tag, f.payload)).frame; @@ -1648,7 +1552,7 @@ test "detached wire: the largest grid is one cell past a run, and survives it" { for (cells) |*c| c.* = .{ .text = "a".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; @memset(mirror, .{}); - const bytes = try encodeFrame(buf, cols, rows, null, cells, &.{}); + const bytes = try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, &.{}, &.{}, &.{}, &.{}, .{}); const f = (try framed(bytes)).?; const msg = (try decodeServer(f.tag, f.payload)).frame; try testing.expectEqual(FrameKind.full, msg.kind); @@ -1658,7 +1562,7 @@ test "detached wire: the largest grid is one cell past a run, and survives it" { try expectGridEqual(cells, mirror); // ...at exactly the two headers the split costs and not one byte more. // `bytes.len <= frameBound(...)` would prove nothing here — the buffer IS - // `frameBound` bytes, so an over-run is `NoSpace` above, not a false + // `frameBound` bytes, so an over-run is `WriteFailed` above, not a false // assertion here — and what wants pinning is that the second run is the // whole of the cost. try testing.expectEqual( @@ -1668,7 +1572,7 @@ test "detached wire: the largest grid is one cell past a run, and survives it" { } test "detached wire: the diff is worth having, in bytes, on the board's grid" { - // The numbers quoted in `encodeFrame`'s comment, asserted so the claim + // The numbers quoted in `encodeFrameLayers`'s comment, asserted so the claim // cannot rot. 56x14 is the ESP32-P4 board's default grid (esp32p4.zig). const cols: u16 = 56; const rows: u16 = 14; @@ -1683,7 +1587,7 @@ test "detached wire: the diff is worth having, in bytes, on the board's grid" { // Every cell painted with a plain ASCII glyph and default colors, which is // what a pardes frame overwhelmingly is: eight bytes a cell. for (cells) |*c| c.* = .{ .text = "a".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - const full = (try encodeFrame(buf, cols, rows, null, cells, &.{})).len; + const full = (try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, &.{}, &.{}, &.{}, &.{}, .{})).len; try testing.expectEqual(@as(usize, header_len + frame_head + run_header + n * 8), full); try testing.expectEqual(@as(usize, 6302), full); @@ -1694,7 +1598,7 @@ test "detached wire: the diff is worth having, in bytes, on the board's grid" { cells[300] = .{ .text = "b".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; cells[301] = .{ .text = "c".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; cells[500] = .{ .text = "d".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - const diff = (try encodeFrame(buf, cols, rows, null, cells, before)).len; + const diff = (try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, before, &.{}, &.{}, &.{}, .{})).len; try testing.expectEqual(@as(usize, header_len + frame_head + 2 * run_header + 3 * 8), diff); try testing.expectEqual(@as(usize, 60), diff); // The whole reason this codec exists rather than shipping the Surface. @@ -1713,7 +1617,7 @@ test "detached wire: a run is coalesced across a gap only when that is cheaper" cells[0] = .{ .text = "a".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; cells[4] = .{ .text = "b".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; { - const f = (try framed(try encodeFrame(&buf, cols, rows, null, &cells, &prev))).?; + const f = (try framed(try encodeFrameLayers(&buf, cols, rows, null, .arrow, &cells, &prev, &.{}, &.{}, &.{}, .{}))).?; try testing.expectEqual(@as(u32, 1), (try decodeServer(f.tag, f.payload)).frame.nruns); } // Now put a PAINTED cell in the gap that both sides already agree about. @@ -1722,12 +1626,12 @@ test "detached wire: a run is coalesced across a gap only when that is cheaper" cells[2] = painted; prev[2] = painted; { - const f = (try framed(try encodeFrame(&buf, cols, rows, null, &cells, &prev))).?; + const f = (try framed(try encodeFrameLayers(&buf, cols, rows, null, .arrow, &cells, &prev, &.{}, &.{}, &.{}, .{}))).?; try testing.expectEqual(@as(u32, 2), (try decodeServer(f.tag, f.payload)).frame.nruns); } // Whichever it chose, the receiver ends up with the same grid. var mirror: [8]pardes.Cell = prev; - const f = (try framed(try encodeFrame(&buf, cols, rows, null, &cells, &prev))).?; + const f = (try framed(try encodeFrameLayers(&buf, cols, rows, null, .arrow, &cells, &prev, &.{}, &.{}, &.{}, .{}))).?; try (try decodeServer(f.tag, f.payload)).frame.apply(&mirror); try expectGridEqual(&cells, &mirror); } @@ -1746,7 +1650,7 @@ test "detached wire: a truncated frame is refused at every length" { // ...and the same bytes handed to the decoder with the header's length // left claiming the whole message, which is how a decoder is walked // off the end of its buffer. - try testing.expectError(error.Truncated, decodeClient(copy[0], copy[header_len..cut])); + try testing.expectError(error.EndOfStream, decodeClient(copy[0], copy[header_len..cut])); } // Shorter than the header itself is not yet a message at all. for (0..header_len + 1) |cut| @@ -1789,7 +1693,7 @@ test "detached wire: an over-long length prefix is refused before it is believed var paste: [8]u8 = undefined; std.mem.writeInt(u32, paste[0..4], 4096, .little); @memcpy(paste[4..8], "abcd"); - try testing.expectError(error.Truncated, decodeClient(@backingInt(ClientTag.paste), &paste)); + try testing.expectError(error.EndOfStream, decodeClient(@backingInt(ClientTag.paste), &paste)); // ...and past the cap, which is refused rather than read. std.mem.writeInt(u32, paste[0..4], max_payload + 1, .little); try testing.expectError(error.Overlong, decodeClient(@backingInt(ClientTag.paste), &paste)); @@ -1836,14 +1740,14 @@ test "detached wire: a frame that lies about its runs cannot walk out of the gri // A run count larger than the grid has cells is refused at DECODE, before // it is used to bound the walk. var head: [frame_head]u8 = undefined; - var w: Writer = .init(&head); - try w.putByte(@backingInt(FrameKind.diff)); - try w.putU16(cols); - try w.putU16(rows); + var w: std.Io.Writer = .fixed(&head); + try w.writeByte(@backingInt(FrameKind.diff)); + try w.writeInt(u16, cols, .little); + try w.writeInt(u16, rows, .little); try putCursor(&w, null, cols, rows); - try w.putU32(0); // no body layers - try w.putU32(9); - try testing.expectError(error.Overlong, decodeServer(@backingInt(ServerTag.frame), w.written())); + try w.writeInt(u32, 0, .little); // no body layers + try w.writeInt(u32, 9, .little); + try testing.expectError(error.Overlong, decodeServer(@backingInt(ServerTag.frame), w.buffered())); // ...and a grid of the wrong size is the receiver's own bug, not a frame // it should paint half of. @@ -1863,7 +1767,7 @@ test "detached wire: link pointer survives full and unchanged grid frames" { var buffer: [256]u8 = undefined; for ([_]pardes.Surface.PointerShape{ .link, .arrow, .link }) |shape| { for ([_]bool{ true, false }) |full| { - const bytes = try encodeFramePointer(&buffer, 1, 1, null, shape, &cells, if (full) &.{} else &cells); + const bytes = try encodeFrameLayers(&buffer, 1, 1, null, shape, &cells, if (full) &.{} else &cells, &.{}, &.{}, &.{}, .{}); const packet = (try framed(bytes)).?; const frame = (try decodeServer(packet.tag, packet.payload)).frame; try std.testing.expectEqual(shape, frame.pointer_shape); @@ -1884,32 +1788,32 @@ test "detached wire: a cursor outside the grid is refused, not painted" { const rows: u16 = 2; var head: [frame_head]u8 = undefined; for ([_][2]u16{ .{ cols, 0 }, .{ 0, rows }, .{ 0xffff, 0xffff } }) |xy| { - var w: Writer = .init(&head); - try w.putByte(@backingInt(FrameKind.diff)); - try w.putU16(cols); - try w.putU16(rows); + var w: std.Io.Writer = .fixed(&head); + try w.writeByte(@backingInt(FrameKind.diff)); + try w.writeInt(u16, cols, .little); + try w.writeInt(u16, rows, .little); // Written by hand: `putCursor` now refuses this too, which is the // other half of the same fix and is asserted below. - try w.putBool(true); - try w.putU16(xy[0]); - try w.putU16(xy[1]); - try w.putBool(false); - try w.putU32(0); + try w.writeByte(@intFromBool(true)); + try w.writeInt(u16, xy[0], .little); + try w.writeInt(u16, xy[1], .little); + try w.writeByte(@intFromBool(false)); + try w.writeInt(u32, 0, .little); try testing.expectError( error.BadValue, - decodeServer(@backingInt(ServerTag.frame), w.written()), + decodeServer(@backingInt(ServerTag.frame), w.buffered()), ); } // The last cell IS in the grid, and a frame carrying it decodes. { - var w: Writer = .init(&head); - try w.putByte(@backingInt(FrameKind.diff)); - try w.putU16(cols); - try w.putU16(rows); + var w: std.Io.Writer = .fixed(&head); + try w.writeByte(@backingInt(FrameKind.diff)); + try w.writeInt(u16, cols, .little); + try w.writeInt(u16, rows, .little); try putCursor(&w, .{ .x = cols - 1, .y = rows - 1, .bar = true }, cols, rows); - try w.putU32(0); // no body layers - try w.putU32(0); - const got = (try decodeServer(@backingInt(ServerTag.frame), w.written())).frame; + try w.writeInt(u32, 0, .little); // no body layers + try w.writeInt(u32, 0, .little); + const got = (try decodeServer(@backingInt(ServerTag.frame), w.buffered())).frame; try testing.expectEqual(@as(u16, cols - 1), got.cursor.?.x); try testing.expectEqual(@as(u16, rows - 1), got.cursor.?.y); } @@ -1919,13 +1823,13 @@ test "detached wire: a cursor outside the grid is refused, not painted" { var buf: [512]u8 = undefined; try testing.expectError( error.BadValue, - encodeFrame(&buf, cols, rows, .{ .x = cols, .y = 0, .bar = false }, &cells, &.{}), + encodeFrameLayers(&buf, cols, rows, .{ .x = cols, .y = 0, .bar = false }, .arrow, &cells, &.{}, &.{}, &.{}, &.{}, .{}), ); // A grid past the protocol's own ceiling is refused by the encoder for the // same reason: `max_payload` is derived from it, so the decoder would. try testing.expectError( error.Overlong, - encodeFrame(&buf, max_cols + 1, 1, null, cells[0..0], &.{}), + encodeFrameLayers(&buf, max_cols + 1, 1, null, .arrow, cells[0..0], &.{}, &.{}, &.{}, &.{}, .{}), ); } @@ -1943,7 +1847,7 @@ test "detached wire: values a field cannot mean are refused" { )); // A pane the core cannot index. `pdf_scroll` reads its pane byte before the // f32 that follows, so a one-byte payload reaches the check this is about - // rather than tripping `Truncated` first. + // rather than tripping `EndOfStream` first. try testing.expectError(error.BadValue, decodeClient( @backingInt(ClientTag.pdf_scroll), &.{pardes.MAX_PANES}, @@ -2002,10 +1906,10 @@ test "detached wire: max_payload bounds every message this protocol can build" { const room = try gpa.alloc(u8, huge.len + header_len + 2); defer gpa.free(room); try testing.expectError(error.Overlong, encodeClient(room, .{ .event = .{ .command = huge } })); - // ...and a buffer the caller sized too small is NoSpace, which is the + // ...and a buffer the caller sized too small is WriteFailed, which is the // server's signal to drop that one message rather than the client. var small: [8]u8 = undefined; - try testing.expectError(error.NoSpace, encodeClient(&small, .{ .event = .{ .paste = "0123456789" } })); + try testing.expectError(error.WriteFailed, encodeClient(&small, .{ .event = .{ .paste = "0123456789" } })); } test "detached wire carries compact body rows and exact pointer metrics" { @@ -2061,7 +1965,7 @@ test "detached wire carries compact body rows and exact pointer metrics" { try std.testing.expect(mouse_encoded.len <= clientBound(message)); var truncated = frame; truncated.layers = frame.layers[0 .. frame.layers.len - 1]; - try std.testing.expectError(error.Truncated, truncated.applyLayers(gpa, &decoded, &tags, ®ions, &chrome)); + try std.testing.expectError(error.EndOfStream, truncated.applyLayers(gpa, &decoded, &tags, ®ions, &chrome)); for (decoded) |item| { try std.testing.expectEqual(@as(u16, 0), item.rows); try std.testing.expectEqual(@as(usize, 0), item.cells.len); @@ -2072,7 +1976,7 @@ test "detached wire carries compact body rows and exact pointer metrics" { try std.testing.expectEqual(@as(usize, 0), item.cells.len); } try std.testing.expectEqual(@as(usize, 0), regions.items.len); - const no_layers = try encodeFramePointer(out, 2, 4, null, .arrow, &cells, &cells); + const no_layers = try encodeFrameLayers(out, 2, 4, null, .arrow, &cells, &cells, &.{}, &.{}, &.{}, .{}); const cleared_packet = (try framed(no_layers)).?; const cleared = (try decodeServer(cleared_packet.tag, cleared_packet.payload)).frame; try cleared.applyLayers(gpa, &decoded, &tags, ®ions, &chrome); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 9cd37b6b..a31c6548 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1135,8 +1135,8 @@ test "GUI completion survives a full output queue" { const PtyTests = struct { fn waitBlocked(queue: *Queue) !void { - const until = shellClock() + 2_000; - while (shellClock() < until) { + const until = std.Io.Clock.awake.now(std.testing.io).toMilliseconds() + 2_000; + while (std.Io.Clock.awake.now(std.testing.io).toMilliseconds() < until) { queue.lock(); const waiting = queue.waiting; queue.unlock(); @@ -1254,8 +1254,8 @@ test "GUI PTY reader delivers a real shell tail before EOF and joins on close" { var bytes: std.ArrayList(u8) = .empty; defer bytes.deinit(gpa); var eof = false; - const until = shellClock() + 2_000; - while (!eof and shellClock() < until) { + const until = std.Io.Clock.awake.now(std.testing.io).toMilliseconds() + 2_000; + while (!eof and std.Io.Clock.awake.now(std.testing.io).toMilliseconds() < until) { var batch = queue.take(); defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); for (batch.slice()) |msg| switch (msg) { @@ -1538,37 +1538,19 @@ fn watchThread(fd: c_int, stop: c_int, q: *Queue) void { } } -fn lspThread(io: std.Io, lsp_allocator: std.mem.Allocator, job: *host_io.Lsp.Job, q: *Queue) anyerror!void { - host_io.Lsp.work(io, lsp_allocator, job, q, pushLspRows); -} - fn pushLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { const q: *Queue = @ptrCast(@alignCast(ctx orelse return)); q.push(.{ .lsp = .{ .id = id, .rows = rows } }); } fn spawnLsp(core: *pardes.Pardes, io: std.Io, q: *Queue, e: host_io.Lsp.Request) void { - const lsp_allocator = q.lsp_allocator; q.lock(); q.lsp_id = e.id; q.discardLsp(); q.unlock(); - // Its waits are cancelation points: this join is short, and what it - // still delivers carries an id the queue now rejects. - if (q.lsp_task) |*old| { - old.future.cancel(io) catch {}; - q.lsp_task = null; - } - const job = host_io.Lsp.snapshot(lsp_allocator, core, e) catch |err| { - core.update(.{ .lsp_resp = .{ .id = e.id, .rows = null } }); - return core.reportError(e.pane, "lsp", err); - }; - const future = io.concurrent(lspThread, .{ io, lsp_allocator, job, q }) catch |err| { - job.free(lsp_allocator); - core.update(.{ .lsp_resp = .{ .id = e.id, .rows = null } }); - return core.reportError(e.pane, "lsp", err); - }; - q.lsp_task = .{ .id = e.id, .future = future }; + // The old query's waits are cancelation points: its join is short, and + // what it still delivers carries an id the queue now rejects. + host_io.Lsp.start(io, q.lsp_allocator, core, &q.lsp_task, e, q, pushLspRows); } test "GUI LSP query answers, and a newer one cancels it" { @@ -1584,7 +1566,7 @@ test "GUI LSP query answers, and a newer one cancels it" { spawnLsp(core, io, &queue, .{ .id = core.lsp_wait.?.id, .kind = .status, .pane = @intCast(core.active), .offset = 0, .arg = "" }); } const last = core.lsp_wait.?.id; - try queue.lsp_task.?.future.await(io); + queue.lsp_task.?.future.await(io); queue.lsp_task = null; var batch = queue.take(); defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); @@ -1595,37 +1577,11 @@ test "GUI LSP query answers, and a newer one cancels it" { try std.testing.expect(core.lsp_wait == null); } -fn pipeThread(io: std.Io, gpa: std.mem.Allocator, job: *selection_pipe.Job, q: *Queue) anyerror!void { - defer job.deinit(gpa); - const response = selection_pipe.runJob(gpa, io, job); +fn pushPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void { + const q: *Queue = @ptrCast(@alignCast(ctx.?)); q.push(.{ .pipe = response }); } -fn spawnPipe( - core: *pardes.Pardes, - io: std.Io, - gpa: std.mem.Allocator, - q: *Queue, - tasks: *PipeTasks, - id: u32, -) void { - if (tasks.len == tasks.items.len) { - core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return; - } - const view = core.pipe.pipeRequest(id) orelse return; - const job = selection_pipe.Job.copy(gpa, view) catch |err| { - core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return core.reportError(core.active, "pipe", err); - }; - const future = io.concurrent(pipeThread, .{ io, gpa, job, q }) catch |err| { - job.deinit(gpa); - core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return core.reportError(core.active, "pipe", err); - }; - std.debug.assert(tasks.add(.{ .id = id, .future = future })); -} - test "GUI worker setup failures finish matching LSP and pipe requests" { const gpa = std.testing.allocator; var failing_vtable = std.testing.io.vtable.*; @@ -1677,7 +1633,7 @@ test "GUI worker setup failures finish matching LSP and pipe requests" { core.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); core.update(.{ .key = .{ .cp = pardes.Key.enter } }); const pipe_id = core.pipe.wait.?.id; - spawnPipe(core, failing_io, failing.allocator(), &queue, &tasks, pipe_id); + selection_pipe.start(failing_io, failing.allocator(), core, &tasks, pipe_id, &queue, pushPipe); try std.testing.expect(core.pipe.wait == null); try std.testing.expectEqualStrings("abc", pane.file.?.content); try std.testing.expect(queue.lsp_task == null); @@ -2543,7 +2499,7 @@ fn localSession( .sdl_wake = true, }; defer { - if (queue.lsp_task) |*task| task.future.cancel(io) catch {}; + if (queue.lsp_task) |*task| task.future.cancel(io); queue.deinit(); } var pipe_tasks: PipeTasks = .{}; @@ -2914,7 +2870,7 @@ fn runGrid(init: std.process.Init, opts_in: pardes.Options) !void { .sdl_wake = false, }; defer { - if (queue.lsp_task) |*task| task.future.cancel(io) catch {}; + if (queue.lsp_task) |*task| task.future.cancel(io); queue.deinit(); } var pipe_tasks: PipeTasks = .{}; @@ -3377,7 +3333,7 @@ const Input = struct { const client = in.client orelse return; if (in.lost != null) return; client.send(.{ .event = ev }) catch |err| switch (err) { - error.Overlong, error.NoSpace => {}, + error.Overlong, error.WriteFailed => {}, else => in.lost = err, }; } @@ -4051,7 +4007,7 @@ const Shell = struct { test_hz: u64 = 60, fn reap(s: *Shell) void { - const now = shellClock(); + const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); for (&s.retired_shells) |*child| host_io.reapRetired(&child.pid, &child.kill_at, now); for (s.ptys) |*slot| if (slot.*) |*pt| if (pt.fd < 0) { host_io.reapRetired(&pt.pid, &pt.kill_at, now); @@ -4075,7 +4031,7 @@ const Shell = struct { } _ = libc.close(pt.fd); pt.fd = -1; - const now = shellClock(); + const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); host_io.reapRetired(&pt.pid, &pt.kill_at, now); if (pt.pid == 0) { s.ptys[pane] = null; @@ -4809,7 +4765,7 @@ fn lsp(ctx: ?*anyopaque, req: host_io.Lsp.Request) void { fn pipe(ctx: ?*anyopaque, id: u32) void { const s = shellOf(ctx); - if (s.threads_ok) return spawnPipe(s.core, s.io, s.gpa, s.queue, s.pipe_tasks, id); + if (s.threads_ok) return selection_pipe.start(s.io, s.gpa, s.core, s.pipe_tasks, id, s.queue, pushPipe); s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); s.core.reportError(s.core.active, "pipe", error.WorkersUnavailable); } @@ -4822,12 +4778,6 @@ fn pollCwds(core: *pardes.Pardes, ptys: *[pardes.MAX_PANES]?Pty) void { }; } -fn shellClock() i64 { - var ts: libc.timespec = undefined; - if (libc.clock_gettime(.MONOTONIC, &ts) != 0) return 0; - return @as(i64, ts.sec) * std.time.ms_per_s + @divTrunc(ts.nsec, std.time.ns_per_ms); -} - fn accumulateWheelDelta(pending: f32, raw_y: f32) f32 { if (!std.math.isFinite(raw_y)) return pending; const next = pending - raw_y; diff --git a/src/host_io.zig b/src/host_io.zig index 2c736e20..a17414fb 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -50,16 +50,10 @@ pub const Host = struct { }; }; -/// The shells' monotonic clock in ns, the one `Host.now` answers with. -/// PARDES_TEST_CLOCK makes a shell answer a virtual clock instead, moved -/// only when a timed wait runs out -- straight to the core's next wake -- so -/// the snapshot harness sees the same frames whatever the machine's load. -pub fn monotonicNs() u64 { - var ts: libc.timespec = undefined; - if (libc.clock_gettime(.MONOTONIC, &ts) != 0) return 0; - return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec)); -} - +/// PARDES_TEST_CLOCK makes a shell's `Host.now` answer a virtual clock +/// instead of `std.Io.Clock.awake`, moved only when a timed wait runs out -- +/// straight to the core's next wake -- so the snapshot harness sees the same +/// frames whatever the machine's load. pub fn testClock() ?u64 { return if (libc.getenv("PARDES_TEST_CLOCK")) |v| (if (std.mem.span(v).len != 0) 0 else null) else null; } @@ -131,7 +125,7 @@ pub const Lsp = struct { pub const Task = struct { id: u32, - future: std.Io.Future(anyerror!void), + future: std.Io.Future(void), }; /// One language query, owned by the worker that runs it. @@ -227,6 +221,26 @@ pub const Lsp = struct { // Null is failure; the receiver frees non-null rows with the job's allocator. pub const Deliver = *const fn (ctx: ?*anyopaque, id: u32, rows: ?[]u8) void; + /// Start `req` on a worker that hands its rows to `deliver`, the query + /// in `task` canceled first. One that cannot start is answered (no rows) + /// and reported here. + pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, task: *?Task, req: Request, ctx: ?*anyopaque, deliver: Deliver) void { + if (task.*) |*old| { + old.future.cancel(io); + task.* = null; + } + const job = snapshot(gpa, core, req) catch |err| { + core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); + return core.reportError(req.pane, "lsp", err); + }; + const future = io.concurrent(work, .{ io, gpa, job, ctx, deliver }) catch |err| { + job.free(gpa); + core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); + return core.reportError(req.pane, "lsp", err); + }; + task.* = .{ .id = req.id, .future = future }; + } + pub fn work(io: std.Io, gpa: std.mem.Allocator, job: *Job, ctx: ?*anyopaque, deliver: Deliver) void { defer job.free(gpa); // The query's memory is the fixed region memory.zig reserved at @@ -1910,10 +1924,8 @@ test "a terminal is where its shell is: under a wrapper the foreground shell, ne try std.testing.expect(sh.waitText("pardes-in-", 5_000)); // printf runs before the inner shell takes the foreground. var tries: usize = 0; - while (ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) == null and tries < 50) : (tries += 1) { - const ts: libc.timespec = .{ .sec = 0, .nsec = 20 * std.time.ns_per_ms }; - _ = libc.nanosleep(&ts, null); - } + while (ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) == null and tries < 50) : (tries += 1) + try std.testing.io.sleep(.fromMilliseconds(20), .awake); try std.testing.expectEqualStrings(start, shellCwd(child.pid, &result) orelse return error.MissingCwd); try std.testing.expectEqualStrings(inner, ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) orelse return error.MissingCwd); } @@ -2694,18 +2706,12 @@ fn haveFile(path: [*:0]const u8) bool { return true; } -pub fn nowMs() i64 { - var ts: libc.timespec = undefined; - _ = libc.clock_gettime(.MONOTONIC, &ts); - return @as(i64, @intCast(ts.sec)) * 1000 + @divFloor(@as(i64, @intCast(ts.nsec)), 1_000_000); +fn nowMs() i64 { + return std.Io.Clock.awake.now(std.testing.io).toMilliseconds(); } fn sleepMs(ms: i64) void { - const ts = libc.timespec{ - .sec = @intCast(@divFloor(ms, 1000)), - .nsec = @intCast(@mod(ms, 1000) * 1_000_000), - }; - _ = libc.nanosleep(&ts, null); + std.testing.io.sleep(.fromMilliseconds(ms), .awake) catch {}; } test "an idle prompt is free, a foreground job takes the tty, and Ctrl-C hands it back" { diff --git a/src/macos.zig b/src/macos.zig index 5d3dfec4..55f6b3f6 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -18,7 +18,7 @@ const image = if (pardes.pdf_enabled) @import("image.zig") else struct {}; const host_io = @import("host_io.zig"); const fonts = @import("fonts.zig"); // the shared fallback preference order const tracy = @import("tracy.zig"); // no-op unless -Dtracy names a checkout -const selection_pipe = @import("selection_pipe.zig"); // Job, runJob and Tasks +const selection_pipe = @import("selection_pipe.zig"); // start, Response and Tasks const crash = @import("crash.zig"); pub const panic = std.debug.FullPanic(struct { @@ -830,7 +830,7 @@ export fn pardes_deinit() void { pardes.lsp.setStatusSink(null, null); host_io.setExitWake(null, null); if (st.lsp_task) |*t| { - t.future.cancel(st.io) catch {}; + t.future.cancel(st.io); st.lsp_task = null; } st.pipe_tasks.cancelAll(st.io); @@ -1333,7 +1333,7 @@ fn drainInbox(st: *State) bool { .lsp_done => |d| { st.core.update(.{ .lsp_resp = .{ .id = d.id, .rows = d.rows } }); if (st.lsp_task) |*t| if (t.id == d.id) { - t.future.await(st.io) catch {}; + t.future.await(st.io); st.lsp_task = null; }; }, @@ -1397,7 +1397,7 @@ fn restoreCore(st: *State) bool { return false; }; if (st.lsp_task) |*task| { - task.future.cancel(st.io) catch {}; + task.future.cancel(st.io); st.lsp_task = null; } st.pipe_tasks.cancelAll(st.io); @@ -1592,7 +1592,7 @@ export fn pardes_animation_tick() bool { defer pardes.turn.rest(); const st = &(state orelse return false); - const now: u64 = @intCast(@max(0, monotonicNs())); + const now: u64 = @intCast(std.Io.Clock.awake.now(st.io).nanoseconds); const spend = spendTickTime(st.last_tick_ns, now, st.tick_bank_ns); st.last_tick_ns = now; st.tick_bank_ns = spend.bank_ns; @@ -1766,7 +1766,7 @@ export fn pardes_scroll_begin() void { st.scroll_velocity = 0; st.scroll_velocity_x = 0; st.scroll_coasting = false; - st.scroll_last_ns = monotonicNs(); + st.scroll_last_ns = std.Io.Clock.awake.now(st.io).nanoseconds; } /// Fingers lifted. A swipe that was still moving when it ended becomes a @@ -1786,7 +1786,7 @@ export fn pardes_scroll_end() void { st.scroll_last_ns = 0; st.scroll_coasting = false; // A hand that came to rest before lifting is not throwing anything. - if (last == 0 or monotonicNs() - last > 90 * std.time.ns_per_ms) { + if (last == 0 or std.Io.Clock.awake.now(st.io).nanoseconds - last > 90 * std.time.ns_per_ms) { st.scroll_velocity = 0; st.scroll_velocity_x = 0; return; @@ -1804,7 +1804,7 @@ export fn pardes_rotate(degrees: f32) void { st.rotate_lag = 0; st.rotate_velocity = 0; st.rotate_coasting = false; - st.rotate_last_ns = monotonicNs(); + st.rotate_last_ns = std.Io.Clock.awake.now(st.io).nanoseconds; return; } noteRotationVelocity(st, degrees); @@ -1818,7 +1818,7 @@ export fn pardes_rotate_end() void { const last = st.rotate_last_ns; st.rotate_last_ns = 0; st.rotate_coasting = false; - if (last == 0 or monotonicNs() - last > 90 * std.time.ns_per_ms) { + if (last == 0 or std.Io.Clock.awake.now(st.io).nanoseconds - last > 90 * std.time.ns_per_ms) { st.rotate_velocity = 0; return; } @@ -1826,14 +1826,8 @@ export fn pardes_rotate_end() void { st.rotate_coasting = st.rotate_velocity != 0; } -fn monotonicNs() i128 { - var ts: libc.timespec = undefined; - if (libc.clock_gettime(.MONOTONIC, &ts) != 0) return 0; - return @as(i128, ts.sec) * std.time.ns_per_s + ts.nsec; -} - fn noteRotationVelocity(st: *State, degrees: f32) void { - const now = monotonicNs(); + const now = std.Io.Clock.awake.now(st.io).nanoseconds; const last = st.rotate_last_ns; st.rotate_last_ns = now; st.rotate_coasting = false; @@ -2380,52 +2374,20 @@ fn hostFor(st: *State) pardes.Host { fn lspRequest(ctx: ?*anyopaque, req: host_io.Lsp.Request) void { const st = hostState(ctx); - const job = host_io.Lsp.snapshot(st.gpa, st.core, req) catch |err| { - st.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); - return st.core.reportError(req.pane, "lsp", err); - }; - if (st.lsp_task) |*old| { - old.future.cancel(st.io) catch {}; - st.lsp_task = null; - } - const future = st.io.concurrent(lspWorker, .{ st, job }) catch |err| { - job.free(st.gpa); - st.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); - return st.core.reportError(req.pane, "lsp", err); - }; - st.lsp_task = .{ .id = req.id, .future = future }; + host_io.Lsp.start(st.io, st.gpa, st.core, &st.lsp_task, req, st, deliverLspRows); } fn pipeRequest(ctx: ?*anyopaque, id: u32) void { const st = hostState(ctx); - if (st.pipe_tasks.full()) { - st.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return; - } - const view = st.core.pipe.pipeRequest(id) orelse return; - const job = selection_pipe.Job.copy(st.gpa, view) catch |err| { - st.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return st.core.reportError(st.core.active, "pipe", err); - }; - const future = st.io.concurrent(pipeWorker, .{ st, job }) catch |err| { - job.deinit(st.gpa); - st.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return st.core.reportError(st.core.active, "pipe", err); - }; - std.debug.assert(st.pipe_tasks.add(.{ .id = id, .future = future })); + selection_pipe.start(st.io, st.gpa, st.core, &st.pipe_tasks, id, st, deliverPipe); } -fn pipeWorker(st: *State, job: *selection_pipe.Job) anyerror!void { - defer job.deinit(st.gpa); - const response = selection_pipe.runJob(st.gpa, st.io, job); +fn deliverPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void { + const st: *State = @ptrCast(@alignCast(ctx orelse return)); st.inbox.push(st.gpa, st.io, .{ .pipe = response }); wake(st); } -fn lspWorker(st: *State, job: *host_io.Lsp.Job) anyerror!void { - host_io.Lsp.work(st.io, st.gpa, job, st, deliverLspRows); -} - fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { const st: *State = @ptrCast(@alignCast(ctx orelse return)); st.inbox.push(st.gpa, st.io, .{ .lsp_done = .{ .id = id, .rows = rows } }); @@ -2662,7 +2624,7 @@ fn encodeCellFlags(default: bool, role: pardes.FontRole, hover: bool) u8 { } fn noteScrollVelocity(st: *State, delta_rows: f32, delta_cols: f32) void { - const now = monotonicNs(); + const now = std.Io.Clock.awake.now(st.io).nanoseconds; const last = st.scroll_last_ns; st.scroll_last_ns = now; // Any real travel ends a coast: that is what lets a host with momentum of diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index 229b3d67..73bac520 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -1,10 +1,11 @@ //! Native runner and boundary-owned values for `|`: pipe every editor //! selection through one shell command. The core imports only the plain value -//! types below; native shells call `Job.copy` before starting a worker, then -//! `runOne` on that worker. No subprocess or borrowed core memory reaches the -//! editor/event-loop thread. +//! types below; native shells call `start`, which copies the request +//! (`Job.copy`) and runs it on a worker. No subprocess or borrowed core +//! memory reaches the editor/event-loop thread. const std = @import("std"); const filesystem = @import("fs.zig"); +const pardes = @import("pardes.zig"); /// A deliberately finite answer. One selection cannot retain more than 1 MiB /// and a multi-selection command cannot retain more than 4 MiB in total. @@ -180,7 +181,7 @@ pub const Tasks = struct { pub const Task = struct { id: u32, - future: std.Io.Future(anyerror!void), + future: std.Io.Future(void), }; items: [capacity]Task = undefined, @@ -201,7 +202,7 @@ pub const Tasks = struct { /// stays deterministic. pub fn finish(tasks: *Tasks, io: std.Io, id: u32) void { for (tasks.items[0..tasks.len], 0..) |*task, i| if (task.id == id) { - task.future.await(io) catch {}; + task.future.await(io); tasks.len -= 1; std.mem.copyForwards(Task, tasks.items[i..tasks.len], tasks.items[i + 1 .. tasks.len + 1]); return; @@ -209,11 +210,36 @@ pub const Tasks = struct { } pub fn cancelAll(tasks: *Tasks, io: std.Io) void { - for (tasks.items[0..tasks.len]) |*task| task.future.cancel(io) catch {}; + for (tasks.items[0..tasks.len]) |*task| task.future.cancel(io); tasks.len = 0; } }; +/// Where a worker hands its response; the host owns it from there. +pub const Sink = *const fn (ctx: ?*anyopaque, response: Response) void; + +/// Start pipe `id` on a worker that hands its response to `sink`. One that +/// cannot start (`tasks` full, no memory, no worker) is answered failed here. +pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, tasks: *Tasks, id: u32, ctx: ?*anyopaque, sink: Sink) void { + if (tasks.full()) return core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); + const view = core.pipe.pipeRequest(id) orelse return; + const job = Job.copy(gpa, view) catch |err| { + core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); + return core.reportError(core.active, "pipe", err); + }; + const future = io.concurrent(work, .{ io, gpa, job, ctx, sink }) catch |err| { + job.deinit(gpa); + core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); + return core.reportError(core.active, "pipe", err); + }; + std.debug.assert(tasks.add(.{ .id = id, .future = future })); +} + +fn work(io: std.Io, gpa: std.mem.Allocator, job: *Job, ctx: ?*anyopaque, sink: Sink) void { + defer job.deinit(gpa); + sink(ctx, runJob(gpa, io, job)); +} + /// The stdin writer's own: its copy of the input, freed by the writer when /// it is done, so a writer stuck on a command that never reads (and cannot /// be killed, below) can be let go of without its input dying under it. diff --git a/src/tty/tty.zig b/src/tty/tty.zig index afc0dbf0..5eecb3e9 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -755,7 +755,6 @@ test "terminal input cancellation joins blocked reads and queued EOF" { } } -const PipeTask = selection_pipe.Tasks.Task; const PipeTasks = selection_pipe.Tasks; const Pty = struct { @@ -1351,7 +1350,7 @@ fn localSession( slot.* = null; }; if (sh.lsp_task) |*t| { - t.future.cancel(io) catch {}; + t.future.cancel(io); sh.lsp_task = null; } sh.pipe_tasks.cancelAll(io); @@ -1417,7 +1416,7 @@ fn localSession( break :blk; }; if (sh.lsp_task) |*task| { - task.future.cancel(io) catch {}; + task.future.cancel(io); sh.lsp_task = null; } sh.pipe_tasks.cancelAll(io); @@ -1562,7 +1561,7 @@ const Shell = struct { fn clockNow(ctx: ?*anyopaque) u64 { const s = of(ctx); - return s.test_clock orelse host_io.monotonicNs(); + return s.test_clock orelse @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); } fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { @@ -1700,7 +1699,7 @@ const Shell = struct { core.update(.{ .lsp_resp = .{ .id = d.id, .rows = d.rows } }); if (d.rows) |rows| s.lsp_gpa.free(rows); if (s.lsp_task) |*t| if (t.id == d.id) { - t.future.await(s.io) catch {}; + t.future.await(s.io); s.lsp_task = null; }; }, @@ -1758,7 +1757,7 @@ const Shell = struct { s.tracks = canonical.panelTracks(); // The last frame is not answered yet: this one stays owed, and the // answer's wake draws the state as it is then. - if (FrameAck.holds(host_io.monotonicNs(), s.io)) { + if (FrameAck.holds(@intCast(std.Io.Clock.awake.now(s.io).nanoseconds), s.io)) { s.core.present_skipped = true; return; } @@ -1868,7 +1867,7 @@ const Shell = struct { const tz_render = tracy.zone(@src(), "vx.render"); const before = s.tty.tty_writer.pos + s.tty.writer().end; vx.render(s.tty.writer()) catch {}; - if (s.tty.tty_writer.pos + s.tty.writer().end != before) FrameAck.ask(s.tty.writer(), host_io.monotonicNs()); + if (s.tty.tty_writer.pos + s.tty.writer().end != before) FrameAck.ask(s.tty.writer(), @intCast(std.Io.Clock.awake.now(s.io).nanoseconds)); tz_render.end(); } @@ -2011,20 +2010,12 @@ const Shell = struct { s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); return s.core.reportError(req.pane, "lsp", error.WorkersUnavailable); } - const job = host_io.Lsp.snapshot(s.lsp_gpa, s.core, req) catch |err| { - s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); - return s.core.reportError(req.pane, "lsp", err); - }; - if (s.lsp_task) |*old| { - old.future.cancel(s.io) catch {}; - s.lsp_task = null; - } - const future = s.io.concurrent(lspWorker, .{ s.io, s.lsp_gpa, job, s.loop }) catch |err| { - job.free(s.lsp_gpa); - s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); - return s.core.reportError(req.pane, "lsp", err); - }; - s.lsp_task = .{ .id = req.id, .future = future }; + host_io.Lsp.start(s.io, s.lsp_gpa, s.core, &s.lsp_task, req, s, deliverLspRows); + } + + fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { + const s = of(ctx); + s.loop.postEvent(.{ .lsp_done = .{ .id = id, .rows = rows } }) catch if (rows) |owned| s.lsp_gpa.free(owned); } fn pipe(ctx: ?*anyopaque, id: u32) void { @@ -2033,21 +2024,13 @@ const Shell = struct { s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); return s.core.reportError(s.core.active, "pipe", error.WorkersUnavailable); } - if (s.pipe_tasks.full()) { - s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return; - } - const view = s.core.pipe.pipeRequest(id) orelse return; - const job = selection_pipe.Job.copy(s.gpa, view) catch |err| { - s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return s.core.reportError(s.core.active, "pipe", err); - }; - const future = s.io.concurrent(pipeWorker, .{ s.io, s.gpa, job, s.loop }) catch |err| { - job.deinit(s.gpa); - s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } }); - return s.core.reportError(s.core.active, "pipe", err); - }; - std.debug.assert(s.pipe_tasks.add(.{ .id = id, .future = future })); + selection_pipe.start(s.io, s.gpa, s.core, &s.pipe_tasks, id, s, deliverPipe); + } + + fn deliverPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void { + const s = of(ctx); + var owned = response; + s.loop.postEvent(.{ .pipe_done = owned }) catch owned.deinit(s.gpa); } }; @@ -2153,23 +2136,6 @@ fn requestClipboard(vx: *vaxis.Vaxis, tty: *vaxis.Tty) void { vx.requestSystemClipboard(tty.writer()) catch {}; } -fn lspWorker(io: std.Io, allocator: std.mem.Allocator, job: *host_io.Lsp.Job, loop: *Loop) anyerror!void { - var sink: LspRowSink = .{ .allocator = allocator, .loop = loop }; - host_io.Lsp.work(io, allocator, job, &sink, LspRowSink.take); -} - -const LspRowSink = struct { - allocator: std.mem.Allocator, - loop: *Loop, - - fn take(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { - const s: *LspRowSink = @ptrCast(@alignCast(ctx orelse return)); - s.loop.postEvent(.{ .lsp_done = .{ .id = id, .rows = rows } }) catch { - if (rows) |owned| s.allocator.free(owned); - }; - } -}; - fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void { const s: *Shell = @ptrCast(@alignCast(ctx orelse return)); const copy = s.lsp_gpa.dupe(u8, text) catch return; @@ -2177,17 +2143,6 @@ fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void { if (!posted) s.lsp_gpa.free(copy); } -fn pipeWorker( - io: std.Io, - gpa: std.mem.Allocator, - job: *selection_pipe.Job, - loop: *Loop, -) anyerror!void { - defer job.deinit(gpa); - var response = selection_pipe.runJob(gpa, io, job); - loop.postEvent(.{ .pipe_done = response }) catch response.deinit(gpa); -} - fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void { if (comptime builtin.os.tag != .linux) { while (file_watch.wait(fd)) loop.postEvent(.files_changed) catch break; @@ -2254,9 +2209,9 @@ fn wakeWatch(io: std.Io, loop: *Loop) void { fn timeWake(io: std.Io, seen: *u32) bool { while (true) { const ms = wake_ms.load(.acquire); - const deadline = host_io.monotonicNs() + @as(u64, ms) * std.time.ns_per_ms; + const deadline = @as(u64, @intCast(std.Io.Clock.awake.now(io).nanoseconds)) + @as(u64, ms) * std.time.ns_per_ms; while (true) { - const now = host_io.monotonicNs(); + const now: u64 = @intCast(std.Io.Clock.awake.now(io).nanoseconds); if (now >= deadline) return true; io.futexWaitTimeout(u32, &wake_gen.raw, seen.*, .{ .duration = .{ .raw = .fromNanoseconds(@intCast(deadline - now)), @@ -2280,14 +2235,13 @@ test "a short wake request cuts short a long one still sleeping" { fn run(timer_io: std.Io, start_seen: u32, done: *std.atomic.Value(u64)) void { var local = start_seen; _ = timeWake(timer_io, &local); - done.store(host_io.monotonicNs(), .release); + done.store(@intCast(std.Io.Clock.awake.now(timer_io).nanoseconds), .release); } }; var done: std.atomic.Value(u64) = .init(0); - const start = host_io.monotonicNs(); + const start: u64 = @intCast(std.Io.Clock.awake.now(io).nanoseconds); const thread = try std.Thread.spawn(.{}, Timer.run, .{ io, seen, &done }); - var pause: std.c.timespec = .{ .sec = 0, .nsec = 50 * std.time.ns_per_ms }; - _ = std.c.nanosleep(&pause, null); + try io.sleep(.fromMilliseconds(50), .awake); requestWake(io, 16); thread.join(); // The 16 ms request lands about 66 ms in: well before the 800 ms one @@ -2898,7 +2852,7 @@ const Attach = struct { fn send(a: *Attach, ev: pardes.Event) ?AttachEnd { a.client.send(.{ .event = ev }) catch |err| switch (err) { - error.Overlong, error.NoSpace => return null, + error.Overlong, error.WriteFailed => return null, else => return .{ .lost = err }, }; return null; -- cgit v1.3