import * as vscode from 'vscode'; import { DiagramSource, readDiagramSource } from '../sources/detect'; import { ModelProblem, Registry, resolveRefs } from './format'; import { ModelService } from './service'; /** * A diagram ready for Archify: the source as written, plus the resolved JSON * with registry references expanded, the model problems found on the way, * and the diagram links the viewer should offer. */ export interface PreparedDiagram { source: DiagramSource; /** Model root when the file is inside a model. */ root?: vscode.Uri; registry?: Registry; problems: ModelProblem[]; /** Node id → diagram path relative to the model root. */ links: Map; } /** * Reads a diagram and resolves it against its model, if any. Every place * that hands a diagram to Archify (preview, Problems, render, browser) goes * through here, so they all see the same resolved diagram. */ export async function prepareDiagram(models: ModelService, uri: vscode.Uri, text: string): Promise { const source = readDiagramSource(text, uri.path); if (!source) return undefined; const found = await models.registryFor(uri); const resolved = resolveRefs(source.diagram, { registry: found?.registry, fileName: uri.path }); // Only diagrams that use refs (or carry a model block) need the resolved form. const changed = resolved.refs.size > 0 || resolved.problems.length > 0 || 'model' in source.diagram || resolved.links.size > 0; const prepared: DiagramSource = changed ? { ...source, diagram: resolved.diagram, json: JSON.stringify(resolved.diagram, null, 2) } : source; return { source: prepared, root: found?.root, registry: found?.registry, problems: resolved.problems, links: resolved.links }; }