Models: main.archify, registries, refs, links and the model page; 0.7.0

A folder with main.archify at its top is a model, a set of diagrams
that belong together. Opening main.archify shows the model page: the
model's name and description, its own overview diagram when the file
has one, the diagrams grouped by folder (click to open), and the
registry with where each element is used. The page is built by
traversing the folder tree and refreshes when files change.

*.archify-registry files define shared elements once; diagrams draw
them with `ref: <id>` plus position and may override label, sublabel
and tag. References are resolved before Archify runs, so Archify is
unchanged. Hover, go-to-definition and completion on ref; unknown
names, forbidden overrides, duplicate ids and broken registries in
Problems; registry edits re-render open diagrams. `diagram:` links on
elements and components open the linked diagram on double-click.

New command Archify | Open Model, a Model template for New Diagram…,
editor schemas for the new keys and for registries, docs/model.md and
examples/online-shop. Single diagrams behave exactly as before.
This commit is contained in:
root-at-skic committed 2026-10-19 11:37:20 +03:00
1 parent 701ffc7607
commit 7a2b661afe
50 files changed
+2154 -47

No files matched your search

+56 -9
View File
@@ -2,8 +2,12 @@ import { parse, ParseError } from 'jsonc-parser';
import * as vscode from 'vscode';
import { ArchifyDiagnostic, isEvidenceError, validate } from '../archify/cli';
import { evidenceMode, runtimeFor } from '../archify/runtime';
import { isArchifyFile, readDiagramSource } from '../sources/detect';
import { ModelProblem, isRegistryFile, parseRegistry } from '../model/format';
import { prepareDiagram } from '../model/prepare';
import { ModelService } from '../model/service';
import { isArchifyFile } from '../sources/detect';
import { log } from '../util/log';
import path from 'node:path';
import { diagnosticPointer, rangeForPointer, rangeForPointerYaml, yamlSyntaxProblems } from './jsonPointer';
const JSON_LANGUAGES = new Set(['json', 'jsonc']);
@@ -29,9 +33,13 @@ export class DiagnosticsController implements vscode.Disposable {
private readonly sequence = new Map<string, number>();
private readonly disposables: vscode.Disposable[];
constructor(private readonly context: vscode.ExtensionContext) {
constructor(
private readonly context: vscode.ExtensionContext,
private readonly models: ModelService,
) {
this.disposables = [
this.collection,
models.onDidChangeModel(() => this.revalidateAll()),
vscode.workspace.onDidOpenTextDocument((doc) => this.schedule(doc, 0)),
vscode.workspace.onDidChangeTextDocument((event) => {
if (event.contentChanges.length) this.schedule(event.document, 700);
@@ -62,7 +70,7 @@ export class DiagnosticsController implements vscode.Disposable {
}
private schedule(doc: vscode.TextDocument, delay: number): void {
if (!JSON_LANGUAGES.has(doc.languageId) && !isArchifyFile(doc.fileName)) return;
if (!JSON_LANGUAGES.has(doc.languageId) && !isArchifyFile(doc.fileName) && !isRegistryFile(doc.fileName)) return;
const key = doc.uri.toString();
clearTimeout(this.timers.get(key));
this.timers.set(key, setTimeout(() => void this.validate(doc), delay));
@@ -79,6 +87,10 @@ export class DiagnosticsController implements vscode.Disposable {
this.collection.delete(doc.uri);
return;
}
if (isRegistryFile(doc.fileName)) {
await this.validateRegistry(doc, seq);
return;
}
if (isArchifyFile(doc.fileName)) {
// Nothing else reports YAML syntax errors in .archify files, and a file
// that does not parse has no diagram to validate yet.
@@ -99,11 +111,16 @@ export class DiagnosticsController implements vscode.Disposable {
return;
}
}
const source = readDiagramSource(text, doc.fileName);
if (!source) {
const prepared = await prepareDiagram(this.models, doc.uri, text);
if (seq !== this.sequence.get(key) || doc.isClosed) return;
if (!prepared) {
this.collection.delete(doc.uri);
return;
}
const source = prepared.source;
const modelDiagnostics = prepared.problems
.filter((problem) => !problem.file)
.map((problem) => this.modelDiagnostic(doc, text, source.format, problem));
if (source.format === 'json') {
// Leave JSON syntax errors to the JSON language service.
const syntax: ParseError[] = [];
@@ -131,9 +148,9 @@ export class DiagnosticsController implements vscode.Disposable {
}
const downgradeEvidence = evidenceMode(doc.uri) === 'fallback';
this.collection.set(
doc.uri,
result.diagnostics.map((item) => {
this.collection.set(doc.uri, [
...modelDiagnostics,
...result.diagnostics.map((item) => {
const pointer = diagnosticPointer(item);
const offsets = source.format === 'yaml' ? rangeForPointerYaml(text, pointer) : rangeForPointer(text, pointer);
const range = new vscode.Range(doc.positionAt(offsets.start), doc.positionAt(offsets.end));
@@ -147,12 +164,42 @@ export class DiagnosticsController implements vscode.Disposable {
if (item.code) diagnostic.code = item.code;
return diagnostic;
}),
);
]);
} catch (error) {
log().error(`Validation failed for ${doc.uri.fsPath}: ${error instanceof Error ? error.message : String(error)}`);
}
}
private modelDiagnostic(doc: vscode.TextDocument, text: string, format: 'yaml' | 'json', problem: ModelProblem): vscode.Diagnostic {
const offsets = format === 'yaml' ? rangeForPointerYaml(text, problem.pointer) : rangeForPointer(text, problem.pointer);
const range = problem.line !== undefined && !problem.pointer ? doc.lineAt(Math.min(problem.line, doc.lineCount - 1)).range : new vscode.Range(doc.positionAt(offsets.start), doc.positionAt(offsets.end));
const diagnostic = new vscode.Diagnostic(range, problem.message, problem.severity === 'warning' ? vscode.DiagnosticSeverity.Warning : vscode.DiagnosticSeverity.Error);
diagnostic.source = 'archify';
return diagnostic;
}
/** A registry file: its own YAML and element problems, plus duplicates across the model. */
private async validateRegistry(doc: vscode.TextDocument, seq: number): Promise<void> {
const text = doc.getText();
const root = await this.models.rootOf(doc.uri);
const rel = root ? path.relative(root.fsPath, doc.uri.fsPath) : path.basename(doc.uri.fsPath);
const problems: ModelProblem[] = [...parseRegistry(text, rel).problems];
if (root) {
try {
const index = await this.models.index(root);
for (const problem of index.registry.problems) {
if (problem.file === rel && !problems.some((p) => p.message === problem.message)) problems.push(problem);
}
} catch {
// The index reports its own failures when the model page opens.
}
} else {
problems.push({ message: `This registry is not inside a model: no main.archify above ${path.basename(doc.uri.fsPath)}.`, severity: 'warning', line: 0 });
}
if (seq !== this.sequence.get(doc.uri.toString()) || doc.isClosed) return;
this.collection.set(doc.uri, problems.map((problem) => this.modelDiagnostic(doc, text, 'yaml', problem)));
}
dispose(): void {
for (const timer of this.timers.values()) clearTimeout(timer);
for (const disposable of this.disposables.splice(0)) disposable.dispose();
+72
View File
@@ -0,0 +1,72 @@
import * as vscode from 'vscode';
import { RegistryElement } from '../model/format';
import { ModelService } from '../model/service';
export const REF_SELECTOR: vscode.DocumentSelector = [{ pattern: '**/*.archify' }];
/** `ref: api` or `{ ref: ap` → what has been typed of the value. */
export function refValueAt(linePrefix: string): { typed: string } | undefined {
const match = /(?:^|[\s{,])ref:\s*["']?([\w.-]*)$/.exec(linePrefix);
return match ? { typed: match[1] } : undefined;
}
/** The `ref` value under a position, with its range, or undefined. */
export function refAt(line: string, character: number): { id: string; start: number; end: number } | undefined {
const match = /(?:^|[\s{,])ref:\s*["']?([\w.-]+)/.exec(line);
if (!match) return undefined;
const start = match.index + match[0].length - match[1].length;
const end = start + match[1].length;
return character >= start && character <= end ? { id: match[1], start, end } : undefined;
}
function describe(element: RegistryElement): vscode.MarkdownString {
const md = new vscode.MarkdownString();
md.appendMarkdown(`**${element.label ?? element.id}**${element.sublabel ? ` · ${element.sublabel}` : ''}${element.type ? ` \`${element.type}\`` : ''}\n\n`);
if (element.description) md.appendMarkdown(`${element.description.trim()}\n\n`);
if (element.diagram) md.appendMarkdown(`Opens: \`${element.diagram}\`\n\n`);
md.appendMarkdown(`*Defined in ${element.file}, line ${element.line + 1}*`);
return md;
}
/** Completion, hover and go-to-definition for `ref:` values in diagrams of a model. */
export class RefLanguage implements vscode.CompletionItemProvider, vscode.HoverProvider, vscode.DefinitionProvider {
constructor(private readonly models: ModelService) {}
async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position): Promise<vscode.CompletionItem[] | undefined> {
const value = refValueAt(document.lineAt(position.line).text.slice(0, position.character));
if (!value) return undefined;
const found = await this.models.registryFor(document.uri);
if (!found) return undefined;
const range = new vscode.Range(position.translate(0, -value.typed.length), position);
return [...found.registry.elements.values()].map((element) => {
const item = new vscode.CompletionItem({ label: element.id, description: element.label }, vscode.CompletionItemKind.Reference);
item.detail = [element.label, element.sublabel, element.type].filter(Boolean).join(' · ');
item.documentation = describe(element);
item.filterText = [element.id, element.label ?? '', element.sublabel ?? ''].join(' ');
item.range = range;
return item;
});
}
async provideHover(document: vscode.TextDocument, position: vscode.Position): Promise<vscode.Hover | undefined> {
const ref = refAt(document.lineAt(position.line).text, position.character);
if (!ref) return undefined;
const found = await this.models.registryFor(document.uri);
const element = found?.registry.elements.get(ref.id);
const range = new vscode.Range(position.line, ref.start, position.line, ref.end);
if (!element) {
return new vscode.Hover(found ? `Unknown element \`${ref.id}\`: no registry of this model defines it.` : `\`${ref.id}\` cannot be resolved: this file is not inside a model.`, range);
}
return new vscode.Hover(describe(element), range);
}
async provideDefinition(document: vscode.TextDocument, position: vscode.Position): Promise<vscode.Location | undefined> {
const ref = refAt(document.lineAt(position.line).text, position.character);
if (!ref) return undefined;
const found = await this.models.registryFor(document.uri);
const element = found?.registry.elements.get(ref.id);
if (!found || !element) return undefined;
const file = vscode.Uri.joinPath(found.root, ...element.file.split(/[\\/]/));
return new vscode.Location(file, new vscode.Position(element.line, 2));
}
}
+18
View File
@@ -9,6 +9,8 @@ export interface DiagramTemplate {
description: string;
/** Suggested name for the new file, without .archify. */
defaultName: string;
/** Reserved file name; the user is not asked for one. */
fixedName?: string;
}
export const TEMPLATES: readonly DiagramTemplate[] = [
@@ -18,6 +20,7 @@ export const TEMPLATES: readonly DiagramTemplate[] = [
{ file: 'dataflow.archify', type: 'dataflow', label: 'Data Flow', description: 'Where data comes from and where it goes', defaultName: 'dataflow' },
{ file: 'lifecycle.archify', type: 'lifecycle', label: 'Lifecycle', description: 'States something moves through', defaultName: 'lifecycle' },
{ file: 'everyday-plan.archify', type: 'workflow', label: 'Everyday Plan', description: 'A plan or process from daily life', defaultName: 'plan' },
{ file: 'model.archify', type: 'architecture', label: 'Model (main.archify)', description: 'Entry point of a folder of diagrams that belong together', defaultName: 'main', fixedName: 'main.archify' },
];
/** Strips a typed .archify suffix and surrounding spaces. */
@@ -40,6 +43,21 @@ export function titleFromName(name: string): string {
return words ? words.charAt(0).toUpperCase() + words.slice(1) : name;
}
/** "online-shop" → "online-shop"; "My Shop 2" → "my-shop-2": a model id from a folder name. */
export function modelIdFromName(name: string): string {
const id = name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
return /^[a-z]/.test(id) ? id : `m-${id || 'model'}`;
}
/** The model template with id, name and overview title taken from the folder name. */
export function modelFromTemplate(templateText: string, folderName: string): string {
const doc = parseDocument(templateText);
doc.setIn(['model', 'id'], modelIdFromName(folderName));
doc.setIn(['model', 'name'], titleFromName(folderName));
doc.setIn(['meta', 'title'], `${titleFromName(folderName)} overview`);
return doc.toString({ lineWidth: 0, flowCollectionPadding: false });
}
/**
* Template YAML with meta.title and meta.output set for the new file.
* Works on the YAML document so the template's comments and layout stay.