From d0785f699f3fe921d00da878e908a5eede3c28e1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:18:19 -0300 Subject: Pipe: a pipe's answer is spliced in by sam_edit.apply, its cuts and outputs as ops, and each range is put over its output by a running delta, not by a second hand-written copy loop Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/Pipe.zig | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/src/Pipe.zig b/src/Pipe.zig index 903c1dba..c8fb83db 100644 --- a/src/Pipe.zig +++ b/src/Pipe.zig @@ -9,6 +9,7 @@ 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 sam = @import("sam_edit.zig"); const Pane = panes.Pane; const Text = panes.Text; const Key = pardes.Key; @@ -308,44 +309,29 @@ pub fn pipeResponse( return t.setRanges(content, kept[0..m], &.{}, pri orelse m - 1, wait.explicit); } + // Each range's cut with its output in place, in order: one splice of + // the text (sam_edit.apply), and each range over the output it got. const n = wait.nranges; + var ops: [panes.Text.max_selections]sam.Op = undefined; var total_output: usize = 0; - var removed: usize = 0; - var previous_end: usize = 0; for (0..n) |i| { const cut = pipeCut(&wait, content, i) orelse return; const output = pipeOutput(&wait, outputs, 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; + if (i > 0 and cut.lo < ops[i - 1].q1) return; + ops[i] = .{ .q0 = @intCast(cut.lo), .q1 = @intCast(cut.hi), .text = output }; } - const kept = 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; - + const replacement = sam.apply(p.gpa, content, ops[0..n]) catch return; var new_ranges: [panes.Text.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, content, i).?; - const output = pipeOutput(&wait, outputs, content, i); - @memcpy(replacement[write_at .. write_at + (cut.lo - read_at)], 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; + var delta: isize = 0; + for (ops[0..n], wait.ranges[0..n], new_ranges[0..n]) |op, range, *moved| { + const start: usize = @intCast(@as(isize, op.q0) + delta); + const end = start + op.text.len; + delta += @as(isize, @intCast(op.text.len)) - @as(isize, op.q1 - op.q0); + moved.* = if (range.anchor > range.head) .{ .anchor = end, .head = start } else .{ .anchor = start, .head = end }; } - @memcpy(replacement[write_at..], content[read_at..]); // One async request is one history transaction, even at 64 cursors. edit.pushUndo(p, t); -- cgit v1.3