Files
lucisv/editors/vscode/media/hierarchy.js
T
Guoliang ChenandClaude Mythos 5.2 71d13b5fbe
ci / checks (push) Successful in 1m0s
ci / corpus (push) Successful in 18s
ci / linux (push) Successful in 6m4s
release / build-and-publish (push) Successful in 5m31s
Release 0.66.2: every source file names its author
Every tracked file that can carry a comment (880 of them) now opens with
"Author: Guoliang Chen, a Senior Digital Design Engineer" and closes with
"Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love,
luciochen.com", in the file's own comment syntax. No behaviour change: the
diff outside the version bump and changelogs is comment lines only.

- tools/stamp_author.py: writes the lines; idempotent, `--check` lists
  what is missing. Headers go under a shebang and under Markdown front
  matter.
- Left alone: test fixtures, cases and snapshots (byte-exact inputs), JSON
  and JSONL, logs, images, lockfiles, docs/site/dist (regenerated) and the
  LICENSE files (already carry the copyright line).
- tools/linux_check.sh: --help reads its header from line 3, now that the
  author line sits under the shebang.
- tools/gates.sh green on this tree: 5 545 tests, corpus 814/6 as before.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-24 12:11:09 -08:00

255 lines
10 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.
// Author: Guoliang Chen, a Senior Digital Design Engineer
// 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;
/** The tree's row buttons by id, so a new active file repaints in place. */
let treeButtons = new Map();
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 === 'lit') {
relight(data.changes);
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();
const buttons = new Map();
for (const row of payload.rows) {
const node = renderRow(row, searching);
buttons.set(row.id, node.firstChild);
fragment.appendChild(node);
}
container.replaceChildren(fragment);
// A search list leaves the tree's rows in the page, so they keep relighting.
if (!searching) treeButtons = buttons;
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');
setLit(button, row.lit);
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;
}
// The path to the active file: level 0 is the row whose module that file
// declares, each step up one shade dimmer, down to a floor (hierarchy.css).
// Only the rows whose level changed are touched; nothing is rebuilt.
function relight(changes) {
if (!Array.isArray(changes)) return;
for (const change of changes) {
// The host only sends pairs; anything else is ignored, never thrown on.
if (!Array.isArray(change)) continue;
const [id, level] = change;
const button = treeButtons.get(id);
if (button) setLit(button, level);
}
}
function setLit(button, level) {
if (Number.isInteger(level) && level >= 0) button.dataset.lit = String(level);
else delete button.dataset.lit;
}
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' });
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com