diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Core.zig | 9 | ||||
| -rw-r--r-- | core/Messages.zig | 98 | ||||
| -rw-r--r-- | core/Pick.zig | 127 | ||||
| -rw-r--r-- | core/Prompt.zig | 84 | ||||
| -rw-r--r-- | core/body_layer.zig | 113 | ||||
| -rw-r--r-- | core/builtins.zig | 5 | ||||
| -rw-r--r-- | core/colors.zig | 54 | ||||
| -rw-r--r-- | core/mouse.zig | 6 | ||||
| -rw-r--r-- | core/normal.zig | 11 |
9 files changed, 456 insertions, 51 deletions
diff --git a/core/Core.zig b/core/Core.zig index cd2254c2..8c946787 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -1795,9 +1795,16 @@ fn updateInner(p: *Pardes, ev: Event) void { p.can_ask = true; defer p.can_ask = false; p.typedInto(); + // A detail unrolled on request rolls up at the next motion (UX D13). + const was = Messages.unrolledAt(p); @import("keys.zig").handleKey(p, key); + Messages.rollUp(p, was); + }, + .mouse => |m| { + const was = Messages.unrolledAt(p); + mouse.handleMouse(p, m); + Messages.rollUp(p, was); }, - .mouse => |m| mouse.handleMouse(p, m), .paste => |bytes| { p.typedInto(); edit.applyPaste(p, bytes) catch |err| p.reportError(.at(v.active), "paste", err); diff --git a/core/Messages.zig b/core/Messages.zig index b89b9676..72d5193d 100644 --- a/core/Messages.zig +++ b/core/Messages.zig @@ -129,15 +129,17 @@ pub const Said = struct { text: Buf(256) = .{}, places: [max_places]Place = undefined, places_len: u8 = 0, - /// The lines that explain it, shown under its first place's line while - /// the caret is on it. + /// The lines that explain it, shown under the line it names when asked + /// (`open`). detail: [max_detail]Buf(128) = undefined, detail_len: u8 = 0, /// The serial of the pane it is about, 0 for none in particular. pane: u32 = 0, asks: Ask = .none, - /// Its detail shown under its first place's line whatever the caret - /// (a question's Diff). + /// Its detail unrolled, on request only (UX D13): K, SPC e or a Look on + /// its words on the caret's line (detailAt), shown under that line until + /// the caret leaves it (rollUp); a question's Diff under its place + /// whatever the caret, until `d` again. open: bool = false, /// How many times in a row it was said. n: u16 = 1, @@ -520,8 +522,9 @@ pub fn answer(p: *Pardes, i: usize, key: u8) bool { return true; } -/// SPC e (the Message word, SPEC §10.4), on the message the notice row -/// leads with: an error that names places lands them as a list in pane +/// SPC e (the Message word, SPEC §10.4): on a line a message names, its +/// detail not unrolled, that detail (UX D13); else on the message the +/// notice row leads with: an error that names places lands them as a list in pane /// `id`'s walk (Pick's `list`, its word `✗ <cause>` in the trail), the /// first landed on, n and N walking the rest, and the error goes (a /// +Search under the pane where jumps do not walk, or for a 9P script: @@ -529,6 +532,13 @@ pub fn answer(p: *Pardes, i: usize, key: u8) bool { /// else, or nothing, opens the history. pub fn act(p: *Pardes, id: usize) void { const m = &p.messages; + // On a line a message names, its detail not unrolled: unrolled (UX + // D13), the message kept; asked again, the walk below. + if (id < MAX_PANES) if (p.panes[id]) |pane| if (pane.focus == .body) if (detailAt(p, pane, pane.body.cur_row)) |s| if (!s.open) { + s.open = true; + p.needs_frame = true; + return; + }; const lead = m.headline() orelse return history(p, id); if (lead.kind == .question) { p.view().answering = lead.pane; @@ -975,29 +985,83 @@ pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect) void { } } -/// A message's detail under the caret's line (SPEC §10.2), as a body of -/// `body_h` rows shows it: the row of that line's last row, how many of the -/// detail's lines fit under it, the message (the first naming the line, as -/// the words after it are); only in the pane with the keyboard, in its body. -/// Bounded by the live messages' places and the rows shown. +/// A message's detail unrolled (SPEC §10.2, UX D13: on request only), as a +/// body of `body_h` rows shows it: the row of its line's last row, how many +/// of the detail's lines fit under it, the message. The line is the +/// caret's, where it was asked (the caret leaving it rolls it up), or a +/// question's place; its message the first naming it, as the words after +/// it are (firstAt). The caret passing a line never unrolls one. Only in +/// the pane with the keyboard, in its body. Bounded by the live messages' +/// places and the rows shown. pub fn detailUnder(p: *const Pardes, pane: *Pane, active: bool, body_h: u16) ?struct { row: u16, rows: u16, said: *const Said } { if (!active or pane.focus != .body or p.messages.live_len == 0) return null; const f = pane.file orelse return null; - const caret = @max(0, pane.body.cur_row); - const said = found: for (p.messages.live[0..p.messages.live_len]) |*s| { - for (s.places[0..s.places_len]) |*place| if (place.line == caret + 1 and std.mem.eql(u8, place.path.slice(), f.path)) break :found s; - } else return null; + const m = &p.messages; + const caret: u32 = @intCast(@max(0, pane.body.cur_row)); + const said, const line = found: { + if (m.firstAt(f.path, caret + 1)) |i| if (m.live[i].open) break :found .{ &m.live[i], @as(i32, @intCast(caret)) }; + for (m.live[0..m.live_len], 0..) |*s, i| if (s.open and s.kind == .question and s.places_len > 0 and std.mem.eql(u8, s.places[0].path.slice(), f.path)) + if (m.firstAt(f.path, s.places[0].line) == i) break :found .{ s, @as(i32, @intCast(s.places[0].line -| 1)) }; + return null; + }; if (said.detail_len == 0) return null; var row: u16 = 0; while (row < body_h) : (row += 1) { - if (pane.wrapAt(p.settings.tab, row).line != caret) continue; - if (row + 1 < body_h and pane.wrapAt(p.settings.tab, row + 1).line == caret) continue; + if (pane.wrapAt(p.settings.tab, row).line != line) continue; + if (row + 1 < body_h and pane.wrapAt(p.settings.tab, row + 1).line == line) continue; const k: u16 = @intCast(@min(said.detail_len, body_h - row - 1)); return if (k == 0) null else .{ .row = row, .rows = k, .said = said }; } return null; } +/// The first live message naming line `line` (1-based) of `path`: the one +/// whose words are drawn after that line (body_layer messageGhosts). +fn firstAt(m: *const Messages, path: []const u8, line: u32) ?usize { + for (m.live[0..m.live_len], 0..) |*s, i| for (s.places[0..s.places_len]) |*place| { + if (line > 0 and place.line == line and std.mem.eql(u8, place.path.slice(), path)) return i; + }; + return null; +} + +/// The message whose words line `row` (0-based) of `pane`'s file shows, +/// when it has a detail to unroll (UX D13): what K with one selection, SPC +/// e and a Look on those words ask for. Not a question's (its Diff is `d`). +pub fn detailAt(p: *Pardes, pane: *const Pane, row: i32) ?*Said { + const f = pane.file orelse return null; + if (row < 0) return null; + const s = &p.messages.live[p.messages.firstAt(f.path, @intCast(row + 1)) orelse return null]; + if (s.detail_len == 0 or s.kind == .question) return null; + return s; +} + +/// The keyboard's pane and its caret's line while a detail is unrolled on +/// request; null with none (nothing to roll up: nothing else read). +pub const Caret = struct { active: usize, row: i32 }; + +pub fn unrolledAt(p: *const Pardes) ?Caret { + const v = p.view(); + for (p.messages.live[0..p.messages.live_len]) |*s| if (s.open and s.kind != .question) { + const pane = p.panes[v.active] orelse return .{ .active = v.active, .row = -1 }; + return .{ .active = v.active, .row = pane.body.cur_row }; + }; + return null; +} + +/// After a key or a click (Core.update): the caret left its line, or the +/// keyboard went to another pane, since `was`: every detail unrolled on +/// request rolls up (UX D13: the next motion; a motion along the line, or +/// a click on the detail, which puts the caret on the line it explains, +/// keeps it), a question's Diff stays. +pub fn rollUp(p: *Pardes, was: ?Caret) void { + const before = was orelse return; + const now = unrolledAt(p) orelse return; + if (std.meta.eql(before, now)) return; + for (p.messages.live[0..p.messages.live_len]) |*s| if (s.kind != .question) { + s.open = false; + }; +} + /// The bands a pane shows over `r` below its `tag_rows`: the live view's /// each step, and any view's for its kind's facts (Kind.viewOf), made from /// what a dump keeps (the bands are derived). diff --git a/core/Pick.zig b/core/Pick.zig index fe7454b3..10c80dfc 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -564,11 +564,19 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) !Row { // changes, which the body is moved to while it is previewed. .actions => { row.key = @intFromBool(cand.ref < 9); + const act = action(pk, cand.ref) orelse return row; + // One Enter cannot apply (apply's refusals): dim, saying why + // (UX E5 12). + if (act.records.len == 0 or std.mem.eql(u8, act.records, "@elsewhere\n")) { + row.dim = true; + row.meta = if (act.records.len == 0) "command" else "other files"; + return row; + } 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; + const edits = pardes.lsp.Ask.parseLspEdits(arena, act.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 } } }; @@ -717,6 +725,30 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator) ![]Band.Step { const r = try rowAt(p, pk, arena, 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 }; } + // A place's text (a Grep's line, a list's): its `path:line` in the dim + // ink and the text after it in one column, the widest place shown + // before it (UX E7 2b: the old +Search's column). Bounded by the rows + // shown. + if (pk.source == .grep or pk.source == .all or pk.source == .list) { + var widest: usize = 0; + for (rows) |row| if (row.pre > 0 and row.pre < row.text.len) { + widest = @max(widest, File.displayWidth(p.settings.tab, row.text[0 .. std.mem.indexOfScalarPos(u8, row.text, row.pre, ' ') orelse row.text.len])); + }; + for (rows) |*row| if (row.pre > 0 and row.pre < row.text.len) { + // The place up to its blank: a column (`:3:1`) and the `:` a + // compiler's `path:line:col: words` puts after it are its. + const end = std.mem.indexOfScalarPos(u8, row.text, row.pre, ' ') orelse row.text.len; + row.quiet = .{ 0, @intCast(end) }; + const pad = widest - File.displayWidth(p.settings.tab, row.text[0..end]); + if (pad == 0) continue; + const blanks = try arena.alloc(u8, pad); + @memset(blanks, ' '); + row.text = try std.mem.concat(arena, u8, &.{ row.text[0..end], blanks, row.text[end..] }); + const marks = try arena.alloc(u16, row.marks.len); + for (marks, row.marks) |*m, at| m.* = if (at >= end) at + @as(u16, @intCast(pad)) else at; + row.marks = marks; + }; + } out[0] = .{ .rows = rows }; return out; } @@ -2429,3 +2461,96 @@ test "a picker's preview is legible as one (UX D7): its landing line on the hit p.update(.{ .key = .{ .cp = pardes.Key.escape } }); try std.testing.expect(!pardes.Messages.noticeRow(p, 120, &buf).quiet_left); } + +test "a list of places (a Grep's, a list's) shows each row's `path:line` in the dim ink and its text after it in one column, the widest place shown before it (UX E7 2b)" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 40 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const a_path = try testFile(p, &tmp, "a.zig", "one\ntwo\nconst x = 1;\n"); + defer p.gpa.free(a_path); + const b_path = try testFile(p, &tmp, "a_longer_name.zig", "fn y() void {}\n"); + defer p.gpa.free(b_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}:3:1 const x = 1;\n{s}:1:4 fn y() void {{}}\n", .{ a_path, b_path }); + defer std.testing.allocator.free(rows); + open(p, asker, .list, "Refs", "", rows); + p.update(.tick); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const got = (try steps(p, frame.allocator()))[0].rows; + try std.testing.expectEqual(@as(usize, 2), got.len); + const code = [2][]const u8{ "const x = 1;", "fn y() void {}" }; + var col: [2]usize = undefined; + for (got, code, &col) |row, text, *c| { + const at = std.mem.indexOf(u8, row.text, text).?; + c.* = File.displayWidth(8, row.text[0..at]); + // The place dim up to its blank (`path:3:1`); the text after it not. + try std.testing.expect(row.pre > 0); + try std.testing.expectEqual(@as(u16, 0), row.quiet[0]); + try std.testing.expectEqual(':', row.text[row.quiet[1] - 2]); + try std.testing.expectEqual(' ', row.text[row.quiet[1]]); + } + try std.testing.expectEqual(col[0], col[1]); + // One cell after the widest place. + try std.testing.expectEqual(File.displayWidth(8, b_path) + ":1:4 ".len, col[1]); + // As the band draws them, its last step the band's width (UX E3): the + // text still in one column. + const host = TagState.hostOf(p).?; + const band = (try Band.of(p, frame.allocator(), p.panes[host].?, host)).?; + const last = band.steps[band.nsteps - 1]; + try std.testing.expectEqual(band.columns, last.x + last.width); + for (code, &col, 0..) |text, *c, y| { + const ln = try band.line(frame.allocator(), p.settings.tab, @intCast(y)); + c.* = File.displayWidth(p.settings.tab, ln.text[0..std.mem.indexOf(u8, ln.text, text).?]); + } + try std.testing.expectEqual(col[0], col[1]); +} + +test "code actions: a row that cannot be applied (a command's, an edit reaching other files) is dim, its meta `command` or `other files`; the edit under the cursor a third of the way down the body (UX E5 12, D7)" { + const Key_ = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + p.settings.jumps = .walk; + var text: std.ArrayList(u8) = .empty; + defer text.deinit(std.testing.allocator); + for (1..81) |i| try text.print(std.testing.allocator, "line {d}\n", .{i}); + const pane = try p.setTestFile(text.items); + const id = p.view().active; + // Line 50 starts after lines 1-49. + var at: usize = 0; + for (1..50) |i| at += std.fmt.count("line {d}\n", .{i}); + const rows = try std.fmt.allocPrint(frame.allocator(), "x:1:1 extract\nx:1:1 move to a new file\n@elsewhere\nx:1:1 rename\n@put {d} {d} LINE\n", .{ at, at + 4 }); + open(p, id, .actions, "", "", rows); + live(p).?.revision = pane.file.?.revision; + const got = (try steps(p, frame.allocator()))[0].rows; + try std.testing.expectEqual(@as(usize, 3), got.len); + try std.testing.expect(got[0].dim); + try std.testing.expectEqualStrings("command", got[0].meta); + try std.testing.expect(got[1].dim); + try std.testing.expectEqualStrings("other files", got[1].meta); + try std.testing.expect(!got[2].dim); + try std.testing.expectEqualStrings("", got[2].meta); + // As the band draws it, its step the band's width (UX E3): the meta + // at the band's right end. + const band = (try Band.of(p, frame.allocator(), pane, TagState.hostOf(p).?)).?; + const ln = try band.line(frame.allocator(), p.settings.tab, 0); + try std.testing.expect(std.mem.endsWith(u8, ln.text, " command")); + try std.testing.expectEqual(@as(usize, band.columns), File.displayWidth(p.settings.tab, ln.text)); + // The rename's: its line (50) a third of the way down the body. + p.update(.{ .key = .{ .cp = Key_.down } }); + p.update(.{ .key = .{ .cp = Key_.down } }); + settle(p); + p.sync(); + var row_at: ?u16 = null; + for (0..pane.rows) |row| if (pane.wrapAt(p.settings.tab, @intCast(row)).line == 49) { + row_at = @intCast(row); + break; + }; + try std.testing.expectEqual(@as(?u16, @intCast((pane.rows + 2) / 3)), row_at); +} diff --git a/core/Prompt.zig b/core/Prompt.zig index 9dfe9ec2..ad2cfbed 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -1083,7 +1083,11 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head { } } // The count and the hints at the row's right end (UX D3). + const left = b.col(); const at = text.items.len; + // Where a search's ` · N lines` starts after its count: dim, and gone + // with the hints (UX E7). + var tail: ?usize = null; if (line.len > 0) { // A pattern that is no regex: why, in the err ink, in place of the // hints (UX D14), what is shown meanwhile dim; in the count's place, @@ -1095,11 +1099,23 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head { return .{ .text = text.items, .spans = spans.items, .cells = 1, .caret = caret }; } else if (behavior(pr.op) != null) { try pipeCount(p, arena, text, spans, pane, pr, line); - } else try count(arena, text, pane, pr); + } else tail = try count(arena, text, pane, pr); } if (pr.op == .command) try Command.count(p, arena, text); const hints = text.items.len; - try styled(arena, text, spans, info.get(pr.op).hint, .dim); + const hint = info.get(pr.op).hint; + if (tail) |t| { + // Both beside the count, or neither: as much room as the right + // block asks for its two gaps (Builder.right drops the hints alone). + const cells = File.displayWidth(p.settings.tab, std.mem.trimStart(u8, text.items[at..], " ")) + File.displayWidth(p.settings.tab, hint); + if (left + 2 + cells + 2 > b.width) { + text.shrinkRetainingCapacity(t); + try b.right(at, t); + return .{ .text = text.items, .spans = spans.items, .cells = 1, .caret = caret }; + } + try spans.append(arena, .{ .lo = @intCast(t), .hi = @intCast(hints), .style = .dim }); + } + try styled(arena, text, spans, hint, .dim); try b.right(at, hints); return .{ .text = text.items, .spans = spans.items, .cells = 1, .caret = caret }; } @@ -1111,24 +1127,34 @@ fn styled(arena: std.mem.Allocator, text: *std.ArrayList(u8), spans: *std.ArrayL } /// The prompt's count after its field: what it found (`4 selections in 6 -/// lines`, `keeps 3 of 9`, `3/7 · 5 lines`, `3/⋯` while its places are -/// found, `0`). -fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *const Prompt) !void { +/// lines`, `keeps 3 of 9`, `3/7 · 5 lines`, `1/7 ↻ · 5 lines`, `3/⋯` while +/// its places are found, `0`); where a search's ` · N lines` starts. +fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *const Prompt) !?usize { switch (pr.op) { .search, .search_back => { const set = &pane.places; - if (!set.shows(pane)) return; + if (!set.shows(pane)) return null; const hits = set.hits.items; - if (set.complete() and hits.len == 0) return text.appendSlice(arena, " 0"); + if (set.complete() and hits.len == 0) { + try text.appendSlice(arena, " 0"); + return null; + } try text.appendSlice(arena, " "); const at = if (set.current) |c| (if (c < hits.len) c else null) else null; if (at) |c| try text.print(arena, "{d}", .{c + 1}) else try text.appendSlice(arena, "–"); - if (!set.complete()) return text.appendSlice(arena, "/⋯"); - try text.print(arena, "/{d} · {d} line{s}", .{ hits.len, pr.lines, plural(pr.lines) }); + if (!set.complete()) { + try text.appendSlice(arena, "/⋯"); + return null; + } + try text.print(arena, "/{d}", .{hits.len}); // Round the end from where it began. - const c = at orelse return; - const origin = set.origin(pane); - if (if (pr.op == .search) hits[c].start < origin else hits[c].start > origin) try text.appendSlice(arena, " ↻"); + if (at) |c| { + const origin = set.origin(pane); + if (if (pr.op == .search) hits[c].start < origin else hits[c].start > origin) try text.appendSlice(arena, " ↻"); + } + const tail = text.items.len; + try text.print(arena, " · {d} line{s}", .{ pr.lines, plural(pr.lines) }); + return tail; }, .select, .split => if (pr.found == 0) try text.appendSlice(arena, " 0") @@ -1137,6 +1163,7 @@ fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *c .keep, .remove => try text.print(arena, " keeps {d} of {d}", .{ pr.found, pane.nsel_snap }), else => {}, } + return null; } /// A pipe prompt's count: `6 lines → 5 · exit 0`, `6 runs · 5 ok · 1 exit 2 @@ -1668,3 +1695,36 @@ test "Alt-Enter in / or s keeps the result as a list (SPEC §7.2): where jumps w try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); try std.testing.expect(p.panes[p.view().active].?.query != null); } + +test "a search's count keeps ` · N lines` after it in the dim ink, and drops it with the hints where they do not fit beside it; a wrap's ↻ is the count's (UX E7, D3)" { + for ([_]u16{ 200, 52 }) |cols| { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = cols, .rows = 30 }); + defer p.deinit(); + const pane = try p.setTestFile(dirty); + pane.body.cur_row = 2; + pane.body.cur_col = 6; + typeKeys(p, "/dirty"); + const h = try head(p, p.scratch.allocator(), pane); + const lines = std.mem.indexOf(u8, h.text, " \u{b7} 5 lines"); + if (cols == 200) { + try std.testing.expect(has(h.text, "3/7 \u{b7} 5 lines \u{23ce} land")); + const dim = for (h.spans) |span| { + if (span.lo == lines.? and span.style == .dim) break span; + } else return error.TestUnexpectedResult; + try std.testing.expectEqualStrings(" \u{b7} 5 lines", h.text[dim.lo..dim.hi]); + } else { + // No room for the hints: the count alone, at the row's end. + try std.testing.expect(lines == null); + try std.testing.expect(std.mem.indexOf(u8, h.text, "\u{23ce} land") == null); + try std.testing.expect(std.mem.endsWith(u8, h.text, "3/7")); + } + } + // From the last line round the end: `↻` after the count, before its lines. + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 }); + defer p.deinit(); + const pane = try p.setTestFile(dirty); + pane.body.cur_row = 7; + pane.body.cur_col = 6; + typeKeys(p, "/dirty"); + try std.testing.expect(has(try tagOf(p), "1/7 \u{21bb} \u{b7} 5 lines \u{23ce} land")); +} diff --git a/core/body_layer.zig b/core/body_layer.zig index 0107b3e0..f00da4a9 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -641,6 +641,28 @@ fn messageGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: /// frame drawn last (its body layer when it has one, else the grid): a /// message's detail is text no buffer holds, read as it is shown. pub fn wordAt(p: *Pardes, arena: std.mem.Allocator, id: usize, row: usize, col: usize) !?[]const u8 { + const text = try drawnRow(p, arena, id, row) orelse return null; + const b = pardes.tagline.wordBoundsAtCol(text, panes.File.rawAtDisplay(p.settings.tab, text, col)) orelse return null; + if (b.lo == b.hi) return null; + return text[b.lo..b.hi]; +} + +/// The message whose words are drawn after its line at body row `row`, +/// text column `col` of pane `id` in the frame drawn last, when it has a +/// detail: a Look there unrolls it (UX D13). The words start at their +/// glyph, two blanks after the line (messageGhosts). +pub fn messageAt(p: *Pardes, arena: std.mem.Allocator, id: usize, row: u16, col: usize) !?*pardes.Messages.Said { + const pane = p.panes[id] orelse return null; + const said = pardes.Messages.detailAt(p, pane, pane.wrapAt(p.settings.tab, pane.textRow(row)).line) orelse return null; + const text = try drawnRow(p, arena, id, row) orelse return null; + var mark: [8]u8 = undefined; + const at = std.mem.lastIndexOf(u8, text, std.fmt.bufPrint(&mark, " {s} ", .{said.kind.glyph()}) catch return null) orelse return null; + return if (panes.File.rawAtDisplay(p.settings.tab, text, col) >= at + 2) said else null; +} + +/// Body row `row` of pane `id` as the frame drawn last shows it: its body +/// layer's when it has one, else the grid's. +fn drawnRow(p: *Pardes, arena: std.mem.Allocator, id: usize, row: usize) !?[]const u8 { const layer = &p.surface.body_layers[id]; const body = (p.surface.region(.body, id) orelse return null).rect; var text: std.ArrayList(u8) = .empty; @@ -651,9 +673,7 @@ pub fn wordAt(p: *Pardes, arena: std.mem.Allocator, id: usize, row: usize, col: if (row >= body.h) return null; for (0..body.w) |x| try text.appendSlice(arena, p.surface.at(body.x + @as(u16, @intCast(x)), body.y + @as(u16, @intCast(row))).grapheme()); } - const b = pardes.tagline.wordBoundsAtCol(text.items, panes.File.rawAtDisplay(p.settings.tab, text.items, col)) orelse return null; - if (b.lo == b.hi) return null; - return text.items[b.lo..b.hi]; + return text.items; } /// A picker's landing line (UX D7): its text on the hit ground across @@ -1141,7 +1161,7 @@ fn rowText(s: *const Surface, x: u16, y: u16, buf: []u8) []const u8 { return buf[0..n]; } -test "a build error is said where its cause is: the mark on its file's tag, its words after its line, its detail under the caret's line pushing the rows below down, the walk on the notice row" { +test "a build error is said where its cause is: the mark on its file's tag, its words after its line, its detail, asked for, under its line pushing the rows below down, the walk on the notice row" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 20 }); defer p.deinit(); const pane = try p.setTestFile("one\ntwo\nthree\nfour\nfive\nsix\n"); @@ -1172,8 +1192,10 @@ test "a build error is said where its cause is: the mark on its file's tag, its // elsewhere. try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 2, &buf), "three \u{2717} expected 2 argument(s), found 3") != null); try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 3, &buf), "four") != null); - // The caret on line 3: its detail under it, the rows below two rows down. + // The caret on line 3 and its detail asked for (K, UX D13): under the + // line, the rows below two rows down. pane.body.cur_row = 2; + err.open = true; _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 3, &buf), "const x = f(1, 2, 3);") != null); @@ -1202,6 +1224,7 @@ test "a click under a message's detail lands on the row drawn there, a click on err.detail_len = 2; pane.focus = .body; pane.body.cur_row = 2; + err.open = true; p.sync(); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); @@ -1215,9 +1238,14 @@ test "a click under a message's detail lands on the row drawn there, a click on p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = x, .row = body.y + 6 } }); p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = x, .row = body.y + 6 } }); try std.testing.expectEqual(@as(i32, 4), pane.body.cur_row); - // On line 3 again, its detail drawn: a click on the detail is on the - // line it explains, and the detail stays. - pane.body.cur_row = 2; + // On line 3 again, its detail asked for again (the click, a motion, + // rolled it up): a click on the detail is on the line it explains, and + // the detail stays. + try std.testing.expect(!err.open); + p.update(.{ .key = .{ .cp = 'k' } }); + p.update(.{ .key = .{ .cp = 'k' } }); + p.update(.{ .key = .{ .cp = 'K' } }); + try std.testing.expect(err.open); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); const wrap_at = std.mem.indexOf(u8, rowText(s, body.x, body.y + 3, &buf), "Wrap").?; @@ -1317,3 +1345,72 @@ test "a motion root's level paints its targets in place of their lines' numbers, s = try p.render(a); try std.testing.expectEqualStrings("5", Field.of(s, gutter, body.y + 4, number, &buf)); } + +test "a message's detail unrolls only on request (UX D13): the caret passing its line inserts no rows; K there unrolls it under the line and the caret leaving the line rolls it up; SPC e there unrolls it; a Look on its words after the line unrolls it; K with two selections is keep's" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 20 }); + defer p.deinit(); + const pane = try p.setTestFile("one\ntwo\nthree\nfour\nfive\nsix\n"); + const id = p.view().active; + const err = pardes.Messages.keep(p, .@"error", 0, "@p9 zig build", "exit 1"); + err.places[0] = .{ .line = 3, .col = 1 }; + err.places[0].path.set("/test.txt"); + err.places[0].text.set("expected 2 argument(s), found 3"); + err.places_len = 1; + err.detail[0] = .{}; + err.detail[0].set("const x = f(1, 2, 3);"); + err.detail_len = 1; + pane.focus = .body; + p.sync(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + var buf: [512]u8 = undefined; + // The caret onto line 3 and past it: no row inserted or removed. + p.update(.{ .key = .{ .cp = 'j' } }); + p.update(.{ .key = .{ .cp = 'j' } }); + try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row); + var s = try p.render(frame.allocator()); + const body = s.region(.body, id).?.rect; + try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 2, &buf), "three \u{2717} expected") != null); + try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 3, &buf), "four") != null); + // K there: its detail under the line, the rows below one lower; no + // keep prompt. + p.update(.{ .key = .{ .cp = 'K' } }); + try std.testing.expectEqual(pardes.TagState.Kind.rest, p.view().tag_state.kind); + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 3, &buf), "const x = f(1, 2, 3);") != null); + try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 4, &buf), "four") != null); + // The next motion rolls it up; back on the line, nothing comes back. + p.update(.{ .key = .{ .cp = 'j' } }); + p.update(.{ .key = .{ .cp = 'k' } }); + try std.testing.expect(!err.open); + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 3, &buf), "four") != null); + // SPC e there: unrolled, the error still live; K rolls it up. + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = 'e' } }); + try std.testing.expect(err.open); + try std.testing.expectEqual(@as(usize, 1), p.messages.live_len); + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + try std.testing.expect(std.mem.indexOf(u8, rowText(s, body.x, body.y + 3, &buf), "const x = f(1, 2, 3);") != null); + p.update(.{ .key = .{ .cp = 'K' } }); + try std.testing.expect(!err.open); + // A Look (B3) on its words after the line: unrolled, the caret where + // it was. + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + const words = std.mem.indexOf(u8, rowText(s, body.x, body.y + 2, &buf), "expected").?; + const wx = body.x + @as(u16, @intCast(words)) + 1; + p.update(.{ .mouse = .{ .button = .right, .kind = .press, .col = wx, .row = body.y + 2 } }); + p.update(.{ .mouse = .{ .button = .right, .kind = .release, .col = wx, .row = body.y + 2 } }); + while (p.nextEffect()) |_| {} + try std.testing.expect(err.open); + try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row); + // K with two selections is keep's prompt, the detail as it was. + p.update(.{ .key = .{ .cp = 'C' } }); + try std.testing.expect(pane.body.nsel > 0); + p.update(.{ .key = .{ .cp = 'K' } }); + try std.testing.expectEqual(pardes.TagState.Kind.prompt, p.view().tag_state.kind); +} diff --git a/core/builtins.zig b/core/builtins.zig index 33f5824f..7aaeee40 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -1727,8 +1727,9 @@ pub const LocationsConfig = struct { /// Act on the message the notice row leads with. /// -/// An error that names places lands them as a list in the pane's walk, n -/// and N walking them (where jumps do not walk, a +Search the Look walk +/// On a line a message names, its detail not shown yet: that detail, under +/// the line until the caret leaves it. Else an error that names places +/// lands them as a list in the pane's walk, n and N walking them (where jumps do not walk, a +Search the Look walk /// steps through); a question takes the keyboard in its pane's tag (its /// answer's key answers it, Esc leaves it waiting); anything else opens the /// history. diff --git a/core/colors.zig b/core/colors.zig index b2069465..4f58aed5 100644 --- a/core/colors.zig +++ b/core/colors.zig @@ -586,7 +586,8 @@ pub const ChromeTheme = struct { .search_bg = th.search_bg orelse th.sel_bg, .search_fg = th.search_fg orelse th.sel_fg, .tag_reading_bg = reading, - .diagnostic_error = th.diagnostic_error orelse (th.fg orelse th.tag_fg), + // A theme's error ink, else its palette's red (UX E3). + .diagnostic_error = th.diagnostic_error orelse if (th.palette) |pal| pal[1] else (th.fg orelse th.tag_fg), .diagnostic_warning = th.diagnostic_warning orelse (th.fg orelse th.tag_fg), .diagnostic_info = th.diagnostic_info orelse (th.fg orelse th.tag_fg), .diagnostic_hint = th.diagnostic_hint orelse (th.fg orelse th.tag_fg), @@ -643,8 +644,8 @@ pub const Roles = struct { pub const rule_floor: f32 = 1.8; pub const rule_ceiling: f32 = 2.5; pub const ink_floor: f32 = 4.5; - pub const hit_floor: f32 = 1.25; - pub const hit_current_floor: f32 = 1.6; + pub const hit_floor: f32 = 1.5; + pub const hit_current_floor: f32 = 1.9; pub const reading_distance: f32 = 0.04; pub fn on(r: *const Roles, ground: TagGround) *const Inks { @@ -692,7 +693,7 @@ pub const Roles = struct { if (contrast(r.hit_current, page) < contrast(r.hit, page)) r.hit_current = r.hit; const message_grounds = [_][3]u8{ plain, focused, reading, page }; const ink = th.fg orelse th.tag_fg; - r.err = pushAway(th.diagnostic_error orelse ink, &message_grounds, ink_floor); + r.err = pushAway(th.diagnostic_error orelse if (th.palette) |pal| pal[1] else ink, &message_grounds, ink_floor); r.warn = pushAway(th.diagnostic_warning orelse ink, &message_grounds, ink_floor); r.question = pushAway(th.diagnostic_info orelse ink, &message_grounds, ink_floor); r.hint = pushAway(th.diagnostic_hint orelse ink, &message_grounds, ink_floor); @@ -794,8 +795,10 @@ fn selectionOf(own: [3]u8, ground: [3]u8, text: [3]u8) [3]u8 { /// A ground off `page` toward `hue` at `t` (past 1, beyond the hue, as far /// as the gamut goes), held where the body's `ink` reads on it at 4.5:1 -/// (or its own on the page, where that is less); moved on toward the ink -/// until it stands off the page at `floor`, as far as the ink allows. +/// (or its own on the page, where that is less); on toward the hue, the +/// least way that stands off the page at `floor`, where the ink still +/// reads on it; else moved on toward the ink until it stands off the page +/// at `floor`, as far as the ink allows. fn hitOf(page: [3]u8, hue: [3]u8, ink: [3]u8, t: f32, floor: f32) [3]u8 { const a = toOklab(page); const b = toOklab(hue); @@ -806,6 +809,22 @@ fn hitOf(page: [3]u8, hue: [3]u8, ink: [3]u8, t: f32, floor: f32) [3]u8 { k -= t / 50; c = fromOklab(.{ a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k, a[2] + (b[2] - a[2]) * k }); } + // Found by halves, seven mixes: the nearer the hue, the further off + // the page (UX E7 7b: a landing line 1.5:1, its span 1.9:1). + if (contrast(c, page) < floor and k < 1 and contrast(hue, page) >= floor) { + var lo = k; + var hi: f32 = 1; + var far = hue; + for (0..7) |_| { + const mid = (lo + hi) / 2; + const m = fromOklab(.{ a[0] + (b[0] - a[0]) * mid, a[1] + (b[1] - a[1]) * mid, a[2] + (b[2] - a[2]) * mid }); + if (contrast(m, page) < floor) lo = mid else { + hi = mid; + far = m; + } + } + if (contrast(ink, far) >= ink_floor) c = far; + } const start = c; var step: f32 = 1; while (contrast(c, page) < floor and step <= 50) : (step += 1) { @@ -1586,7 +1605,9 @@ test "every theme's walk UI roles meet their floors on every ground they are dra for ([_][3]u8{ th.tag_fg, th.tag_active_fg orelse th.tag_fg }) |ink| try checks.append(a, .{ .what = "text on reading", .got = contrast(ink, chrome.tag_reading_bg), .floor = @min(7, contrast(ink, th.tag_bg)) - 0.02 }); const ink_floor = @min(Roles.ink_floor, contrast(body_ink, page)); - for ([_][3]u8{ r.hit, r.hit_current }, [_]f32{ Roles.hit_floor, Roles.hit_current_floor }) |ground, floor| { + // A preview's landing line 1.5:1 off the page, its matched span (the + // current hit) a step stronger, 1.9:1 (UX E7 7b: 1.29 read as faint). + for ([_][3]u8{ r.hit, r.hit_current }, [_]f32{ 1.5, 1.9 }) |ground, floor| { const on = contrast(body_ink, ground); try checks.append(a, .{ .what = "hit", .got = contrast(ground, page), .floor = if (on < ink_floor + 0.3) 0 else floor }); try checks.append(a, .{ .what = "ink on hit", .got = on, .floor = ink_floor - 0.01 }); @@ -1602,13 +1623,15 @@ test "every theme's walk UI roles meet their floors on every ground they are dra test "neonotte84's and acme's roles are the ones UX derived (DESIGN.md D4, tools/roles.py)" { const Want = struct { name: []const u8, text: ?[3]u8, dim: [3]u8, rule: [3]u8, key: [3]u8, match: [3]u8, reading: [3]u8, hit: [3]u8, hit_current: [3]u8 }; + // The hit grounds past UX's 18 % / 32 % mix, to 1.5:1 and 1.9:1 off the + // page (UX E7 7b). const wants = [_]Want{ - .{ .name = "neonotte84", .text = .{ 0xb5, 0xbf, 0xe3 }, .dim = .{ 0x70, 0x76, 0x8f }, .rule = .{ 0x4c, 0x50, 0x63 }, .key = .{ 0xf8, 0xde, 0xd9 }, .match = .{ 0xf1, 0xa2, 0x71 }, .reading = .{ 0x29, 0x27, 0x1e }, .hit = .{ 0x36, 0x28, 0x2a }, .hit_current = .{ 0x52, 0x38, 0x31 } }, + .{ .name = "neonotte84", .text = .{ 0xb5, 0xbf, 0xe3 }, .dim = .{ 0x70, 0x76, 0x8f }, .rule = .{ 0x4c, 0x50, 0x63 }, .key = .{ 0xf8, 0xde, 0xd9 }, .match = .{ 0xf1, 0xa2, 0x71 }, .reading = .{ 0x29, 0x27, 0x1e }, .hit = .{ 0x47, 0x32, 0x2e }, .hit_current = .{ 0x5f, 0x3f, 0x33 } }, // acme's key and match a little darker than UX's #5c5c8d and // #974d00 (which hold on its tag and its #9eeeee): a reading pane's // band's selection is pushed off the reading ground (#9eeeee is // 1.12:1 off #f3f0cc), and they read on that one too. - .{ .name = "acme", .text = null, .dim = .{ 0x7a, 0x86, 0x86 }, .rule = .{ 0xa8, 0xb7, 0xb7 }, .key = .{ 0x55, 0x55, 0x83 }, .match = .{ 0x8f, 0x48, 0x00 }, .reading = .{ 0xf3, 0xf0, 0xcc }, .hit = .{ 0xec, 0xd8, 0xbb }, .hit_current = .{ 0xdd, 0xbc, 0x99 } }, + .{ .name = "acme", .text = null, .dim = .{ 0x7a, 0x86, 0x86 }, .rule = .{ 0xa8, 0xb7, 0xb7 }, .key = .{ 0x55, 0x55, 0x83 }, .match = .{ 0x8f, 0x48, 0x00 }, .reading = .{ 0xf3, 0xf0, 0xcc }, .hit = .{ 0xe6, 0xcd, 0xae }, .hit_current = .{ 0xd9, 0xb4, 0x90 } }, }; for (wants) |want| { const chrome = ChromeTheme.fromTheme(&themes[themeIndex(want.name).?]); @@ -1621,3 +1644,16 @@ test "neonotte84's and acme's roles are the ones UX derived (DESIGN.md D4, tools }; } } + +test "the err role falls back from diagnostic.error's ink to error's, then to the theme's red (UX E3): neonotte84's (its diagnostic.error an underline only) is its red, acme's its palette's" { + const neonotte = &themes[themeIndex("neonotte84").?]; + // neonotte.toml: "error" = { fg = "red" }, its palette's red #ff7ea2. + try std.testing.expectEqual([3]u8{ 0xff, 0x7e, 0xa2 }, neonotte.diagnostic_error.?); + for ([_][]const u8{ "neonotte84", "acme" }) |name| { + const chrome = ChromeTheme.fromTheme(&themes[themeIndex(name).?]); + const err = chrome.roles.err; + // Red: its red channel well over the others, on the page and the tags. + try std.testing.expect(err[0] > err[1] + 40 and err[0] > err[2] + 40); + } + try std.testing.expectEqual([3]u8{ 0xaa, 0x22, 0x22 }, ChromeTheme.fromTheme(&themes[themeIndex("acme").?]).diagnostic_error); +} diff --git a/core/mouse.zig b/core/mouse.zig index 6516ea71..72f599dd 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -884,6 +884,12 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { const word = (body_layer.wordAt(p, arena, id, row, @intCast(@max(0, pointed.sel.c0))) catch return) orelse return; return exec.runBuiltin(p, if (m.button == config.exec_button) config.exec_cmd else config.look_cmd, id, "", arena.dupe(u8, word) catch return); } + // A Look on a message's words after its line: its + // detail unrolled, or rolled up (UX D13). + if (m.button == config.look_button and !pointed.on_tag and mrow >= body_y) if (body_layer.messageAt(p, p.scratch.allocator(), id, mrow - body_y, @intCast(@max(0, pointed.sel.c0))) catch null) |said| { + said.open = !said.open; + return; + }; pane.clearPointerSelection(@backingInt(m.button)); pane.sel[@backingInt(m.button)] = pointed.sel; v.drag = .{ diff --git a/core/normal.zig b/core/normal.zig index 3aad765d..e6d6dbb0 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -1369,7 +1369,16 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! .page_up => return t.scrollViewMove(p.settings.tab, text, range, -@as(i32, pane.rows)), // s, S and | answer for the text they are pressed in; `/` searches // the body wherever it is pressed, as acme's Look from a tag does. - inline .select_regex, .split_regex, .keep_regex, .remove_regex => |role| return startSelRegex(p, pane, t, @field(modal.Normal.RegexMode, @tagName(role)[0 .. @tagName(role).len - "_regex".len])), + // K with one selection on a line a message with a detail names: + // that detail unrolled, or rolled up (UX D13); else keep's prompt. + .keep_regex => { + if (t == &pane.body and t.nsel == 0) if (pardes.Messages.detailAt(p, pane, t.cur_row)) |said| { + said.open = !said.open; + return; + }; + return startSelRegex(p, pane, t, .keep); + }, + inline .select_regex, .split_regex, .remove_regex => |role| return startSelRegex(p, pane, t, @field(modal.Normal.RegexMode, @tagName(role)[0 .. @tagName(role).len - "_regex".len])), // Language shortcuts run the same builtins as tags and leader commands. .goto_definition => return exec.runBuiltin(p, .Def, v.active, "", null), .goto_references => return exec.runBuiltin(p, .Refs, v.active, "", null), |
