summaryrefslogtreecommitdiff
path: root/src/edit.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 11:02:29 -0300
committerGabriel Schneider <[email protected]>2026-11-05 13:14:28 -0300
commit8640107e29a940d50a5eef0dc7d12689a515d856 (patch)
tree13eb927f31e34d8b50366b3cabc8491c04debdcb /src/edit.zig
parent3f1533813b6692a736251e4d467a7f108229ddee (diff)
downloadpardes-8640107e29a940d50a5eef0dc7d12689a515d856.tar.gz
pardes-8640107e29a940d50a5eef0dc7d12689a515d856.zip
text journal, the primitive: File.replace(off, len, bytes) puts an edit in the text's own buffer (its tail moved with @memmove), the buffer grown by an eighth when it must be (State.cap), and moves its line index in place from the edit on (shiftIndex, State.line_cap), so a key, a backspace and a 9P write cost their bytes and what follows them, never a copy of the text (on the 50k-line file, Debug, medians against the base run beside it: edit-char 1105 -> 420 us, edit-twin 930 -> 433, body-2m 8.6 -> 4.7 ms, body-appends 173 -> 79 ms); setContent is the same edit with the caller's whole new text taken as the buffer; a replica's core (its allocator an epoch's, rollback.isEpoch) and a text a join still sends (memory.isPinned) keep their bytes, the edit putting a new buffer in their place as every edit did, so the rollback copy, its pins and the join's blobs work as they were; a pane's event readers hear an edit's span found from the edit (events.editSpan), the one diffSpan found from two texts; tests: 'edited line index equals a rebuilt one' becomes 'a line index shifted in place equals a rebuilt one' (editedLineStarts goes), 'an edit's span is where the texts before and after it differ' is new, and the panes test of history storage counts one allocation less for a redo (the index moves in place)
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/edit.zig')
-rw-r--r--src/edit.zig38
1 files changed, 31 insertions, 7 deletions
diff --git a/src/edit.zig b/src/edit.zig
index 84db62c0..ec37d0e3 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -545,6 +545,17 @@ fn setEditTextSpan(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor, sp
panes.terminal.setEditText(p, pane, new);
}
+/// The file whose text `t` edits in place (File.replace), the bytes it puts
+/// in and no copy of the text: a file's body, not a live query's (whose
+/// edits `setEditTextSpan` cuts to its line 1).
+fn bodyFile(t: *Text) ?*panes.File.State {
+ if (t.what != .body) return null;
+ const pane = t.pane() orelse return null;
+ const f = if (pane.file) |*file| file else return null;
+ if (pane.query) |q| if (!q.frozen) return null;
+ return f;
+}
+
/// Take a text an edit made that nothing keeps, holding it until the next
/// one: the edit's caller still reads it to place the cursor.
pub fn retireEditText(p: *Pardes, new: []u8) void {
@@ -841,9 +852,14 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) void {
panes.File.textLineStart(t, text, c.row) + @min(c.col, panes.File.textLine(t, text, c.row).len)
else
modal.insertOffset(text, c);
- const new = modal.spliceAlloc(p.gpa, text, at, at, key.text) catch return;
const end = modal.advanceBy(c, key.text);
- setEditTextSpan(p, t, new, end, .{ .head = at, .tail = text.len - at });
+ if (bodyFile(t)) |f| {
+ if (!panes.File.replace(p, f, at, 0, key.text)) return;
+ t.last_edit = .{ .row = @intCast(end.row), .col = @intCast(end.col) };
+ } else {
+ const new = modal.spliceAlloc(p.gpa, text, at, at, key.text) catch return;
+ setEditTextSpan(p, t, new, end, .{ .head = at, .tail = text.len - at });
+ }
t.cur_row = @as(i32, @intCast(end.row)) + eb.row0;
t.cur_col = @intCast(end.col);
t.cur_pinned = true;
@@ -958,9 +974,16 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) void {
// deleteSpan's splice, the line found once.
const s = start + @min(prev, line.len);
const e = 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);
+ if (bodyFile(t)) |f| {
+ // As the whole text put back would be: a no-op edit
+ // when there is nothing before the cursor to take.
+ if (!(if (e > s) panes.File.replace(p, f, s, e - s, "") else panes.File.replace(p, f, text.len, 0, ""))) return;
+ t.last_edit = .{ .row = @intCast(c.row), .col = @intCast(prev) };
+ } else {
+ 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();
@@ -2272,8 +2295,9 @@ test "typing and backspace through the line index keep the text and the index as
// 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 = &.{};
+ gpa.free(f.line_starts.ptr[0..@max(f.line_cap, f.line_starts.len)]);
+ f.line_starts = &.{};
+ f.line_cap = 0;
try std.testing.expectEqualSlices(usize, try panes.File.lineIndex(gpa, &pane.file.?), kept);
}
}