Files
lucisv/editors/vscode/media/hierarchy.js
T
lucioandClaude Fable 5 651e58bfb3 The design-hierarchy sidebar: codex's feature, integrated
An instance-oriented hierarchy view in the Activity Bar —
lucisv/moduleHierarchy snapshots (schema 2: uri table, flat ranges,
derived ids; 11.6 MB -> 2.3 MB on opentitan) plus
lucisv/moduleHierarchyChanged invalidation, gated on the extension's
initializationOptions. Format 5 carries decl spans, instance
occurrences and client positions for file-scope declarations
(+1.7-1.9 % store), so a snapshot reads no files at all: handler
145-167 -> 17-20 ms settled, a save with the sidebar open 366 ->
141 ms. complete is !scanning && !must_wait, pinned. Owner UI rounds:
no hover preview, all levels collapsed by default with stable ids
across refreshes, and search over a resident flat model (freeze
diagnosed: materialized expanded tree x undebounced double
fuzzyScore x whole-tree clone per keystroke) — prebuilt lowercased
index, bitmask prefilter, top-K counting, 200-row cap with an honest
remainder, single-digit ms over a 120k-entry fixture.
LineIndex::utf16_position unifies the index's and the wire's position
math under a cross-crate agreement test.

Implementation: codex. Integration fixes + owner items: Opus dev.
QA: lead (measurement pass) + Sonnet (SHIP, independent oracles).

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-29 03:39:06 -07:00

193 lines
7.5 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: '' };
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';
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';
icon.textContent = 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) => {
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]; }
function openMenu(event, row) {
event.preventDefault();
if (row.kind !== 'instance') return;
menuNode = row;
menu.hidden = false;
menu.style.left = `${Math.min(event.clientX, innerWidth - 190)}px`;
menu.style.top = `${Math.min(event.clientY, innerHeight - 180)}px`;
menu.querySelector('[data-action="bookmark"]').textContent = row.bookmarked
? 'Remove Highlight / Bookmark' : 'Highlight / Bookmark';
menu.querySelector('button').focus();
}
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')];
const at = items.indexOf(document.activeElement);
if (event.key === 'Escape') closeMenu();
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' });