Add a 3D view of every diagram; organise source by job; 0.6.0

3D view: a "3D" menu beside Export switches the diagram to a three.js
scene built from the live Archify SVG. Nodes become blocks topped with
the node as Archify draws it, relationships become lit pipes along
Archify's routes, and the canvas, frames, lanes and legend form the
floor. Clicks and hover go to the SVG nodes, so focus, the Semantic
Passport and the intent trace work as in 2D; Archify's own animations
(trace, node pulse, passport/path/lens flows) are mirrored live. The
menu also offers flat or stacked levels, reset view and a top view;
Export gains "3D view" (PNG). Trackpad scrolling pans, pinch and the
mouse wheel zoom, and the zoom buttons work beyond the 2D limits.
The scene draws only when something moves. New setting
archify.defaultView.

Source: src/ is split by job (commands, views, webview, archify,
sources, language, util, viewer3d); behaviour unchanged.
This commit is contained in:
2026-10-11 00:54:00 +03:00
parent 20786f4b5a
commit 97b3b5f685
40 changed files with 2553 additions and 353 deletions
+21 -243
View File
@@ -1,254 +1,32 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import * as vscode from 'vscode';
import { BRAND_COMPLETION_SELECTOR, BrandCompletionProvider } from './brandCompletion';
import { diagramBaseName, isArchifyFile, isArchifyHtml, isDiagramFileName, readDiagramSource } from './detect';
import { DiagnosticsController } from './diagnostics';
import { HTML_VIEW_TYPE, HtmlViewerProvider } from './htmlViewer';
import { DIAGRAM_EDITOR_VIEW_TYPE, PreviewManager } from './preview';
import { isEvidenceError, renderDiagram, renderToFile } from './renderer';
import { clearGitRootCache, evidenceMode, log, readText, runtimeFor } from './runtime';
import { diagramFromTemplate, diagramName, invalidNameReason, TEMPLATES } from './templates';
const JSON_LANGUAGES = new Set(['json', 'jsonc']);
function isHtmlUri(uri: vscode.Uri): boolean {
return /\.html?$/i.test(uri.path);
}
import { CommandContext } from './commands/context';
import { registerContextKeys } from './commands/contextKeys';
import { newDiagramCommands } from './commands/newDiagram';
import { renderCommands } from './commands/renderCommands';
import { viewCommands } from './commands/viewCommands';
import { BRAND_COMPLETION_SELECTOR, BrandCompletionProvider } from './language/brandCompletion';
import { DiagnosticsController } from './language/diagnostics';
import { log } from './util/log';
import { HtmlViewerProvider } from './views/htmlViewer';
import { DiagramViewManager } from './views/diagramView';
export function activate(context: vscode.ExtensionContext): void {
const previews = new PreviewManager(context);
const htmlViewer = new HtmlViewerProvider();
const ctx: CommandContext = {
extension: context,
views: new DiagramViewManager(context),
htmlViewer: new HtmlViewerProvider(context.extensionUri),
};
context.subscriptions.push(
previews,
htmlViewer,
ctx.views,
ctx.htmlViewer,
new DiagnosticsController(context),
log(),
vscode.languages.registerCompletionItemProvider(BRAND_COMPLETION_SELECTOR, new BrandCompletionProvider(context.extensionPath), ' ', '"'),
...viewCommands(ctx),
...newDiagramCommands(ctx),
...renderCommands(ctx),
);
/** The file a command applies to: explicit argument, focused preview or viewer, then the text editor. */
function targetUri(arg?: unknown): vscode.Uri | undefined {
if (arg instanceof vscode.Uri) return arg;
return previews.activePreview?.uri ?? htmlViewer.activeUri ?? vscode.window.activeTextEditor?.document.uri;
}
function sourceUri(arg?: unknown): vscode.Uri | undefined {
const uri = targetUri(arg);
if (!uri) vscode.window.showWarningMessage('Archify: open a .archify diagram first.');
return uri;
}
// Context keys for editor title buttons and keybindings.
let contextTimer: NodeJS.Timeout | undefined;
const updateContext = () => {
const doc = vscode.window.activeTextEditor?.document;
const diagram = Boolean(
doc &&
(isArchifyFile(doc.fileName) ||
(JSON_LANGUAGES.has(doc.languageId) && (isDiagramFileName(doc.fileName) || readDiagramSource(doc.getText(), doc.fileName)))),
);
const html = Boolean(doc && doc.languageId === 'html' && isArchifyHtml(doc.getText()));
void vscode.commands.executeCommand('setContext', 'archify.isDiagramSource', diagram);
void vscode.commands.executeCommand('setContext', 'archify.isArchifyHtml', html);
};
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(updateContext),
vscode.workspace.onDidChangeTextDocument((event) => {
if (event.document !== vscode.window.activeTextEditor?.document) return;
clearTimeout(contextTimer);
contextTimer = setTimeout(updateContext, 300);
}),
vscode.workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration('archify.repoRoot')) clearGitRootCache();
}),
{ dispose: () => clearTimeout(contextTimer) },
);
updateContext();
const openHtmlViewer = (uri: vscode.Uri, viewColumn?: vscode.ViewColumn) =>
vscode.commands.executeCommand('vscode.openWith', uri, HTML_VIEW_TYPE, viewColumn);
const exists = async (uri: vscode.Uri) => {
try {
await vscode.workspace.fs.stat(uri);
return true;
} catch {
return false;
}
};
/** Folder for a new diagram: the clicked folder (or a file's folder), else a workspace folder. */
async function newDiagramFolder(arg?: unknown): Promise<vscode.Uri | undefined> {
if (arg instanceof vscode.Uri) {
const stat = await vscode.workspace.fs.stat(arg);
return stat.type & vscode.FileType.Directory ? arg : vscode.Uri.joinPath(arg, '..');
}
const folders = vscode.workspace.workspaceFolders ?? [];
if (folders.length <= 1) return folders[0]?.uri;
return (await vscode.window.showWorkspaceFolderPick({ placeHolder: 'Folder for the new diagram' }))?.uri;
}
context.subscriptions.push(
vscode.commands.registerCommand('archify.showPreview', (arg?: unknown) => {
const uri = sourceUri(arg);
if (!uri) return;
if (isHtmlUri(uri)) return openHtmlViewer(uri);
if (isArchifyFile(uri.path)) return vscode.commands.executeCommand('vscode.openWith', uri, DIAGRAM_EDITOR_VIEW_TYPE);
previews.show(uri, vscode.window.activeTextEditor?.viewColumn ?? vscode.ViewColumn.Active);
}),
vscode.commands.registerCommand('archify.showPreviewToSide', (arg?: unknown) => {
const uri = sourceUri(arg);
if (!uri) return;
if (isHtmlUri(uri)) return openHtmlViewer(uri, vscode.ViewColumn.Beside);
if (isArchifyFile(uri.path)) {
return vscode.commands.executeCommand('vscode.openWith', uri, DIAGRAM_EDITOR_VIEW_TYPE, {
viewColumn: vscode.ViewColumn.Beside,
preserveFocus: true,
});
}
previews.show(uri, vscode.ViewColumn.Beside, true);
}),
vscode.commands.registerCommand('archify.openHtmlViewer', (arg?: unknown) => {
const uri = sourceUri(arg);
if (uri) return openHtmlViewer(uri);
}),
vscode.commands.registerCommand('archify.newDiagram', async (arg?: unknown) => {
const template = await vscode.window.showQuickPick(
TEMPLATES.map((item) => ({ label: item.label, description: item.description, template: item })),
{ title: 'Archify | New Diagram', placeHolder: 'Kind of diagram' },
);
if (!template) return;
const { file, defaultName } = template.template;
const folder = await newDiagramFolder(arg);
let target: vscode.Uri | undefined;
if (folder) {
const name = await vscode.window.showInputBox({
title: 'Archify | New Diagram',
prompt: `File name in ${vscode.workspace.asRelativePath(folder, true)}`,
value: defaultName,
validateInput: async (value) =>
invalidNameReason(value) ??
((await exists(vscode.Uri.joinPath(folder, `${diagramName(value)}.archify`))) ? `${diagramName(value)}.archify already exists.` : undefined),
});
if (name === undefined) return;
target = vscode.Uri.joinPath(folder, `${diagramName(name)}.archify`);
} else {
// No workspace: let the user choose where to save.
target = await vscode.window.showSaveDialog({
title: 'Archify | New Diagram',
filters: { 'Archify Diagram': ['archify'] },
saveLabel: 'Create',
});
if (!target) return;
if (!isArchifyFile(target.path)) target = target.with({ path: `${target.path}.archify` });
}
const templateText = await readText(vscode.Uri.joinPath(context.extensionUri, 'templates', file));
const text = diagramFromTemplate(templateText, diagramBaseName(target.path));
await vscode.workspace.fs.writeFile(target, Buffer.from(text, 'utf8'));
await vscode.commands.executeCommand('vscode.openWith', target, DIAGRAM_EDITOR_VIEW_TYPE);
}),
vscode.commands.registerCommand('archify.refreshPreview', () => previews.activePreview?.update(true)),
vscode.commands.registerCommand('archify.showSource', async () => {
const preview = previews.activePreview;
if (preview?.isEditor) {
// The diagram is the editor; open the YAML text beside it.
await vscode.commands.executeCommand('vscode.openWith', preview.uri, 'default', vscode.ViewColumn.Beside);
return;
}
if (preview) {
const column = preview.panel.viewColumn === vscode.ViewColumn.One ? vscode.ViewColumn.Two : vscode.ViewColumn.One;
await vscode.window.showTextDocument(preview.uri, { viewColumn: column });
return;
}
const html = htmlViewer.activeUri;
if (html) await vscode.commands.executeCommand('vscode.openWith', html, 'default');
}),
vscode.commands.registerCommand('archify.renderToHtml', async (arg?: unknown) => {
const uri = sourceUri(arg);
if (!uri) return;
const text = await readText(uri);
const source = readDiagramSource(text, uri.path);
if (!source) {
vscode.window.showWarningMessage(`Archify: ${path.basename(uri.path)} has no "diagram_type" (architecture, workflow, sequence, dataflow or lifecycle).`);
return;
}
const meta = (source.diagram.meta ?? {}) as { output?: unknown };
const authored = typeof meta.output === 'string' && !/[\\/]/.test(meta.output) ? meta.output : undefined;
const fileName = authored ?? `${diagramBaseName(uri.path)}.html`;
const defaultUri = uri.scheme === 'file' ? vscode.Uri.joinPath(uri, '..', fileName) : undefined;
const target = await vscode.window.showSaveDialog({
defaultUri,
filters: { HTML: ['html'] },
saveLabel: 'Render',
});
if (!target) return;
const runtime = await runtimeFor(context, uri);
let result = await vscode.window.withProgress(
{ location: vscode.ProgressLocation.Notification, title: 'Archify: rendering…' },
() => renderToFile(runtime, source, target.fsPath),
);
if (!result.ok && isEvidenceError(result.error)) {
const choice = await vscode.window.showWarningMessage(
'Archify could not verify the diagram\'s source evidence against a local checkout.',
{ modal: true, detail: result.error },
'Render Without Source Links',
);
if (choice) result = await renderToFile(runtime, source, target.fsPath, true);
else return;
}
if (!result.ok) {
log().error(result.error ?? 'render failed');
vscode.window.showErrorMessage('Archify: render failed.', { modal: true, detail: result.error });
return;
}
const choice = await vscode.window.showInformationMessage(
`Rendered ${path.basename(target.fsPath)}`,
'Open in Viewer',
'Open in Browser',
);
if (choice === 'Open in Viewer') await openHtmlViewer(target);
if (choice === 'Open in Browser') await vscode.env.openExternal(target);
}),
vscode.commands.registerCommand('archify.openInBrowser', async (arg?: unknown) => {
const uri = sourceUri(arg);
if (!uri) return;
if (isHtmlUri(uri)) {
await vscode.env.openExternal(uri);
return;
}
const text = await readText(uri);
const source = readDiagramSource(text, uri.path);
if (!source) {
vscode.window.showWarningMessage(`Archify: ${path.basename(uri.path)} has no "diagram_type" (architecture, workflow, sequence, dataflow or lifecycle).`);
return;
}
const runtime = await runtimeFor(context, uri);
const result = await vscode.window.withProgress(
{ location: vscode.ProgressLocation.Notification, title: 'Archify: rendering…' },
() => renderDiagram(runtime, source, evidenceMode(uri)),
);
if (!result.ok || !result.html) {
vscode.window.showErrorMessage('Archify: render failed.', { modal: true, detail: result.error });
return;
}
const dir = path.join(context.globalStorageUri.fsPath, 'browser');
await fs.mkdir(dir, { recursive: true });
const file = path.join(dir, `${diagramBaseName(uri.path)}.html`);
await fs.writeFile(file, result.html, 'utf8');
await vscode.env.openExternal(vscode.Uri.file(file));
}),
);
registerContextKeys(context);
}
export function deactivate(): void {}