From a0d367b6c54ff29737662fd7cf5a78d4d183cb20 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 23 Oct 2026 10:56:33 -0300 Subject: Audit (core, modal): goto/insert/select tables, edit through sam.Op and sam.apply, Turn.bump and awaitPast, dead helpers out Co-Authored-By: Claude Opus 5.5 --- src/9p_io.zig | 12 +-- src/Output.zig | 2 +- src/builtins.zig | 2 +- src/detached/server.zig | 2 +- src/edit.zig | 107 +++++++-------------- src/edit_cmd.zig | 6 +- src/exec.zig | 82 +++++++--------- src/gui/gui.zig | 32 +++---- src/look.zig | 4 +- src/macos.zig | 4 +- src/mini.zig | 6 +- src/modal.zig | 76 +++------------ src/ninep/ctl.zig | 4 +- src/ninep/pane.zig | 2 +- src/pardes.zig | 244 +++++++++++++++--------------------------------- src/sam_edit.zig | 36 +++---- src/tty/tty.zig | 2 +- test/output.zig | 2 +- test/panes.zig | 44 ++++----- test/pdf.zig | 18 ++-- 20 files changed, 237 insertions(+), 450 deletions(-) diff --git a/src/9p_io.zig b/src/9p_io.zig index 87972b43..a825475f 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -343,7 +343,7 @@ pub const Listener = struct { @memcpy(words, reply.ename[0..words.len]); var waited = reply; waited.ename = words; - pardes.turn.awaitSettled(epoch); + pardes.turn.awaitPast(&pardes.turn.epoch, epoch); return conn.reply(&waited, ""); } // Any other refusal answers at once, for the same buffer's sake. @@ -371,11 +371,11 @@ pub const Listener = struct { core.fs.late_failure_len = 0; // What fails while this waits is this write's err, never a msg too. core.fs.write_waits = true; - if (core.effects_len != 0) pardes.turn.awaitSettled(epoch); + if (core.effects_len != 0) pardes.turn.awaitPast(&pardes.turn.epoch, epoch); if (core.fs.lsp_answer_at) |n| { core.fs.lsp_answer_at = null; core.fs.lsp_result = null; - pardes.turn.awaitLsp(n); + pardes.turn.awaitPast(&pardes.turn.lsp_answers, n); // The answer filled a pane (a Rename's preview): that is what // this write's open reads back, as look and exec answer. if (l.core == core) if (core.fs.lsp_result) |serial| { @@ -410,7 +410,7 @@ pub const Listener = struct { // hears that it happened rather than a cut; a failed one leaves the // connection and says why on the message row and in the log. if (restoring) { - if (l.core == core) pardes.turn.awaitRestored(restores); + if (l.core == core) pardes.turn.awaitPast(&pardes.turn.restores, restores); return conn.reply(&reply, ""); } // Once a wait returns `core` may be gone: a Restore meanwhile put a @@ -695,8 +695,8 @@ pub const Listener = struct { // fids it names are the old editor's. A client that connects now // is the replacement's, and stays. l.cut_before = l.accepts.load(.acquire); - pardes.turn.settle(); - pardes.turn.restoreSettled(); + pardes.turn.bump(&pardes.turn.epoch); + pardes.turn.bump(&pardes.turn.restores); pardes.turn.rest(); const flush_by = nowMs() +| 200; while (nowMs() < flush_by) { diff --git a/src/Output.zig b/src/Output.zig index 9ed33ebe..3fc08d9a 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -774,7 +774,7 @@ pub fn openUnsaved(p: *Pardes, id: usize, content: []u8) void { const own = if (pane.file) |f| panes.Dir.ofName(f.path) else pane.cwdSlice(); // Its directory gone (or never there, a name not yet saved): the list // goes in the session's, where a look from it can still resolve. - const dir = if (comptime pardes.hosted) (if (pardes.filesystem.localPath(own)) |local| (if (exec.isDirectory(local)) own else p.sessionDir()) else own) else own; + const dir = if (comptime pardes.hosted) (if (pardes.filesystem.localPath(own)) |local| (if (exec.kindOf(local) == .directory) own else p.sessionDir()) else own) else own; const free = p.freeSlot() orelse return p.gpa.free(content); _ = open(p, free, dir, .unsaved, "", content) catch return; exec.placeNew(p, id, id, free, .doc); diff --git a/src/builtins.zig b/src/builtins.zig index fdce534b..7b1541ac 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1226,7 +1226,7 @@ pub const Incl = struct { return p.reportFailure(c.id, std.fmt.bufPrint(&said, "Incl: invalid directory: {s} is relative", .{shown}) catch "Incl: invalid directory"); var home_buf: [4096]u8 = undefined; const expanded = @import("fs.zig").expandHome(dir_word, &home_buf); - if (comptime pardes.hosted) if (@import("fs.zig").localPath(expanded)) |local| if (!exec.isDirectory(local)) + if (comptime pardes.hosted) if (@import("fs.zig").localPath(expanded)) |local| if (exec.kindOf(local) != .directory) return p.reportFailure(c.id, if (exec.kindOf(local) != null) std.fmt.bufPrint(&said, "Incl: not a directory: {s}", .{shown}) catch "Incl: not a directory" else diff --git a/src/detached/server.zig b/src/detached/server.zig index 989bdc0f..62dff011 100644 --- a/src/detached/server.zig +++ b/src/detached/server.zig @@ -1288,7 +1288,7 @@ pub fn run(init: std.process.Init, opts: pardes.Options, name: []const u8) !void for ([_]std.posix.SIG{ .HUP, .INT, .TERM }) |sig| std.posix.sigaction(sig, &on_quit, null); defer quit_wake = -1; while (!session.core.quit and !quit_signalled.load(.acquire)) { - pardes.turn.restoreSettled(); + pardes.turn.bump(&pardes.turn.restores); try session.core.pump(h); if (session.core.quit) break; if (session.core.takeRestore()) |path| restore: { diff --git a/src/edit.zig b/src/edit.zig index 55365692..c9477c29 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -8,6 +8,7 @@ const mouse = @import("mouse.zig"); const normal = @import("normal.zig"); const std = @import("std"); const modal = @import("modal.zig"); +const sam = @import("sam_edit.zig"); const panes = @import("panes.zig"); const body_layer = @import("body_layer.zig"); const config = @import("config.zig"); @@ -1318,23 +1319,10 @@ pub fn normalChange(p: *Pardes, t: *Text) void { const r1 = @max(t.msel.r0, t.msel.r1); pushUndo(p, t); const eb = editText(p, t, 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); + // the lines are adjacent in the text already, their newlines and all + setYank(p, eb.text[modal.lineStartOffset(eb.text, lo)..modal.lineEndOffset(eb.text, hi)]); var body: []u8 = eb.text; if (hi > lo) { const d = modal.deleteLines(p.gpa, body, lo + 1, hi) catch { @@ -1485,45 +1473,22 @@ pub fn normalCase(p: *Pardes, t: *Text, op: modal.CaseOp) void { setEditText(p, t, 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 { +/// Where `pos` lands after `chs`, which are ascending and disjoint. +fn mapThroughChanges(chs: []const sam.Op, 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); + if (pos < ch.q0) break; + if (ch.q0 == ch.q1) { + delta += @intCast(ch.text.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)); + if (pos == ch.q0) break; + if (pos < ch.q1) return @intCast(@as(i64, @intCast(ch.q0)) + delta); + delta += @as(i64, @intCast(ch.text.len)) - @as(i64, @intCast(ch.q1 - ch.q0)); } 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; -} - /// `J` / `Alt-J`: join every range's lines with the next, in ONE edit /// (helix join_selections): the newline and the next line's leading blanks /// become one space, or nothing when that line is blank, and a line two @@ -1545,7 +1510,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void { // 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(); - var changes: std.ArrayList(TextChange) = .empty; + var changes: std.ArrayList(sam.Op) = .empty; var next: usize = 0; // lines below this one are joined already for (rs[0..got.n]) |r| { // Text.rangeLineSpan, through rowOf @@ -1562,7 +1527,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void { while (to < text.len and (text[to] == ' ' or text[to] == '\t')) to += 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; + changes.append(arena, .{ .q0 = @intCast(from), .q1 = @intCast(to), .text = sep }) catch return; } next = @max(next, end); } @@ -1570,7 +1535,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void { const chs_len = chs.len; if (chs_len == 0) return; pushUndo(p, t); - const new = applyChanges(p, text, chs[0..chs_len]) catch return; + const new = sam.apply(p.gpa, text, chs[0..chs_len]) catch return; // every range's cells ride through the edit; Alt-J replaces them with // the spaces, when it put any in var cells: [Text.max_selections]Text.SelRange = undefined; @@ -1579,9 +1544,9 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void { if (select_space) { var delta: i64 = 0; for (chs[0..chs_len]) |ch| { - const at: usize = @intCast(@as(i64, @intCast(ch.from)) + delta); - delta += @as(i64, @intCast(ch.ins.len)) - @as(i64, @intCast(ch.to - ch.from)); - if (ch.ins.len == 0 or n == cells.len) continue; + const at: usize = @intCast(@as(i64, @intCast(ch.q0)) + delta); + delta += @as(i64, @intCast(ch.text.len)) - @as(i64, @intCast(ch.q1 - ch.q0)); + if (ch.text.len == 0 or n == cells.len) continue; const pos = modal.positionAt(new, at); cells[n] = .{ .row = @as(i32, @intCast(pos.row)) + eb.row0, .col = @intCast(pos.col), .arow = @as(i32, @intCast(pos.row)) + eb.row0, .acol = @intCast(pos.col) }; n += 1; @@ -1627,23 +1592,23 @@ pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) voi const n = got.n; if (n < 2) return; const by = @min(cnt, n); - var chs: [Text.max_selections]TextChange = undefined; + var chs: [Text.max_selections]sam.Op = undefined; for (rs[0..n], 0..) |r, i| { const src = rs[if (forward) (i + n - by) % n else (i + by) % n]; chs[i] = .{ - .from = @min(r.anchor, r.head), - .to = @max(r.anchor, r.head), - .ins = text[@min(src.anchor, src.head)..@max(src.anchor, src.head)], + .q0 = @intCast(@min(r.anchor, r.head)), + .q1 = @intCast(@max(r.anchor, r.head)), + .text = text[@min(src.anchor, src.head)..@max(src.anchor, src.head)], }; } pushUndo(p, t); - const new = applyChanges(p, text, chs[0..n]) catch return; + const new = sam.apply(p.gpa, text, chs[0..n]) catch return; var cells: [Text.max_selections]Text.SelRange = undefined; var delta: i64 = 0; for (chs[0..n], rs[0..n], 0..) |ch, r, i| { - const from: usize = @intCast(@as(i64, @intCast(ch.from)) + delta); - delta += @as(i64, @intCast(ch.ins.len)) - @as(i64, @intCast(ch.to - ch.from)); - const to = from + ch.ins.len; + const from: usize = @intCast(@as(i64, @intCast(ch.q0)) + delta); + delta += @as(i64, @intCast(ch.text.len)) - @as(i64, @intCast(ch.q1 - ch.q0)); + const to = from + ch.text.len; const c = Text.rangeCells(new, if (r.anchor > r.head) .{ .anchor = to, .head = from } else .{ .anchor = from, .head = to }); const cc = modal.positionAt(new, c.cur); const ac = modal.positionAt(new, c.anc); @@ -1700,7 +1665,7 @@ pub fn normalAlign(p: *Pardes, t: *Text) void { // each column's head position, left to right for (1..n) |i| widths[i] += widths[i - 1]; const arena = p.scratch.allocator(); - var chs: [Text.max_selections]TextChange = undefined; + var chs: [Text.max_selections]sam.Op = undefined; var m: usize = 0; line = std.math.maxInt(usize); for (rs[0..n], 0..) |r, i| { @@ -1716,12 +1681,12 @@ pub fn normalAlign(p: *Pardes, t: *Text) void { const spaces = arena.alloc(u8, pad) catch return; @memset(spaces, ' '); const at = @min(r.anchor, r.head); - chs[m] = .{ .from = at, .to = at, .ins = spaces }; + chs[m] = .{ .q0 = @intCast(at), .q1 = @intCast(at), .text = spaces }; m += 1; } if (m == 0) return; pushUndo(p, t); - const new = applyChanges(p, text, chs[0..m]) catch return; + const new = sam.apply(p.gpa, text, chs[0..m]) catch return; var cells: [Text.max_selections]Text.SelRange = undefined; for (rs[0..n], 0..) |r, i| { const c = Text.rangeCells(text, r); @@ -1751,7 +1716,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) void { // 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; + const chs = arena.alloc(sam.Op, span.end - span.start + 1) catch return; var chs_len: usize = 0; var l = span.start; while (l <= span.end) : (l += 1) { @@ -1762,7 +1727,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) void { 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[chs_len] = .{ .q0 = @intCast(ls), .q1 = @intCast(ls), .text = pad[0..ins] }; chs_len += 1; } else { const want = modal.INDENT_W * cnt; @@ -1778,7 +1743,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) void { if (w >= want) break; } if (pos > 0) { - chs[chs_len] = .{ .from = ls, .to = ls + pos, .ins = "" }; + chs[chs_len] = .{ .q0 = @intCast(ls), .q1 = @intCast(ls + pos), .text = "" }; chs_len += 1; } } @@ -1790,7 +1755,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) void { modal.offsetAt(text, .{ .row = @intCast(@max(0, t.vsel.row - eb.row0)), .col = @intCast(@max(0, t.vsel.col)) }) else cur_off; - const new = applyChanges(p, text, chs[0..chs_len]) catch return; + const new = sam.apply(p.gpa, 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, t, new); @@ -1864,7 +1829,7 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) void { } 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; + const chs = arena.alloc(sam.Op, lines_len) catch return; var chs_len: usize = 0; for (lines[0..lines_len]) |l| { const ls = modal.lineStartOffset(text, l); @@ -1875,16 +1840,16 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) void { // 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 = "" } + .{ .q0 = @intCast(at), .q1 = @intCast(@min(at + token.len + margin, le)), .text = "" } else - .{ .from = at, .to = at, .ins = ins }; + .{ .q0 = @intCast(at), .q1 = @intCast(at), .text = ins }; chs_len += 1; } pushUndo(p, t); // one edit, and the WHOLE selection rides through it (helix maps the // selection with the transaction) var cells: [Text.max_selections]Text.SelRange = undefined; - const new = applyChanges(p, text, chs[0..chs_len]) catch return; + const new = sam.apply(p.gpa, text, chs[0..chs_len]) catch return; for (rs[0..got.n], 0..) |r, i| { const c = Text.rangeCells(new, .{ .anchor = mapThroughChanges(chs[0..chs_len], r.anchor), diff --git a/src/edit_cmd.zig b/src/edit_cmd.zig index c8473e99..9542f4a9 100644 --- a/src/edit_cmd.zig +++ b/src/edit_cmd.zig @@ -78,7 +78,7 @@ const World = struct { } 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; + if (@import("exec.zig").kindOf(local) == .directory) 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; @@ -93,7 +93,7 @@ const World = struct { 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)) + if (comptime pardes.hosted) if (filesystem.localPath(path)) |local| if (@import("exec.zig").kindOf(local) == .directory) 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. @@ -113,7 +113,7 @@ const World = struct { 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)) + if (comptime pardes.hosted) if (filesystem.localPath(path)) |local| if (@import("exec.zig").kindOf(local) == .directory) return said([]const u8, why, "{s} is a directory", .{path}); const bytes = filesystem.read(w.p, path) catch |err| { // In the words Get says them (builtins.Get). diff --git a/src/exec.zig b/src/exec.zig index c9f304b0..eb15dbc9 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -210,13 +210,7 @@ pub fn operandAt(p: *Pardes, pane: *Pane, clicked: Pane.Sel, borrow: bool, for_l var expanded = clicked; if (pane.file != null) { result.file_word = .{ .row = row, .lo = 0, .hi = hi }; - const first = pane.wrapRow(row, 0); - const last = pane.wrapRow(row, hi - 1); - if (first.row < 0 or last.row < 0) return result; - expanded.r0 = first.row + @as(i32, pane.tag_rows); - expanded.c0 = @as(i32, panes.File.gutterWidth(pane)) + first.pad + panes.File.displayOffset(pane, row, first.at, 0); - expanded.r1 = last.row + @as(i32, pane.tag_rows); - expanded.c1 = @as(i32, panes.File.gutterWidth(pane)) + last.pad + panes.File.displayEndOffset(pane, row, last.at, hi - 1); + expanded = fileWordCells(pane, clicked, row, 0, hi) orelse return result; } else { const shown = if (raw) raw_line else line; expanded.c0 = 0; @@ -236,16 +230,7 @@ pub fn operandAt(p: *Pardes, pane: *Pane, clicked: Pane.Sel, borrow: bool, for_l const hi = std.math.cast(i32, b.hi) orelse return result; result.text = line[b.lo..b.hi]; result.file_word = .{ .row = row, .lo = lo, .hi = hi }; - - var expanded = clicked; - const first = pane.wrapRow(row, lo); - const last = pane.wrapRow(row, hi - 1); - if (first.row >= 0 and last.row >= 0) { - expanded.r0 = first.row + @as(i32, pane.tag_rows); - expanded.c0 = @as(i32, panes.File.gutterWidth(pane)) + first.pad + panes.File.displayOffset(pane, row, first.at, lo); - expanded.r1 = last.row + @as(i32, pane.tag_rows); - expanded.c1 = @as(i32, panes.File.gutterWidth(pane)) + last.pad + panes.File.displayEndOffset(pane, row, last.at, hi - 1); - } + const expanded = fileWordCells(pane, clicked, row, lo, hi) orelse clicked; result.preview = expanded; result.expanded = expanded; return result; @@ -259,16 +244,35 @@ pub fn operandAt(p: *Pardes, pane: *Pane, clicked: Pane.Sel, borrow: bool, for_l return result; } +/// The cells bytes `lo..hi` of a file pane's line `row` take on screen, +/// soft-wrapped or not, as `clicked` reshaped; null when scrolled off. +fn fileWordCells(pane: *Pane, clicked: Pane.Sel, row: i32, lo: i32, hi: i32) ?Pane.Sel { + const first = pane.wrapRow(row, lo); + const last = pane.wrapRow(row, hi - 1); + if (first.row < 0 or last.row < 0) return null; + var cells = clicked; + cells.r0 = first.row + @as(i32, pane.tag_rows); + cells.c0 = @as(i32, panes.File.gutterWidth(pane)) + first.pad + panes.File.displayOffset(pane, row, first.at, lo); + cells.r1 = last.row + @as(i32, pane.tag_rows); + cells.c1 = @as(i32, panes.File.gutterWidth(pane)) + last.pad + panes.File.displayEndOffset(pane, row, last.at, hi - 1); + return cells; +} + +/// Where a held selection is looked for, step `k` of `p.panes.len + 2`: +/// `first`, then the active pane, then the rest; null for one seen. +fn heldPane(p: *Pardes, first: usize, k: usize) ?*Pane { + const i = switch (k) { + 0 => first, + 1 => p.active, + else => k - 2, + }; + if (k > 0 and (i == first or (k > 1 and i == p.active))) return null; + return p.panes[i]; +} + pub fn heldSelection(p: *Pardes, first: usize) ?[]const u8 { - var k: usize = 0; - while (k < p.panes.len + 2) : (k += 1) { - const i = switch (k) { - 0 => first, - 1 => p.active, - else => k - 2, - }; - if (k > 0 and (i == first or (k > 1 and i == p.active))) continue; - const t = p.panes[i] orelse continue; + for (0..p.panes.len + 2) |k| { + const t = heldPane(p, first, k) orelse continue; if (comptime pdf_enabled) if (p.native_images) if (t.pdf) |pv| { if (pv.selection != null and pv.selection_text.len > 0) return pv.selection_text; @@ -285,15 +289,8 @@ pub fn heldSelection(p: *Pardes, first: usize) ?[]const u8 { /// Where the selection `heldSelection` would give is, as acme's argument /// location (exec.c:162, getbytearg): `path:#q0,#q1` of a file pane's dot. pub fn heldSelectionPlace(p: *Pardes, first: usize, buf: []u8) ?[]const u8 { - var k: usize = 0; - while (k < p.panes.len + 2) : (k += 1) { - const i = switch (k) { - 0 => first, - 1 => p.active, - else => k - 2, - }; - if (k > 0 and (i == first or (k > 1 and i == p.active))) continue; - const t = p.panes[i] orelse continue; + for (0..p.panes.len + 2) |k| { + const t = heldPane(p, first, k) orelse continue; if (t.sel[sel_slot].state == .done) return null; // a pointer selection: no byte range kept if ((t.body.vsel.active and t.body.vsel.explicit) or t.body.msel.active) { const f = t.file orelse return null; @@ -464,18 +461,7 @@ pub fn missingDir(p: *Pardes, dir: []const u8) bool { // the shell would start: no command or terminal runs there, and none // is made to say `exit 127`. const local = @import("fs.zig").localPath(dir) orelse return std.mem.startsWith(u8, dir, "/n/"); - return !isDirectory(local); -} - -/// Whether `path` is a directory on this host, the turn out while it asks -/// (it may be inside a mount this editor serves). -pub fn isDirectory(path: []const u8) bool { - var buf: [4096]u8 = undefined; - const z = std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0) catch return false; - pardes.turn.yield(); - defer pardes.turn.back(); - const stat = @import("fs.zig").statPath(std.Io.Threaded.global_single_threaded.io(), z, .{}) catch return false; - return stat.kind == .directory; + return kindOf(local) != .directory; } /// Whether `path` is a plain file on this host, the turn out while it asks. @@ -484,7 +470,7 @@ pub fn isFile(path: []const u8) bool { } /// What `path` is on this host, null when it is not there; the turn out -/// while it asks. +/// while it asks (it may be inside a mount this editor serves). pub fn kindOf(path: []const u8) ?std.Io.File.Kind { var buf: [4096]u8 = undefined; const z = std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0) catch return null; diff --git a/src/gui/gui.zig b/src/gui/gui.zig index a31c6548..d3734fac 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -2567,7 +2567,7 @@ fn localSession( defer host_io.setExitWake(null, null); while (!core.quit) { - pardes.turn.restoreSettled(); + pardes.turn.bump(&pardes.turn.restores); try core.pump(host); if (core.quit) break; // a session that ended does not restore into one if (core.takeAttach()) |req| { @@ -2785,7 +2785,7 @@ test "an attached frontend draws the session's chrome from its frame alone" { try std.testing.expect(pardes.layout.splitColumn(core, 0, 1, false)); core.settings.focus_tint = true; try std.testing.expect(core.executeBuiltinLine(0, "Msg a notice with a rule under it")); - pardes.test_api.sync(core); + core.sync(); // Past its slide into place, where its rule shows. for (0..30) |_| core.update(.tick); const s = try core.render(core.scratch.allocator()); @@ -7016,7 +7016,7 @@ test "every pixel of a grip column is band, button or ring, and the button sits core.settings.tag_bottom = bottom; core.active = focus; pane.file.?.revision = pane.file.?.saved_revision + @intFromBool(dirty); - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); var g: Gui = undefined; g.words = .{}; @@ -7327,7 +7327,7 @@ test "lapis plaques stay in their tag's band, clear of every grip; only the focu try std.testing.expect(pardes.layout.splitColumn(core, 0, 1, false)); try std.testing.expect(core.executeBuiltinLine(0, "Theme lapis")); for (0..40) |_| core.update(.tick); - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); try std.testing.expect(s.chrome.decor_box_border != null and s.chrome.decor_checker != null and s.chrome.decor_dots != null and s.chrome.decor_title != null); var g: Gui = undefined; @@ -8878,7 +8878,7 @@ test "a lift never touches a tag, a grip or a header, on a light theme or a dark core.settings.lift = style; try std.testing.expect(core.executeBuiltinLine(0, "Msg a floating notice")); core.advance(core.now_ns + 2 * std.time.ns_per_s); - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); core.advance(core.now_ns + 2 * std.time.ns_per_s); const settled = try core.render(core.scratch.allocator()); @@ -9748,7 +9748,7 @@ test "a theme's own rules and scrollbar are drawn at its widths: acme's 2px blac try std.testing.expect(pardes.layout.splitColumn(core, 0, 1, false)); try std.testing.expect(core.executeBuiltinLine(0, "Theme acme")); for (0..40) |_| core.update(.tick); - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); try std.testing.expectEqual(@as(u8, 2), s.chrome.rule_px); // acme's 12px, at GripWidth's 150% default. @@ -9805,7 +9805,7 @@ test "the scrollbar thumb moves by the pixel and follows a glide" { // The thumb's top, in window pixels. fn top(gui: *Gui, p: *pardes.Pardes) !f32 { gui.decor.clearRetainingCapacity(); - pardes.test_api.sync(p); + p.sync(); const s = try p.render(p.scratch.allocator()); var groups = makeGroups(&.{}, false); try buildDecor(gui, std.testing.allocator, s, &groups, fixedCellLayout(gui), 800, 480, true); @@ -9839,7 +9839,7 @@ test "the rule between stacked panes stays in the tag band's slack, at any tagli defer core.deinit(); _ = try core.newShell(1, ""); core.newScratchBelow(0); - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); const lower = for (s.regionList()) |region| { if (region.kind == .tag and region.rect.y > 2) break region; @@ -9879,7 +9879,7 @@ test "an empty column is ruled on both sides, as between two filled ones" { try std.testing.expect(pardes.layout.splitColumn(core, 0, 1, false)); // Newcol from the left pane: an empty column between the two filled ones. core.newColumn(0); - pardes.test_api.sync(core); + core.sync(); try std.testing.expectEqual(@as(usize, 3), core.ncol); try std.testing.expectEqual(@as(usize, 0), core.col_n[1]); const s = try core.render(core.scratch.allocator()); @@ -9917,7 +9917,7 @@ test "at a 2x display every logical pixel is two: rules, the tag rule, the grip' try std.testing.expect(pardes.layout.splitColumn(core, 0, 1, false)); try std.testing.expect(core.executeBuiltinLine(0, "Theme acme")); for (0..40) |_| core.update(.tick); - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); for ([_]f32{ 1, 2 }) |scale| { display_scale = scale; @@ -9987,7 +9987,7 @@ test "decor is read from the regions, each pane's in its own group" { defer core.deinit(); _ = try core.newShell(1, ""); try std.testing.expect(pardes.layout.splitColumn(core, 0, 1, false)); - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); var g: Gui = undefined; g.decor = .empty; @@ -10055,7 +10055,7 @@ test "a pane on the move casts a shadow on the other panes' bodies alone, most a try std.testing.expect(pardes.layout.splitColumn(core, 0, 1, false)); try std.testing.expect(core.executeBuiltinLine(0, "Theme acme")); for (0..40) |_| core.update(.tick); - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); var g: Gui = undefined; g.decor = .empty; @@ -10100,7 +10100,7 @@ test "the selection glows round its block, on the body only, fading in over 100 defer core.deinit(); _ = try core.setTestFile(std.mem.asBytes(&@as([30][20]u8, @splat("some text on a line\n".*)))); core.settings.selection_glow = true; - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); try std.testing.expectEqual(@as(u8, 1), s.chrome.fx); // Two rows of a selection in the body, as the core paints one. @@ -10153,7 +10153,7 @@ test "a hovered word glows along its foot, never over the cursor, fading in over defer core.deinit(); _ = try core.setTestFile(std.mem.asBytes(&@as([30][20]u8, @splat("some text on a line\n".*)))); core.settings.hover_glow = true; - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); try std.testing.expectEqual(@as(u8, 2), s.chrome.fx); const body = for (s.regionList()) |region| { @@ -10206,7 +10206,7 @@ test "occlusion shades each body's edges, inside it, never a tag, a grip or the defer core.deinit(); _ = try core.setTestFile(std.mem.asBytes(&@as([30][20]u8, @splat("some text on a line\n".*)))); core.settings.occlusion = true; - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); try std.testing.expectEqual(@as(u8, 4), s.chrome.fx); const body = for (s.regionList()) |region| { @@ -10251,7 +10251,7 @@ test "a body tells its first line, and lapis's dots under it move a quarter as f try std.testing.expect(core.executeBuiltinLine(0, "Theme lapis")); core.settings.parallax = true; for (0..40) |_| core.update(.tick); - pardes.test_api.sync(core); + core.sync(); const s = try core.render(core.scratch.allocator()); const body = for (s.regionList()) |region| { if (region.kind == .body) break region; diff --git a/src/look.zig b/src/look.zig index 02754fff..588574ed 100644 --- a/src/look.zig +++ b/src/look.zig @@ -1453,7 +1453,7 @@ fn lookUnresolved(p: *Pardes, id: usize, pane: *Pane, trimmed: []const u8, text: if (filesystem.dotRelative(trimmed)) { const dir = Pardes.paneDir(pane); var said: [4200]u8 = undefined; - const gone = if (comptime pardes.hosted) if (filesystem.localPath(dir)) |local| !exec.isDirectory(local) else false else true; + const gone = if (comptime pardes.hosted) if (filesystem.localPath(dir)) |local| exec.kindOf(local) != .directory else false else true; if (gone) return p.reportFailure(id, std.fmt.bufPrint(&said, "look: {s}: no such directory", .{dir[0..@min(dir.len, 4096)]}) catch "look: no such directory"); // A file not there is a miss, as `x` and `/abs/x` are: said and // logged, the write answered with nothing. @@ -1893,7 +1893,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { .dir => |dir| { // A directory that is not there (the pane's own, gone): said, // ENOENT, no terminal made in it. - if (comptime pardes.hosted) if (filesystem.localPath(dir)) |local| if (!exec.isDirectory(local)) { + if (comptime pardes.hosted) if (filesystem.localPath(dir)) |local| if (exec.kindOf(local) != .directory) { var said: [4200]u8 = undefined; return p.reportFailure(id, std.fmt.bufPrint(&said, "look: {s}: no such directory", .{dir[0..@min(dir.len, 4096)]}) catch "look: no such directory"); }; diff --git a/src/macos.zig b/src/macos.zig index 55f6b3f6..d2122967 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -1378,9 +1378,9 @@ export fn pardes_tick() bool { did = true; st.core.perform(effect); } - pardes.turn.settle(); + pardes.turn.bump(&pardes.turn.epoch); if (restoreCore(st)) did = true; - pardes.turn.restoreSettled(); + pardes.turn.bump(&pardes.turn.restores); return did; } diff --git a/src/mini.zig b/src/mini.zig index 8d8ea0b2..9f7d865d 100644 --- a/src/mini.zig +++ b/src/mini.zig @@ -52,11 +52,7 @@ const Row = struct { // unreachable below: `generate` refuses a source that is no UTF-8 const n = std.unicode.utf8ByteSequenceLength(grapheme[0]) catch unreachable; const cp = std.unicode.utf8Decode(grapheme[0..n]) catch unreachable; - const blank = switch (cp) { - '\t'...'\r', ' ', 0x85, 0xa0, 0x1680, 0x2000...0x200a, 0x2028, 0x2029, 0x202f, 0x205f, 0x3000 => true, - else => false, - }; - row.ink = !blank or grapheme.len != n; + row.ink = !modal.isUnicodeWhitespace(cp) or grapheme.len != n; row.color = if (styles.len == 0) 0 else styles[row.base + row.at]; row.at = end; } diff --git a/src/modal.zig b/src/modal.zig index ad06833f..7e10a877 100644 --- a/src/modal.zig +++ b/src/modal.zig @@ -454,25 +454,12 @@ pub const Normal = struct { switch (state.prefix) { .goto => { state.prefix = .none; - if (key.has(.goto_file_start)) return resultAction(.{ .goto = .{ .target = .file_start, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_last_line)) return resultAction(.{ .goto = .{ .target = .last_line, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_line_start)) return resultAction(.{ .goto = .{ .target = .line_start, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_line_end)) return resultAction(.{ .goto = .{ .target = .line_end, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_first_nonws)) return resultAction(.{ .goto = .{ .target = .first_nonws, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_line_down)) return resultAction(.{ .goto = .{ .target = .line_down, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_line_up)) return resultAction(.{ .goto = .{ .target = .line_up, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_column)) return resultAction(.{ .goto = .{ .target = .column, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_view_top)) return resultAction(.{ .goto = .{ .target = .view_top, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_view_center)) return resultAction(.{ .goto = .{ .target = .view_center, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_view_bottom)) return resultAction(.{ .goto = .{ .target = .view_bottom, .count = count, .explicit_count = counted.explicit } }); + // each target's role is its name after `goto_` + inline for (comptime std.enums.values(Goto)) |target| + if (key.has(@field(Role, "goto_" ++ @tagName(target)))) return resultAction(.{ .goto = .{ .target = target, .count = count, .explicit_count = counted.explicit } }); if (key.has(.goto_word)) return resultAction(.goto_word); - if (key.has(.goto_last_modification)) return resultAction(.{ .goto = .{ .target = .last_modification, .count = count, .explicit_count = counted.explicit } }); - if (key.has(.goto_definition)) return resultAction(.{ .lsp = .definition }); - if (key.has(.goto_declaration)) return resultAction(.{ .lsp = .declaration }); - if (key.has(.goto_type_definition)) return resultAction(.{ .lsp = .type_definition }); - if (key.has(.goto_implementation)) return resultAction(.{ .lsp = .implementation }); - if (key.has(.goto_references)) return resultAction(.{ .lsp = .references }); - if (key.has(.goto_callstack)) return resultAction(.{ .lsp = .callstack }); + inline for (.{ .definition, .declaration, .type_definition, .implementation, .references, .callstack }) |lsp| + if (key.has(@field(Role, "goto_" ++ @tagName(lsp)))) return resultAction(.{ .lsp = lsp }); return .ignored; }, .view => { @@ -608,20 +595,11 @@ pub const Normal = struct { if (key.has(.page_down)) return resultAction(.{ .page = .{ .kind = .down, .count = count } }); if (key.has(.page_up)) return resultAction(.{ .page = .{ .kind = .up, .count = count } }); - if (key.has(.insert)) return resultAction(.{ .insert = .{ .kind = .at, .count = count } }); - if (key.has(.append)) return resultAction(.{ .insert = .{ .kind = .append, .count = count } }); - if (key.has(.insert_line_start)) return resultAction(.{ .insert = .{ .kind = .line_start, .count = count } }); - if (key.has(.insert_line_end)) return resultAction(.{ .insert = .{ .kind = .line_end, .count = count } }); - if (key.has(.open_below)) return resultAction(.{ .insert = .{ .kind = .open_below, .count = count } }); - if (key.has(.open_above)) return resultAction(.{ .insert = .{ .kind = .open_above, .count = count } }); - - if (key.has(.select_mode)) return resultAction(.{ .select = .{ .kind = .mode, .count = count } }); - if (key.has(.select_line)) return resultAction(.{ .select = .{ .kind = .line, .count = count } }); - if (key.has(.select_line_bounds)) return resultAction(.{ .select = .{ .kind = .line_bounds, .count = count } }); - if (key.has(.shrink_to_line_bounds)) return resultAction(.{ .select = .{ .kind = .shrink_to_line_bounds, .count = count } }); - if (key.has(.collapse_selection)) return resultAction(.{ .select = .{ .kind = .collapse, .count = count } }); - if (key.has(.flip_selection)) return resultAction(.{ .select = .{ .kind = .flip, .count = count } }); - if (key.has(.select_all)) return resultAction(.{ .select = .{ .kind = .all, .count = count } }); + inline for (.{ .{ .insert, .at }, .{ .append, .append }, .{ .insert_line_start, .line_start }, .{ .insert_line_end, .line_end }, .{ .open_below, .open_below }, .{ .open_above, .open_above } }) |pair| + if (key.has(pair[0])) return resultAction(.{ .insert = .{ .kind = pair[1], .count = count } }); + + inline for (.{ .{ .select_mode, .mode }, .{ .select_line, .line }, .{ .select_line_bounds, .line_bounds }, .{ .shrink_to_line_bounds, .shrink_to_line_bounds }, .{ .collapse_selection, .collapse }, .{ .flip_selection, .flip }, .{ .select_all, .all } }) |pair| + if (key.has(pair[0])) return resultAction(.{ .select = .{ .kind = pair[1], .count = count } }); if (key.has(.copy_sel_below)) return resultAction(.{ .multi = .{ .kind = .copy_below, .count = count } }); if (key.has(.copy_sel_above)) return resultAction(.{ .multi = .{ .kind = .copy_above, .count = count } }); @@ -788,7 +766,7 @@ fn codepointAt(text: []const u8, off: usize) u21 { return std.unicode.utf8Decode(text[off .. off + n]) catch 0xFFFD; } -fn isUnicodeWhitespace(cp: u21) bool { +pub fn isUnicodeWhitespace(cp: u21) bool { if (cp == ' ' or (cp >= '\t' and cp <= '\r') or cp == 0x85) return true; return switch (uucode.get(.general_category, cp)) { .separator_space, .separator_line, .separator_paragraph => true, @@ -857,13 +835,6 @@ pub fn firstNonWs(line: []const u8) usize { return i; } -pub fn firstNonWsOf(lines: []const []const u8, c: Cursor) Cursor { - // the row can sit past the content (mouse click below a short pane's - // last line) — out of range reads as an empty line, like lineLenOf - if (c.row >= lines.len) return .{ .row = c.row, .col = 0 }; - return .{ .row = c.row, .col = firstNonWs(lines[c.row]) }; -} - pub fn charLeft(lines: []const []const u8, c: Cursor) Cursor { if (c.row >= lines.len) return .{ .row = c.row, .col = 0 }; return .{ .row = c.row, .col = prevGrapheme(lines[c.row], @min(c.col, lines[c.row].len)) }; @@ -1717,18 +1688,6 @@ pub fn moveParagraph(text: []const u8, r: Selection, count: usize, fwd: bool, ex return .{ .anchor = anchor, .head = head }; } -/// j/k target: helix move_vertically — goal_col clamps to the line's content -/// length, i.e. the cursor may land ON the '\n' of a shorter line. -pub fn verticalTarget(text: []const u8, pos: usize, down: bool, count: usize, goal_col: usize) usize { - const nlines = cursorLineCount(text); - const line = lineAtOffset(text, pos); - const nline = if (down) @min(line + @max(1, count), nlines - 1) else line -| @max(1, count); - const s = lineStartOffset(text, nline); - // lineEndOffset(text, nline) without its second walk to nline (see offsetAt) - const e = std.mem.indexOfScalarPos(u8, text, s, '\n') orelse text.len; - return s + graphemeStart(text[s..e], @min(goal_col, e - s)); -} - /// f/F/t/T target cell. helix find_char: the exclusive (till) search starts /// one further out so repeats make progress; not-found = null (no move). pub fn findTarget(text: []const u8, cursor: usize, ch: u21, fwd: bool, till: bool, count: usize) ?usize { @@ -1976,14 +1935,6 @@ test "paragraph selections cross blank lines" { try std.testing.expectEqual(@as(usize, 7), r.head); } -test "vertical target clamps the goal column onto the newline cell" { - const t = "abcdef\nab\nabcdef\n"; - // from (0,5) down: line "ab" clamps to its '\n' at offset 9 - try std.testing.expectEqual(@as(usize, 9), verticalTarget(t, 5, true, 1, 5)); - // two down with the same goal restores col 5 - try std.testing.expectEqual(@as(usize, 15), verticalTarget(t, 9, true, 1, 5)); -} - test "find targets count matches and skip adjacent till targets" { const t = "abcabc\n"; try std.testing.expectEqual(@as(usize, 2), findTarget(t, 0, 'c', true, false, 1).?); @@ -2148,14 +2099,11 @@ test "codepoint classes distinguish words punctuation and whitespace" { try std.testing.expectEqual(Kind.ws, kindOfCodepoint('\n')); } -test "insert cursor steps characters and skips indentation" { +test "insert cursor steps characters" { const lines = [_][]const u8{ "alpha beta", " two words", "x" }; const c = Cursor{ .row = 0, .col = 5 }; try std.testing.expectEqual(Cursor{ .row = 0, .col = 4 }, charLeft(&.{"hello"}, c)); try std.testing.expectEqual(Cursor{ .row = 0, .col = 6 }, charRight(&lines, c)); - try std.testing.expectEqual(Cursor{ .row = 1, .col = 2 }, firstNonWsOf(&lines, Cursor{ .row = 1, .col = 0 })); - // cursor row past the content (mouse click below a short pane): no panic - try std.testing.expectEqual(Cursor{ .row = 24, .col = 0 }, firstNonWsOf(&lines, Cursor{ .row = 24, .col = 3 })); } test "lineSpan agrees with the whole-document scans it replaces" { diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 480c1dc7..f9bec775 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -110,7 +110,7 @@ pub fn run(p: *Pardes, id: usize, exec: bool, text: []const u8, in_tag: bool) vo /// A write's messages, a line each, except that an `Edit` line takes the /// lines after it while its `{` group or `a`/`c`/`i` text block is open -/// (sam_edit.needsMore): a block written whole reaches Edit whole, never +/// (sam_edit.waitsFor): a block written whole reaches Edit whole, never /// as lines of their own. const Messages = struct { p: *Pardes, @@ -708,7 +708,7 @@ pub fn writePager(p: *Pardes, req: Req) Reply { var shown: [256]u8 = undefined; return tree.failText(req.tag, E.INVAL, std.fmt.bufPrint(&p.fs.ename, "pager: invalid directory \"{s}\": must be absolute", .{blanked(dir[0..@min(dir.len, shown.len)], &shown)}) catch "pager: invalid directory: must be absolute"); } - if (comptime pardes.hosted) if (dir.len > 0) if (pardes.filesystem.localPath(dir)) |local| if (!exec_line.isDirectory(local)) { + if (comptime pardes.hosted) if (dir.len > 0) if (pardes.filesystem.localPath(dir)) |local| if (exec_line.kindOf(local) != .directory) { // A file there is no directory, ENOTDIR; nothing there, ENOENT. if (exec_line.kindOf(local) != null) return tree.failText(req.tag, E.NOTDIR, std.fmt.bufPrint(&p.fs.ename, "pager: {s}: not a directory", .{dir[0..@min(dir.len, 256)]}) catch "pager: not a directory"); diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index dc104e4b..f9112ae2 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -963,7 +963,7 @@ fn writeName(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply { // A directory (`~`, `/`, `foo/`) is no file to name a pane after: a // Save could only fail. Said now, EISDIR. const is_dir = name[name.len - 1] == '/' or std.mem.eql(u8, full, "/") or - (if (comptime pardes.hosted) (if (fs.localPath(full)) |local| exec.isDirectory(local) else false) else false); + (if (comptime pardes.hosted) (if (fs.localPath(full)) |local| exec.kindOf(local) == .directory else false) else false); if (is_dir) return tree.failText(req.tag, E.ISDIR, std.fmt.bufPrint(&p.fs.ename, "name: {s} is a directory, not a file", .{full[0..@min(full.len, 256)]}) catch "name: is a directory, not a file"); nameBuffer(p, id, full, false) catch |err| return Reply.fail(req.tag, switch (err) { error.OutOfMemory => E.NOMEM, diff --git a/src/pardes.zig b/src/pardes.zig index c2a3ba21..80ff3495 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -188,40 +188,20 @@ pub const Turn = struct { t.release(); } - /// The editor performed the core's effects. - pub fn settle(t: *Turn) void { + /// One of `epoch`, `restores` or `lsp_answers` moved on: the editor + /// performed the core's effects, a shell attempted a Restore (at the + /// top of each loop step, when the previous step's attempt is over), + /// or a language server's answer was taken in. + pub fn bump(t: *Turn, counter: *u64) void { const io = t.io orelse return; - t.epoch +%= 1; + counter.* +%= 1; t.settled.broadcast(io); } - /// With the turn: waits, turn given up meanwhile, until the editor has - /// performed past `epoch`; stopping ends the wait early. - pub fn awaitSettled(t: *Turn, epoch: u64) void { - while (t.epoch == epoch) t.settled.wait(t.io.?, &t.mutex) catch return; - } - - /// The shell attempted the Restore the core asked for, whichever way it - /// went; called at the top of each loop step, when the previous step's - /// attempt is over. - pub fn restoreSettled(t: *Turn) void { - const io = t.io orelse return; - t.restores +%= 1; - t.settled.broadcast(io); - } - - pub fn lspAnswered(t: *Turn) void { - const io = t.io orelse return; - t.lsp_answers +%= 1; - t.settled.broadcast(io); - } - - pub fn awaitLsp(t: *Turn, answers: u64) void { - while (t.lsp_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; + /// With the turn: waits, turn given up meanwhile, until `counter` has + /// moved past `seen`; stopping ends the wait early. + pub fn awaitPast(t: *Turn, counter: *const u64, seen: u64) void { + while (counter.* == seen) t.settled.wait(t.io.?, &t.mutex) catch return; } fn release(t: *Turn) void { @@ -520,18 +500,6 @@ pub const test_api = if (@import("builtin").is_test) struct { exec.runBuiltin(p, command, id, txt, arg); return true; } - - pub fn sync(p: *Pardes) void { - p.sync(); - } - - pub fn searchStep(p: *Pardes, id: usize, delta: i32) bool { - return look.searchStep(p, id, delta); - } - - pub fn heldSelection(p: *Pardes, first: usize) ?[]const u8 { - return exec.heldSelection(p, first); - } } else struct {}; fn drainForSavePath(p: *Pardes, buf: []u8) ?[]const u8 { @@ -2982,89 +2950,36 @@ fn chordName(comptime c: config.Chord) []const u8 { } } -fn shortcutCount(comptime b: Builtin) comptime_int { - var count = 0; - if (b == .Window) count += 1; - for (config.jump_keys) |jk| if (jk.cmd == b) { - count += 1; - }; - if (config.look_cmd == b) count += config.look_key.len + 1; - if (config.exec_cmd == b) count += config.exec_key.len + 1; - var it = std.mem.tokenizeScalar(u8, config.topbar_str, ' '); - while (it.next()) |w| if (std.mem.eql(u8, w, @tagName(b))) { - count += 1; - }; - return count; -} - fn shortcuts(comptime b: Builtin) []const u8 { comptime { @setEvalBranchQuota(20000); - var parts: [shortcutCount(b)][]const u8 = undefined; - var part: usize = 0; - if (b == .Window) { - parts[part] = chordName(config.window_prefix[0]); - part += 1; - } - for (config.jump_keys) |jk| { - if (jk.cmd == b) { - parts[part] = chordName(jk.chord); - part += 1; - } - } + // each part after ", "; the first one's is cut off at the end + var s: []const u8 = ""; + if (b == .Window) s = s ++ ", " ++ chordName(config.window_prefix[0]); + for (config.jump_keys) |jk| if (jk.cmd == b) { + s = s ++ ", " ++ chordName(jk.chord); + }; if (config.look_cmd == b) { - for (config.look_key) |k| { - parts[part] = chordName(k); - part += 1; - } - parts[part] = @tagName(config.look_button) ++ "-click"; - part += 1; + for (config.look_key) |k| s = s ++ ", " ++ chordName(k); + s = s ++ ", " ++ @tagName(config.look_button) ++ "-click"; } if (config.exec_cmd == b) { - for (config.exec_key) |k| { - parts[part] = chordName(k); - part += 1; - } - parts[part] = @tagName(config.exec_button) ++ "-click"; - part += 1; + for (config.exec_key) |k| s = s ++ ", " ++ chordName(k); + s = s ++ ", " ++ @tagName(config.exec_button) ++ "-click"; } var it = std.mem.tokenizeScalar(u8, config.topbar_str, ' '); - while (it.next()) |w| { - if (std.mem.eql(u8, w, @tagName(b))) { - parts[part] = "topbar"; - part += 1; - } - } - var len: usize = if (parts.len == 0) 0 else 2 * (parts.len - 1); - for (parts) |text| len += text.len; - var out: [len]u8 = undefined; - var at: usize = 0; - for (parts, 0..) |text, i| { - if (i > 0) { - @memcpy(out[at..][0..2], ", "); - at += 2; - } - @memcpy(out[at..][0..text.len], text); - at += text.len; - } - return &out; + while (it.next()) |w| if (std.mem.eql(u8, w, @tagName(b))) { + s = s ++ ", topbar"; + }; + return if (s.len == 0) s else s[2..]; } } fn leaderKeys(comptime path: []const u8) []const u8 { comptime { - var out: [4 + 2 * path.len - 1]u8 = undefined; - @memcpy(out[0..4], "SPC "); - var at: usize = 4; - for (path, 0..) |c, i| { - if (i > 0) { - out[at] = ' '; - at += 1; - } - out[at] = c; - at += 1; - } - return &out; + var s: []const u8 = "SPC"; + for (path) |c| s = s ++ " " ++ [_]u8{c}; + return s; } } @@ -5658,6 +5573,21 @@ pub const Pardes = struct { if (!p.fallback.writeFile(path, bytes)) p.saveFailed(pane, path, error.OutOfMemory); } + /// Writes `bytes` to `path` for pane `id`, its directories made first: + /// the pane after it, if it is still the one `serial` names and the + /// write did not fail. A failed write changes nothing: its file goes + /// back to the saved revision `was`, dirty as it was. + fn writeChecked(p: *Pardes, id: u8, serial: u32, path: []const u8, bytes: []const u8, was: u32) ?*Pane { + const failures = p.fs.failures; + makeParents(path); + p.hostWriteFile(id, path, bytes); + const pane = p.panes[id] orelse return null; + if (pane.serial != serial) return null; + if (p.fs.failures == failures) return pane; + if (pane.file) |*f| f.saved_revision = was; + return null; + } + /// A write of `path` the host could not do: the pane stays dirty, the /// message row says `Save : `, and the 9P write that asked /// for it, waiting on the save, fails with that (`fs.late_failure`). @@ -5740,6 +5670,18 @@ pub const Pardes = struct { filesystem.makeDirs(std.fs.path.dirname(local) orelse "/"); } + /// The directory a write of `path` that failed with `err` went into: + /// what is there (a directory when it is no local path), whether it is + /// missing, and whether that is because one above refuses (EACCES). + fn parentTrouble(path: []const u8, err: anyerror) struct { dir: []const u8, kind: ?std.Io.File.Kind, missing: bool, denied: bool } { + const dir = std.fs.path.dirname(path) orelse "/"; + const local = if (comptime hosted) filesystem.localPath(dir) else null; + const kind: ?std.Io.File.Kind = if (comptime hosted) (if (local) |l| exec.kindOf(l) else .directory) else .directory; + const missing = err == error.FileNotFound or err == error.NotDir or kind != .directory; + const denied = missing and (if (comptime hosted) (if (local) |l| filesystem.deniedAbove(l) else false) else false); + return .{ .dir = dir, .kind = kind, .missing = missing, .denied = denied }; + } + pub fn saveFailed(p: *Pardes, id: u8, path: []const u8, err: anyerror) void { if (p.panes[id]) |pane| if (pane.file) |*f| { // The `-%` spelling fs.zig already uses for "make this dirty". @@ -5747,15 +5689,11 @@ pub const Pardes = struct { }; var what: [limits.host_path_cap + 32]u8 = undefined; // A write into a directory that is not there: said so, in the words - // a mount reads as ENOENT. - const parent = std.fs.path.dirname(path) orelse "/"; - const no_dir = err == error.FileNotFound or err == error.NotDir or - (if (comptime hosted) (if (filesystem.localPath(parent)) |local| !exec.isDirectory(local) else false) else false); - // Not there because one above refuses: that is the reason, EACCES. - const denied = no_dir and (if (comptime hosted) (if (filesystem.localPath(parent)) |local| filesystem.deniedAbove(local) else false) else false); - if (denied) + // a mount reads as ENOENT; EACCES when one above refuses. + const parent = parentTrouble(path, err); + if (parent.denied) p.reportFailure(id, std.fmt.bufPrint(&what, "Save {s}: permission denied", .{path}) catch "Save: permission denied") - else if (no_dir) + else if (parent.missing) p.reportFailure(id, std.fmt.bufPrint(&what, "Save {s}: no such directory", .{path}) catch "Save: no such directory") else p.reportError(id, std.fmt.bufPrint(&what, "Save {s}", .{path}) catch "Save", err); @@ -5770,18 +5708,14 @@ pub const Pardes = struct { const operation = std.fmt.bufPrint(&what, "Dump {s}", .{path}) catch "Dump"; // Into a DumpDir that is not there: said so, ENOENT, as a Save is; // one that is there but no directory (`/dev/null`) says that. - const parent = std.fs.path.dirname(path) orelse "/"; - const kind: ?std.Io.File.Kind = if (comptime hosted) (if (filesystem.localPath(parent)) |local| exec.kindOf(local) else .directory) else .directory; - const no_dir = err == error.FileNotFound or err == error.NotDir or kind != .directory; - // Not there because one above refuses: that is why, EACCES. - const denied = no_dir and kind == null and (if (comptime hosted) (if (filesystem.localPath(parent)) |local| filesystem.deniedAbove(local) else false) else false); - if (denied) { + const parent = parentTrouble(path, err); + if (parent.denied and parent.kind == null) { var said: [limits.host_path_cap + 48]u8 = undefined; p.reportFailure(p.active, std.fmt.bufPrint(&said, "{s}: permission denied", .{operation}) catch "Dump: permission denied"); - } else if (kind != null and kind != .directory) { + } else if (parent.kind != null and parent.kind != .directory) { var said: [limits.host_path_cap + 48]u8 = undefined; - p.reportFailure(p.active, std.fmt.bufPrint(&said, "{s}: {s} is not a directory", .{ operation, parent }) catch "Dump: not a directory"); - } else if (no_dir) { + p.reportFailure(p.active, std.fmt.bufPrint(&said, "{s}: {s} is not a directory", .{ operation, parent.dir }) catch "Dump: not a directory"); + } else if (parent.missing) { var said: [limits.host_path_cap + 48]u8 = undefined; p.reportFailure(p.active, std.fmt.bufPrint(&said, "{s}: no such directory", .{operation}) catch "Dump: no such directory"); } else p.reportError(p.active, operation, err); @@ -5836,22 +5770,13 @@ pub const Pardes = struct { .save_file => |sf| { const pane = p.panes[sf.pane] orelse return; const f = if (pane.file) |*file| file else return; - const serial = pane.serial; const was = f.saved_revision; - const failures = p.fs.failures; f.saved_revision = f.revision; - makeParents(f.path); - p.hostWriteFile(sf.pane, f.path, f.content); - const saved_pane = p.panes[sf.pane] orelse return; - if (saved_pane.serial != serial) return; - const saved = if (saved_pane.file) |*file| file else return; // A save the host could not do (saveFailed) is its err // record alone, not a `save`, and changes nothing: a clean // file stays clean. - if (p.fs.failures != failures) { - saved.saved_revision = was; - return; - } + const saved_pane = p.writeChecked(sf.pane, pane.serial, f.path, f.content, was) orelse return; + const saved = if (saved_pane.file) |*file| file else return; // Edited while it was written: what is on disk is not this. if (saved.saved_revision != saved.revision) return; saved.disk_gone = false; @@ -5867,28 +5792,16 @@ pub const Pardes = struct { const pane = p.panes[st.pane] orelse return; if (pane.serial != st.serial) return; // a recycled slot: not ours if (pane.file) |*f| { - const was = f.saved_revision; - const failures = p.fs.failures; - makeParents(st.path.slice()); - p.hostWriteFile(st.pane, st.path.slice(), f.content); - const after = p.panes[st.pane] orelse return; - if (after.serial != st.serial) return; - const g = if (after.file) |*file| file else return; // Failed, it changes nothing: not the name, not dirty. - if (p.fs.failures != failures) { - g.saved_revision = was; - return; - } + const after = p.writeChecked(st.pane, st.serial, st.path.slice(), f.content, f.saved_revision) orelse return; + if (after.file == null) return; if (st.promote) p.promoteSaved(st.pane, st.path.slice()) else ctlfs.events.noteLogAs(p, .save, after, st.path.slice()); return; } if (!pane.isTerminal()) return; const text = panes.terminal.screenTextAlloc(pane, p.gpa) catch return; defer p.gpa.free(text); - makeParents(st.path.slice()); - const failures = p.fs.failures; - p.hostWriteFile(st.pane, st.path.slice(), text); - if (p.fs.failures == failures) if (p.panes[st.pane]) |still| if (still.serial == st.serial) + if (p.writeChecked(st.pane, st.serial, st.path.slice(), text, 0)) |still| ctlfs.events.noteLogAs(p, .save, still, st.path.slice()); }, .write_dump => { @@ -5929,17 +5842,9 @@ pub const Pardes = struct { } 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; - } + const pane = p.writeChecked(w.pane, w.serial, w.path, w.bytes, was) orelse continue; + const f = if (pane.file) |*file| file else continue; if (w.promote) { p.promoteSaved(w.pane, w.path); } else if (w.clean != null and w.clean.? == f.revision) { @@ -5994,7 +5899,7 @@ pub const Pardes = struct { ctlfs.runClosedLines(p); while (p.nextQueued()) |ev| p.update(ev); while (p.nextEffect()) |e| p.perform(e); - turn.settle(); + turn.bump(&turn.epoch); // A quitting frame has already freed what it would draw. if (p.quit) return; if (v.poll_frame) |f| f(h.ctx); @@ -6234,10 +6139,7 @@ pub const Pardes = struct { // ---- tag + selection text (chord sources) ---- pub fn paneIdOf(p: *const Pardes, pane: *const Pane) ?usize { - for (p.panes, 0..) |slot, id| if (slot) |sp| { - if (sp == pane) return id; - }; - return null; + return std.mem.findScalar(?*const Pane, &p.panes, pane); } /// Cycle only the body modes supported by this pane. @@ -7036,7 +6938,7 @@ pub const Pardes = struct { } pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void { - defer turn.lspAnswered(); + defer turn.bump(&turn.lsp_answers); const w = p.lsp_wait orelse return; if (w.id != id) return; // superseded by a newer press, or the pane died p.lsp_wait = null; diff --git a/src/sam_edit.zig b/src/sam_edit.zig index 94b76816..73cd6ecd 100644 --- a/src/sam_edit.zig +++ b/src/sam_edit.zig @@ -187,9 +187,12 @@ pub fn run(arena: std.mem.Allocator, files: []const File, cur: usize, command: [ /// an a, c or i text block, or the `}` of a group. pub const Wait = enum { none, text, group }; -/// As `needsMore`, saying which: while a text block is open only a `.` -/// line can end it, so a caller need not parse the block again for every -/// line of text in it (ninep/ctl.zig, editEnd), which was quadratic. +/// Whether `command` stops inside a `{` group or an `a`, `c` or `i` text +/// block still waiting for its `.` line, and which: a writer of lines (a +/// ctl, exec) hands an Edit the lines after it until it does not. While a +/// text block is open only a `.` line can end it, so a caller need not +/// parse the block again for every line of text in it (ninep/ctl.zig, +/// editEnd), which was quadratic. pub fn waitsFor(arena: std.mem.Allocator, command: []const u8) Wait { var why: Why = .{}; const src = std.fmt.allocPrint(arena, "{s}\n", .{command}) catch return .none; @@ -217,19 +220,6 @@ 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; - var ps: Parser = .{ .arena = arena, .src = src, .why = &why }; - while (true) { - const c = ps.parse(0) catch return std.mem.eql(u8, why.text(), "unmatched `{'") or ps.open_text; - if (c == null) return ps.open_text; - } -} - /// 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 { @@ -1191,13 +1181,13 @@ test "an Edit wants the lines after it until its braces and text blocks close" { var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena_state.deinit(); const a = arena_state.allocator(); - try std.testing.expect(needsMore(a, ",x/foo/{")); - try std.testing.expect(needsMore(a, ",x/foo/{\ni/[/")); - try std.testing.expect(!needsMore(a, ",x/foo/{\ni/[/\n}")); - try std.testing.expect(needsMore(a, "$a\nline one")); - try std.testing.expect(!needsMore(a, "$a\nline one\n.")); - try std.testing.expect(!needsMore(a, ",s/{/x/")); - try std.testing.expect(!needsMore(a, ",x/foo/c/bar/")); + try std.testing.expect(waitsFor(a, ",x/foo/{") != .none); + try std.testing.expect(waitsFor(a, ",x/foo/{\ni/[/") != .none); + try std.testing.expectEqual(Wait.none, waitsFor(a, ",x/foo/{\ni/[/\n}")); + try std.testing.expect(waitsFor(a, "$a\nline one") != .none); + try std.testing.expectEqual(Wait.none, waitsFor(a, "$a\nline one\n.")); + try std.testing.expectEqual(Wait.none, waitsFor(a, ",s/{/x/")); + try std.testing.expectEqual(Wait.none, waitsFor(a, ",x/foo/c/bar/")); } test "the dot an Edit leaves selects what a change put where it stood" { diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 5eecb3e9..8f5f102a 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -1403,7 +1403,7 @@ fn localSession( } } frames: while (!core.quit) { - pardes.turn.restoreSettled(); + pardes.turn.bump(&pardes.turn.restores); try core.pump(host); if (core.takeRestore()) |rp| blk: { const bytes = filesystem.readRestore(gpa, rp, core.settings.dump_dir.get()) catch |err| { diff --git a/test/output.zig b/test/output.zig index 8403effa..523e8129 100644 --- a/test/output.zig +++ b/test/output.zig @@ -387,7 +387,7 @@ test "result stepping reaches an unterminated last row" { const source = try p.setTestFile("one\ntwo\n"); const rows = try std.fmt.allocPrint(p.gpa, "{s}:1:1 first\n{s}:2:1 last", .{ source.file.?.path, source.file.?.path }); try panes.Output.fillResults(p, 0, "/tmp", .search, "", rows, 0); - try std.testing.expect(pardes.test_api.searchStep(p, 0, 1)); + try std.testing.expect(pardes.look.searchStep(p, 0, 1)); try std.testing.expectEqual(@as(usize, 1), source.search_row.?); const result = p.panes[source.search_pane.?].?; try std.testing.expectEqual(@as(i32, 1), result.body.cur_row); diff --git a/test/panes.zig b/test/panes.zig index 1b1b3eea..28f14d94 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -610,7 +610,7 @@ const BodyHitTests = struct { // Release at a different source row without an intermediate drag event. p.update(.{ .mouse = mouse(layer, last, prefix + 14, .release) }); // A stream: the last row from its start, indentation and all. - try std.testing.expect(std.mem.endsWith(u8, pardes.test_api.heldSelection(p, 0).?, "// body") and std.mem.startsWith(u8, pardes.test_api.heldSelection(p, 0).?, "body\n")); + try std.testing.expect(std.mem.endsWith(u8, pardes.exec.heldSelection(p, 0).?, "// body") and std.mem.startsWith(u8, pardes.exec.heldSelection(p, 0).?, "body\n")); p.update(.{ .mouse = mouse(layer, last, 10, .press) }); p.update(.{ .mouse = mouse(layer, last, 10, .release) }); try std.testing.expectEqual(expected, pane.body.cur_row); @@ -915,7 +915,7 @@ const TtySelectionTests = struct { JumpSelectionTests.drag(p, x + @as(u16, if (prompt) 0 else 1), y, x + 3, y + @as(u16, if (prompt) 0 else 1)); const expected = if (prompt) "$ ab" else "lpha\nbrav"; try std.testing.expectEqual(panes.Text.Mode.tty, pane.body.mode); - try std.testing.expectEqualStrings(expected, pardes.test_api.heldSelection(p, 0).?); + try std.testing.expectEqualStrings(expected, pardes.exec.heldSelection(p, 0).?); try std.testing.expectEqualStrings(expected, p.registers.text(p.gpa, '"') orelse return error.MissingAutomaticTtyYank); try noChildInput(p); @@ -934,7 +934,7 @@ const TtySelectionTests = struct { try std.testing.expect(!std.meta.eql(selected, normal.at(x + 4, y + 1).style.bg)); try std.testing.expect(!std.meta.eql(selected, normal.at(x, y).style.bg)); } - try std.testing.expectEqualStrings(expected, pardes.test_api.heldSelection(p, 0).?); + try std.testing.expectEqualStrings(expected, pardes.exec.heldSelection(p, 0).?); p.update(.{ .key = .{ .cp = 'y' } }); try std.testing.expectEqualStrings(expected, p.registers.text(p.gpa, '"').?); try noChildInput(p); @@ -942,11 +942,11 @@ const TtySelectionTests = struct { try std.testing.expectEqual(panes.Text.Mode.tty, pane.body.mode); try noChildInput(p); _ = try p.render(frame.allocator()); - try std.testing.expectEqualStrings(expected, pardes.test_api.heldSelection(p, 0).?); + try std.testing.expectEqualStrings(expected, pardes.exec.heldSelection(p, 0).?); p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]133;C\x07" ++ std.mem.asBytes(&@as([30][12]u8, @splat("\r\nnew output".*))) } }); _ = try p.render(frame.allocator()); try std.testing.expectEqual(panes.Text.Mode.tty, pane.body.mode); - try std.testing.expectEqualStrings(expected, pardes.test_api.heldSelection(p, 0).?); + try std.testing.expectEqualStrings(expected, pardes.exec.heldSelection(p, 0).?); try std.testing.expectEqualStrings(expected, p.registers.text(p.gpa, '"').?); try noChildInput(p); } @@ -1007,7 +1007,7 @@ const TtySelectionTests = struct { try std.testing.expectEqualStrings(expected, written.written()); try std.testing.expectEqual(panes.Text.Mode.tty, p.panes[0].?.body.mode); try std.testing.expectEqual(panes.Text.Mode.tty, p.panes[1].?.body.mode); - try std.testing.expectEqualStrings("lpha\nbrav", pardes.test_api.heldSelection(p, 0).?); + try std.testing.expectEqualStrings("lpha\nbrav", pardes.exec.heldSelection(p, 0).?); } } } @@ -1277,7 +1277,7 @@ const ClickCursorTests = struct { const x = rect.x + config.GUTTER + panes.File.gutterWidth(pane); const y = bodyY(p, rect); JumpSelectionTests.drag(p, x + 1, y + 4, x + 3, y + 4); - try std.testing.expectEqualStrings("lph", pardes.test_api.heldSelection(p, 0).?); + try std.testing.expectEqualStrings("lph", pardes.exec.heldSelection(p, 0).?); for (0..2) |_| p.update(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = x + 2, .row = y + 4 } }); try std.testing.expectEqual(@as(usize, 2), pane.file.?.scroll); _ = try p.render(frame.allocator()); @@ -1339,7 +1339,7 @@ const JumpSelectionTests = struct { else drag(p, x + first_col, y, x + last_col, y + 1); - const selected = try p.gpa.dupe(u8, pardes.test_api.heldSelection(p, 0) orelse return error.MissingMouseSelection); + const selected = try p.gpa.dupe(u8, pardes.exec.heldSelection(p, 0) orelse return error.MissingMouseSelection); defer p.gpa.free(selected); if (expected) |want| try std.testing.expectEqualStrings(want, selected); try std.testing.expect(std.mem.indexOfScalar(u8, selected, '\n') != null); @@ -1358,7 +1358,7 @@ const JumpSelectionTests = struct { try std.testing.expect(pane.file.?.scroll > 20); try LookResetTests.cleared(pane); try std.testing.expect(!pane.body.vsel.active); - try std.testing.expect(pardes.test_api.heldSelection(p, 0) == null); + try std.testing.expect(pardes.exec.heldSelection(p, 0) == null); try std.testing.expectEqual(@as(i32, 59), pane.body.cur_row); try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col); @@ -1366,7 +1366,7 @@ const JumpSelectionTests = struct { // Back announces itself, so the body sits under a notice band now. const y2 = p.bodyTop(pane, p.rects[0]); drag(p, x + 1, y2, x + 3, y2 + 1); - try std.testing.expect(pardes.test_api.heldSelection(p, 0) != null); + try std.testing.expect(pardes.exec.heldSelection(p, 0) != null); p.update(.{ .key = .{ .cp = 'i', .ctrl = true } }); _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); @@ -1374,7 +1374,7 @@ const JumpSelectionTests = struct { try std.testing.expectEqual(@as(usize, 0), pane.file.?.scroll); try LookResetTests.cleared(pane); try std.testing.expect(!pane.body.vsel.active); - try std.testing.expect(pardes.test_api.heldSelection(p, 0) == null); + try std.testing.expect(pardes.exec.heldSelection(p, 0) == null); try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row); try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col); } @@ -1392,10 +1392,10 @@ const JumpSelectionTests = struct { // From the press to the end of its line, the line between whole, the // last from its start: no line numbers, no rectangle. drag(p, x + 2, y, x + 1, y + 2); - try std.testing.expectEqualStrings("pha\nbravo\nch", pardes.test_api.heldSelection(p, 0).?); + try std.testing.expectEqualStrings("pha\nbravo\nch", pardes.exec.heldSelection(p, 0).?); // Upward, the same text. drag(p, x + 1, y + 2, x + 2, y); - try std.testing.expectEqualStrings("pha\nbravo\nch", pardes.test_api.heldSelection(p, 0).?); + try std.testing.expectEqualStrings("pha\nbravo\nch", pardes.exec.heldSelection(p, 0).?); // A tag of several rows sweeps the same way. if (pane.tag.own) |own| p.gpa.free(own); pane.tag.own = try p.gpa.dupe(u8, "Save Del\nLook Edit"); @@ -2919,17 +2919,17 @@ const OutputTests = struct { } try std.testing.expectEqual(@as(usize, 2), count); try std.testing.expectEqual(matches[1], pane.search_row.?); - try std.testing.expect(pardes.test_api.searchStep(p, 0, -1)); + try std.testing.expect(pardes.look.searchStep(p, 0, -1)); try std.testing.expectEqual(matches[0], pane.search_row.?); try std.testing.expectEqual(@as(i32, 3), pane.body.cur_row); - try std.testing.expect(pardes.test_api.searchStep(p, 0, 1)); + try std.testing.expect(pardes.look.searchStep(p, 0, 1)); try std.testing.expectEqual(matches[1], pane.search_row.?); try std.testing.expectEqual(@as(i32, 5), pane.body.cur_row); - try std.testing.expect(pardes.test_api.searchStep(p, 0, 1)); + try std.testing.expect(pardes.look.searchStep(p, 0, 1)); try std.testing.expectEqual(matches[1], pane.search_row.?); pane.search_row = null; - try std.testing.expect(pardes.test_api.searchStep(p, 0, 1)); + try std.testing.expect(pardes.look.searchStep(p, 0, 1)); try std.testing.expectEqual(matches[0], pane.search_row.?); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); @@ -2971,11 +2971,11 @@ const OutputTests = struct { // still governs stepping when that output is attached to a source. restored_source.search_pane = restored_id; restored_source.search_row = null; - try std.testing.expect(pardes.test_api.searchStep(restored, 0, 1)); + try std.testing.expect(pardes.look.searchStep(restored, 0, 1)); try std.testing.expectEqual(matches[0], restored_source.search_row.?); - try std.testing.expect(pardes.test_api.searchStep(restored, 0, 1)); + try std.testing.expect(pardes.look.searchStep(restored, 0, 1)); try std.testing.expectEqual(matches[1], restored_source.search_row.?); - try std.testing.expect(pardes.test_api.searchStep(restored, 0, -1)); + try std.testing.expect(pardes.look.searchStep(restored, 0, -1)); try std.testing.expectEqual(matches[0], restored_source.search_row.?); // Editing drops source metadata; hidden context padding still keeps @@ -2983,9 +2983,9 @@ const OutputTests = struct { panes.File.setContent(p, file, try p.gpa.dupe(u8, file.content)); try std.testing.expectEqual(@as(usize, 0), file.location_rows.len); pane.search_row = null; - try std.testing.expect(pardes.test_api.searchStep(p, 0, 1)); + try std.testing.expect(pardes.look.searchStep(p, 0, 1)); try std.testing.expectEqual(matches[0], pane.search_row.?); - try std.testing.expect(pardes.test_api.searchStep(p, 0, 1)); + try std.testing.expect(pardes.look.searchStep(p, 0, 1)); try std.testing.expectEqual(matches[1], pane.search_row.?); } diff --git a/test/pdf.zig b/test/pdf.zig index cc4a258d..ea23f90a 100644 --- a/test/pdf.zig +++ b/test/pdf.zig @@ -1610,7 +1610,7 @@ test "PdfTint cycles pane-local state without adding tag clutter" { // here rather than inherited from init. _ = try p.newShell(1, ""); _ = layout.splitColumn(p, 0, 1, false); - pardes.test_api.sync(p); // rects for the new column, exactly as the two-pane boot did + p.sync(); // rects for the new column, exactly as the two-pane boot did try std.testing.expect(!hasPdf(p.panes[1].?)); try std.testing.expect(pardes.test_api.runBuiltin( p, @@ -1852,7 +1852,7 @@ test "PDF pane geometry change preserves its page-relative position" { // so the divider drag below needs a second column to take the width from. _ = try p.newShell(1, ""); _ = layout.splitColumn(p, 0, 1, false); - pardes.test_api.sync(p); // rects for the new column, exactly as the two-pane boot did + p.sync(); // rects for the new column, exactly as the two-pane boot did const pane = p.panes[0].?; const pv = &pane.pdf.?; p.native_images = true; @@ -1949,7 +1949,7 @@ test "PDF n/N addresses and reveals distinct MuPDF hits on one page" { var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); - try std.testing.expect(pardes.test_api.searchStep(p, 0, 1)); + try std.testing.expect(pardes.look.searchStep(p, 0, 1)); try std.testing.expectEqual(@as(usize, 0), pv.search_hit); try std.testing.expectEqual(@as(usize, 0), pane.search_row.?); const first = try p.render(frame.allocator()); @@ -1968,14 +1968,14 @@ test "PDF n/N addresses and reveals distinct MuPDF hits on one page" { try std.testing.expect(first_y >= first_geometry.src.y and first_y < first_geometry.src.y + first_geometry.src.h); - try std.testing.expect(pardes.test_api.searchStep(p, 0, 1)); + try std.testing.expect(pardes.look.searchStep(p, 0, 1)); try std.testing.expectEqual(@as(usize, 1), pv.search_hit); try std.testing.expectEqual(@as(usize, 2), pane.search_row.?); _ = frame.reset(.retain_capacity); const second = try p.render(frame.allocator()); try std.testing.expectEqual(revision, second.images[0].?.native.revision); - try std.testing.expect(pardes.test_api.searchStep(p, 0, 1)); + try std.testing.expect(pardes.look.searchStep(p, 0, 1)); try std.testing.expectEqual(@as(usize, 2), pv.search_hit); try std.testing.expectEqual(@as(usize, 4), pane.search_row.?); _ = frame.reset(.retain_capacity); @@ -2092,7 +2092,7 @@ test "PDF native mouse selection, Look, and highlights share page geometry" { }; const word_col = selected_col orelse return error.PdfMouseMappingMissedWord; try std.testing.expect(std.ascii.findIgnoreCase( - pardes.test_api.heldSelection(p, 0).?, + pardes.exec.heldSelection(p, 0).?, "Pardes", ) != null); @@ -2368,7 +2368,7 @@ test "Esc back into a PDF keeps the offset within its page" { const doc = p.active; const dp = p.panes[doc].?; try std.testing.expect(dp.pdf != null); - pardes.test_api.sync(p); + p.sync(); // Read a little way DOWN the page you are on, then step away. const pv = &dp.pdf.?; @@ -2376,12 +2376,12 @@ test "Esc back into a PDF keeps the offset within its page" { const mid = pv.document_scroll_y; pv.scroll_to_page_pending = false; p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a shell under the doc - pardes.test_api.sync(p); + p.sync(); const shell = p.active; try std.testing.expect(shell != doc); p.update(.{ .key = .{ .cp = Key.escape } }); // Esc: back into the PDF - pardes.test_api.sync(p); + p.sync(); try std.testing.expectEqual(doc, p.active); // The jumps stack records a PDF as its PAGE, so returning revealed the page -- cgit v1.3