diff options
Diffstat (limited to 'core/body_layer.zig')
| -rw-r--r-- | core/body_layer.zig | 206 |
1 files changed, 204 insertions, 2 deletions
diff --git a/core/body_layer.zig b/core/body_layer.zig index 3736e467..a182f218 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -350,6 +350,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane panes.Tables.paint(p, s, pane, r, tx, tw, body_h); drawWrapMarkers(p, s, pane, r, tx, tw, body_h, pane_bg); pane.places.paint(p, s, pane, r, body_h); + pardes.Prompt.paintGutter(p, s, pane, r, body_h); panes.Query.paint(p, s, pane, r, body_h); } else if (pane.isTerminal()) { ansi.recolor(p, s, pane, r, tx, tw, body_h, body); @@ -382,7 +383,10 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane if (b == pardes.sel_slot and @min(sl.r0, sl.r1) < pane.tag_rows) continue; paintPointerSelection(p, s, pane, r, sl, pane.tag_rows, sel_btn[b], buttonInks(th)[b]); } - const preview = normal.selRegexArmed(pane) != null; + // A selecting prompt's live result (Prompt.zig), dimmed while its + // field is no regex. + const selecting = pardes.Prompt.selecting(p, pane); + const preview = normal.selRegexArmed(pane) != null or selecting != null; const show_prim = (pane.body.mode == .normal and pane.body.vsel.active) or preview or (modal_hover and pane.body.vsel.active); const show_extra = pane.body.mode != .tty and pane.body.nsel > 0; @@ -400,7 +404,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane } else pane.body.sels[si]; const bnd = edit.cellBounds(sr); const hover_only = primary and modal_hover and pane.body.mode != .normal and !preview; - const bg = if (hover_only) + const bg = if (hover_only or (selecting != null and selecting.?.dim)) colors.mix(page_rgb, colors.mix(page_rgb, th.sel_bg)) else if (primary) th.sel_bg @@ -471,7 +475,12 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane } } + // What the body would do, drawn and never written (ruling R3): a motion + // root's targets in the gutter first, so the ghost rows below push them + // with their lines; then a message's words and detail (Messages), a + // hover's lines (SPEC §3.5), a rename's preview. if (pane.file != null) paintTargets(p, s, arena, pane, r, tx, tw, body_y, body_h); + if (pane.file != null and p.messages.live_len > 0) messageGhosts(p, s, pane, tx, tw, body_y, body_h); if (p.lsp_ask.hover != null) hoverGhosts(p, s, pane, tx, tw, body_y, body_h); if (p.view().tag_state.kind == .rename) renameGhosts(p, s, pane, tx, tw, body_y, body_h); @@ -545,6 +554,9 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane // A replica's predicted echo, over what its authority's screen shows. if (p.echo) |e| if (pane.isTerminal()) e.paint(p, s, pane, id, tx, tw, body_y, body_h, active); + // A pipe prompt's live run, as ghost lines over what is painted + // (Prompt.zig). + if (pane.file != null) try pardes.Prompt.ghosts(p, s, arena, pane, r, body_h); if (rail) |bar| if (bar.rect.h > 0) { // The rail covers the notice bands too: nothing else paints those // two gutter columns, so a gap there keeps the frame-wide border @@ -555,6 +567,89 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane }; } +/// A message at the lines it names, drawn and never written (SPEC §10.2, +/// ruling R3): after the end of each such line shown, the message's glyph +/// and its words there; under the caret's line, when the keyboard is in +/// this body, the lines that explain it where the step put them +/// (Pane.ghost_at, ghost_rows: Messages.detailUnder), pushing the rows +/// below down (the rows past the body's foot are not shown meanwhile). +/// Bounded by the rows shown and the live messages' places +/// (Messages.max_live, Said.max_places). +fn messageGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: u16, body_h: u16) void { + const Messages = pardes.Messages; + const f = pane.file.?; + const chrome = p.chromeTheme(); + const th = p.theme(); + const pane_bg: Color = if (th.bg) |c| .{ .rgb = c } else .default; + const Hit = struct { line: usize, said: *const Messages.Said, place: *const Messages.Place }; + var hits: [Messages.max_live * Messages.Said.max_places]Hit = undefined; + var n: usize = 0; + for (p.messages.live[0..p.messages.live_len]) |*said| for (said.places[0..said.places_len]) |*place| { + if (place.line == 0 or !std.mem.eql(u8, place.path.slice(), f.path)) continue; + hits[n] = .{ .line = place.line - 1, .said = said, .place = place }; + n += 1; + }; + if (n == 0) return; + var detail: ?*const Messages.Said = null; + var row: u16 = 0; + while (row < body_h) : (row += 1) { + const w = pane.wrapAt(p.settings.tab, row); + if (w.line < 0) continue; + const line: usize = @intCast(w.line); + // A line's words go after its last row. + if (row + 1 < body_h and pane.wrapAt(p.settings.tab, row + 1).line == w.line) continue; + const hit = for (hits[0..n]) |h| { + if (h.line == line) break h; + } else continue; + const cells = s.cells[@as(usize, body_y + row) * s.cols + tx ..][0..tw]; + var end: usize = tw; + while (end > 0 and std.mem.eql(u8, cells[end - 1].grapheme(), " ")) end -= 1; + const at = end + 2; + if (at < tw) { + var buf: [160]u8 = undefined; + const words = std.fmt.bufPrint(&buf, "{s} {s}", .{ hit.said.kind.glyph(), hit.place.text.slice() }) catch &buf; + _ = s.print(tx + @as(u16, @intCast(at)), body_y + row, @intCast(tw - at), words, .{ .fg = .{ .rgb = Messages.ink(chrome, hit.said.kind) }, .bg = pane_bg }); + } + if (pane.ghost_rows > 0 and row == pane.ghost_at) detail = hit.said; + } + const said = detail orelse return; + const under_row = pane.ghost_at; + // The rows below the caret's line move down by the detail's rows. + const k: u16 = @min(pane.ghost_rows, body_h -| (under_row + 1)); + if (k == 0) return; + var dst: u16 = body_h - 1; + while (dst >= under_row + 1 + k) : (dst -= 1) { + const to = @as(usize, body_y + dst) * s.cols + tx; + const from = @as(usize, body_y + dst - k) * s.cols + tx; + @memcpy(s.cells[to..][0..tw], s.cells[from..][0..tw]); + } + const gutter: u16 = panes.File.gutterWidth(pane); + for (said.detail[0..k], 0..) |*text, i| { + const y = body_y + under_row + 1 + @as(u16, @intCast(i)); + s.fill(tx, y, tw, 1, .{ .bg = pane_bg }); + if (gutter + 2 < tw) _ = s.print(tx + gutter + 2, y, tw - gutter - 2, text.slice(), .{ .fg = .{ .rgb = Messages.ink(chrome, said.kind) }, .bg = pane_bg }); + } +} + +/// The word drawn at body row `row`, text column `col` of pane `id` in the +/// 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 layer = &p.surface.body_layers[id]; + const body = (p.surface.region(.body, id) orelse return null).rect; + var text: std.ArrayList(u8) = .empty; + if (layer.rows > 0) { + if (row >= layer.rows) return null; + for (layer.cells[row * layer.cols ..][0..layer.cols]) |cell| try text.appendSlice(arena, cell.grapheme()); + } else { + 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]; +} + /// Painted targets (SPEC §4.4), while a motion root's level shows in this /// body's tag: each of its keys at the line it would go to, in the gutter /// (`g e t b .` for `g`; for `]` the next and for `[` the previous @@ -953,6 +1048,113 @@ fn fillBody(dst: ?[]u8, pane: *Pane, viewport: []const []const u8) usize { return written; } +/// Grid row `y` of `s` from column `x`, as text. +fn rowText(s: *const Surface, x: u16, y: u16, buf: []u8) []const u8 { + var n: usize = 0; + for (x..s.cols) |col| { + const g = s.cells[@as(usize, y) * s.cols + col].grapheme(); + if (n + g.len > buf.len) break; + @memcpy(buf[n..][0..g.len], g); + n += g.len; + } + 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" { + 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[1] = .{}; + err.detail[1].set(" ^"); + err.detail_len = 2; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + pane.focus = .body; + pane.body.cur_row = 0; + p.sync(); + var s = try p.render(frame.allocator()); + const body = s.region(.body, id).?.rect; + // The mark, in the box's second cell; the tag's text as it was. + const r = p.view().rects[id]; + try std.testing.expectEqualStrings("\u{2717}", s.at(r.x + config.GUTTER - 1, p.tagTop(pane, r)).grapheme()); + try std.testing.expect(std.mem.startsWith(u8, try tagline.tagPrefix(p, pane), "/test.txt")); + var buf: [512]u8 = undefined; + // Its words after line 3's end; nothing inserted while the caret is + // 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. + pane.body.cur_row = 2; + _ = 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 + 5, &buf), "four") != null); + // ...never written: the file is as it was. + try std.testing.expectEqualStrings("one\ntwo\nthree\nfour\nfive\nsix\n", pane.file.?.content); + // The notice row offers the walk. + const foot = s.region(.notice_row, pardes.Region.none).?; + try std.testing.expect(std.mem.endsWith(u8, std.mem.trimEnd(u8, rowText(s, 0, foot.rect.y, &buf), " "), "SPC e walk")); +} + +test "a click under a message's detail lands on the row drawn there, a click on the detail on the line it explains, and B2 on a word of the detail runs it" { + 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("bad"); + err.places_len = 1; + err.detail[0] = .{}; + err.detail[0].set("try Wrap here"); + err.detail[1] = .{}; + err.detail[1].set(" ^"); + err.detail_len = 2; + pane.focus = .body; + pane.body.cur_row = 2; + p.sync(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + var s = try p.render(frame.allocator()); + const body = s.region(.body, id).?.rect; + var buf: [512]u8 = undefined; + // Line 5 drawn two rows lower than its place in the row map, under the + // detail: a click on it lands on it. + const five = std.mem.indexOf(u8, rowText(s, body.x, body.y + 6, &buf), "five").?; + const x = body.x + @as(u16, @intCast(five)) + 1; + 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; + _ = 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").?; + const wx = body.x + @as(u16, @intCast(wrap_at)) + 1; + p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = wx, .row = body.y + 4 } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = wx, .row = body.y + 4 } }); + try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row); + // B2 on a word of it: the word runs, as a typed one would. + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + const wrapped = p.settings.wrap; + p.update(.{ .mouse = .{ .button = .middle, .kind = .press, .col = wx, .row = body.y + 3 } }); + p.update(.{ .mouse = .{ .button = .middle, .kind = .release, .col = wx, .row = body.y + 3 } }); + while (p.nextEffect()) |_| {} + try std.testing.expect(p.settings.wrap != wrapped); + try std.testing.expectEqualStrings("one\ntwo\nthree\nfour\nfive\nsix\n", pane.file.?.content); +} + test "a motion root's level paints its targets in the gutter: `]` the next paragraph's p, `g` g and e; `m` browsed on `i` tints the braces' inside; nothing at rest" { const Key = pardes.Key; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 20 }); |
