//! Drawing a pane body into a surface: its text, colours, selections and //! cursor, and joining a body with compact context rows through its layer //! (Layer.zig). const std = @import("std"); const pardes = @import("root.zig"); const normal = @import("normal.zig"); const edit = @import("edit.zig"); const config = @import("config.zig"); const layout = @import("layout.zig"); const modal = @import("modal.zig"); const tracy = @import("tracy.zig"); const panes = @import("panes.zig"); const colors = @import("colors.zig"); const ansi = @import("ansi.zig"); const Color = @import("surface.zig").Color; const CellStyle = @import("surface.zig").CellStyle; const Cell = @import("surface.zig").Cell; const Surface = @import("surface.zig").Surface; const Pane = panes.Pane; const BOX_H = pardes.BOX_H; const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET; const Rect = layout.Rect; const Pardes = pardes.Pardes; const tagline = @import("tagline.zig"); const Text = panes.Text; const Layer = @import("Layer.zig"); const TagState = @import("TagState.zig"); const bindings = @import("bindings.zig"); const File = panes.File; // ---- drawing a pane body into the surface ---- fn paintTerminalSelection(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, rows: []const Pane.PointerRow, bg: [3]u8, fg: ?[3]u8) void { const body = bodyText(p.settings.tab, p.scratch.allocator(), pane) catch return; const raw = pane.body.mode == .tty; const tx = r.x + config.GUTTER; const width = r.w -| config.GUTTER; const body_y = p.bodyTop(pane, r); var lines = std.mem.splitScalar(u8, body, '\n'); var visible: i32 = 0; while (lines.next()) |line| : (visible += 1) { if (visible >= r.h -| pane.tag_rows) break; const source_row = if (raw) @as(i32, @intCast(pane.termTop())) + visible else pane.wrapAt(p.settings.tab, visible).line; for (rows) |row| { const target = if (row.raw_terminal) (if (raw) row.row else pane.surfRow(row.row)) else (if (raw) pane.gridRow(row.row) else row.row); if (target != source_row) continue; var lo = row.lo; var hi = row.hi; if (row.raw_terminal and !raw) { lo -|= row.prompt_bytes; hi -|= row.prompt_bytes; } else if (!row.raw_terminal and raw) { const prefix = panes.Term.promptPrefix(p.settings.tab, pane.terminal, source_row, line); lo += prefix; hi += prefix; } lo = modal.graphemeStart(line, @min(lo, line.len)); hi = @min(hi, line.len); if (lo >= hi) continue; const col = panes.File.rawDisplayCol(p.settings.tab, line, lo); const end = @min(width, if (row.to_edge and hi == line.len) width else panes.File.rawDisplayCol(p.settings.tab, line, hi)); if (col < end) s.tint(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(visible)), @intCast(end - col), bg, fg); } } } /// Whether `col` lies past the last column its visible row shows: the last /// visible row of a line owns every column after its start (wrapRow), so a /// head millions of bytes along a long line is on that row, off the view. fn pastView(tab: u16, pane: *Pane, body: []const u8, line: i32, row: i32, at: i32, col: i32, tw: u16) bool { const text = if (pane.file != null) panes.File.sourceLine(pane, line) else modal.lineSlice(body, @intCast(@max(0, row))); const from: usize = @intCast(@max(0, at)); const to: usize = @intCast(@max(0, col)); if (from >= text.len) return to >= from +| tw; // A file row's last shown byte counts its table pads (File.rowPads). const shown: usize = if (pane.file != null) @intCast(panes.File.byteAtRowDisplay(tab, pane, line, at, tw)) else from + panes.File.byteAtDisplay(tab, text[from..], tw); return to >= shown and to < text.len; } fn paintSourceSelection(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, rows: []const Pane.PointerRow, bg: [3]u8, fg: ?[3]u8) void { if (pane.isTerminal()) return paintTerminalSelection(p, s, pane, r, rows, bg, fg); const terminal_lines = if (pane.file == null) edit.terminalLines(p, pane) catch return else &.{}; const tx = r.x + config.GUTTER; const width = r.w -| config.GUTTER; const body_y = p.bodyTop(pane, r); const prefix: i32 = if (pane.file != null) panes.File.gutterWidth(pane) else 0; var visible: i32 = 0; while (visible < @as(i32, r.h -| pane.tag_rows)) : (visible += 1) { const source = pane.wrapAt(p.settings.tab, visible); const line = edit.pointerSourceLine(pane, terminal_lines, source.line); const start = @min(line.len, @as(usize, @intCast(@max(0, source.at)))); const next = pane.wrapAt(p.settings.tab, visible + 1); const end = if (next.line == source.line) @min(line.len, @as(usize, @intCast(@max(0, next.at)))) else line.len; for (rows) |row| { if (row.row != source.line) continue; const lo = @max(start, row.lo); const hi = @min(end, row.hi); if (lo >= hi) continue; // A file row through its pads (File.rowPads), as it is drawn. const col = prefix + source.pad + if (pane.file != null) panes.File.displayOffset(p.settings.tab, pane, source.line, @intCast(start), @intCast(lo)) else @as(i32, @intCast(panes.File.rawDisplayCol(p.settings.tab, line[start..], lo - start))); const last = @min(width, if (row.to_edge and hi == end) @as(i32, width) else prefix + source.pad + if (pane.file != null) panes.File.displayOffset(p.settings.tab, pane, source.line, @intCast(start), @intCast(hi)) else @as(i32, @intCast(panes.File.rawDisplayCol(p.settings.tab, line[start..], hi - start)))); if (col < last) s.tint(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(visible)), @intCast(last - col), bg, fg); } } } /// A pointer sweep over pane `pane`'s rows in `r`, its tag's and its body's. pub fn paintPointerSelection( p: *Pardes, s: *Surface, pane: *Pane, r: Rect, sl: Pane.Sel, /// Rows of the selection above this are left alone. from_row: u16, bg: [3]u8, fg: ?[3]u8, ) void { if (pane.pointerSelection(sl)) |rows| return paintSourceSelection(p, s, pane, r, rows, bg, fg); const tag_y = p.tagTop(pane, r); const body_y = p.bodyTop(pane, r); var row: u16 = 0; while (row < r.h) : (row += 1) { if (row < from_row) continue; const cols = edit.sweepCols(sl, row) orelse continue; const c0: i32 = if (cols.first) cols.lo else 0; const c1: i32 = cols.hi orelse std.math.maxInt(i16); const sy = if (row < pane.tag_rows) tag_y + row else body_y + row - pane.tag_rows; // File line numbers occupy a gutter only in the body. The tag is // row zero in Sel space and starts at its real first text cell. const scroll: i32 = if (row < pane.tag_rows) pane.tag_scroll else 0; const col: i32 = if (pane.file != null and row >= pane.tag_rows) @max(c0, @as(i32, panes.File.gutterWidth(pane))) else @max(0, c0 - scroll); const inset = if (row < pane.tag_rows) TAG_TEXT_INSET else config.GUTTER; const row_x = r.x + @min(inset, r.w); const row_width = r.w -| inset; const end = @min(c1 - scroll + 1, row_width); if (col < end) s.tint(row_x + @as(u16, @intCast(col)), sy, @intCast(end - col), bg, fg); } } /// The rail's column in the theme's rail and thumb, and the column right /// of it the page. fn paintRail(c: *const colors.Chrome, s: *Surface, bar: pardes.Region) void { s.fill(bar.rect.x, bar.rect.y, 1, bar.rect.h, .{ .bg = .{ .rgb = c.rail } }); s.fill(bar.rect.x + 1, bar.rect.y, 1, bar.rect.h, .{ .bg = if (c.host_page) .default else .{ .rgb = c.page.ground } }); s.fill(bar.rect.x, bar.rect.y + bar.thumb_y, 1, bar.thumb_h, .{ .bg = .{ .rgb = c.rail_thumb } }); } /// A pane's body, painted once. A file body with compact tree-sitter /// context rows is painted into its body layer -- more rows than the grid /// has, the context rows at the tagline's height -- and the grid's rows are /// copies of that layer's first ones, at body height. Any other body has no /// rows the grid cannot show, and is painted straight onto the grid. The /// rail is the grid's alone. pub fn joinBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, rail: pardes.Region) !void { if (pane.file == null) return renderBody(p, s, arena, pane, r, id, active, rail); // The pane's geometry as it is (File.geometry): made by the step that // moved it if one read it, else here, and the same either way. panes.File.geometry(p.settings.tab, pane); // Which rows are context rows is decided by the text the body shows: a // layer taller than the grid's rows when there are any. if (pane.collapsed or r.h <= pane.tag_rows or pane.body_rows == 0) return renderBody(p, s, arena, pane, r, id, active, rail); const viewport_rows = r.h - pane.tag_rows; const logical_rows: usize = pane.body_rows; const width = r.w - config.GUTTER; const layer = &s.body_layers[id]; const count = logical_rows * width; if (layer.cells.len != count) layer.cells = try p.gpa.realloc(layer.cells, count); const temporary_cells = try arena.alloc(Cell, @as(usize, r.w) * (logical_rows + pane.tag_rows)); @memset(temporary_cells, .{}); var temporary: Surface = .{ .cols = r.w, .rows = @intCast(logical_rows + pane.tag_rows), .cells = temporary_cells, .tab = p.settings.tab }; const synthetic: Rect = .{ .x = 0, .y = 0, .w = r.w, .h = @intCast(logical_rows + pane.tag_rows) }; try renderBody(p, &temporary, arena, pane, synthetic, id, active, null); for (0..logical_rows) |row| { const start = (row + pane.tag_rows) * r.w + config.GUTTER; @memcpy(layer.cells[row * width ..][0..width], temporary_cells[start..][0..width]); } layer.kind = .body; layer.id = @intCast(id); layer.serial = pane.serial; const body = s.region(.body, id).?; layer.viewport = .{ .x = body.rect.x, .y = body.rect.y, .w = body.rect.w, .h = body.rect.h }; layer.cols = width; layer.rows = @intCast(logical_rows); layer.context_rows = pane.context_rows; layer.setContextLines(pane.wrap_line[0..pane.context_rows]); layer.cursor = null; if (temporary.cursor) |cursor| { if (cursor.x >= config.GUTTER and cursor.y >= pane.tag_rows) layer.cursor = .{ .x = cursor.x - config.GUTTER, .y = @intCast(cursor.y - pane.tag_rows), .bar = cursor.bar }; } // The grid: the layer's first rows, as many as the body has, each a // body row high; its caret where one of them has it. (pane.rows is the // body's height after sync, and context rows are only prepared while // the rows fit the wrap table, so the layer always has these rows.) std.debug.assert(layer.rows >= viewport_rows); for (0..viewport_rows) |row| { const grid_row = s.cells[(body.rect.y + row) * s.cols ..][body.rect.x..][0..width]; @memcpy(grid_row, layer.cells[row * width ..][0..width]); } if (layer.cursor) |cursor| if (cursor.y < viewport_rows) { s.cursor = .{ .x = body.rect.x + cursor.x, .y = body.rect.y + cursor.y, .bar = cursor.bar }; }; if (rail.rect.h > 0) paintRail(p.chrome(), s, rail); } fn drawGutter(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16, active: bool) void { const prefix_width = panes.File.gutterWidth(pane); const goff = pane.scroll(); const gcrow = if (pane.body.cur_pinned) pane.body.cur_row else @as(i32, panes.Term.grid(pane.terminal).cursor_y) + goff; const typing_body = pane.focus == .body and (pane.prompt == .none or pane.prompt == .del_side or pane.prompt == .repl_choice); const cur_line: i32 = if (active and typing_body) gcrow else std.math.minInt(i32); // the body's first row, the way renderPane derives it const body_y = p.bodyTop(pane, r); const chrome = p.chrome(); // The context rows on the tag's ground, as a header. for (0..@min(pane.context_rows, body_h)) |context_row| { for (0..tw) |col| { const cell = s.at(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(context_row))); cell.default = false; cell.style.bg = .{ .rgb = chrome.tag[0].ground }; cell.style.font_role = .tagline; } } var vr: u16 = 0; while (vr < body_h) : (vr += 1) { const row_line: i32 = if (pane.wrap_n == 0) goff + @as(i32, vr) else if (vr < pane.wrap_n) pane.wrap_line[vr] else std.math.maxInt(i32); // The `~` of the slot past the text is no line's number: never lit. const on_cursor = row_line == cur_line and row_line >= 0 and !panes.File.endSlot(pane, @intCast(row_line)); var c: u16 = 0; while (c < prefix_width and c < tw) : (c += 1) { const cell = s.at(tx + c, body_y + vr); cell.default = false; // paints blank gutter rows too cell.style.fg = .{ .rgb = if (on_cursor) chrome.line_number_current else chrome.line_number }; cell.style.bold = on_cursor; } } } fn drawWrapMarkers( p: *Pardes, s: *Surface, pane: *const Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16, pane_bg: pardes.Color, ) void { if (tw <= panes.File.gutterWidth(pane) + 1) return; const body_y = p.bodyTop(pane, r); const marker_fg = p.chrome().line_number; var row: u16 = 0; while (row + 1 < pane.wrap_n and row + 1 < body_h) : (row += 1) { if (pane.wrap_line[row + 1] != pane.wrap_line[row] or pane.wrap_col[row] < 0) continue; s.set(tx + tw - 1, body_y + row, config.wrap_marker, .{ .fg = .{ .rgb = marker_fg }, .bg = pane_bg, }); } } pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, rail: ?pardes.Region) !void { const c = p.chrome(); const tx = r.x + config.GUTTER; const tw = r.w - config.GUTTER; // Below the notice bands: they are stacked at the top of the body the // way tree-sitter context rows are, so the body starts under them // rather than being painted over by them. const body_y = p.bodyTop(pane, r); // A theme that leaves its page to the host leaves it its text too. const pane_bg: Color = if (c.host_page) .default else .{ .rgb = c.page.ground }; // body: emulator rows, prompt rows hidden outside tty mode const body_h = r.h -| pane.tag_rows; const body_bottom = r.h; const body_style: CellStyle = .{ .fg = if (c.host_page) .default else .{ .rgb = c.page.text }, .bg = pane_bg, }; const tz_body = tracy.zone(@src(), "bodyText"); const probe_text = pardes.FrameStats.nowNs(); const body = try bodyText(p.settings.tab, arena, pane); p.frames.zone(.body_text, probe_text); tz_body.end(); // A select sweep begun on the tag and dragged into the body marks the // body rows it crossed only where no text is: it is laid down before // the rows, which paint over it. const sweep = pane.sel[pardes.sel_slot]; if (sweep.state != .none and (sweep.state != .done or pardes.sel_slot == 0) and @min(sweep.r0, sweep.r1) < pane.tag_rows) paintPointerSelection(p, s, pane, r, sweep, pane.tag_rows, c.sweeps[pardes.sel_slot].ground, c.sweeps[pardes.sel_slot].text); { // The rows themselves. `bodyText` above is only the string BUILD; // this is what writes it into the surface. const tz_rows = tracy.zone(@src(), "paneBodyRows"); defer tz_rows.end(); const probe_rows = pardes.FrameStats.nowNs(); defer p.frames.zone(.body_rows, probe_rows); var it = std.mem.splitScalar(u8, body, '\n'); var i: u16 = 0; while (it.next()) |line| : (i += 1) { if (i >= body_h) break; _ = s.print(tx, body_y + i, tw, line, body_style); } } const tz_color = tracy.zone(@src(), "paneRecolor"); const probe_color = pardes.FrameStats.nowNs(); defer p.frames.zone(.body_color, probe_color); // File layout is independent of whether its producer requests colors. if (pane.file) |*f| { drawGutter(p, s, pane, r, tx, tw, body_h, active); // Never inlined: in renderBody's own frame it cost every body a // frame (a terminal's output about 33,000 instructions a pump). if (pane.layers.lanes | pane.layers.heat > 0) @call(.never_inline, @import("jj.zig").paint, .{ p, s, pane, r, tx, tw, body_h }); panes.Query.paintMark(p, s, pane, r, body_h); if (pane.colorAlgo() != .none) panes.File.recolorSyntax(p, s, pane, f, r, tx, tw, body_h); panes.Tables.paint(p, s, pane, r, tx, tw, body_h); drawWrapMarkers(p, s, pane, r, tx, tw, body_h, pane_bg); // A picker's landing line under the places it lights (UX D7). const landing = pardes.Pick.landingLine(p, arena, pane); if (landing) |at| paintLanding(p, s, pane, r, tx, tw, body_h, at.line); pane.places.paint(p, s, pane, r, body_h); if (landing) |at| paintLandingSpan(p, s, pane, r, f, at.line, at.col); 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); ansi.recolorDiffOutput(p, s, pane, r, tx, tw, body_h, body); } tz_color.end(); if (pane.isTerminal()) ansi.images(p, s, pane, id, tx, tw, body_y, body_h); if (p.look_hover_preview) |preview| { if (preview.pane == id and preview.serial == pane.serial) { // Preserve body ink, then let every real user selection below // win over this quiet affordance. if (preview.file_word) |word| panes.File.paintWordSelection(p, s, pane, r, word.row, word.lo, word.hi, c.selection_secondary, null) else if (preview.sel) |sel| paintPointerSelection(p, s, pane, r, sel, pane.tag_rows, c.selection_secondary, null); } } const modal_hover = if (p.look_hover_preview) |preview| preview.pane == id and preview.serial == pane.serial and preview.sel == null and preview.file_word == null else false; for (pane.sel, 0..) |sl, b| { if (sl.state == .none) continue; if (sl.state == .done and b != 0) continue; // A select sweep over the tag is the tag's selection, painted there // in the tag's own selection colours. if (b == pardes.sel_slot and @min(sl.r0, sl.r1) < pane.tag_rows) continue; paintPointerSelection(p, s, pane, r, sl, pane.tag_rows, c.sweeps[b].ground, c.sweeps[b].text); } // 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; // The primary selection that is the current place (a live `/`, `n`, a // preview select it) is drawn as that place, its solid chip, not the // selection: one meaning, one look; the caret still shows. const on_place = place: { const f = pane.file orelse break :place false; const at = pane.places.current orelse break :place false; if (!pane.places.shows(pane) or at >= pane.places.hits.items.len) break :place false; const range = pane.body.primaryRange(f.content, 0); const hit = pane.places.hits.items[at]; break :place hit.start == @min(range.anchor, range.head) and hit.end == @max(range.anchor, range.head); }; if (show_prim or show_extra) { const vpfx: i32 = if (pane.file != null) panes.File.gutterWidth(pane) else 0; var si: usize = 0; while (si <= pane.body.nsel) : (si += 1) { const primary = si == pane.body.nsel; if (if (primary) !show_prim else !show_extra) continue; const sr = if (primary) panes.Text.SelRange{ .row = pane.body.cur_row, .col = pane.body.cur_col, .arow = if (pane.body.vsel.active) pane.body.vsel.row else pane.body.cur_row, .acol = if (pane.body.vsel.active) pane.body.vsel.col else pane.body.cur_col, } else pane.body.sels[si]; const bnd = edit.cellBounds(sr); const hover_only = primary and modal_hover and pane.body.mode != .normal and !preview; // The primary on the selection in its ink; every other range, // a hover and a dimmed preview on its faint half, the text's // own ink kept (R7). const full = primary and !hover_only and !(selecting != null and selecting.?.dim); const bg = if (full) c.page.selection.ground else c.selection_secondary; var vr: i32 = 0; while (vr + @as(i32, pane.tag_rows) < @as(i32, body_bottom)) : (vr += 1) { if (primary and on_place) break; const w = pane.wrapAt(p.settings.tab, vr); if (w.line < bnd.lo_row or w.line > bnd.hi_row) continue; const visible_line = modal.lineSlice(body, @intCast(vr)); // Only the visible columns are measured: a selection that // starts before this row's first column starts at it, and // one that ends past its last ends there. const row_text = if (pane.file != null) panes.File.sourceLine(pane, w.line) else visible_line; const row_at: usize = @intCast(@max(0, w.at)); // The byte past the row's last column, walked from its first: // bounded by the view, not the line. // A line that ends inside the view has no column past it. const shown_to = if (row_at >= row_text.len) row_text.len else if (pane.file != null) @as(usize, @intCast(panes.File.byteAtRowDisplay(p.settings.tab, pane, w.line, w.at, tw))) else row_at + panes.File.byteAtDisplay(p.settings.tab, row_text[row_at..], tw); const past_view = if (shown_to >= row_text.len) std.math.maxInt(usize) else shown_to; // A row that ends before the selection starts has none of it. if (w.line == bnd.lo_row and @as(usize, @intCast(@max(0, bnd.lo_col))) >= past_view) continue; const cstart: i32 = if (w.line == bnd.lo_row and bnd.lo_col > w.at) (if (pane.file != null) panes.File.displayOffset(p.settings.tab, pane, w.line, w.at, bnd.lo_col) else panes.File.lineDisplayOffset(p.settings.tab, visible_line, @intCast(@max(0, w.at)), @intCast(@max(0, bnd.lo_col)))) + vpfx + w.pad else vpfx + w.pad; const cend: i32 = if (w.line == bnd.hi_row and @as(usize, @intCast(@max(0, bnd.hi_col))) < past_view) (if (pane.file != null) panes.File.displayEndOffset(p.settings.tab, pane, w.line, w.at, bnd.hi_col) else panes.File.lineDisplayEndOffset(p.settings.tab, visible_line, @intCast(@max(0, w.at)), @intCast(@max(0, bnd.hi_col)))) + vpfx + w.pad else @as(i32, tw) - 1; const col: i32 = @max(cstart, vpfx); const end = @min(cend + 1, tw); if (col < end) s.tint(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(vr)), @intCast(end - col), bg, if (full) c.page.selection.text else null); } if (primary and !preview and !hover_only) continue; // the hardware cursor IS the primary's if (hover_only) continue; // quiet preview preserves the source ink const cw = pane.wrapRow(p.settings.tab, sr.row, sr.col); // A head off the view is measured not at all: its row is not // drawn, and its column may be millions of bytes along. if (cw.row < 0 or cw.row + @as(i32, pane.tag_rows) >= @as(i32, body_bottom)) continue; if (pastView(p.settings.tab, pane, body, sr.row, cw.row, cw.at, sr.col, tw)) continue; const crow = cw.row + @as(i32, pane.tag_rows); const ccol = (if (pane.file != null) panes.File.displayOffset(p.settings.tab, pane, sr.row, cw.at, sr.col) else panes.File.lineDisplayOffset(p.settings.tab, modal.lineSlice(body, @intCast(@max(0, cw.row))), @intCast(@max(0, cw.at)), @intCast(@max(0, sr.col)), )) + vpfx + cw.pad; if (crow >= pane.tag_rows and crow < @as(i32, body_bottom) and ccol >= vpfx and ccol < tw) { const cell = s.at(tx + @as(u16, @intCast(ccol)), body_y + @as(u16, @intCast(crow - pane.tag_rows))); cell.default = false; cell.style.bg = .{ .rgb = c.page.selection.text }; cell.style.fg = .{ .rgb = c.page.selection.ground }; } } } // 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); // `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; const letters = config.jump_label_alphabet; const style: CellStyle = .{ .fg = .{ .rgb = c.page.selection.text }, .bg = .{ .rgb = c.page.selection.ground }, .bold = true }; for (pane.jump.items, 0..) |word, i| { if (pane.jump_first) |first| if (i / letters.len != first) continue; var at = @min(word.anchor, word.head); for ([_]u8{ letters[i / letters.len], letters[i % letters.len] }) |letter| { const pos = modal.positionAt(flat, at); const row: i32 = @intCast(pos.row); const wp = pane.wrapRow(p.settings.tab, row, @intCast(pos.col)); const cx = if (pane.file != null) @as(i32, panes.File.gutterWidth(pane)) + wp.pad + panes.File.displayOffset(p.settings.tab, pane, row, wp.at, @intCast(pos.col)) else panes.File.lineDisplayOffset(p.settings.tab, modal.lineSlice(flat, pos.row), @intCast(@max(0, wp.at)), pos.col); if (wp.row >= 0 and wp.row < body_h and cx >= 0 and cx < tw) s.set(tx + @as(u16, @intCast(cx)), body_y + @as(u16, @intCast(wp.row)), &.{letter}, style); at = modal.nextGrapheme(flat, at); } } } // cursor: tracks the shell cursor until pinned by a click or a key // (the tag's or a prompt's cursor wins while that is being typed into) if (active and pane.focus == .body and (pane.prompt == .none or pane.prompt == .del_side or pane.prompt == .repl_choice)) { if (pane.body.mode != .tty) { const grid = panes.Term.grid(pane.terminal); const crow = if (pane.body.cur_pinned) pane.body.cur_row else pane.surfRow(@as(i32, grid.cursor_y) + @as(i32, @intCast(pane.termTop()))); const ccol = if (pane.body.cur_pinned) pane.body.cur_col else @as(i32, grid.cursor_x); const cwp = pane.wrapRow(p.settings.tab, crow, ccol); const prow = cwp.row + @as(i32, pane.tag_rows); // Off the view, the cursor is not measured (its column may be // millions of bytes along a line whose start is shown). if (cwp.row >= 0 and prow < body_bottom and !pastView(p.settings.tab, pane, body, crow, cwp.row, cwp.at, ccol, tw)) { // Files store source-byte columns; the Surface stores display // cells, so account for every expanded tab before the cursor. const cx = if (pane.file != null) @as(i32, panes.File.gutterWidth(pane)) + cwp.pad + panes.File.displayOffset(p.settings.tab, pane, crow, cwp.at, ccol) else if (pane.body.cur_pinned) panes.File.lineDisplayOffset(p.settings.tab, modal.lineSlice(body, @intCast(@max(0, cwp.row))), @intCast(@max(0, cwp.at)), @intCast(@max(0, ccol)), ) else ccol; // Left of a view scrolled sideways it is off the view, not // in the gutter. const left: i32 = if (pane.file != null) panes.File.gutterWidth(pane) else 0; if (prow >= pane.tag_rows and cx >= left and prow < body_bottom and cx < tw) s.cursor = .{ .x = tx + @as(u16, @intCast(cx)), .y = body_y + @as(u16, @intCast(prow - pane.tag_rows)), .bar = pane.body.mode == .insert }; } } else if (pane.terminal != null) visible: { // Where the program shows its cursor, if it does and in the rows // the view shows (past the rows a short view pans over). const grid = panes.Term.grid(pane.terminal); if (!grid.cursor_visible) break :visible; const row = grid.total - grid.len + grid.cursor_y; const top = pane.termTop(); if (row < top + pane.pan or row - top >= grid.len) break :visible; const y: u16 = @intCast(row - top - pane.pan); if (y + pane.tag_rows < body_bottom and grid.cursor_x < tw) s.cursor = .{ .x = tx + grid.cursor_x, .y = body_y + y }; } } // 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); // A code action's edit, previewed (Pick.zig). if (pane.file != null) try pardes.Pick.ghosts(p, s, arena, pane, r, body_h); // The rail covers the notice bands too: nothing else paints those two // gutter columns, so a gap there keeps the frame-wide border fill and // reads as a notch cut out of the scrollbar. if (rail) |bar| if (bar.rect.h > 0) paintRail(c, s, bar); } /// 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, unless the file changed since (Place.stale); /// 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 page = &p.chrome().page; const pane_bg: Color = if (p.chrome().host_page) .default else .{ .rgb = page.ground }; 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 place.stale 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.inkOn(page, 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.inkOn(page, 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 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; 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()); } return text.items; } /// A picker's landing line (UX D7): its text on the hit ground across /// the body's width, and `▸` in the key ink in the gutter's cell after its /// number (the one a prompt's current place has, Prompt.paintGutter), on /// each row it wraps onto. fn paintLanding(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, tx: u16, tw: u16, body_h: u16, line: usize) void { const c = p.chrome(); const gutter = File.gutterWidth(pane); const number = gutter -| File.laneCells(pane) -| pane.layers.heat -| 1; const body_y = p.bodyTop(pane, r); var vr: u16 = pane.context_rows; while (vr < body_h) : (vr += 1) { const w = pane.wrapAt(p.settings.tab, vr); if (w.line != @as(i32, @intCast(line))) continue; for (gutter..tw) |x| { const cell = s.at(tx + @as(u16, @intCast(x)), body_y + vr); cell.default = false; cell.style.bg = .{ .rgb = c.hit }; } if (gutter > 0 and w.at == 0) { var style = s.at(tx + number, body_y + vr).style; style.fg = .{ .rgb = c.page.key }; s.set(tx + number, body_y + vr, "▸", style); } } } /// The landing line's matched span (a Grep's match at the row's column, /// byte `col` of the line) as the current hit, solid in the hue with the /// page's ink, over the places lit on it (UX D7, R8). fn paintLandingSpan(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, f: *panes.File.State, line: usize, col: usize) void { if (!pane.places.shows(pane)) return; const hits = pane.places.hits.items; const ls = File.lineStart(p.gpa, f, line); const caret = ls + col; var i = panes.Places.firstFrom(hits, ls); while (i < hits.len and hits[i].start <= caret) : (i += 1) { if (hits[i].end <= caret) continue; File.paintWordSelection(p, s, pane, r, @intCast(line), @intCast(hits[i].start - ls), @intCast(hits[i].end - ls), p.chrome().hit_current, p.chrome().hit_current_text); return; } } /// 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 /// function, paragraph, test, comment, change and diagnostic: `f p T c g /// d`, the spec's, each tree object a query of the rows shown); browsing `m a` or `m i` unrolled, the brackets' /// region around the caret tinted. Only the rows shown are looked at: a /// target off them is not painted (the tag's arrows, SPEC §15, are not /// built). At rest it costs one test of the view's tag state. fn paintTargets(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, tx: u16, tw: u16, body_y: u16, body_h: u16) void { const host = TagState.hostOf(p) orelse return; if (p.panes[host] != pane) return; const v = p.view(); const cur = TagState.current(p, v) orelse return; if (!TagState.shown(p, v)) return; const f = &pane.file.?; if (f.line_starts.len == 0) return; const tab = p.settings.tab; const held = pane.context_rows; if (body_h <= held) return; // The lines the rows show, past the context rows held at the top. const lo: usize = @intCast(@max(0, pane.wrapAt(tab, held).line)); var hi = lo; for (held..body_h) |row| { const line = pane.wrapAt(tab, @intCast(row)).line; if (line >= 0 and line < f.line_starts.len) hi = @max(hi, @as(usize, @intCast(line))); } var children: [bindings.max_children]bindings.Child = undefined; const level = bindings.level(p, cur.root, cur.path, &children); if (cur.root == .match) { const st = &v.tag_state; if (!st.unrolled) return; const on = for (level) |c| { if (c.key == st.hi) break c; } else return; const leaf = (on.node orelse return).leaf orelse return; if (leaf != .role or (leaf.role != .match_inside and leaf.role != .match_around)) return; const role = leaf.role; const caret = lineStart(f, @intCast(@max(0, pane.body.cur_row))) + @as(usize, @intCast(@max(0, pane.body.cur_col))); const span = enclosing(f.content, @min(caret, f.content.len), lineStart(f, lo), lineStart(f, hi + 1)) orelse return; const from = if (role == .match_inside) span[0] + 1 else span[0]; const to = if (role == .match_inside) span[1] else span[1] + 1; const first = lineOf(f, from); const last = lineOf(f, to -| 1); const rows = arena.alloc(Pane.PointerRow, last - first + 1) catch return; for (rows, first..) |*row, line| row.* = .{ .row = @intCast(line), .lo = if (line == first) from - lineStart(f, line) else 0, .hi = if (line == last) to - lineStart(f, line) else File.sourceLine(pane, @intCast(line)).len, .to_edge = line != last, }; paintSourceSelection(p, s, pane, r, rows, p.chrome().selection_secondary, null); return; } if (cur.root != .goto and cur.root != .next and cur.root != .prev) return; // Each target's row and key, in the level's order. var rows: [bindings.max_children]u16 = undefined; var keys: [bindings.max_children]u8 = undefined; var n: usize = 0; for (level) |c| { const leaf = (c.node orelse continue).leaf orelse continue; if (leaf != .role) continue; const line = targetLine(p, pane, f, leaf.role, cur.root != .prev, lo, hi) orelse continue; if (line < lo or line > hi) continue; var row: u16 = held; while (row < body_h) : (row += 1) { const w = pane.wrapAt(tab, row); if (w.line == line and w.at == 0) break; } else continue; rows[n] = row; keys[n] = c.key; n += 1; } // In place of the line's number (UX D10): its cells blank and the // row's keys, as many as they hold, right-aligned to its last digit, // bold in the key ink on the gutter's ground. With no numbers, over // the line's first cells on the selection's ground. Never a shift. const ink = p.chrome().page.key; const number = File.gutterWidth(pane) -| File.laneCells(pane) -| pane.layers.heat -| 1; const cells = @min(if (number > 0) number else tw, tw); for (rows[0..n], 0..) |row, i| { if (std.mem.indexOfScalar(u16, rows[0..i], row) != null) continue; const y = body_y + row; const shown = @min(std.mem.count(u16, rows[0..n], &.{row}), cells); if (number > 0) for (0..cells) |x| { s.at(tx + @as(u16, @intCast(x)), y).text[0] = ' '; s.at(tx + @as(u16, @intCast(x)), y).len = 1; }; var x: u16 = if (number > 0) @intCast(cells - shown) else 0; for (rows[i..n], keys[i..n]) |other, key| { if (other != row) continue; if (x >= (if (number > 0) cells else shown)) break; const bg = if (number > 0) s.at(tx + x, y).style.bg else pardes.Color{ .rgb = p.chrome().page.selection.ground }; s.set(tx + x, y, bindings.keyName(key), .{ .fg = .{ .rgb = ink }, .bg = bg, .bold = true }); x += 1; } } } /// A hover's answer as ghost lines under the line it was asked at (SPEC /// §3.5, R3): the rows below move down by its lines (those past the body's /// foot not shown meanwhile), the buffer untouched. Bounded by the rows /// shown and the hover's `Ask.hover_max` bytes. fn hoverGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: u16, body_h: u16) void { const h = &p.lsp_ask.hover.?; if (h.serial != pane.serial) return; const f = if (pane.file) |*file| file else return; if (f.revision != h.revision) return; const tab = p.settings.tab; // The last row the line is shown on. var under: ?u16 = null; var row: u16 = pane.context_rows; while (row < body_h) : (row += 1) { if (pane.wrapAt(tab, row).line == h.row) under = row else if (under != null) break; } const at = under orelse return; const text = std.mem.trimEnd(u8, h.text.slice(), "\n"); const k: u16 = @intCast(@min(std.mem.count(u8, text, "\n") + 1, body_h - at - 1)); if (k == 0) return; var dst: u16 = body_h - 1; while (dst >= at + 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]); } // On the tag's ground in its ink, as a header. const tag = &p.chrome().tag[0]; const style: CellStyle = .{ .fg = .{ .rgb = tag.text }, .bg = .{ .rgb = tag.ground } }; const gutter: u16 = File.gutterWidth(pane); var lines = std.mem.splitScalar(u8, text, '\n'); for (0..k) |i| { const y = body_y + at + 1 + @as(u16, @intCast(i)); s.fill(tx, y, tw, 1, .{ .bg = .{ .rgb = tag.ground } }); if (gutter + 2 < tw) _ = s.print(tx + gutter + 2, y, tw - gutter - 2, lines.next() orelse "", style); } } /// A Rename typed in this body's tag, shown in its rows (SPEC §3.5, R3): /// each whole-word place of the name under the caret in the rows shown /// struck, the name typed after it, the rest of its row pushed right. /// Never written; bounded by the rows shown. fn renameGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: u16, body_h: u16) void { const host = TagState.hostOf(p) orelse return; if (p.panes[host] != pane or pane.file == null) return; const old = @import("lsp/Ask.zig").renamedWord(pane) orelse return; const new = p.view().tag_state.field.text(); const tab = p.settings.tab; const chrome = p.chrome(); const added: CellStyle = .{ .fg = .{ .rgb = chrome.page.severity[3] }, .bg = if (chrome.host_page) .default else .{ .rgb = chrome.page.ground }, .bold = true }; const wide: u16 = @intCast(@min(File.displayWidth(tab, new), tw)); const gutter: i32 = File.gutterWidth(pane); var row: u16 = pane.context_rows; while (row < body_h) : (row += 1) { const w = pane.wrapAt(tab, row); if (w.line < 0) continue; const line = File.sourceLine(pane, w.line); const from: usize = @min(line.len, @as(usize, @intCast(@max(0, w.at)))); const next = pane.wrapAt(tab, row + 1); const to: usize = if (next.line == w.line) @min(line.len, @as(usize, @intCast(@max(0, next.at)))) else line.len; // Right to left, so a push leaves the places before it where they are. var end = to; while (std.mem.lastIndexOf(u8, line[from..end], old)) |k| { const at = from + k; end = at; const before = at > 0 and (std.ascii.isAlphanumeric(line[at - 1]) or line[at - 1] == '_'); const after = at + old.len < line.len and (std.ascii.isAlphanumeric(line[at + old.len]) or line[at + old.len] == '_'); if (before or after) continue; const c0 = gutter + w.pad + File.displayOffset(tab, pane, w.line, w.at, @intCast(at)); const c1 = gutter + w.pad + File.displayOffset(tab, pane, w.line, w.at, @intCast(at + old.len)); if (c0 < gutter or c1 > tw) continue; const y = body_y + row; var c: u16 = @intCast(c0); while (c < c1) : (c += 1) { const cell = s.at(tx + c, y); cell.style.strikethrough = true; cell.style.fg = .{ .rgb = chrome.page.severity[0] }; } const x: u16 = @intCast(c1); if (x + wide < tw) { const cells = s.cells[@as(usize, y) * s.cols + tx ..][0..tw]; std.mem.copyBackwards(Cell, cells[x + wide ..], cells[x .. tw - wide]); } _ = s.print(tx + x, y, tw - x, new, added); } } } /// The line a motion role would go to from the caret of `pane`, looked for /// in lines `lo` to `hi` (the rows shown) alone; null for none there, or a /// role that is no line's. fn targetLine(p: *Pardes, pane: *Pane, f: *panes.File.State, role: modal.Normal.Role, forward: bool, lo: usize, hi: usize) ?usize { const tab = p.settings.tab; const caret: usize = @intCast(@max(0, pane.body.cur_row)); const nlines = f.line_starts.len; switch (role) { .goto_file_start => return 0, .goto_last_line => return if (nlines > 1 and f.line_starts[nlines - 1] == f.content.len) nlines - 2 else nlines - 1, .goto_last_modification => return if (pane.body.last_edit) |at| @intCast(@max(0, at.row)) else null, // helix's goto_window, as normal.gotoWindow counts its rows. .goto_view_top, .goto_view_bottom => { const shown: i32 = File.shownRows(tab, pane); const held: i32 = pane.context_rows; var last: i32 = shown - 1; while (last > 0 and pane.wrapAt(tab, held + last).line >= @as(i32, @intCast(nlines))) last -= 1; const margin: i32 = @min(config.scroll_off, @divTrunc(shown - 1, 2)); const vr = std.math.clamp(@min(@max(if (role == .goto_view_top) margin else last - margin, margin), last - margin), 0, last); const line = pane.wrapAt(tab, held + vr).line; return if (line < 0) null else @intCast(line); }, // helix's paragraph: past the lines with text, then past the blank // ones (modal.moveParagraph). .goto_paragraph => { var l = caret; if (forward) { while (l <= hi and !blank(pane, l)) l += 1; while (l <= hi and blank(pane, l)) l += 1; return if (l <= hi and l != caret) l else null; } while (l > lo and blank(pane, l - 1)) l -= 1; while (l > lo and !blank(pane, l - 1)) l -= 1; return if (l == caret or (l == lo and lo > 0)) null else l; }, .goto_change => return @import("jj.zig").nextChange(p, pane, caret, forward, hi - lo + 1), // Once a Diagnostics list fed them, the pane's places (normal.zig's // `]d`): the first after the caret's line, or the last before it. .goto_diagnostic => { const set = &pane.places; const fed = set.started and set.provenance == .list and if (p.paneBySerial(set.from)) |qid| (if (p.panes[qid].?.query) |q| q.source == .diagnostics else false) else false; if (!fed) return null; const hits = set.hits.items; const edge = if (forward) lineStart(f, caret + 1) else lineStart(f, caret); var a: usize = 0; var b: usize = hits.len; while (a < b) { const m = (a + b) / 2; if (hits[m].start < edge) a = m + 1 else b = m; } if (forward) return if (a < hits.len) lineOf(f, hits[a].start) else null; return if (a > 0) lineOf(f, hits[a - 1].start) else null; }, // With the Changes layer's lines in the buffer the tree's offsets // are not the buffer's: not painted then. .goto_function => return if (f.layer.lines.len > 0) null else treeTargets(p, pane, f, forward, lo, hi)[0], .goto_comment => return if (f.layer.lines.len > 0) null else treeTargets(p, pane, f, forward, lo, hi)[1], .goto_test => return if (f.layer.lines.len > 0) null else treeTargets(p, pane, f, forward, lo, hi)[2], else => return null, } } /// The lines `]f`, `]c` and `]T` (or `[`'s) go to from the caret, in lines /// `lo` to `hi` (the rows shown): one pass of the grammar's query over the /// tree the motions read (made once a revision), kept on the pane while /// its text, its caret and those lines stay (`Pane.targets`), so a frame /// that paints them again asks nothing. fn treeTargets(p: *Pardes, pane: *Pane, f: *panes.File.State, forward: bool, lo: usize, hi: usize) [3]?usize { const at = lineStart(f, @intCast(@max(0, pane.body.cur_row))) + @as(usize, @intCast(@max(0, pane.body.cur_col))); const key: panes.TargetsKey = .{ .revision = f.revision, .at = at, .lo = lo, .hi = hi, .forward = forward }; if (pane.targets.key) |was| if (std.meta.eql(was, key)) return pane.targets.lines; var lines: [3]?usize = @splat(null); if (normal.fileTree(p, f)) |tree| { var spans: [3]?@import("syntax.zig").Span = undefined; @import("syntax.zig").objectNext(f.path, tree, f.content, &.{ .function, .comment, .@"test" }, forward, @min(at, f.content.len), .{ lineStart(f, lo), lineStart(f, hi + 1) }, &spans); for (spans, &lines) |span, *line| line.* = if (span) |s| lineOf(f, s.from) else null; } pane.targets = .{ .key = key, .lines = lines }; return lines; } fn blank(pane: *const Pane, line: usize) bool { return std.mem.trim(u8, File.sourceLine(pane, @intCast(line)), " \t\r").len == 0; } /// Where line `line` starts in a file's buffer; past its last, its end. fn lineStart(f: *const panes.File.State, line: usize) usize { return if (line < f.line_starts.len) f.line_starts[line] else f.content.len; } /// The line holding byte `off` of a file's buffer. fn lineOf(f: *const panes.File.State, off: usize) usize { var a: usize = 0; var b = f.line_starts.len; while (a + 1 < b) { const m = (a + b) / 2; if (f.line_starts[m] <= off) a = m else b = m; } return a; } /// The innermost bracket pair around byte `at` of `text`, both in bytes /// `lo` to `hi` (the rows shown): its opening and its closing byte. fn enclosing(text: []const u8, at: usize, lo: usize, hi: usize) ?[2]usize { var depth: usize = 0; var i = at; const open = while (i > lo) { i -= 1; switch (text[i]) { ')', ']', '}' => depth += 1, '(', '[', '{' => if (depth == 0) break i else { depth -= 1; }, else => {}, } } else return null; const close: u8 = switch (text[open]) { '(' => ')', '[' => ']', else => '}', }; depth = 0; i = at; while (i < @min(hi, text.len)) : (i += 1) { if (text[i] == text[open]) depth += 1 else if (text[i] == close) { if (depth == 0) return .{ open, i }; depth -= 1; } } return null; } 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); @memset(buf, '\n'); return buf; } if (pane.file) |*f| return panes.File.bodyText(tab, arena, pane, f); // A terminal's: the rows its view shows (outside raw mode its prompts // cut off), the edit copy's lines over the rows the copy covers. const vp: []const []const u8 = if (pane.terminal) |state| try panes.Term.viewport(state, arena, pane.body.mode != .tty, pane.scrolled_to) else &.{}; const len = fillBody(null, pane, vp); const out = try arena.alloc(u8, len); const filled = fillBody(out, pane, vp); std.debug.assert(filled == out.len); return out; } pub const BodyRow = union(enum) { grid: i32, edit: struct { line: []const u8, idx: usize }, }; /// A terminal pane's body rows, top down: a row of this view of its /// emulator (`grid`, counted from the view's top), or a line of the edit /// copy standing in for the rows it covers (`edit`). pub const BodyWalk = struct { pane: *Pane, goff: i32, g: i32, /// the buffer can start above the viewport: drop the lines scrolled past skip: usize, n: usize = 0, lines: ?std.mem.SplitIterator(u8, .scalar) = null, covered: i32 = 0, line_idx: usize = 0, pub fn init(pane: *Pane) BodyWalk { const off = pane.scroll(); const goff: i32 = @intCast(pane.termTop()); return .{ .pane = pane, .goff = goff, .g = if (pane.body.mode == .tty) goff + pane.pan else pane.gridRow(off), .skip = if (pane.ovl) |o| @intCast(@max(0, off - pane.surfRow(o.row))) else 0, }; } pub fn next(w: *BodyWalk) ?BodyRow { while (w.n < w.pane.rows) { if (w.lines) |*it| { if (it.next()) |line| { const idx = w.line_idx; w.line_idx += 1; // Lines scrolled off the top still count: the index names a // line of the BUFFER, not of the visible body. if (w.skip > 0) { w.skip -= 1; continue; } w.n += 1; return .{ .edit = .{ .line = line, .idx = idx } }; } // The buffer stands in for `rows` shell rows however many lines // it actually spelled, which is the whole slide. w.g += w.covered; w.skip = 0; w.lines = null; continue; } if (w.pane.body.mode != .tty) if (w.pane.ovl) |o| if (w.g == o.row) { w.lines = std.mem.splitScalar(u8, o.text, '\n'); w.covered = o.rows; w.line_idx = 0; continue; }; const vi = w.g - w.goff; w.g += 1; w.n += 1; return .{ .grid = vi }; } return null; } }; /// Run the terminal body row walk. A null destination counts bytes; a slice /// fills the exact allocation made from that count. fn fillBody(dst: ?[]u8, pane: *Pane, viewport: []const []const u8) usize { var walk: BodyWalk = .init(pane); var written: usize = 0; var first = true; while (walk.next()) |row| { if (!first) { if (dst) |out| out[written] = '\n'; written += 1; } first = false; const bytes = switch (row) { .edit => |e| e.line, .grid => |vi| if (vi >= 0 and @as(usize, @intCast(vi)) < viewport.len) viewport[@intCast(vi)] else "", }; if (dst) |out| @memcpy(out[written..][0..bytes.len], bytes); written += bytes.len; } 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, 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"); 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); // In the error's ink on the page (R9). const mark = for (0..body.w) |i| { const cell = s.at(body.x + @as(u16, @intCast(i)), body.y + 2); if (std.mem.eql(u8, cell.grapheme(), "\u{2717}")) break cell; } else return error.TestUnexpectedResult; try std.testing.expectEqual(pardes.Color{ .rgb = p.chrome().page.severity[0] }, mark.style.fg); // 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); 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; err.open = true; 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 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").?; 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 place of their lines' numbers, right-aligned to the last digit, bold in the key ink: `]` the next paragraph's p and an unsaved edit's g, which `]g` goes to; `g` g and e; `m` browsed on `i` tints the braces' inside; nothing at rest (UX D10)" { const Key = pardes.Key; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 20 }); defer p.deinit(); const pane = try p.setTestFile("fn a() {\n one;\n}\n\nfn b() {\n two;\n}\n"); const id = p.view().active; var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); const a = frame.allocator(); p.clock.now_ns = 5 * std.time.ns_per_s; p.clock.stepped_ns = p.clock.now_ns; pane.focus = .body; // An edit on line 6, not saved; the caret back on line 2. for ("5jAx") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); p.update(.{ .key = .{ .cp = Key.escape } }); for ("4k") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); p.sync(); var s = try p.render(a); const body = s.region(.body, id).?.rect; const gutter = body.x; // The number's cells: the gutter less its blank. const number = panes.File.gutterWidth(pane) - 1; const Field = struct { /// Row `y`'s number field, its blanks trimmed. fn of(sf: *pardes.Surface, x: u16, y: u16, n: u16, buf: *[16]u8) []const u8 { var w = std.Io.Writer.fixed(buf); for (0..n) |i| w.writeAll(sf.at(x + @as(u16, @intCast(i)), y).grapheme()) catch {}; return std.mem.trim(u8, w.buffered(), " "); } }; var buf: [16]u8 = undefined; try std.testing.expectEqualStrings("5", Field.of(s, gutter, body.y + 4, number, &buf)); const plain_bg = s.at(gutter + panes.File.gutterWidth(pane) + 4, body.y + 1).style.bg; // `]`: nothing until its level shows; then `p` on the next paragraph's // number, `g` on the edit's, the digits gone. p.update(.{ .key = .{ .cp = ']' } }); p.advance(p.clock.now_ns); _ = frame.reset(.retain_capacity); s = try p.render(a); try std.testing.expectEqualStrings("5", Field.of(s, gutter, body.y + 4, number, &buf)); p.clock.now_ns = p.view().tag_state.due_ns; _ = frame.reset(.retain_capacity); s = try p.render(a); try std.testing.expectEqualStrings("p", Field.of(s, gutter, body.y + 4, number, &buf)); const key = s.at(gutter + number - 1, body.y + 4); try std.testing.expectEqualStrings("p", key.grapheme()); try std.testing.expectEqual(pardes.Color{ .rgb = p.chrome().page.key }, key.style.fg); try std.testing.expect(key.style.bold); try std.testing.expect(std.mem.indexOfScalar(u8, Field.of(s, gutter, body.y + 5, number, &buf), 'g') != null); try std.testing.expect(std.mem.indexOfAny(u8, Field.of(s, gutter, body.y + 5, number, &buf), "0123456789") == null); // The text never shifts. try std.testing.expectEqualStrings("t", s.at(gutter + panes.File.gutterWidth(pane) + 4, body.y + 5).grapheme()); // `]g` goes where its target is. p.update(.{ .key = .{ .cp = 'g', .text = "g" } }); try std.testing.expectEqual(@as(i32, 5), pane.body.cur_row); for ("4k") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); // `g`: its first and last lines; its first under the level's rows // drawn over the body's top (UX DESIGN I1: a target on a covered line // is hidden until rest), its last painted. p.update(.{ .key = .{ .cp = 'g' } }); p.advance(p.clock.now_ns); p.clock.now_ns = p.view().tag_state.due_ns; _ = frame.reset(.retain_capacity); s = try p.render(a); try std.testing.expect(pardes.Band.covered(p, pane, id).over > 0); try std.testing.expect(std.mem.indexOfScalar(u8, Field.of(s, gutter, body.y + 6, number, &buf), 'e') != null); p.update(.{ .key = .{ .cp = Key.escape } }); // `m` unrolled, its cursor on `i`: the inside of the braces around the // caret tinted. p.update(.{ .key = .{ .cp = 'm' } }); p.update(.{ .key = .{ .cp = Key.down } }); p.view().tag_state.hi = bindings.nodeAt(.match, "i").?.key; _ = frame.reset(.retain_capacity); s = try p.render(a); const lit = s.at(gutter + panes.File.gutterWidth(pane) + 4, body.y + 1).style.bg; try std.testing.expect(!std.meta.eql(plain_bg, lit)); // At rest again: the gutter as it was. p.update(.{ .key = .{ .cp = Key.escape } }); _ = frame.reset(.retain_capacity); 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); } test "the body's selections are one family (R7): the primary on the selection in its ink, another range on its faint half in the text's own ink, its head the selection reversed, a B2 sweep on the chrome's exec sweep" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 20 }); defer p.deinit(); colors.setThemeIndex(p, colors.themeIndex("orchard").?); const pane = try p.setTestFile("alpha beta\ngamma delta\n"); const id = p.view().active; pane.focus = .body; // `e` selects alpha, `C` copies it below: gamma the primary. for ("eC") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); try std.testing.expect(pane.body.vsel.active and pane.body.nsel == 1); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); const c = p.chrome(); const r = p.view().rects[id]; const x = r.x + config.GUTTER + panes.File.gutterWidth(pane); const y = p.bodyTop(pane, r); var s = try p.render(frame.allocator()); for (0..5) |i| { const primary = s.at(x + @as(u16, @intCast(i)), y + 1).style; try std.testing.expectEqual(pardes.Color{ .rgb = c.page.selection.ground }, primary.bg); if (i < 4) try std.testing.expectEqual(pardes.Color{ .rgb = c.page.selection.text }, primary.fg); const other = s.at(x + @as(u16, @intCast(i)), y).style; if (i < 4) { try std.testing.expectEqual(pardes.Color{ .rgb = c.selection_secondary }, other.bg); try std.testing.expectEqual(pardes.Color{ .rgb = c.page.text }, other.fg); } else { try std.testing.expectEqual(pardes.Color{ .rgb = c.page.selection.text }, other.bg); try std.testing.expectEqual(pardes.Color{ .rgb = c.page.selection.ground }, other.fg); } } // B2 swept over beta, not let go: the exec sweep. p.update(.{ .mouse = .{ .button = .middle, .kind = .press, .col = x + 6, .row = y } }); p.update(.{ .mouse = .{ .button = .middle, .kind = .drag, .col = x + 9, .row = y } }); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); for (6..10) |i| { const swept = s.at(x + @as(u16, @intCast(i)), y).style; try std.testing.expectEqual(pardes.Color{ .rgb = c.sweeps[1].ground }, swept.bg); try std.testing.expectEqual(pardes.Color{ .rgb = c.sweeps[1].text }, swept.fg); } } test "the body's surfaces take every colour from the chrome: none reads a theme's own fields or the old views (DESIGN.md B6)" { const sources = [_]struct { []const u8, []const u8 }{ .{ "body_layer.zig", @embedFile("body_layer.zig") }, .{ "Places.zig", @embedFile("Places.zig") }, .{ "Query.zig", @embedFile("Query.zig") }, .{ "Output.zig", @embedFile("Output.zig") }, .{ "Tables.zig", @embedFile("Tables.zig") }, .{ "File.zig", @embedFile("File.zig") }, .{ "Prompt.zig", @embedFile("Prompt.zig") }, .{ "Pick.zig", @embedFile("Pick.zig") }, .{ "jj.zig", @embedFile("jj.zig") }, .{ "ansi.zig", @embedFile("ansi.zig") }, .{ "draw.zig", @embedFile("draw.zig") }, .{ "tagline.zig", @embedFile("tagline.zig") }, .{ "Band.zig", @embedFile("Band.zig") }, .{ "Messages.zig", @embedFile("Messages.zig") }, .{ "Kind.zig", @embedFile("Kind.zig") }, }; var wrong: usize = 0; for (sources) |source| { const name, const text = source; // The code, not its tests. const code = text[0 .. std.mem.indexOf(u8, text, "\ntest \"") orelse text.len]; // A theme is read for its name alone. var at: usize = 0; while (std.mem.indexOfPos(u8, code, at, "theme()")) |i| : (at = i + 1) { if (std.mem.startsWith(u8, code[i + "theme()".len ..], ".name")) continue; std.debug.print("{s}: a theme's own colours read at byte {d}\n", .{ name, i }); wrong += 1; } for ([_][]const u8{ "chromeTheme()", "bodyChrome", ".roles", "Grounds", "LegacyTheme" }) |old| if (std.mem.indexOf(u8, code, old)) |i| { std.debug.print("{s}: `{s}` at byte {d}\n", .{ name, old, i }); wrong += 1; }; } try std.testing.expectEqual(@as(usize, 0), wrong); } test "a selection that is the current place is drawn as the current place, its solid chip, not the selection: one meaning, one look (the coordinator's ruling on L2b's open point); the caret still shows, and any other selection is the selection" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); colors.setThemeIndex(p, colors.themeIndex("orchard").?); // Its first line under the `/` field's row (UX DESIGN M2), the places // on the lines after it. const pane = try p.setTestFile("zero\nalpha beta\ngamma alpha\nb\n"); const c = p.chrome(); var frame: std.heap.ArenaAllocator = .init(std.testing.allocator); defer frame.deinit(); const r = p.view().rects[0]; const x = r.x + config.GUTTER + panes.File.gutterWidth(pane); const y = p.bodyTop(pane, r); const keys = struct { fn typed(q: *Pardes, text: []const u8) void { for (text) |ch| q.update(.{ .key = .{ .cp = ch, .text = &[_]u8{ch} } }); } }.typed; // A live `/`: the first place current, and selected. keys(p, "/alpha"); try std.testing.expectEqual(@as(?usize, 0), pane.places.current); var s = try p.render(frame.allocator()); for (0..5) |i| { const cell = s.at(x + @as(u16, @intCast(i)), y + 1).style; try std.testing.expectEqual(Color{ .rgb = c.hit_current }, cell.bg); try std.testing.expectEqual(Color{ .rgb = c.hit_current_text }, cell.fg); } // Landed, then `n`: the next one current and selected, the same chip, // and the caret on it. p.update(.{ .key = .{ .cp = pardes.Key.enter } }); keys(p, "n"); try std.testing.expectEqual(@as(?usize, 1), pane.places.current); s = try p.render(frame.allocator()); for (6..11) |i| try std.testing.expectEqual(Color{ .rgb = c.hit_current }, s.at(x + @as(u16, @intCast(i)), y + 2).style.bg); try std.testing.expect(s.cursor != null and s.cursor.?.y == y + 2 and s.cursor.?.x >= x + 6 and s.cursor.?.x <= x + 10); // The line selected, past the place: a selection again. keys(p, "x"); s = try p.render(frame.allocator()); try std.testing.expectEqual(Color{ .rgb = c.page.selection.ground }, s.at(x + 6, y + 2).style.bg); }