diff options
Diffstat (limited to 'core/body_layer.zig')
| -rw-r--r-- | core/body_layer.zig | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/core/body_layer.zig b/core/body_layer.zig index 9c4a4c73..c2a20213 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -468,6 +468,8 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane } } + if (pane.file != null and p.messages.live_len > 0) messageGhosts(p, s, pane, tx, tw, body_y, body_h, active); + // `gw`'s labels, two letters over the start of each word if (pane.jump.items.len > 0) labels: { const flat = edit.flatSurface(p.editor(), &pane.body) catch break :labels; @@ -548,6 +550,68 @@ 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, 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, active: bool) 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; + const caret_line: usize = @intCast(@max(0, pane.body.cur_row)); + var detail: ?struct { row: u16, said: *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 (active and pane.focus == .body and line == caret_line and hit.said.detail_len > 0 and detail == null) detail = .{ .row = row, .said = hit.said }; + } + const under = detail orelse return; + // The rows below the caret's line move down by the detail's rows. + const k: u16 = @intCast(@min(under.said.detail_len, 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 (under.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, under.said.kind) }, .bg = pane_bg }); + } +} + pub fn bodyText(tab: u16, arena: std.mem.Allocator, pane: *Pane) ![]const u8 { if (pane.addon != null) { const buf = try arena.alloc(u8, pane.rows -| 1); @@ -656,3 +720,59 @@ 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")); +} |
