Files
lucisv/editors/vscode/hierarchyView_test.js
Guoliang ChenandClaude Mythos 5.2 e9f5090632
ci / checks (push) Successful in 52s
ci / corpus (push) Successful in 15s
release / build-and-publish (push) Successful in 4m59s
Release 0.65.2: the Hierarchy pane shows where you are
TODO §71.6. The rows leading to the file being edited are lit in the
extension's Hierarchy pane: every instance of the module the open file
declares in a light, bright orange with a solid bar and a bold label, each
ancestor a shade dimmer with a narrower bar, down to a floor. A pure module
(hierarchyCurrent.js) computes each module's distance with one breadth-first
search up the instantiation graph; the webview repaints only the rows whose
level changed, with no server request and no tree rebuild. Six theme colours
under contributes.colors and the setting lucisv.hierarchy.highlightCurrent.
hierarchyPalette_test.js computes label contrast (>= 4.5:1 on every level,
six built-in themes) and neighbour distinctness from the shipped defaults.

Dev, QA (pass with findings, one fix round) and lead review are in
docs/features/hierarchy-current-path/README.md. Not seen in a running editor.

Version bump (Cargo.toml, Cargo.lock, editors/vscode/package.json and both
package-lock.json spots); notes in CHANGELOG.md (## 0.65.2) and
editors/vscode/CHANGELOG.md.

Gates: tools/gates.sh PASS on this tree (20261119-043114), node 12/0.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-19 04:32:43 -08:00

408 lines
18 KiB
JavaScript

const assert = require('assert/strict');
const Module = require('module');
const disposables = () => ({ dispose() {} });
const posted = [];
const copied = [];
const opened = [];
const shown = [];
const warned = [];
const informed = [];
const commands = new Map();
/** What `lucisv.hierarchy.duts` is set to, as a checked-in setting would be. */
let configuredDuts = [];
let picked;
let requests = 0;
let provider;
let receive;
let changed;
let configurationChanged;
let activeEditorChanged;
/** What `lucisv.hierarchy.highlightCurrent` is set to; unset is the default. */
let highlightCurrent;
class Range {
constructor(startLine, startCharacter, endLine, endCharacter) {
this.start = { line: startLine, character: startCharacter };
this.end = { line: endLine, character: endCharacter };
}
}
const vscode = {
Uri: {
parse(value) { return { value, fsPath: value.replace('file://', ''), toString: () => value }; },
joinPath(base, ...parts) {
const value = [base.value || base.toString(), ...parts].join('/');
return { value, toString: () => value };
},
},
Range,
ViewColumn: { Active: 1, Beside: 2 },
env: { clipboard: { async writeText(value) { copied.push(value); } } },
commands: {
registerCommand(id, handler) { commands.set(id, handler); return disposables(); },
},
workspace: {
asRelativePath(uri) { return uri.fsPath; },
getConfiguration(section) {
assert.equal(section, 'lucisv');
return {
get(key, fallback) {
if (key === 'hierarchy.duts') return configuredDuts;
if (key === 'hierarchy.highlightCurrent' && highlightCurrent !== undefined) return highlightCurrent;
return fallback;
},
};
},
onDidChangeConfiguration(handler) { configurationChanged = handler; return disposables(); },
async openTextDocument(uri) {
opened.push(uri.value);
return {
uri,
lineCount: 6,
lineAt(line) { return { text: `line ${line + 1}` }; },
};
},
},
window: {
activeTextEditor: undefined,
visibleTextEditors: [],
onDidChangeActiveTextEditor(handler) { activeEditorChanged = handler; return disposables(); },
registerWebviewViewProvider(_id, value) { provider = value; return disposables(); },
async showTextDocument(document, options) { shown.push({ document, options }); },
async showQuickPick(items) { picked = items; return items[0]; },
showWarningMessage(text) { warned.push(text); },
showInformationMessage(text) { informed.push(text); },
},
};
const originalLoad = Module._load;
Module._load = function load(request, parent, main) {
if (request === 'vscode') return vscode;
return originalLoad(request, parent, main);
};
const { HierarchyView } = require('./hierarchyView');
Module._load = originalLoad;
// The schema-2 wire: a file table and four-number ranges, no ids and no
// per-instance `definitions` — `hierarchyModel.js` rebuilds all three.
function place(file, line, start, end) {
return { file, range: [line, 0, line, end + 2], selectionRange: [line, start, line, end] };
}
const snapshot = {
schemaVersion: 2,
revision: 1,
complete: true,
files: ['file:///top.sv', 'file:///sub.sv'],
modules: [
{ moduleName: 'top', definition: place(0, 0, 7, 10), instances: [
{ instanceName: 'u', moduleName: 'sub', site: place(0, 1, 6, 9) },
] },
{ moduleName: 'sub', definition: place(1, 0, 7, 10), instances: [] },
],
};
const state = new Map();
const context = {
extensionUri: { value: 'file:///extension', toString: () => 'file:///extension' },
subscriptions: [],
workspaceState: {
get(key, fallback) { return state.has(key) ? state.get(key) : fallback; },
async update(key, value) { if (value === undefined) state.delete(key); else state.set(key, value); },
},
};
const client = {
async sendRequest(method) {
assert.equal(method, 'lucisv/moduleHierarchy');
requests += 1;
return snapshot;
},
onNotification(method, handler) {
assert.equal(method, 'lucisv/moduleHierarchyChanged');
changed = handler;
return disposables();
},
};
new HierarchyView(context, client);
assert(provider, 'the webview provider is registered');
const webview = {
cspSource: 'vscode-webview:',
options: undefined,
html: '',
asWebviewUri(uri) { return uri.toString(); },
postMessage(message) { posted.push(message); },
onDidReceiveMessage(handler) { receive = handler; return disposables(); },
};
provider.resolveWebviewView({
webview,
onDidDispose() { return disposables(); },
});
assert(webview.html.includes('Search module or instance'));
assert(webview.html.includes('Content-Security-Policy'));
async function run() {
await receive({ type: 'ready' });
const rendered = () => posted.findLast((message) => message.type === 'render');
assert(rendered(), 'ready requests and renders a snapshot');
// Owner, 2026-08-29: everything starts collapsed — one row, the root.
assert.equal(rendered().mode, 'tree');
assert.deepEqual(rendered().rows.map((row) => row.label), ['top']);
const root = rendered().rows[0];
assert.equal(root.hasChildren, true);
assert.equal(root.expanded, false);
// Opening is the user's own act, and it is the only thing that builds the
// level below.
await receive({ type: 'toggle', id: root.id });
assert.deepEqual(rendered().rows.map((row) => row.label), ['top', 'u (sub)']);
const instance = rendered().rows[1];
// A query swaps in a flat, capped result list; clearing it puts the tree
// back with the same row open and no new snapshot request.
const requestsBefore = requests;
await receive({ type: 'search', query: 'u' });
assert.equal(rendered().mode, 'search');
assert(rendered().rows.some((row) => row.label === 'u (sub)'));
assert.equal(rendered().truncated, 0);
await receive({ type: 'search', query: '' });
assert.equal(rendered().mode, 'tree');
assert.deepEqual(rendered().rows.map((row) => row.label), ['top', 'u (sub)']);
assert.equal(requests, requestsBefore, 'searching never asks the server again');
await receive({ type: 'context', action: 'copyPath', id: instance.id });
assert.deepEqual(copied, ['top.u']);
await receive({ type: 'context', action: 'instance', id: instance.id });
assert.equal(opened.at(-1), 'file:///top.sv');
assert.equal(shown.at(-1).options.viewColumn, 1);
await receive({ type: 'context', action: 'openRight', id: instance.id });
assert.equal(opened.at(-1), 'file:///sub.sv');
assert.equal(shown.at(-1).options.viewColumn, 2);
assert.equal(shown.at(-1).options.preview, false);
await receive({ type: 'context', action: 'bookmark', id: instance.id });
assert.equal(state.get('lucisv.hierarchy.bookmark'), 'top.u');
assert.equal(rendered().rows.find((row) => row.bookmarked).label, 'u (sub)');
// A search row navigates, but has no one path to copy or bookmark.
await receive({ type: 'search', query: 'u' });
const hit = rendered().rows.find((row) => row.kind === 'instance');
await receive({ type: 'context', action: 'definition', id: hit.id });
assert.equal(opened.at(-1), 'file:///sub.sv');
const copiedBefore = copied.length;
await receive({ type: 'context', action: 'copyPath', id: hit.id });
assert.equal(copied.length, copiedBefore, 'a flat hit has no full path to copy');
assert.equal(warned.at(-1), 'Clear the search to copy or bookmark an instance path.');
await receive({ type: 'search', query: '' });
// The source-line hover preview was removed at the owner's request
// (2026-08-29): a `hover` message is now one the view does not answer, and
// it must not resurrect the code that read a document behind the tree.
const before = posted.length;
await receive({ type: 'hover', id: instance.id });
assert.equal(posted.length, before, 'a hover posts nothing back');
assert(!posted.some((message) => message.type === 'preview'), 'no preview is ever posted');
assert(!webview.html.includes('id="preview"'), 'and the popover is gone from the markup');
assert.equal(typeof changed, 'function');
// ---- DUT sections (TODO §56) -------------------------------------------
// What the pane looks like with nothing starred, kept to compare against
// after an unstar: starring must be perfectly reversible.
const plainRows = rendered().rows.map((row) => ({ ...row }));
assert(plainRows.every((row) => row.contextValue === row.kind), 'nothing is a DUT yet');
assert.deepEqual(Object.keys(rendered().menus).sort(), [
'instance', 'instance.dut', 'instance.dut.locked', 'module', 'module.dut',
'module.dut.locked', 'notice', 'section',
], 'the menu table travels with every render');
// Starring from the pane's own menu, on an instance row: the module starred
// is the row's type.
await receive({ type: 'context', action: 'starDut', id: instance.id });
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['sub'], 'persisted in starred order');
assert.deepEqual(rendered().rows.map((row) => [row.label, row.depth]), [
['DUT: sub', 0],
['sub', 1],
['Parents of sub', 0],
['sub', 1],
['top', 2],
['Other modules', 0],
], 'three sections, the DUT ones opened one level, others closed');
assert.equal(rendered().rows.find((row) => row.label === 'top').detail, 'via u');
assert.equal(rendered().rows[1].contextValue, 'module.dut', 'the DUT row offers Unstar');
assert.equal(rendered().rows.at(-1).contextValue, 'section');
// A section is an ordinary collapsible row: closing it builds none of its
// tree, opening `others` shows what the exclusion left.
await receive({ type: 'toggle', id: 'sec:dut:sub' });
assert(!rendered().rows.some((row) => row.label === 'sub' && row.depth === 1
&& row.id.startsWith('dut:')), 'a closed section shows nothing under it');
await receive({ type: 'toggle', id: 'sec:dut:sub' });
await receive({ type: 'toggle', id: 'sec:others' });
assert.deepEqual(
rendered().rows.filter((row) => row.depth === 1 && !row.id.startsWith('dut:')
&& !row.id.startsWith('parents:')).map((row) => row.label),
[], 'top is above the DUT, so the others section is empty',
);
// A second DUT is two more sections, in starred order.
await receive({ type: 'context', action: 'starDut', id: rendered().rows[1].id });
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['sub']);
assert.equal(informed.at(-1), 'sub is already a DUT.');
await commands.get('lucisv.hierarchy.starDut')('top');
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['sub', 'top']);
assert.deepEqual(
rendered().rows.filter((row) => row.kind === 'section').map((row) => row.label),
['DUT: sub', 'Parents of sub', 'DUT: top', 'Parents of top', 'Other modules'],
'two DUTs are five sections',
);
// Unstarring from the menu, and clearing the rest from the title bar, put
// the plain pane back exactly as it was.
await receive({ type: 'context', action: 'unstarDut', id: rendered().rows[1].id });
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['top']);
await commands.get('lucisv.hierarchy.clearDuts')();
assert.equal(state.get('lucisv.hierarchy.duts'), undefined);
assert.deepEqual(rendered().rows, plainRows, 'the pane is byte-identical to before the star');
// The command palette route picks from the index. Starring the same module
// again after a clear must open its sections again, not remember that it
// once seeded them.
await commands.get('lucisv.hierarchy.starDut')();
assert.deepEqual(picked, ['sub', 'top'], 'every module name, sorted');
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['sub']);
assert.deepEqual(rendered().rows.map((row) => row.label), [
'DUT: sub', 'sub', 'Parents of sub', 'sub', 'top', 'Other modules',
], 'the sections are open again');
await commands.get('lucisv.hierarchy.unstarDut')();
assert.deepEqual(picked, ['sub'], 'only what this workspace starred');
assert.equal(state.get('lucisv.hierarchy.duts'), undefined);
// The setting is the second way in: its DUTs come first, are locked, and the
// pane refuses to unstar them.
configuredDuts = ['top', ''];
await commands.get('lucisv.hierarchy.starDut')('sub');
configurationChanged({ affectsConfiguration: (key) => key === 'lucisv.hierarchy.duts' });
assert.deepEqual(
rendered().rows.filter((row) => row.kind === 'section').map((row) => row.label),
['DUT: top', 'Parents of top', 'DUT: sub', 'Parents of sub', 'Other modules'],
'the setting first, then the star; the blank entry is dropped',
);
const locked = rendered().rows.find((row) => row.label === 'top' && row.depth === 1);
assert.equal(locked.contextValue, 'module.dut.locked');
assert.match(locked.title, /lucisv\.hierarchy\.duts/);
await receive({ type: 'context', action: 'unstarDut', id: locked.id });
assert.match(warned.at(-1), /^top comes from the lucisv\.hierarchy\.duts setting/);
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['sub'], 'and nothing changed');
await commands.get('lucisv.hierarchy.clearDuts')();
assert.match(informed.at(-1), /come from the lucisv\.hierarchy\.duts setting/);
assert.deepEqual(
rendered().rows.filter((row) => row.kind === 'section').map((row) => row.label),
['DUT: top', 'Parents of top', 'Other modules'],
'clearing takes the stars, not the setting',
);
// A starred name the snapshot does not hold keeps its sections and says so.
configuredDuts = [];
await commands.get('lucisv.hierarchy.starDut')('renamed_away');
assert.deepEqual(rendered().rows.map((row) => row.label), [
'DUT: renamed_away',
'renamed_away is not in the index',
'Parents of renamed_away',
'renamed_away is not in the index',
'Other modules',
]);
await commands.get('lucisv.hierarchy.clearDuts')();
await theActiveFileIsLitWithoutARefetch(rendered);
await persistenceSurvivesAReload();
console.log('hierarchy view: OK — collapsed rows, lazy opening, search list, navigation, menu, bookmark, no preview, DUT sections (star, unstar, clear, persistence, the setting lock), and the active file lit (relit in place, no refetch, the setting)');
}
function editorFor(value, scheme = 'file') {
return { document: { uri: { scheme, toString: () => value } } };
}
/// TODO §71.6: the rows leading to the active file are lit, and a new active
/// file repaints only the rows whose level changed — no snapshot request, no
/// render, ids untouched.
async function theActiveFileIsLitWithoutARefetch(rendered) {
// The bookmark from earlier paints its own background; clear it so these
// rows show only the lighting.
await commands.get('lucisv.hierarchy.clearBookmark')();
const plain = rendered();
assert(plain.rows.every((row) => !('lit' in row)), 'with no active file, no row carries `lit`');
const ids = plain.rows.map((row) => row.id);
assert.deepEqual(plain.rows.map((row) => row.label), ['top', 'u (sub)']);
const requestsBefore = requests;
const rendersBefore = posted.filter((message) => message.type === 'render').length;
vscode.window.visibleTextEditors = [editorFor('file:///sub.sv')];
activeEditorChanged(editorFor('file:///sub.sv'));
assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], 1], [ids[1], 0]] });
activeEditorChanged(editorFor('file:///top.sv'));
assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], 0], [ids[1], null]] },
'only the two rows whose level moved');
const before = posted.length;
activeEditorChanged(editorFor('file:///top.sv'));
activeEditorChanged(undefined);
activeEditorChanged(editorFor('output:channel', 'output'));
assert.equal(posted.length, before, 'the same file, focus leaving for a panel, or the Output panel repaint nothing');
assert.equal(requests, requestsBefore, 'relighting never asks the server');
assert.equal(posted.filter((message) => message.type === 'render').length, rendersBefore, 'nor re-renders');
// A render — opening a row, a refresh — carries the level on the lit rows.
await receive({ type: 'toggle', id: ids[0] });
await receive({ type: 'toggle', id: ids[0] });
assert.deepEqual(rendered().rows.map((row) => [row.label, row.lit]), [['top', 0], ['u (sub)', undefined]]);
assert.deepEqual(rendered().rows.map((row) => row.id), ids, 'ids are untouched, so expansion survives');
// A file no module is declared in lights nothing.
activeEditorChanged(editorFor('file:///notes.txt'));
assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], null]] });
// The setting turns it off, and back on.
activeEditorChanged(editorFor('file:///sub.sv'));
highlightCurrent = false;
configurationChanged({ affectsConfiguration: (key) => key === 'lucisv.hierarchy.highlightCurrent' });
assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], null], [ids[1], null]] });
await receive({ type: 'toggle', id: ids[0] });
await receive({ type: 'toggle', id: ids[0] });
assert(rendered().rows.every((row) => !('lit' in row)), 'off means no row is lit on a render either');
highlightCurrent = undefined;
configurationChanged({ affectsConfiguration: (key) => key === 'lucisv.hierarchy.highlightCurrent' });
assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], 1], [ids[1], 0]] });
// Closing every editor clears it.
vscode.window.visibleTextEditors = [];
activeEditorChanged(undefined);
assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], null], [ids[1], null]] });
}
/// A fresh view over the same `workspaceState`: what was starred is starred
/// again, in the order it was starred, and the sections are open on arrival.
async function persistenceSurvivesAReload() {
state.set('lucisv.hierarchy.duts', ['sub', 'nope', 7, '']);
new HierarchyView(context, client);
const reposted = [];
provider.resolveWebviewView({
webview: { ...webview, postMessage(message) { reposted.push(message); } },
onDidDispose() { return disposables(); },
});
await receive({ type: 'ready' });
const render = reposted.findLast((message) => message.type === 'render');
assert.deepEqual(render.rows.map((row) => row.label), [
'DUT: sub', 'sub', 'Parents of sub', 'sub', 'top',
'DUT: nope', 'nope is not in the index', 'Parents of nope', 'nope is not in the index',
'Other modules',
], 'both starred names come back, the number 7 and the blank do not');
}
run().catch((error) => {
console.error(error);
process.exitCode = 1;
});