diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 00:35:08 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | d7b3d57ed0ba649f01559356fbf2bf89561990ca (patch) | |
| tree | 37c95f8c04fd86961197a54adbb920675034d98a /src | |
| parent | 218d574b59abd7fb8dd6fe027b12927fc31c3506 (diff) | |
| download | pardes-d7b3d57ed0ba649f01559356fbf2bf89561990ca.tar.gz pardes-d7b3d57ed0ba649f01559356fbf2bf89561990ca.zip | |
rows: a pane's rows from outside, one definition and two sinks: File.setRows is the one writer of a pane's rows (locations.Row, the stored form as it was: digest, dump and blobs unchanged), refusing what its text cannot hold (more rows than lines, an offset or styles past a line's end) and keeping the painting of equal rows; Output.fillRows keeps what is the editor's (the pane, its refill identity, the steps) and hands its rows to it, as the query panes' three writers do; locations.formatRows and parseRows are its text, a line a row, fields a blank apart and the path last (kind m c p with d and u, line col end_line end_col source_column location_end code_start, styles as `-` or `N:start+len=role,...` a role a syntax class and grounds joined by dots); each pane directory over 9P has `rows`, read as that text (+Search's, +Callstack's addresses as data) and written whole lines at a time, at 0 replacing and later following (a line cut by a mount waits for its open's next write; tree.writeLines passes a line on at the offset it was begun at), `>` emptying them; tests: the rows of a +Search, a +Callstack and a +Diff read back from their text equal, byte for byte, and the file read, written whole and across writes, refused and emptied; README, reference and changelog say so
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src')
| -rw-r--r-- | src/CHANGELOG.md | 10 | ||||
| -rw-r--r-- | src/File.zig | 23 | ||||
| -rw-r--r-- | src/Output.zig | 17 | ||||
| -rw-r--r-- | src/Query.zig | 35 | ||||
| -rw-r--r-- | src/fs-help.txt | 4 | ||||
| -rw-r--r-- | src/locations.zig | 176 | ||||
| -rw-r--r-- | src/ninep/pane.zig | 93 | ||||
| -rw-r--r-- | src/ninep/pty.zig | 4 | ||||
| -rw-r--r-- | src/ninep/tree.zig | 14 |
9 files changed, 330 insertions, 46 deletions
diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index da489b82..a64de8e1 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,6 +2,16 @@ ## Unreleased +- Each pane's directory over 9P has a `rows` file: a line a body row of a + results pane (+Search, +Callstack, +Diff, a language server's list), + where it points (`path`, line, column, the address's end, where its code + starts) and how it is coloured (runs of `keyword`, `string`, ... and the + grounds `add`, `del`, `emph`, `line`), so a 9P client reads their + addresses as data, not by parsing the text. A write at offset 0 replaces + a pane's rows, a later one adds to them; rows its text cannot hold are + refused. An edit drops them, as before. The editor's own producers set + rows the same one way. + - A drag draws no guide lines: no dashes where a pane or column would land, no rail for a carried column, no dashed seam under a pane rule held. The line between two columns no longer resizes them (nor, at a diff --git a/src/File.zig b/src/File.zig index 1988fb89..84e88dff 100644 --- a/src/File.zig +++ b/src/File.zig @@ -660,6 +660,29 @@ pub fn setContent(p: *Pardes, f: *State, new: []u8) void { setContentSpan(p, f, new, null); } +/// `rows`, a row a line of the text from its first (where each points, how +/// it is coloured: locations.Row), made the pane's, taken: the one way a +/// pane's rows are set, by a producer in the editor (Output.fillRows, a +/// query pane) or from outside (the pane's `rows` file). RowsMismatch, the +/// rows still the caller's, for rows the text cannot hold: more of them +/// than it has lines, or an offset or styles past its line's end. Rows +/// equal to the pane's keep its painting; others drop it, to be painted +/// from them. An edit of the text drops them (setContentSpan). +pub fn setRows(p: *Pardes, f: *State, rows: []locations.Row) error{RowsMismatch}!void { + var lines = std.mem.splitScalar(u8, f.content, '\n'); + for (rows) |row| { + const line = lines.next() orelse return error.RowsMismatch; + if (row.location_end > line.len or row.code_start > line.len or row.colors.len > line.len - row.code_start) return error.RowsMismatch; + } + if (locations.equalRows(f.location_rows, rows)) return locations.freeRows(p.gpa, rows); + locations.freeRows(p.gpa, f.location_rows); + f.location_rows = rows; + if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); + f.highlights = &.{}; + f.highlight_start = 0; + f.syntax_dirty = true; +} + /// `setContent` for a splice that knows what it left alone at either end, /// so it is not found again by comparing the whole text. The edit is what /// lies between them, made by `edit`, which takes `new`, a whole text its diff --git a/src/Output.zig b/src/Output.zig index 5bad7ab7..3bcc3fa5 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -452,16 +452,10 @@ pub fn fillRows(p: *Pardes, id: usize, dir: []const u8, from: Origin, name: ?[]c File.setContent(p, rf, result_content); resetBody(p, rp); } - if (locations.equalRows(rf.location_rows, result_rows)) { + File.setRows(p, rf, result_rows) catch |err| { locations.freeRows(p.gpa, result_rows); - } else { - locations.freeRows(p.gpa, rf.location_rows); - rf.location_rows = result_rows; - if (rf.highlights.len > 0) p.tree_sitter_gpa.free(rf.highlights); - rf.highlights = &.{}; - rf.highlight_start = 0; - rf.syntax_dirty = true; - } + p.reportError(id, "rows", err); + }; p.view().active = id; if (traits(from).steps) { pane.search_pane = i; @@ -478,7 +472,10 @@ pub fn fillRows(p: *Pardes, id: usize, dir: []const u8, from: Origin, name: ?[]c p.gpa.free(np.file.?.path); np.file.?.path = owned; } - np.file.?.location_rows = result_rows; + File.setRows(p, &np.file.?, result_rows) catch |err| { + locations.freeRows(p.gpa, result_rows); + p.reportError(id, "rows", err); + }; layout.placeNew(p, id, id, free, .doc); p.view().active = id; if (traits(from).steps) { diff --git a/src/Query.zig b/src/Query.zig index caf64d54..c1a89164 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -859,19 +859,14 @@ fn setRows(p: *Pardes, rp: *Pane, out: *std.ArrayList(u8), rows: *std.ArrayList( if (std.mem.eql(u8, content, f.content)) { // Same text: the metadata alone, no undo step, no syntax pass. p.gpa.free(content); - if (locations.equalRows(f.location_rows, meta)) { - locations.freeRows(p.gpa, meta); - } else { - locations.freeRows(p.gpa, f.location_rows); - f.location_rows = meta; - dropHighlights(p, f); - } } else { File.setContent(p, f, content); - locations.freeRows(p.gpa, f.location_rows); - f.location_rows = meta; f.saved_revision = f.revision; } + File.setRows(p, f, meta) catch |err| { + locations.freeRows(p.gpa, meta); + if (p.paneBySerial(rp.serial)) |id| p.reportError(id, "rows", err); + }; if (f.pager_spans.len > 0) p.gpa.free(f.pager_spans); f.pager_spans = &.{}; // The cursor stays where it was typing; on a row that went, the last. @@ -996,24 +991,10 @@ pub fn recolor(p: *Pardes) void { fresh = rows; @memcpy(rows[i].colors[0..got.len], got); } - if (fresh) |rows| { - locations.freeRows(p.gpa, f.location_rows); - f.location_rows = rows; - dropHighlights(p, f); - } + if (fresh) |rows| File.setRows(p, f, rows) catch locations.freeRows(p.gpa, rows); } } -/// The pane's painted colours let go, so the next frame paints them from -/// its rows again: with its text the same, a frame would keep the old ones -/// (refreshHighlights keeps a painting that covers the view). -fn dropHighlights(p: *Pardes, f: *File.State) void { - if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); - f.highlights = &.{}; - f.highlight_start = 0; - f.syntax_dirty = true; -} - /// The path a row of pane `tp`'s text says: its file's, or `@pN` for a /// text that is no file (a list, a scratch). fn addressOf(arena: std.mem.Allocator, tp: *const Pane) []const u8 { @@ -1910,8 +1891,10 @@ pub fn cancel(p: *Pardes, id: usize) void { } const f = &rp.file.?; File.setContent(p, f, s.content); - locations.freeRows(p.gpa, f.location_rows); - f.location_rows = s.rows; + File.setRows(p, f, s.rows) catch |err| { + locations.freeRows(p.gpa, s.rows); + p.reportError(id, "query", err); + }; p.gpa.free(q.asked); q.asked = s.asked; q.source = s.source; diff --git a/src/fs-help.txt b/src/fs-help.txt index bc699418..e6a48491 100644 --- a/src/fs-help.txt +++ b/src/fs-help.txt @@ -20,12 +20,12 @@ dumps the dumps the state file keeps, the newest first: <label> <date> <si layout a line per column: serial index x width current|notcurrent empty|full pane-serials; then active <serial> tag the workspace tag, tagexec a word as a click in it; col/<serial>/ a column's tag, ctl, exec; rmdir: empty only pane/new open it to make a pane (<session dir>/+New) in the active column; the read answers its serial -pane/<n>/ name body tag ctl addr dot limit data xdata sel dirty mark scroll errors event look exec tagexec; rmdir closes +pane/<n>/ name body tag ctl addr dot limit data xdata sel dirty mark scroll errors event look exec tagexec rows; rmdir closes + rows: a line a row, kind (m c p; d decl, u unmarked) line col end_line end_col source_column location_end code_start styles path; styles - or N:start+len=role,... (keyword string number comment, add del emph line, joined by .); a write at 0 replaces on terminals pty/data (keys in, output out), pty/status, pty/ctl (winsize, sig, exec), pty/run (a line, its exit and output) os/, src/ the host filesystem; the editor's own sources $m below: $PARDES_MOUNT (set under 9ns --mntgen); without it, 9p -a "unix!$PARDES_9P". - cat $m/index which panes exist n=$(cat $m/pane/new) make a pane of your own (each open makes one), take its serial exec 3<>$m/pane/$n/look; echo /etc/hosts:3 >&3; cat <&3 open a file through it, see the pane it went to diff --git a/src/locations.zig b/src/locations.zig index fc39f92d..3dc0412d 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -103,6 +103,135 @@ pub fn equalRows(a: []const Row, b: []const Row) bool { return true; } +// ---- rows as text: a pane's `rows` file (ninep/pane.zig) ---- + +/// Rows as text, a line a row, its fields one blank apart and its path +/// last (a path may hold blanks): its kind (`m` a match n/N step to, `c` +/// context, `p` a preview of source; then `d` when it is a declaration and +/// `u` when its occurrence is not marked), its place (line, col, end_line, +/// end_col; 0 for none), source_column, location_end and code_start (byte +/// offsets in its line), its styles and its path. Styles are `-` for none, +/// else how many bytes they cover from code_start, a colon and the runs +/// that are not plain, `start+len=role`, comma-separated: a role is a +/// syntax class (keyword string number comment) and grounds (add del emph +/// line, syntax.ground_add's), joined by dots. `p 12 0 0 0 0 0 0 +/// 9:0+1=add.line,5+3=keyword.add src/a.zig`. +pub fn formatRows(rows: []const Row, w: *std.Io.Writer) std.Io.Writer.Error!void { + for (rows) |r| { + try w.writeByte(switch (r.kind) { + .match => 'm', + .context => 'c', + .preview => 'p', + }); + if (r.declaration) try w.writeByte('d'); + if (!r.mark_occurrence) try w.writeByte('u'); + try w.print(" {d} {d} {d} {d} {d} {d} {d} ", .{ r.at.line, r.at.col, r.at.end_line, r.at.end_col, r.source_column, r.location_end, r.code_start }); + if (r.colors.len == 0) try w.writeByte('-') else try w.print("{d}:", .{r.colors.len}); + var sep = false; + var at: usize = 0; + while (at < r.colors.len) { + const style = r.colors[at]; + var end = at + 1; + while (end < r.colors.len and r.colors[end] == style) end += 1; + defer at = end; + if (style == 0) continue; + if (sep) try w.writeByte(','); + sep = true; + try w.print("{d}+{d}=", .{ at, end - at }); + var dot = false; + const syn = style & syntax.syn_bits; + if (syn != 0) { + if (std.enums.fromInt(syntax.Syn, syn)) |known| try w.writeAll(@tagName(known)) else try w.print("{d}", .{syn}); + dot = true; + } + for (grounds) |g| if (style & g[0] != 0) { + if (dot) try w.writeByte('.'); + try w.writeAll(g[1]); + dot = true; + }; + } + try w.print(" {s}\n", .{r.path}); + } +} + +const grounds = [_]struct { u8, []const u8 }{ .{ syntax.ground_add, "add" }, .{ syntax.ground_del, "del" }, .{ syntax.ground_emph, "emph" }, .{ syntax.ground_line, "line" } }; + +/// formatRows' text read back, the rows and all they own `arena`'s. +/// BadRows for a line that is not one; a last newline ends the last row. +pub fn parseRows(arena: std.mem.Allocator, text: []const u8) error{ OutOfMemory, BadRows }![]Row { + const body = if (std.mem.endsWith(u8, text, "\n")) text[0 .. text.len - 1] else text; + if (body.len == 0) return &.{}; + var rows: std.ArrayList(Row) = .empty; + var lines = std.mem.splitScalar(u8, body, '\n'); + while (lines.next()) |line| { + var fields: [9][]const u8 = undefined; + var at: usize = 0; + for (&fields) |*field| { + const blank = std.mem.indexOfScalarPos(u8, line, at, ' ') orelse return error.BadRows; + field.* = line[at..blank]; + at = blank + 1; + } + const kind = fields[0]; + if (kind.len == 0) return error.BadRows; + var row: Row = .{ + .kind = switch (kind[0]) { + 'm' => .match, + 'c' => .context, + 'p' => .preview, + else => return error.BadRows, + }, + .path = try arena.dupe(u8, line[at..]), + .at = .{}, + .location_end = 0, + .code_start = 0, + }; + for (kind[1..]) |flag| switch (flag) { + 'd' => row.declaration = true, + 'u' => row.mark_occurrence = false, + else => return error.BadRows, + }; + var numbers: [7]usize = undefined; + for (&numbers, fields[1..8]) |*n, field| n.* = std.fmt.parseInt(usize, field, 10) catch return error.BadRows; + row.at = .{ .line = numbers[0], .col = numbers[1], .end_line = numbers[2], .end_col = numbers[3] }; + row.source_column = numbers[4]; + row.location_end = numbers[5]; + row.code_start = numbers[6]; + const styles = fields[8]; + if (!std.mem.eql(u8, styles, "-")) { + const colon = std.mem.indexOfScalar(u8, styles, ':') orelse return error.BadRows; + const len = std.fmt.parseInt(usize, styles[0..colon], 10) catch return error.BadRows; + // A line is never longer than a file: bounded before it is made. + if (len > @import("memory.zig").limits.max_file_bytes) return error.BadRows; + row.colors = try arena.alloc(u8, len); + @memset(row.colors, 0); + var runs = std.mem.splitScalar(u8, styles[colon + 1 ..], ','); + while (runs.next()) |run| { + if (run.len == 0) continue; + const plus = std.mem.indexOfScalar(u8, run, '+') orelse return error.BadRows; + const eq = std.mem.indexOfScalarPos(u8, run, plus, '=') orelse return error.BadRows; + const start = std.fmt.parseInt(usize, run[0..plus], 10) catch return error.BadRows; + const n = std.fmt.parseInt(usize, run[plus + 1 .. eq], 10) catch return error.BadRows; + if (start > len or n > len - start) return error.BadRows; + var style: u8 = 0; + var names = std.mem.splitScalar(u8, run[eq + 1 ..], '.'); + names: while (names.next()) |name| { + for (grounds) |g| if (std.mem.eql(u8, name, g[1])) { + style |= g[0]; + continue :names; + }; + if (style & syntax.syn_bits != 0) return error.BadRows; + const syn: u8 = if (std.meta.stringToEnum(syntax.Syn, name)) |known| @backingInt(known) else std.fmt.parseInt(u4, name, 10) catch return error.BadRows; + if (syn == 0) return error.BadRows; + style |= syn; + } + @memset(row.colors[start..][0..n], style); + } + } + try rows.append(arena, row); + } + return rows.items; +} + /// Compare native producer rows with an already formatted, context-free view. /// Identical refreshes retain allocations, syntax and selection state. pub fn sameInput(input: []const u8, rendered: []const u8, rows: []const Row, layout: Config.Layout) bool { @@ -984,3 +1113,50 @@ test "locations cache transfers oversized completed analysis without reparsing" try std.testing.expectEqual(@as(usize, 0), cache.len); try std.testing.expectEqual(@as(usize, 0), cache.bytes); } + +test "rows read back from their text are the rows: +Search's, +Callstack's and +Diff's" { + const testing = std.testing; + const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true }); + defer p.deinit(); + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.zig", .data = "const S = struct {\n fn f() void {\n a(\"x y\");\n b();\n }\n};\n" }); + var buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(testing.io, &buf)]; + const patch = try std.Io.Dir.path.join(testing.allocator, &.{ dir, "c.patch" }); + defer testing.allocator.free(patch); + const search = try format(p, dir, "a.zig:3:9 a(\"x y\");\na.zig:4:9 b();\n", null, true); + defer p.gpa.free(search.content); + defer freeRows(p.gpa, search.rows); + const callstack = try formatCallstack(p, dir, "a.zig:2:8 f\nāā a.zig:3:9 a\n"); + defer p.gpa.free(callstack.content); + defer freeRows(p.gpa, callstack.rows); + const hid = try pardes.panes.Output.openDiff(p, 0, patch, "--- a.zig\n+++ a.zig\n@@ -2,3 +2,4 @@\n fn f() void {\n- a(\"x\");\n+ a(\"x y\");\n+ b();\n }\n"); + const stacked = p.panes[hid].?.file.?.location_rows; + // A +Diff's rows carry styles with grounds: the codec is tried on them. + var grounded = false; + for (stacked) |row| for (row.colors) |c| { + grounded = grounded or c & syntax.ground_add != 0; + }; + try testing.expect(grounded and search.rows.len > 0 and callstack.rows.len > 0); + var arena_state: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena_state.deinit(); + const arena = arena_state.allocator(); + for ([_][]const Row{ search.rows, callstack.rows, stacked }) |rows| { + var text: std.Io.Writer.Allocating = .init(arena); + try formatRows(rows, &text.writer); + const back = try parseRows(arena, text.written()); + try testing.expect(equalRows(rows, back)); + var again: std.Io.Writer.Allocating = .init(arena); + try formatRows(back, &again.writer); + try testing.expectEqualStrings(text.written(), again.written()); + } + try testing.expectError(error.BadRows, parseRows(arena, "x 1 0 0 0 0 0 0 - a\n")); + try testing.expectError(error.BadRows, parseRows(arena, "m 1 0 0 0 0 0 0 2:0+3=keyword a\n")); + try testing.expectError(error.BadRows, parseRows(arena, "m 1 0 0 0 0 0 0 2:0+1=keyword.string a\n")); + try testing.expectError(error.BadRows, parseRows(arena, "m 1 0 0\n")); + const one = try parseRows(arena, "pd 12 3 0 0 0 0 0 4:0+1=add.line,1+2=keyword.add src/a b.zig\n"); + try testing.expectEqualStrings("src/a b.zig", one[0].path); + try testing.expect(one[0].declaration and one[0].kind == .preview and one[0].at.line == 12 and one[0].at.col == 3); + try testing.expectEqualSlices(u8, &.{ syntax.ground_add | syntax.ground_line, @backingInt(syntax.Syn.keyword) | syntax.ground_add, @backingInt(syntax.Syn.keyword) | syntax.ground_add, 0 }, one[0].colors); +} diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index 3fa45404..3f62aef9 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -12,6 +12,7 @@ const modal = @import("../modal.zig"); const config = @import("../config.zig"); const fs = @import("../fs.zig"); const tree = @import("tree.zig"); +const locations = @import("../locations.zig"); const events = @import("events.zig"); const addressing = @import("addr.zig"); const ctl = @import("ctl.zig"); @@ -319,8 +320,8 @@ pub fn fileSize(p: *Pardes, id: usize, f: PaneFile) u64 { // A terminal's body reads its screen and history, as text. .body => if (pane.file == null and pane.isTerminal()) panes.terminal.screenTextLen(pane) else if (kindText(p, pane)) |t| t.len else bodyOf(pane).len, // What addr selects, generated by each read: no length, as acme's - // data and xdata have none. - .data, .xdata => 0, + // data and xdata have none; and rows, made by each read. + .data, .xdata, .rows => 0, .tag => if (tagOf(p, pane)) |tag| tag.len else |_| 0, // a stat's length is a hint: none when the tag cannot be made .name => nameOf(p, pane).len + 1, .ctl => ctl: { @@ -396,6 +397,12 @@ pub fn read(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply .mark => readFlag(p, req, !pf.nomark), .scroll => readFlag(p, req, !pf.noscroll), .look, .exec, .tagexec => ctl.readResults(p, req), + .rows => rows: { + const f = fileOf(pane) orelse break :rows tree.window(p, req, ""); + var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); + locations.formatRows(f.location_rows, &text.writer) catch break :rows Reply.fail(req.tag, E.NOMEM); + break :rows tree.window(p, req, text.written()); + }, .event => events.readQueue(p, req, &pf.events), .pty_status => pty.readStatus(p, req, id, pane), .pty_data => pty.readData(p, req, pf), @@ -498,6 +505,7 @@ pub fn write(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply // A click in its tag: an event reader holding the pane hears it as // an `x` record (ctl.run), as acme's tag clicks are. .tagexec => ctl.command(p, req, pane.serial, true, true), + .rows => writeRows(p, req, pane), .event => events.writeEvent(p, req, id), .errors => if (appendErrors(p, id, req.data)) |took| .{ .tag = req.tag, .written = @intCast(took) } @@ -510,6 +518,36 @@ pub fn write(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply }; } +pub const e_rows_line = "invalid rows: a row is a line, ending in a newline"; +pub const e_rows_text = "invalid rows: a line is no row (the README's rows line says what one is)"; +pub const e_rows_fit = "invalid rows: more rows than the text has lines, or one past its line's end"; + +/// Rows, whole lines of them (a write through a mount is cut anywhere: the +/// rest of a line waits for the open's next write, tree.writeLines): at +/// offset 0 they replace the pane's, at a later one they follow those it +/// holds. Parsed (locations.parseRows) and handed to File.setRows, which +/// refuses what the text cannot hold. +fn writeRows(p: *Pardes, req: Req, pane: *Pane) Reply { + const f = fileOf(pane) orelse return Reply.fail(req.tag, E.PERM); + if (req.data.len == 0) return .{ .tag = req.tag, .written = 0 }; + if (req.data[req.data.len - 1] != '\n') return tree.failText(req.tag, E.INVAL, e_rows_line); + const arena = p.scratch.allocator(); + const written = locations.parseRows(arena, req.data) catch |err| return switch (err) { + error.OutOfMemory => Reply.fail(req.tag, E.NOMEM), + error.BadRows => tree.failText(req.tag, E.INVAL, e_rows_text), + }; + const kept = if (req.off == 0) 0 else f.location_rows.len; + const all = arena.alloc(locations.Row, kept + written.len) catch return Reply.fail(req.tag, E.NOMEM); + @memcpy(all[0..kept], f.location_rows[0..kept]); + @memcpy(all[kept..], written); + const rows = locations.cloneRows(p.gpa, all) catch return Reply.fail(req.tag, E.NOMEM); + panes.File.setRows(p, f, rows) catch { + locations.freeRows(p.gpa, rows); + return tree.failText(req.tag, E.INVAL, e_rows_fit); + }; + return .{ .tag = req.tag, .written = @intCast(req.data.len) }; +} + /// spliceBody for a write of the open `handle`, in the undo step its open /// is making: it joins the step being made only when it is the same open's /// (or the step is anyone's, `joined_by` 0), another open's starting its @@ -981,6 +1019,8 @@ pub fn truncate(p: *Pardes, pane: *Pane, file: PaneFile) tree.Status { pf.limit = null; }, .dot => pf.dot_truncated = fileOf(pane) != null, + // `>` starts the rows over, as a write at 0 does. + .rows => if (fileOf(pane)) |f| panes.File.setRows(p, f, &.{}) catch unreachable, else => {}, } return .ok; @@ -1248,3 +1288,52 @@ test "index names a command's pane cmd, a shell's term" { const shell = try p.newShell(p.freeSlot().?, "/tmp"); try testing.expectEqualStrings("term", kindOf(shell)); } + +test "rows: a pane's rows read as lines, written whole or across writes, refused when its text cannot hold them" { + const gpa = testing.allocator; + const p = try withFile(gpa, "a.txt:3 one\n two\n"); + defer p.deinit(); + const rows = Node.of(serialOf(p), .rows); + try testing.expectEqualStrings("", rd(p, rows, 0, 4096).bytes); + const text = "m 3 0 0 0 0 7 8 3:0+3=keyword.add a.txt\ncd 0 0 0 0 0 0 0 - \n"; + try testing.expectEqual(Status.ok, wr(p, rows, text).reply.status); + try testing.expectEqualStrings(text, rd(p, rows, 0, 4096).bytes); + const f = &p.panes[0].?.file.?; + try testing.expectEqual(@as(usize, 2), f.location_rows.len); + try testing.expectEqualStrings("a.txt", f.location_rows[0].path); + try testing.expect(f.location_rows[1].declaration and f.location_rows[1].kind == .context); + // A write at 0 starts them over; one open's writes, cut mid-line, + // make one set of rows, its later writes following its first. + const h = call(p, .{ .tag = 1, .op = .open, .node = rows, .omode = 1 }).reply.handle; + try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = rows, .handle = h, .off = 0, .data = "m 3 0 0 0 0 7 8 - a.t" }).reply.status); + try testing.expectEqual(Status.ok, call(p, .{ .tag = 3, .op = .write, .node = rows, .handle = h, .off = 21, .data = "xt\np 4 0 0 0 0 0 0 - a.txt\n" }).reply.status); + _ = call(p, .{ .tag = 4, .op = .release, .node = rows, .handle = h }); + try testing.expectEqualStrings("m 3 0 0 0 0 7 8 - a.txt\np 4 0 0 0 0 0 0 - a.txt\n", rd(p, rows, 0, 4096).bytes); + // More rows than lines, an offset or styles past a line's end, a line + // that is no row: refused, the rows as they were. + for ([_][]const u8{ "m 1 0 0 0 0 0 0 - a\nm 1 0 0 0 0 0 0 - a\nm 1 0 0 0 0 0 0 - a\nm 1 0 0 0 0 0 0 - a\n", "m 1 0 0 0 0 12 0 - a\n", "m 1 0 0 0 0 0 8 9:0+1=add a\n", "q 1 0 0 0 0 0 0 - a\n", "m 1 0 0 0 0 0 0 - a" }) |bad| + try testing.expectEqual(Status.err, wr(p, rows, bad).reply.status); + try testing.expectEqual(@as(usize, 2), f.location_rows.len); + // `>` truncates: no rows. + _ = call(p, .{ .tag = 5, .op = .setattr, .node = rows, .truncate = true }); + try testing.expectEqualStrings("", rd(p, rows, 0, 4096).bytes); + // An edit drops them. + try testing.expectEqual(Status.ok, wr(p, rows, text).reply.status); + try testing.expectEqual(Status.ok, wr(p, Node.of(serialOf(p), .body), "more\n").reply.status); + try testing.expectEqual(@as(usize, 0), f.location_rows.len); +} + +test "rows: a +Search's rows read back as the addresses its text shows" { + const gpa = testing.allocator; + const p = try withFile(gpa, "x\n"); + defer p.deinit(); + try panes.Output.fillResults(p, 0, "/tmp", .search, "foo", try p.gpa.dupe(u8, "a.txt:1:1 foo\nb.txt:2:3 foo\n"), null); + const serial = for (p.panes) |slot| { + const pane = slot orelse continue; + const f = pane.file orelse continue; + if (f.output != null and f.location_rows.len > 0) break pane.serial; + } else return error.NoSearch; + const text = rd(p, Node.of(serial, .rows), 0, 4096).bytes; + try testing.expect(std.mem.indexOf(u8, text, "m 1 1 ") == 0); + try testing.expect(std.mem.indexOf(u8, text, " a.txt\n") != null and std.mem.indexOf(u8, text, "\nm 2 3 ") != null and std.mem.endsWith(u8, text, " b.txt\n")); +} diff --git a/src/ninep/pty.zig b/src/ninep/pty.zig index 7333fac7..350978e3 100644 --- a/src/ninep/pty.zig +++ b/src/ninep/pty.zig @@ -507,7 +507,7 @@ test "a pane that is not a terminal has no pty/ at all" { var buf: [32]th.Dirent = undefined; const files = th.dirents(rdir(p, dir, 0).bytes, &buf); - try testing.expectEqual(@as(usize, 18), files.len); + try testing.expectEqual(@as(usize, 19), files.len); try testing.expect(th.nameAt(files, "pty") == null); try testing.expectEqual(E.NOENT, look_up(p, dir, "pty_ctl").errno()); @@ -528,7 +528,7 @@ test "a terminal pane's pty/ holds exactly ctl, status and data" { var buf: [32]th.Dirent = undefined; const files = th.dirents(rdir(p, dir, 0).bytes, &buf); - try testing.expectEqual(@as(usize, 19), files.len); + try testing.expectEqual(@as(usize, 20), files.len); try testing.expect(th.nameAt(files, "pty").?.dir); const inside = th.dirents(rdir(p, Node.of(serial, .pty), 0).bytes, &buf); diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index bca89466..e0cb69b5 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -313,6 +313,9 @@ pub const PaneFile = enum(u5) { /// A word run as a click in the pane's tag, as /tagexec is in the /// workspace's and col/<n>/exec in a column's. tagexec, + /// A line a body row: where it points and how it is coloured + /// (locations.formatRows), a results pane's; written, the pane's. + rows, pty, pty_ctl, pty_status, @@ -1163,7 +1166,7 @@ fn linesFile(target: Target) bool { .col => |c| c.file == .ctl or c.file == .exec, // `name` too: one name, however its writes cut it, applied once // its newline comes, or with its write when that is whole. - .pane => |t| t.file == .look or t.file == .exec or t.file == .ctl or t.file == .tagexec or t.file == .name, + .pane => |t| t.file == .look or t.file == .exec or t.file == .ctl or t.file == .tagexec or t.file == .name or t.file == .rows, }; } @@ -1326,6 +1329,9 @@ fn writeLines(p: *Pardes, req: Req, target: Target) Reply { o.pending.shrinkRetainingCapacity(rest); var w = req; w.data = whole; + // At the offset its first byte was written at, an earlier write's + // held line included (a rows write at 0 starts the rows over). + w.off = (req.off + (req.data.len - data.len)) -| (end + rest - data.len); // The command pane this open was answered holds no lease against its // own next command (exec.leased): that one may run there again. if (resultsFile(target)) o.results.len = 0; @@ -1611,10 +1617,10 @@ test "readdir lists the root and a pane directory without creating anything" { const dir = rdir(p, Node.of(serial, .dir), 0); const files = th.dirents(dir.bytes, &buf); - try testing.expectEqual(@as(usize, 18), files.len); + try testing.expectEqual(@as(usize, 19), files.len); for ([_][]const u8{ - "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", - "sel", "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", + "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", "sel", + "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", "rows", }, 0..) |name, i| try testing.expectEqualStrings(name, files[i].name); try testing.expect(th.nameAt(files, ".") == null); |
