0.9.2: Source view, schema checks without the YAML extension
Every Archify page can show its file's YAML in place. Diagrams get Diagram | Graph | Source (Graph only inside a model), main.archify Model | Graph | Source, registry and decision pages View | Source, and the Graph page a Source segment. 2D | 3D moves left of that switch and hides while Source shows. The YAML opens in a CodeMirror editor inside the page (dist/source.js, loaded on first use), themed with the page's colours. Edits go to the VS Code document as WorkspaceEdits; outside changes come back to the page. Undo and redo are the editor's own, since VS Code sends ⌘Z in a focused webview to the webview; save and find stay VS Code's. Problems come from the host only (what the Problems panel holds for the file) and are counted on the Source segment; completion and hover come from the schema, folded into one draft-07 document per diagram type. The host now checks .archify files against the extension's schema with ajv on the YAML as written, before archify validate, and checks .archify-registry files against the registry schema for the first time. Registry and decision pages become text editor providers. While Source shows, new renders wait and appear when the picture comes back. Show Source switches the page to Source; Open in Text Editor (the editor title button) opens the text beside it, as Show Source did. The decision schema is no longer given to the YAML extension. Fixed: YAML errors in registry and decision files were reported on line 1; "Webview is disposed" when a diagram closed mid-render; the model heading under the toolbar in narrow editors.
This commit is contained in:
1 parent
5bcd34b839
commit
27d4794ff8
32 files changed
+2486
-127
No files matched your search
@@ -10,6 +10,7 @@ import { isArchifyFile } from '../sources/detect';
|
||||
import { log } from '../util/log';
|
||||
import path from 'node:path';
|
||||
import { diagnosticPointer, rangeForPointer, rangeForPointerYaml, yamlSyntaxProblems } from './jsonPointer';
|
||||
import { DIAGRAM_SCHEMA, REGISTRY_SCHEMA, SchemaChecker, SchemaProblem, readSchemas } from './schemaCheck';
|
||||
|
||||
const JSON_LANGUAGES = new Set(['json', 'jsonc']);
|
||||
|
||||
@@ -33,6 +34,7 @@ export class DiagnosticsController implements vscode.Disposable {
|
||||
private readonly timers = new Map<string, NodeJS.Timeout>();
|
||||
private readonly sequence = new Map<string, number>();
|
||||
private readonly disposables: vscode.Disposable[];
|
||||
private checker: SchemaChecker | undefined;
|
||||
|
||||
constructor(
|
||||
private readonly context: vscode.ExtensionContext,
|
||||
@@ -54,6 +56,22 @@ export class DiagnosticsController implements vscode.Disposable {
|
||||
this.revalidateAll();
|
||||
}
|
||||
|
||||
/** Our schemas, compiled on first use. */
|
||||
private schemas(): SchemaChecker {
|
||||
this.checker ??= new SchemaChecker(readSchemas(vscode.Uri.joinPath(this.context.extensionUri, 'schemas').fsPath));
|
||||
return this.checker;
|
||||
}
|
||||
|
||||
private schemaDiagnostic(doc: vscode.TextDocument, problem: SchemaProblem): vscode.Diagnostic {
|
||||
const diagnostic = new vscode.Diagnostic(
|
||||
new vscode.Range(doc.positionAt(problem.from), doc.positionAt(problem.to)),
|
||||
problem.message,
|
||||
vscode.DiagnosticSeverity.Error,
|
||||
);
|
||||
diagnostic.source = 'archify';
|
||||
return diagnostic;
|
||||
}
|
||||
|
||||
private enabled(uri: vscode.Uri): boolean {
|
||||
return vscode.workspace.getConfiguration('archify', uri).get<boolean>('validation.enabled', true);
|
||||
}
|
||||
@@ -136,6 +154,17 @@ export class DiagnosticsController implements vscode.Disposable {
|
||||
}
|
||||
}
|
||||
|
||||
if (isArchifyFile(doc.fileName)) {
|
||||
// The schema check sees the source as written and answers at once; a
|
||||
// file that fails it would fail archify validate the same way, so that
|
||||
// runs only on files the schema accepts.
|
||||
const schemaProblems = this.schemas().check(text, DIAGRAM_SCHEMA);
|
||||
if (schemaProblems.length) {
|
||||
this.collection.set(doc.uri, [...modelDiagnostics, ...schemaProblems.map((problem) => this.schemaDiagnostic(doc, problem))]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const runtime = await runtimeFor(this.context, doc.uri);
|
||||
const result = await validate(runtime, source.type, source.json);
|
||||
@@ -210,6 +239,10 @@ export class DiagnosticsController implements vscode.Disposable {
|
||||
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];
|
||||
// The schema adds what the parser does not check (types, unknown keys,
|
||||
// ids); a file the parser already rejects as a whole gets no second opinion.
|
||||
const shapeless = problems.some((p) => /not valid YAML|must be a mapping|needs an "elements" mapping/.test(p.message));
|
||||
const schemaProblems = shapeless ? [] : this.schemas().check(text, REGISTRY_SCHEMA);
|
||||
if (root) {
|
||||
try {
|
||||
const index = await this.models.index(root);
|
||||
@@ -223,7 +256,10 @@ export class DiagnosticsController implements vscode.Disposable {
|
||||
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)));
|
||||
this.collection.set(doc.uri, [
|
||||
...problems.map((problem) => this.modelDiagnostic(doc, text, 'yaml', problem)),
|
||||
...schemaProblems.map((problem) => this.schemaDiagnostic(doc, problem)),
|
||||
]);
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
|
||||
Reference in new issue
Block a user