diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-23 17:10:58 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-23 17:10:58 -0300 |
| commit | 0073d05329335dbb0eb85f5c3044e42805d144e5 (patch) | |
| tree | 744edfc317719b2018b1c3a8657c223aec058d47 /src/edit.zig | |
| parent | a8b457a306a4442975210bcac3f04b56513c61c3 (diff) | |
| download | pardes-0073d05329335dbb0eb85f5c3044e42805d144e5.tar.gz pardes-0073d05329335dbb0eb85f5c3044e42805d144e5.zip | |
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 <[email protected]>
Diffstat (limited to 'src/edit.zig')
| -rw-r--r-- | src/edit.zig | 190 |
1 files changed, 164 insertions, 26 deletions
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); + } +} |
