diff options
Diffstat (limited to 'core/Pick.zig')
| -rw-r--r-- | core/Pick.zig | 461 |
1 files changed, 431 insertions, 30 deletions
diff --git a/core/Pick.zig b/core/Pick.zig index 52e9601d..c5b639cd 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -35,6 +35,10 @@ const Stream = @import("Stream.zig"); const Trail = @import("Trail.zig"); const TagState = @import("TagState.zig"); const Band = @import("Band.zig"); +const Places = @import("Places.zig"); +const colors = @import("colors.zig"); +const Surface = @import("surface.zig").Surface; +const Cell = @import("surface.zig").Cell; const Query = panes.Query; const File = panes.File; const Output = panes.Output; @@ -48,9 +52,10 @@ const Key = pardes.Key; /// declarations, the lines holding the query in every open pane (All, its /// places in each), the jump list, rows handed over (a language server's /// answer, SPC e's places), titled by their word, the messages' history -/// (SPC h m), and the themes (SPC t t, the window wearing the one under -/// the cursor). -pub const Source = enum { find, grep, recent, panes, symbols, all, jumplist, list, messages, themes }; +/// (SPC h m), the themes (SPC t t, the window wearing the one under +/// the cursor), and a language server's code actions for the caret's place +/// (SPC l a, the body showing the one under the cursor as its edit). +pub const Source = enum { find, grep, recent, panes, symbols, all, jumplist, list, messages, themes, actions }; /// Each source's word in the tag and the leader keys of the builtin that /// opens it, its key cells (`␣ f Find`). @@ -65,6 +70,7 @@ const words = std.EnumArray(Source, struct { word: []const u8, keys: []const u8 .list = .{ .word = "List", .keys = "l" }, .messages = .{ .word = "Messages", .keys = "hm" }, .themes = .{ .word = "Themes", .keys = "t" }, + .actions = .{ .word = "Actions", .keys = "la" }, }); /// The pickers kept after they roll up, for their list words and Resume. @@ -94,8 +100,11 @@ names: Names = .{}, /// A Grep's lines, as the disk engine sends them (Query.take). found: std.ArrayList(Query.Found) = .empty, found_bytes: std.ArrayList(u8) = .empty, -/// A list's rows, as they were handed over (`path:line:col text`). +/// A list's rows, as they were handed over (`path:line:col text`); code +/// actions' each followed by its edit's records (lsp_client actionEdits). rows: []u8 = &.{}, +/// The revision of the asking text its actions' edits are of. +revision: u32 = 0, /// The query and the cursor as it rolled up (live, they are its view's /// field and cursor). query: Kind.Buf(256) = .{}, @@ -149,10 +158,11 @@ pub fn deinit(pk: *Pick, gpa: std.mem.Allocator) void { /// Whether a search a builtin asks for from pane `id` is a picker in the /// tag (SPEC §14.3 phase 4): in a session whose jumps walk, from the -/// keyboard or a click, never a script's (a 9P write's: the query pane, -/// whose rows it reads back, and whose `not found` it hears). +/// keyboard or a click (kbdin's and mousein's too), never a script's word +/// (a 9P exec's: the query pane, whose rows it reads back, and whose `not +/// found` it hears). pub fn asked(p: *const Pardes) bool { - return p.settings.jumps == .walk and !p.fs.serving; + return p.settings.jumps == .walk and (!p.fs.serving or p.fs.typed); } /// Picker `id`, kept or live. @@ -224,8 +234,8 @@ fn hostIn(p: *Pardes, pk: *Pick, from: usize) !void { pk.owner = asker.serial; pk.made = false; // A theme is no place: its list is the asking pane's own tag (§3.5), - // and its preview the whole window. - if (pk.source == .themes) { + // and its preview the whole window; an action's is that text's edit. + if (pk.source == .themes or pk.source == .actions) { pk.landing = asker.serial; p.view().active = from; return; @@ -301,6 +311,21 @@ fn start(p: *Pardes, pk: *Pick) void { .themes => for (pardes.colors.themes, 0..) |t, i| { pk.names.add(p.gpa, t.name, 0, t.name.len, 0, @intCast(i)) catch return; }, + // Each action by its title, its digit before it (the first nine, + // which `1`-`9` apply), the title what the query matches. + .actions => { + var lines = std.mem.splitScalar(u8, pk.rows, '\n'); + var i: u32 = 0; + while (lines.next()) |ln| { + if (ln.len == 0 or ln[0] == '@') continue; + const title = std.mem.trimStart(u8, ln[Output.location(ln).end..], " "); + var buf: [2 + 512]u8 = undefined; + const cut = title[0..@min(title.len, 512)]; + const text = if (i < 9) std.fmt.bufPrint(&buf, "{d} {s}", .{ i + 1, cut }) catch unreachable else cut; // 2 + 512 fit + pk.names.add(p.gpa, text, text.len - cut.len, cut.len, 0, i) catch return; + i += 1; + } + }, } } @@ -399,7 +424,7 @@ pub fn produceAll(p: *Pardes, tick: bool) void { for (p.picks) |slot| if (slot) |pk| if (pk.unrolled) { if (pk.names.busy()) pk.names.rescore(p.gpa); // A theme's preview in the step of the key that moved to it. - if (pk.source == .themes) pardes.colors.previewTheme(p, themeAt(p, pk)); + if (pk.source == .themes) pardes.colors.previewTheme(p, refAt(p, pk)); if (tick) preview(p, pk); }; } @@ -414,8 +439,9 @@ pub fn busy(p: *const Pardes) bool { // ---- the rows ---- -/// The theme the cursor of a themes picker is on. -fn themeAt(p: *const Pardes, pk: *const Pick) ?usize { +/// What the row under the cursor is of: a themes picker's theme, an +/// actions picker's action. +fn refAt(p: *const Pardes, pk: *const Pick) ?usize { const n = pk.names.hits.items.len; if (n == 0) return null; const at = @min(p.view().tag_state.rows.at, n - 1); @@ -457,7 +483,7 @@ const Target = union(enum) { none, place: struct { path: []const u8, at: look.Sp /// Row `i` as the band shows it and what it points at, its text in /// `arena`. -const Row = struct { text: []const u8, pre: u16 = 0, faint: u16 = 0, dim: bool = false, marks: []const u16 = &.{}, target: Target = .none }; +const Row = struct { text: []const u8, key: u8 = 0, pre: u16 = 0, faint: u16 = 0, dim: bool = false, marks: []const u16 = &.{}, target: Target = .none, meta: []const u8 = "" }; fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) !Row { const dir = pk.dir.slice(); @@ -525,7 +551,23 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) !Row { const f = owner.file orelse return row; row.target = .{ .place = .{ .path = f.path, .at = .{ .line = cand.ref } } }; }, - .themes => {}, + // The theme in use says so, whichever the window wears (POLISH 12). + .themes => if (p.custom_theme == null and cand.ref == p.settings.theme) { + row.meta = "in use"; + }, + // An action: its digit a key; its place the first line its edit + // changes, which the body is moved to while it is previewed. + .actions => { + row.key = @intFromBool(cand.ref < 9); + const owner = p.panes[p.paneBySerial(pk.landing) orelse return row].?; + // With the `Changes` layer's lines shown the body stays where it is. + const f = if (owner.file) |*file| file else return row; + if (f.layer.lines.len > 0 or f.revision != pk.revision) return row; + const edits = pardes.lsp.Ask.parseLspEdits(arena, (action(pk, cand.ref) orelse return row).records, null) orelse return row; + if (edits[0].start > f.content.len) return row; + const line = @import("Places.zig").lineOf(try File.lineIndex(p.gpa, f), edits[0].start); + row.target = .{ .place = .{ .path = f.path, .at = .{ .line = line + 1 } } }; + }, .recent, .jumplist, .list, .grep, .all => { const t = look.parsePathLine(text); row.pre = @intCast(t.end); @@ -580,10 +622,10 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { if (n == 0) try text.append(arena, '0') else try text.print(arena, "{d}/{d}", .{ s.rows.at + 1, n }); if (pk.marks.items.len > 0) try text.print(arena, " •{d}", .{pk.marks.items.len}); if (pk.names.busy() or p.stream.busyFor(pk.id)) try text.appendSlice(arena, " ⋯"); - // Themes have no acts and no other sources: no second row. - if (pk.source == .themes) { + // Themes and actions have no acts and no other sources: no second row. + if (pk.source == .themes or pk.source == .actions) { const hints = text.items.len; - try b.styled("⏎ wear it · esc", .dim); + try b.styled(if (pk.source == .themes) "⏎ wear it · esc" else "⏎ or 1-9 apply it · esc", .dim); try b.right(counted, hints); var h = b.done(); h.caret = caret; @@ -592,9 +634,9 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { try b.right(counted, text.items.len); // The second row, one row (UX D2): under the spines from the key // cells, from two cells right of the last, the acts, `|`, the other - // sources' words, each switching to it; what does not fit is dropped - // from the right (the sources first) and the row ends in a dim `…` - // (M-l and M-h reach them all). + // sources' words, each switching to it, the `|` and the sources dim + // (UX D4); what does not fit is dropped from the right (the sources + // first) and the row ends in a dim `…` (M-l and M-h reach them all). try b.row(if (b.ncols > 0) b.cols[b.ncols - 1] + 2 else 0); var offer: [acts.len + 1 + std.enums.values(Source).len][]const u8 = undefined; var nw: usize = 0; @@ -602,9 +644,10 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { offer[nw] = act.word; nw += 1; }; + const nacts = nw; offer[nw] = "|"; nw += 1; - inline for (comptime std.enums.values(Source)) |src| if (src != .list and src != .messages and src != .themes and src != pk.source) { + inline for (comptime std.enums.values(Source)) |src| if (src != .list and src != .messages and src != .themes and src != .actions and src != pk.source) { offer[nw] = words.get(src).word; nw += 1; }; @@ -617,7 +660,7 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { break; } try b.put(if (gap > 0) " " else ""); - try b.put(word); + if (i >= nacts) try b.styled(word, .dim) else try b.put(word); } var h = b.done(); h.caret = caret; @@ -670,7 +713,7 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator) ![]Band.Step { const rows = try arena.alloc(Band.Row, w.hi - w.lo); for (rows, w.lo..) |*row, i| { const r = try rowAt(p, pk, arena, i); - row.* = .{ .text = r.text, .on = i == s.rows.at, .mark = marked(pk, idOf(pk, i)) != null, .dim = r.dim, .pre = r.pre, .faint = r.faint, .marks = r.marks, .index = @intCast(i) }; + row.* = .{ .text = r.text, .key = r.key, .on = i == s.rows.at, .mark = marked(pk, idOf(pk, i)) != null, .dim = r.dim, .pre = r.pre, .faint = r.faint, .marks = r.marks, .index = @intCast(i), .meta = r.meta }; } out[0] = .{ .rows = rows }; return out; @@ -678,6 +721,47 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator) ![]Band.Step { // ---- the preview ---- +/// The line (from 0) pane `pane`'s body lands the live picker's cursor row +/// on, while it is that picker's landing pane previewing that row's file +/// (UX D7: drawn on the hit ground, body_layer.zig); null else. +pub fn landingLine(p: *Pardes, arena: std.mem.Allocator, pane: *const Pane) ?usize { + const pk = live(p) orelse return null; + // A theme or an action lands nowhere: the window wearing it, its + // edit on the Changes grounds are their previews. + if (!pk.previews or pk.landing != pane.serial or pk.source == .themes or pk.source == .actions) return null; + const s = &p.view().tag_state; + if (pk.shown != s.rows.at or count(p, pk) == 0) return null; + const row = rowAt(p, pk, arena, s.rows.at) catch return null; + const place = switch (row.target) { + .place => |t| t, + else => return null, + }; + const f = pane.file orelse return null; + if (!std.mem.eql(u8, f.path, place.path)) return null; + return @intCast(@max(0, pane.body.cur_row)); +} + +/// The notice row's sentence while a picker previews (UX D7), written to +/// `w`: where Enter lands (`here`, or the owner's reader by its address, +/// a word to Look at), that the body is the preview, and Shift-Enter's new +/// pane; the address's bytes in what was written, or null with no picker +/// previewing. +pub fn sentence(p: *const Pardes, w: *std.Io.Writer) ?[2]usize { + const pk = live(p) orelse return null; + if (!pk.previews or pk.source == .themes or pk.source == .actions) return null; + if (pk.landing == pk.owner) { + w.writeAll("⏎ lands here") catch return null; + } else { + w.writeAll("⏎ lands in ") catch return null; + } + const lo = w.end; + if (pk.landing != pk.owner) w.print("@p{d}", .{pk.owner}) catch return null; + const hi = w.end; + if (pk.landing != pk.owner) w.writeAll("'s reader") catch return null; + w.writeAll(" · its text is the preview · S-⏎ a new pane") catch return null; + return .{ lo, hi }; +} + /// The landing pane's body showing the current row's place (SPEC §5.3): /// a file at its line, the query's matches lit for a Grep. Its own text is /// set aside the first time, put back when the picker rolls up. A file an @@ -743,7 +827,7 @@ fn preview(p: *Pardes, pk: *Pick) void { pane.body.cur_col = 0; return; } - look.focusPaneLine(p, id, at, .center); + look.focusPaneLine(p, id, at, .third); // A Grep's matches in the file lit, as its own pane would have them. if (pk.source == .grep) pane.places.search(p, pane, .list, pk.id, std.mem.trim(u8, s.field.text(), " "), true, null, false); } @@ -828,6 +912,128 @@ pub fn deinitAll(p: *Pardes) void { }; } +// ---- code actions ---- + +/// Action `index` of a code actions picker's rows: its title and its edit's +/// records (the `@put` lines after its row, `@elsewhere` for an edit that +/// reaches other files, none for a command's); null past the last. +fn action(pk: *const Pick, index: usize) ?struct { title: []const u8, records: []const u8 } { + var lines = std.mem.splitScalar(u8, pk.rows, '\n'); + var n: usize = 0; + while (lines.next()) |ln| { + if (ln.len == 0 or ln[0] == '@') continue; + if (n < index) { + n += 1; + continue; + } + const from = lines.index orelse pk.rows.len; + var to = from; + while (lines.peek()) |next| { + if (next.len == 0 or next[0] != '@') break; + _ = lines.next(); + to = lines.index orelse pk.rows.len; + } + return .{ .title = std.mem.trimStart(u8, ln[Output.location(ln).end..], " "), .records = pk.rows[from..to] }; + } + return null; +} + +/// Action `index` of the live actions picker applied to the text it was +/// asked of as one undo step, the tag put to rest first (its caret back +/// where it was, then moved with the edit); one with no edit here (a +/// command's, or one reaching other files) said and not applied. +fn apply(p: *Pardes, pk: *Pick, index: usize) void { + const act = action(pk, index) orelse return; + const id = p.paneBySerial(pk.landing) orelse return TagState.toRest(p); + TagState.toRest(p); + var said: Kind.Buf(256) = .{}; + if (std.mem.eql(u8, act.records, "@elsewhere\n")) return p.reportFailure(id, said.print("Actions: {s}: its edit reaches other files, not applied", .{act.title})); + if (act.records.len == 0) return p.reportFailure(id, said.print("Actions: {s}: no edit to apply", .{act.title})); + if (!pardes.lsp.Ask.applyAction(p, p.panes[id].?, pk.revision, act.records)) return p.reportFailure(id, said.print("Actions: {s}: not applied, the text is not what it was asked of", .{act.title})); + p.setMessage(.at(id), said.print("Actions: {s}", .{act.title})); +} + +/// The live code actions picker's preview in its body (SPEC §5.1, the +/// coordinator's R3): the edit of the action under the cursor, the lines it +/// changes on the deleted ground and what they become after them as ghost +/// rows on the added ground, the rows below pushed down and the last off +/// the view. Drawn, never written; the rows shown alone. +pub fn ghosts(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: pardes.Rect, body_h: u16) !void { + const pk = live(p) orelse return; + if (pk.source != .actions or !pk.previews or pk.landing != pane.serial) return; + const f = if (pane.file) |*file| file else return; + if (f.revision != pk.revision) return; + const records = (action(pk, refAt(p, pk) orelse return) orelse return).records; + const edits = pardes.lsp.Ask.parseLspEdits(arena, records, null) orelse return; + const text = f.content; + const index = try File.lineIndex(p.gpa, f); + // The edits by the lines they touch: each run of them its old lines + // [lo, hi) and their new text, which goes before line hi. + const Group = struct { lo: usize, hi: usize, new: []const u8 }; + var groups: std.ArrayList(Group) = .empty; + var k: usize = 0; + while (k < edits.len) { + const begin = File.bufferOffset(f, edits[k].start, false); + if (begin > text.len) return; + const lo = Places.lineOf(index, begin); + var new: std.ArrayList(u8) = .empty; + try new.appendSlice(arena, text[index[lo]..begin]); + var hi = lo; + var at = begin; + while (k < edits.len) : (k += 1) { + const from = File.bufferOffset(f, edits[k].start, false); + const to = File.bufferOffset(f, edits[k].end, edits[k].end > edits[k].start); + if (from < at or to < from or to > text.len) return; + if (from > begin and Places.lineOf(index, from) >= hi) break; + try new.appendSlice(arena, text[at..from]); + try new.appendSlice(arena, edits[k].text); + at = to; + const line = Places.lineOf(index, to); + // Up to a line's start, from one or past its own text: that + // line is not changed. + hi = if (to == index[line] and (to > begin or begin == index[lo])) line else line + 1; + } + try new.appendSlice(arena, text[at..if (hi < index.len) index[hi] else text.len]); + try groups.append(arena, .{ .lo = lo, .hi = hi, .new = new.items }); + } + const tab = p.settings.tab; + const y0 = p.bodyTop(pane, r); + const rows: usize = @min(body_h, s.rows -| y0); + const first: usize = pane.context_rows; + if (rows <= first) return; + const w: usize = s.cols; + const old = try arena.dupe(Cell, s.cells[(y0 + first) * w .. (y0 + rows) * w]); + const grounds = colors.Grounds.of(p.theme()); + const ink = p.theme().fg orelse p.theme().tag_fg; + const x0 = r.x + config.GUTTER + File.gutterWidth(pane); + const width = r.w -| (x0 - r.x); + var out: usize = first; + var g: usize = 0; + var vr: usize = first; + while (vr < rows and out < rows) : (vr += 1) { + const shown = pane.wrapAt(tab, @intCast(vr)).line; + const line: usize = if (shown < 0) index.len else @intCast(shown); + if (vr == first or pane.wrapAt(tab, @intCast(vr - 1)).line != shown) { + while (g < groups.items.len and groups.items[g].hi < line) g += 1; + while (g < groups.items.len and groups.items[g].hi == line) : (g += 1) { + var news = std.mem.splitScalar(u8, std.mem.trimEnd(u8, groups.items[g].new, "\n"), '\n'); + if (groups.items[g].new.len > 0) while (news.next()) |l| { + if (out >= rows) break; + const y: u16 = @intCast(y0 + out); + s.fill(0, y, @intCast(s.cols), 1, .{}); + s.fill(x0, y, width, 1, .{ .bg = .{ .rgb = grounds.add.row } }); + _ = s.print(x0, y, width, l, .{ .fg = .{ .rgb = ink }, .bg = .{ .rgb = grounds.add.row } }); + out += 1; + }; + } + } + if (out >= rows) break; + @memcpy(s.cells[(y0 + out) * w ..][0..w], old[(vr - first) * w ..][0..w]); + if (g < groups.items.len and line >= groups.items[g].lo and line < groups.items[g].hi) s.tint(x0, @intCast(y0 + out), width, grounds.del.row, null); + out += 1; + } +} + // ---- keys ---- /// A key in the live picker (keys.zig tagStateKey, SPEC §5.4). Esc is @@ -845,15 +1051,22 @@ pub fn key(p: *Pardes, k: Key) void { return; } if (pk.spine == .acts) return actKey(p, pk, k); - // Themes: Enter wears the one under the cursor; the rows and the field. - if (pk.source == .themes) { - if (plain and k.cp == Key.enter) { - const at = themeAt(p, pk) orelse return; + // Themes: Enter wears the one under the cursor; actions: Enter, or an + // action's digit with the field empty, applies it; the rows and the + // field. + if (pk.source == .themes or pk.source == .actions) { + if (plain and k.cp == Key.enter and pk.source == .themes) { + const at = refAt(p, pk) orelse return; TagState.toRest(p); pardes.colors.setThemeIndex(p, at); return; } - if (s.rows.key(k, n)) return; + if (plain and k.cp == Key.enter) return apply(p, pk, refAt(p, pk) orelse return); + if (pk.source == .actions and plain and s.field.len == 0 and k.cp >= '1' and k.cp <= '9') return apply(p, pk, k.cp - '1'); + if (s.rows.key(k, n)) { + pk.names.reachTo(@as(usize, s.rows.at) + Band.Cursor.shown); + return; + } if (s.field.key(k)) filter(p, pk); return; } @@ -895,7 +1108,11 @@ pub fn key(p: *Pardes, k: Key) void { return; } if (plain and k.cp == Key.backspace and s.field.len == 0) return back(p, pk); - if (s.rows.key(k, n)) return; + // A cursor gone past the rows sorted sorts as far (Names.reach). + if (s.rows.key(k, n)) { + if (fuzzy(pk)) pk.names.reachTo(@as(usize, s.rows.at) + Band.Cursor.shown); + return; + } if (s.field.key(k)) filter(p, pk); } @@ -1051,6 +1268,7 @@ fn keep(p: *Pardes, pk: *Pick) void { out.writer.print("{s} ", .{pk.word.slice()}) catch return; for (pk.refined[0..pk.nrefined]) |r| out.writer.print("{s} | ", .{r.query.slice()}) catch return; out.writer.print("{s}\n", .{s.field.text()}) catch return; + if (fuzzy(pk)) pk.names.reachTo(Query.max_rows); var kept: usize = 0; for (0..count(p, pk)) |i| { if (kept == Query.max_rows) break; @@ -1994,3 +2212,186 @@ test "n and N walk the newest list or search word, or the one Looked at (SPEC § p.update(.{ .key = .{ .cp = 'n', .text = "n" } }); try std.testing.expect(pane.places.current != null and !std.meta.eql(was, pane.places.current)); } + +test "code actions: a picker in the asking pane's tag, the one under the cursor its edit in the body (the lines it changes on the deleted ground, what they become after them), Enter or its digit applies it as one undo step, a command's said" { + const Key_ = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + p.settings.jumps = .walk; + const pane = try p.setTestFile("one\ntwo add\nthree\n"); + const id = p.view().active; + // `add` is bytes 8..11. + const rows = "x:1:1 extract\nx:1:1 add a line\n@put 0 0 //%20top%0A\nx:1:1 rename add to sum\n@put 8 11 sum\n"; + open(p, id, .actions, "", "", rows); + live(p).?.revision = pane.file.?.revision; + const s = &p.view().tag_state; + try std.testing.expectEqual(Source.actions, live(p).?.source); + try std.testing.expectEqual(pane.serial, s.host); + try std.testing.expectEqual(@as(usize, 3), count(p, live(p).?)); + // The rename: `two add` on the deleted ground, `two sum` after it. + p.update(.{ .key = .{ .cp = Key_.down } }); + p.update(.{ .key = .{ .cp = Key_.down } }); + settle(p); + p.sync(); + var shot = try p.render(frame.allocator()); + const r = p.view().rects[id]; + const x = r.x + config.GUTTER + File.gutterWidth(pane); + const y = p.bodyTop(pane, r); + const grounds = colors.Grounds.of(p.theme()); + for ([_][]const u8{ "o", "t", "t", "t" }, [_][]const u8{ "n", "w", "w", "h" }, 0..) |c0, c1, row| { + try std.testing.expectEqualStrings(c0, shot.at(x, y + @as(u16, @intCast(row))).grapheme()); + try std.testing.expectEqualStrings(c1, shot.at(x + 1, y + @as(u16, @intCast(row))).grapheme()); + } + try std.testing.expectEqualStrings("a", shot.at(x + 4, y + 1).grapheme()); + try std.testing.expectEqualStrings("s", shot.at(x + 4, y + 2).grapheme()); + try std.testing.expectEqual(pardes.Color{ .rgb = grounds.del.row }, shot.at(x, y + 1).style.bg); + try std.testing.expectEqual(pardes.Color{ .rgb = grounds.add.row }, shot.at(x, y + 2).style.bg); + try std.testing.expectEqualStrings("one\ntwo add\nthree\n", pane.file.?.content); + // An action lands nowhere: no landing line, no `▸` before the line it + // changes, no sentence of where Enter lands (UX D7's are a jump's; an + // action's preview is its edit on the Changes grounds). + try std.testing.expect(landingLine(p, frame.allocator(), pane) == null); + try std.testing.expect(!std.mem.eql(u8, "▸", shot.at(x - 1, y + 1).grapheme())); + var notice: [pardes.Messages.notice_row_cap]u8 = undefined; + try std.testing.expect(!pardes.Messages.noticeRow(p, 100, ¬ice).quiet_left); + // Up: the line put in at the top, before line 1, nothing deleted. + p.update(.{ .key = .{ .cp = Key_.up } }); + settle(p); + p.sync(); + frame.deinit(); + frame = std.heap.ArenaAllocator.init(std.testing.allocator); + shot = try p.render(frame.allocator()); + try std.testing.expectEqualStrings("/", shot.at(x, y).grapheme()); + try std.testing.expectEqual(pardes.Color{ .rgb = grounds.add.row }, shot.at(x, y).style.bg); + try std.testing.expectEqualStrings("o", shot.at(x, y + 1).grapheme()); + try std.testing.expect(!std.meta.eql(pardes.Color{ .rgb = grounds.del.row }, shot.at(x, y + 1).style.bg)); + try std.testing.expect(!std.meta.eql(pardes.Color{ .rgb = p.chromeTheme().roles.hit }, shot.at(x, y + 1).style.bg)); + // `3`: the rename applied, the tag at rest; one undo takes it back. + p.update(.{ .key = .{ .cp = '3', .text = "3" } }); + try std.testing.expectEqual(TagState.Kind.rest, s.kind); + try std.testing.expectEqualStrings("one\ntwo sum\nthree\n", pane.file.?.content); + p.update(.{ .key = .{ .cp = 'u', .text = "u" } }); + try std.testing.expectEqualStrings("one\ntwo add\nthree\n", pane.file.?.content); + // Enter on a command's: said, nothing changed. + open(p, id, .actions, "", "", rows); + live(p).?.revision = pane.file.?.revision; + p.update(.{ .key = .{ .cp = Key_.enter } }); + try std.testing.expectEqual(TagState.Kind.rest, s.kind); + try std.testing.expectEqualStrings("one\ntwo add\nthree\n", pane.file.?.content); + try std.testing.expectEqualStrings("Actions: extract: no edit to apply", p.messages.saidBy(pane)); +} + +test "the themes picker marks the theme in use: `in use` at its row's right end in the dim ink, the cursor on it when it opens, the mark staying on it while the window wears another (POLISH 12, UX A15)" { + const Key_ = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile("one\n"); + const id = p.view().active; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const was = p.settings.theme; + for (" tt") |c| p.update(.{ .key = .{ .cp = c } }); + const s = &p.view().tag_state; + try std.testing.expectEqual(@as(u16, @intCast(was)), s.rows.at); + // The band's spines hang from the head's first `cells` spans: its key + // cells (`␣ t`), never its dim hint. + const h = (try head(p, a, pane)).?; + try std.testing.expectEqual(@as(u8, 2), h.cells); + for (h.spans[0..h.cells]) |span| try std.testing.expectEqual(TagState.Style.key, span.style); + const Find = struct { + fn row(band: Band, text: []const u8) ?Band.Row { + for (band.steps[0..band.nsteps]) |step| for (step.rows) |r| if (std.mem.eql(u8, r.text, text)) return r; + return null; + } + }; + var band = (try Band.of(p, a, pane, id)).?; + const worn = Find.row(band, pardes.colors.themes[was].name).?; + try std.testing.expectEqualStrings("in use", worn.meta); + try std.testing.expect(worn.on); + // One row says it. + var said: usize = 0; + for (band.steps[0..band.nsteps]) |step| for (step.rows) |r| { + said += @intFromBool(r.meta.len > 0); + }; + try std.testing.expectEqual(@as(usize, 1), said); + // Drawn at the row's right end, in the dim ink of the selection it is on. + _ = try p.render(a); + const layer = &p.surface.tag_layers[pardes.BAND_LAYER]; + const roles = &p.chromeTheme().roles; + const on = roles.selectedOn(if (pardes.Trail.reading(p, pane)) .reading_focused else .focused); + var found = false; + for (0..layer.rows) |row| { + const cells = layer.cells[row * layer.cols ..][0..layer.cols]; + var at: usize = 0; + while (at + 6 <= cells.len) : (at += 1) { + if (!std.mem.eql(u8, cells[at].grapheme(), "i") or !std.mem.eql(u8, cells[at + 1].grapheme(), "n") or !std.mem.eql(u8, cells[at + 3].grapheme(), "u")) continue; + try std.testing.expectEqual(pardes.Color{ .rgb = on.dim }, cells[at].style.fg); + found = true; + } + } + try std.testing.expect(found); + // The window wearing the next: the mark stays on the theme in use. + p.update(.{ .key = .{ .cp = if (was + 1 < pardes.colors.themes.len) Key_.down else Key_.up } }); + band = (try Band.of(p, a, pane, id)).?; + try std.testing.expectEqualStrings("in use", Find.row(band, pardes.colors.themes[was].name).?.meta); +} + +test "a picker's preview is legible as one (UX D7): its landing line on the hit ground with `▸` in the gutter, a third of the body above it, and the notice row saying where Enter lands" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(std.testing.allocator); + for (1..81) |i| try text.print(std.testing.allocator, "line {d} of the file\n", .{i}); + const path = try testFile(p, &tmp, "long.zig", text.items); + defer p.gpa.free(path); + _ = try p.setTestFile("held\n"); + const asker = p.view().active; + Trail.keep(p.panes[asker].?); + const rows = try std.fmt.allocPrint(std.testing.allocator, "{s}:40:6 line 40 of the file\n", .{path}); + defer std.testing.allocator.free(rows); + open(p, asker, .list, "Refs", "", rows); + const pk = live(p).?; + const reader = p.paneBySerial(pk.landing).?; + p.update(.tick); + const pane = p.panes[reader].?; + try std.testing.expectEqualStrings(path, pane.file.?.path); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const s = try p.render(frame.allocator()); + const r = p.view().rects[reader]; + const body_y = p.bodyTop(pane, r); + // Line 40 a third of the way down: ⌈rows/3⌉ rows of context above it. + var at: ?u16 = null; + for (0..pane.rows) |row| if (pane.wrapAt(p.settings.tab, @intCast(row)).line == 39) { + at = @intCast(row); + break; + }; + try std.testing.expectEqual(@as(?u16, @intCast((pane.rows + 2) / 3)), at); + const y = body_y + at.?; + const roles = &p.chromeTheme().roles; + const tx = r.x + pardes.config.GUTTER; + const gutter = File.gutterWidth(pane); + // Its text on the hit ground across the row; the row above as it was. + for (gutter..r.w - pardes.config.GUTTER) |x| try std.testing.expectEqual(pardes.Color{ .rgb = roles.hit }, s.at(tx + @as(u16, @intCast(x)), y).style.bg); + try std.testing.expect(!std.meta.eql(s.at(tx + gutter + 2, y - 1).style.bg, pardes.Color{ .rgb = roles.hit })); + // `▸` in the key ink in the gutter's cell after the number. + const mark = s.at(tx + gutter - 1, y); + try std.testing.expectEqualStrings("▸", mark.grapheme()); + try std.testing.expectEqual(pardes.Color{ .rgb = roles.key }, mark.style.fg); + // The notice row: where Enter lands, the owner's address a word in it. + var buf: [pardes.Messages.notice_row_cap]u8 = undefined; + const said = pardes.Messages.noticeRow(p, 120, &buf); + const want = try std.fmt.allocPrint(frame.allocator(), "⏎ lands in @p{d}'s reader · its text is the preview · S-⏎ a new pane", .{p.panes[asker].?.serial}); + try std.testing.expect(std.mem.startsWith(u8, said.text, want)); + try std.testing.expect(said.quiet_left); + try std.testing.expectEqualStrings(try std.fmt.allocPrint(frame.allocator(), "@p{d}", .{p.panes[asker].?.serial}), said.text[said.word[0]..said.word[1]]); + // At rest: no ground, no mark, the row back to its own. + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + try std.testing.expect(!pardes.Messages.noticeRow(p, 120, &buf).quiet_left); +} |
