// Which hierarchy rows lead to the file being edited, and how far from it each // one is. No `vscode` dependency, so plain Node tests cover all of it. See // docs/features/hierarchy-current-path/README.md. // // **A row's distance is a property of its module, not of its path.** It is how // many instantiation levels the module sits above the nearest module declared // in the current file — 0 for that module itself. The level below a row is the // module's own instance list wherever the row sits, so the answer is the same // for every row of a module, and one breadth-first walk up the module graph // answers it for every module at once. A row then costs a map lookup, and // nothing below a closed row is ever built to find out whether to light it. // // In a forward tree the lit rows are exactly the path from the root down to // each current instance. In an upward "Parents of" tree the same rule lights // the current file's parents among the DUT's parents — dimmer the further up // the design they are, which on that tree is further *down* the page. const { headerId, parentIndex } = require('./hierarchySections'); /** The contributed theme colours, brightest first; the last one is the floor * every ancestor four or more levels up shares. */ const LIT_COLORS = [ 'lucisv.hierarchyCurrent', 'lucisv.hierarchyParent1', 'lucisv.hierarchyParent2', 'lucisv.hierarchyParent3', 'lucisv.hierarchyAncestor', ]; /** The solid bar at a lit row's left edge. The tints stay light enough for the * label to keep 4.5:1, so the bar is what carries "brightest": widest, solid * and fully opaque on the current row, narrower and fainter each level up. */ const BAR_COLOR = 'lucisv.hierarchyCurrentBar'; /** The level a distance is painted at: 0 is the current row itself. */ function litLevel(distance) { if (!Number.isInteger(distance) || distance < 0) return undefined; return Math.min(distance, LIT_COLORS.length - 1); } /** The CSS variable a webview sees a theme colour as. VS Code replaces only the * *first* dot (`themeing.ts`: `entry.id.replace('.', '-')`), which is why every * id above has exactly one. */ function cssVariableFor(colorId) { return `--vscode-${colorId.replace('.', '-')}`; } /// The model's own spellings of the active file: every distinct definition URI /// `sameFile` accepts. The server and VS Code may spell one file differently, so /// the comparison is the caller's; this asks it once per file, not per module. function matchingFiles(model, sameFile) { const files = new Set(); const asked = new Set(); for (const module of model.modules) { const uri = module.definition.uri; if (asked.has(uri)) continue; asked.add(uri); if (sameFile(uri)) files.add(uri); } return files; } /// Everything a render needs to light rows for one active file: the matching /// URIs, every module's distance, and a memo for the upward sections' headers. /// `undefined` when no module is declared there. function currentPath(model, files) { const current = model.modules.filter((module) => files.has(module.definition.uri)); if (current.length === 0) return undefined; const parentsOf = (id) => (parentIndex(model).get(id) || []).map((edge) => edge.module); return { files, above: walk(current, parentsOf), parentsOf, sections: new Map() }; } /// Breadth-first from every current module at once, so where two chains meet /// the nearer one is the one recorded. Linear in modules plus instances. function walk(start, next) { const distance = new Map(start.map((module) => [module.id, 0])); let frontier = start; for (let step = 1; frontier.length > 0; step += 1) { const reached = []; for (const module of frontier) { for (const neighbour of next(module.id)) { if (distance.has(neighbour.id)) continue; distance.set(neighbour.id, step); reached.push(neighbour); } } frontier = reached; } return distance; } /// The node's distance, or `undefined` when it is not lit. A forward row that /// cannot open (a leaf, a `recursive` repeat, an unresolved or ambiguous type) /// can only be current itself: what it would lead to is not under it on screen. /// An upward row is always its own module, so its module's distance is its own. function nodeDistance(node, path) { if (node.definition && path.files.has(node.definition.uri)) return 0; if (!node.moduleId || (!node.up && !node.hasChildren)) return undefined; return path.above.get(node.moduleId); } /// A header lights one step dimmer than the nearest module its section holds, /// shown or not — which is what tells the user a *closed* section holds the /// current file, or the current file's parents. function sectionDistance(section, path) { const nearest = section.kind === 'parents' ? closureDistance(section, path) : forwardDistance(section, path); return nearest === undefined ? undefined : nearest + 1; } /// A forward tree holds the current module exactly when one of its roots has /// a distance, and the current module is 0. function forwardDistance(section, path) { const roots = section.kind === 'others' ? section.roots.map(({ module }) => module) : (section.root ? [section.root] : []); return roots.some((root) => path.above.has(root.moduleId || root.id)) ? 0 : undefined; } /// The upward tree holds the DUT and everything above it: the nearest of /// those, walked once per (path, section) and remembered on the path. function closureDistance(section, path) { if (!section.root) return undefined; const key = section.root.moduleId; if (path.sections.has(key)) return path.sections.get(key); let nearest; const seen = new Set([key]); const stack = [section.root.moduleId]; while (stack.length > 0) { const id = stack.pop(); const distance = path.above.get(id); if (distance !== undefined && (nearest === undefined || distance < nearest)) nearest = distance; for (const parent of path.parentsOf(id)) { if (!seen.has(parent.id)) { seen.add(parent.id); stack.push(parent.id); } } } path.sections.set(key, nearest); return nearest; } /// Row id → lit level for the rows on screen. `nodes` is the id → node map the /// tree builders return; `list` is the section list, empty for the plain tree. /// Only lit rows are in the answer. function rowLevels(rows, nodes, list, path) { const levels = new Map(); if (!path) return levels; const headers = new Map(list.map((section) => [headerId(section), section])); for (const row of rows) { const node = nodes.get(row.id); let distance; if (node) distance = nodeDistance(node, path); else if (row.kind === 'section' && headers.has(row.id)) distance = sectionDistance(headers.get(row.id), path); const level = litLevel(distance); if (level !== undefined) levels.set(row.id, level); } return levels; } /// What changed between two answers of `rowLevels`, as `[id, level | null]` /// pairs: the only rows a new active file repaints. function levelChanges(before, after) { const changes = []; for (const [id, level] of after) { if (before.get(id) !== level) changes.push([id, level]); } for (const id of before.keys()) { if (!after.has(id)) changes.push([id, null]); } return changes; } module.exports = { BAR_COLOR, LIT_COLORS, cssVariableFor, currentPath, levelChanges, litLevel, matchingFiles, nodeDistance, rowLevels, };