Files
lucisv/editors/vscode/media/hierarchy.js
T
lucioandClaude Fable 5.1 449b874798
ci / checks (push) Successful in 1m7s
ci / corpus (push) Successful in 31s
release / build-and-publish (push) Successful in 6m20s
DUT-centred hierarchy view: star a module as DUT, its children and its parents in their own sections, everything else re-rooted
TODO §56 (docs/features/dut-hierarchy), the owner's request of 2026-09-06.

Client-only, over the flat `lucisv/moduleHierarchy` snapshot the server
already sends. `hierarchySections.js`: per starred DUT, in starred
order, the DUT's forward subtree (the DUT at the top) and an upward tree
from the DUT through every module that instantiates it (labelled `via
<instance>`, cycles cut with one `recursive` row), then one section for
every module in no DUT's subtree or ancestor closure, re-rooted where
its only parents are DUT ancestors (the review ruling — a monitor beside
the DUT under the same bench stays on screen); a module both below one
DUT and above another appears in both DUT sections; a starred name the
index lacks keeps its sections with a "not in the index" row. Star and
unstar from the webview row menu (the pane is a WebviewView, so the
menu is the renderer's, driven by a `menuTable()` per render), the
`lucisv.hierarchy.{starDut,unstarDut,clearDuts}` commands and title
actions, persistence in `workspaceState` in starred order, and the
`lucisv.hierarchy.duts` setting whose entries show locked. With nothing
starred the pane is byte-identical to before (pinned).

opentitan `hw/ip` (847 modules): `aes` → 3 sections with `tb` in its
parents and 5 modules under `aes_tb` re-rooted into others; `aes` +
`hmac` → 5 sections; every module reachable in both configurations; QA's
200-trial fuzz found no overlap and no gap. `extract_test.js` added to
`.vscodeignore`.

(Committed after the 0.48.0 version commit: the release chain broke at a
test-output grep and cut the tag one commit early; the tag is moved here.)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-09-06 00:04:05 -05:00

218 lines
8.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The hierarchy view's renderer. It draws exactly the rows the extension host
// sends and owns no model of its own: the host holds the flat snapshot, opens
// one level at a time, and answers a query from a prebuilt index. What used to
// arrive here was every path of the whole design, rebuilt and re-sent on every
// keystroke — the freeze the owner reported on 2026-08-29.
const vscode = acquireVsCodeApi();
const search = document.getElementById('search');
const tree = document.getElementById('tree');
const results = document.getElementById('results');
const more = document.getElementById('more');
const status = document.getElementById('status');
const menu = document.getElementById('menu');
/** Long enough that a fast typist sends one query, short enough to feel live. */
const SEARCH_DEBOUNCE_MS = 60;
let payload = { mode: 'tree', rows: [], truncated: 0, query: '', menus: {} };
let menuNode;
let searchTimer;
search.addEventListener('input', () => {
clearTimeout(searchTimer);
searchTimer = setTimeout(
() => vscode.postMessage({ type: 'search', query: search.value }),
SEARCH_DEBOUNCE_MS,
);
});
search.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && search.value) {
search.value = '';
clearTimeout(searchTimer);
vscode.postMessage({ type: 'search', query: '' });
}
if (event.key === 'ArrowDown') firstVisibleRow()?.focus();
});
window.addEventListener('message', ({ data }) => {
if (!data || typeof data.type !== 'string') return;
if (data.type === 'status') {
status.textContent = data.text;
status.classList.toggle('error', Boolean(data.error));
status.hidden = false;
if (data.error) {
tree.replaceChildren();
results.replaceChildren();
}
return;
}
if (data.type !== 'render') return;
payload = data;
if (search.value !== data.query) search.value = data.query;
render();
});
// The tree and the result list are two containers, and a query swaps which one
// is shown. Searching therefore never tears the tree down, and clearing the
// search puts back exactly the rows that were open — no re-request, no rebuild.
function render() {
const searching = payload.mode === 'search';
const container = searching ? results : tree;
const fragment = document.createDocumentFragment();
for (const row of payload.rows) fragment.appendChild(renderRow(row, searching));
container.replaceChildren(fragment);
tree.hidden = searching;
results.hidden = !searching;
more.textContent = payload.truncated > 0
? `${payload.truncated} more match${payload.truncated === 1 ? '' : 'es'} — narrow the search.`
: '';
more.hidden = payload.truncated === 0;
const message = payload.empty
? 'No indexed modules.'
: (searching && payload.rows.length === 0
? 'No module or instance name matches.'
: (!payload.complete ? 'Indexing… showing a partial hierarchy.' : ''));
status.textContent = message;
status.hidden = message === '';
status.classList.remove('error');
}
function renderRow(row, searching) {
const wrapper = document.createElement('div');
wrapper.className = 'node';
wrapper.dataset.id = row.id;
const button = document.createElement('button');
button.className = 'row';
button.type = 'button';
if (row.kind === 'section') button.classList.add('section');
if (row.title) button.title = row.title;
button.setAttribute('role', searching ? 'option' : 'treeitem');
button.setAttribute('aria-level', String(row.depth + 1));
button.style.setProperty('--depth', row.depth);
if (row.bookmarked) button.classList.add('bookmarked');
if (row.warning) button.classList.add('warning');
if (searching) button.classList.add('match');
if (row.hasChildren) button.setAttribute('aria-expanded', String(row.expanded));
const twisty = document.createElement('span');
twisty.className = 'twisty';
twisty.textContent = row.hasChildren ? (row.expanded ? '⌄' : '›') : '';
twisty.setAttribute('aria-hidden', 'true');
const icon = document.createElement('span');
icon.className = 'icon';
// A section header and a "not in the index" notice bring their own glyph;
// everything else is a module box or an instance diamond.
icon.textContent = row.icon
|| (row.kind === 'module' ? '▣' : (row.detail === 'recursive' ? '↻' : '◇'));
icon.setAttribute('aria-hidden', 'true');
const label = document.createElement('span');
label.className = 'label';
label.textContent = row.label;
const detail = document.createElement('span');
detail.className = 'detail';
detail.textContent = row.detail || '';
button.append(twisty, icon, label, detail);
button.addEventListener('click', (event) => {
// A section header has nothing to navigate to, so the whole row opens it.
if (row.kind === 'section') {
vscode.postMessage({ type: 'toggle', id: row.id });
} else if (row.hasChildren && event.detail === 1 && event.offsetX < 28 + row.depth * 12) {
vscode.postMessage({ type: 'toggle', id: row.id });
} else {
vscode.postMessage({ type: 'activate', id: row.id });
}
});
button.addEventListener('dblclick', () => {
if (row.hasChildren) vscode.postMessage({ type: 'toggle', id: row.id });
});
button.addEventListener('contextmenu', (event) => openMenu(event, row));
button.addEventListener('keydown', (event) => key(event, row));
wrapper.appendChild(button);
return wrapper;
}
function key(event, row) {
if ((event.key === 'ContextMenu') || (event.shiftKey && event.key === 'F10')) {
event.preventDefault();
const rect = event.currentTarget.getBoundingClientRect();
openMenu({ preventDefault() {}, clientX: rect.left + 16, clientY: rect.bottom }, row);
return;
}
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
vscode.postMessage({ type: 'activate', id: row.id });
} else if (event.key === 'ArrowRight' && row.hasChildren) {
event.preventDefault();
if (!row.expanded) vscode.postMessage({ type: 'toggle', id: row.id });
else nextRow(event.currentTarget)?.focus();
} else if (event.key === 'ArrowLeft') {
event.preventDefault();
if (row.hasChildren && row.expanded) vscode.postMessage({ type: 'toggle', id: row.id });
else previousRow(event.currentTarget)?.focus();
} else if (event.key === 'ArrowDown') {
event.preventDefault();
nextRow(event.currentTarget)?.focus();
} else if (event.key === 'ArrowUp') {
event.preventDefault();
previousRow(event.currentTarget)?.focus();
}
}
function shown() { return payload.mode === 'search' ? results : tree; }
function rows() { return [...shown().querySelectorAll('.row')]; }
function firstVisibleRow() { return rows()[0]; }
function nextRow(row) { const all = rows(); return all[all.indexOf(row) + 1]; }
function previousRow(row) { const all = rows(); return all[all.indexOf(row) - 1]; }
// Which items a row offers is the host's decision, sent once per render as a
// table keyed by `contextValue`: an instance row has the five it always had, a
// module row has "Star as DUT", a starred one "Unstar DUT", and one the
// settings name a single disabled item that says where it came from.
function openMenu(event, row) {
event.preventDefault();
const items = (payload.menus && payload.menus[row.contextValue]) || [];
if (items.length === 0) return;
menuNode = row;
menu.replaceChildren(...items.map((item) => menuItem(item, row)));
menu.hidden = false;
menu.style.left = `${Math.min(event.clientX, innerWidth - 190)}px`;
menu.style.top = `${Math.min(event.clientY, innerHeight - 180 - items.length * 4)}px`;
const first = menu.querySelector('button:not([disabled])');
if (first) first.focus();
}
function menuItem(item, row) {
const button = document.createElement('button');
button.setAttribute('role', 'menuitem');
button.dataset.action = item.action;
button.textContent = item.action === 'bookmark' && row.bookmarked
? 'Remove Highlight / Bookmark' : item.label;
if (item.disabled) button.disabled = true;
if (item.title) button.title = item.title;
return button;
}
menu.addEventListener('click', (event) => {
const action = event.target && event.target.dataset.action;
if (action && menuNode) vscode.postMessage({ type: 'context', action, id: menuNode.id });
closeMenu();
});
menu.addEventListener('keydown', (event) => {
const items = [...menu.querySelectorAll('button:not([disabled])')];
const at = items.indexOf(document.activeElement);
if (event.key === 'Escape') closeMenu();
if (items.length === 0) return;
if (event.key === 'ArrowDown') items[(at + 1) % items.length].focus();
if (event.key === 'ArrowUp') items[(at - 1 + items.length) % items.length].focus();
});
document.addEventListener('mousedown', (event) => {
if (!menu.contains(event.target)) closeMenu();
});
function closeMenu() { menu.hidden = true; menuNode = undefined; }
vscode.postMessage({ type: 'ready' });