diff options
Diffstat (limited to 'core/body_layer.zig')
| -rw-r--r-- | core/body_layer.zig | 100 |
1 files changed, 86 insertions, 14 deletions
diff --git a/core/body_layer.zig b/core/body_layer.zig index c2a20213..ef3401ed 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -468,7 +468,7 @@ 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); + if (pane.file != null and p.messages.live_len > 0) messageGhosts(p, s, pane, tx, tw, body_y, body_h); // `gw`'s labels, two letters over the start of each word if (pane.jump.items.len > 0) labels: { @@ -553,10 +553,12 @@ 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 { +/// 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(); @@ -571,8 +573,7 @@ fn messageGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: 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 detail: ?*const Messages.Said = null; var row: u16 = 0; while (row < body_h) : (row += 1) { const w = pane.wrapAt(p.settings.tab, row); @@ -592,26 +593,46 @@ fn messageGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: 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 }; + if (pane.ghost_rows > 0 and row == pane.ghost_at) detail = hit.said; } - const under = detail orelse return; + 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 = @intCast(@min(under.said.detail_len, body_h - under.row - 1)); + 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) { + 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)); + 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, under.said.kind) }, .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]; +} + 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); @@ -776,3 +797,54 @@ test "a build error is said where its cause is: the mark on its file's tag, its 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); +} |
