import type { CodeModel, NodeId } from "./model"; export interface Box { readonly x: number; readonly y: number; readonly w: number; readonly h: number; } /** Code blocks are sized to their lines, so the map at the closest level shows the code itself. */ export const LINE_HEIGHT = 18; export const CODE_TOP = 30; const CODE_WIDTH = 440; const MAX_CODE_LINES = 160; const MAX_COLUMN_ITEMS = 8; /** A storyline wider than this many times its height wraps into rows, read left to right, then down. */ const MAX_ASPECT = 2.4; interface Spacing { readonly pad: number; readonly head: number; readonly gapX: number; readonly gapY: number; } const SPACING: Record<"project" | "folder" | "file" | "class", Spacing> = { project: { pad: 48, head: 0, gapX: 160, gapY: 64 }, folder: { pad: 24, head: 44, gapX: 100, gapY: 28 }, file: { pad: 14, head: 32, gapX: 60, gapY: 16 }, class: { pad: 12, head: 30, gapX: 40, gapY: 12 }, }; interface Sized { readonly w: number; readonly h: number; readonly kids: readonly (readonly [NodeId, number, number])[]; } /** Places every block. Inside each block, its children run left to right in the order they use each other, as a storyline. */ export function layoutMap(model: CodeModel): ReadonlyMap { const sizes = new Map(); size(model, "root", sizes); const boxes = new Map(); place("root", 0, 0, sizes, boxes); return boxes; } export function codeLineCount(model: CodeModel, id: NodeId): number { const symbol = model.symbol(id); return symbol ? Math.min(MAX_CODE_LINES, symbol.end - symbol.start + 1) : 0; } function spacingOf(model: CodeModel, id: NodeId): Spacing { const kind = model.kind(id); return kind === "project" ? SPACING.project : kind === "folder" ? SPACING.folder : kind === "file" ? SPACING.file : SPACING.class; } function size(model: CodeModel, id: NodeId, sizes: Map): Sized { const kids = model.kids(id); if (model.symbol(id) && !kids.length) { const sized = { w: CODE_WIDTH, h: CODE_TOP + codeLineCount(model, id) * LINE_HEIGHT + 8, kids: [] }; sizes.set(id, sized); return sized; } for (const kid of kids) size(model, kid, sizes); const { pad, head, gapX, gapY } = spacingOf(model, id); const columns = storyline(model, id, kids).map((column) => ({ ids: column, w: Math.max(...column.map((kid) => sizes.get(kid)!.w)), h: column.reduce((sum, kid) => sum + sizes.get(kid)!.h, 0) + gapY * (column.length - 1) })); const placed: [NodeId, number, number][] = []; let x = pad; let y = head + pad; let rowHeight = 0; let widest = 0; const rowWidth = wrapWidth(columns, gapX); for (const column of columns) { if (x > pad && x + column.w > pad + rowWidth) { widest = Math.max(widest, x - gapX); x = pad; y += rowHeight + gapY * 3; rowHeight = 0; } let top = y; for (const kid of column.ids) { placed.push([kid, x, top]); top += sizes.get(kid)!.h + gapY; } rowHeight = Math.max(rowHeight, column.h); x += column.w + gapX; } widest = Math.max(widest, x - gapX); const height = y + rowHeight; const sized = { w: Math.max(columns.length ? widest + pad : 0, 200), h: height + pad, kids: placed }; sizes.set(id, sized); return sized; } /** The row width at which a storyline wraps, so a block stays close to a screen's shape. */ function wrapWidth(columns: readonly { w: number; h: number }[], gapX: number): number { const total = columns.reduce((sum, column) => sum + column.w + gapX, 0); const tallest = Math.max(0, ...columns.map((column) => column.h)); if (!tallest || total <= tallest * MAX_ASPECT) return Number.POSITIVE_INFINITY; return Math.max(...columns.map((column) => column.w), Math.sqrt(total * tallest * MAX_ASPECT * 0.75)); } /** Splits children into columns: a child that uses another sits to its left. Tall columns wrap. */ function storyline(model: CodeModel, holder: NodeId, kids: readonly NodeId[]): NodeId[][] { const index = new Map(kids.map((kid, i) => [kid, i])); const uses = kids.map(() => new Set()); const childOf = (id: string): number | undefined => { const chain = model.chain(id); const at = chain.indexOf(holder); return at >= 0 ? index.get(chain[at + 1]) : undefined; }; for (const edge of model.graph.edges) { const from = childOf(edge.from); const to = childOf(edge.to); if (from !== undefined && to !== undefined && from !== to) uses[from].add(to); } const rank = new Array(kids.length).fill(-1); const visiting = new Set(); const depth = (i: number): number => { if (rank[i] >= 0) return rank[i]; visiting.add(i); let best = 0; for (const next of uses[i]) if (!visiting.has(next)) best = Math.max(best, depth(next) + 1); visiting.delete(i); rank[i] = best; return best; }; kids.forEach((_, i) => depth(i)); const top = Math.max(0, ...rank); const columns: NodeId[][] = []; kids.forEach((kid, i) => (columns[top - rank[i]] ??= []).push(kid)); return columns.filter(Boolean).flatMap((column) => Array.from({ length: Math.ceil(column.length / MAX_COLUMN_ITEMS) }, (_, part) => column.slice(part * MAX_COLUMN_ITEMS, (part + 1) * MAX_COLUMN_ITEMS))); } function place(id: NodeId, x: number, y: number, sizes: ReadonlyMap, boxes: Map) { const sized = sizes.get(id)!; boxes.set(id, { x, y, w: sized.w, h: sized.h }); for (const [kid, kx, ky] of sized.kids) place(kid, x + kx, y + ky, sizes, boxes); }