0.3.0: .archify files are YAML and open straight into the diagram
- .archify content is YAML (JSON still accepted); converted to JSON for the renderer - Archify Diagram custom editor is the default for *.archify; Show Source opens the YAML beside it - YAML syntax errors and validation problems mapped to YAML lines - yamlValidation for schema completion with redhat.vscode-yaml - scripts/to-yaml.mjs converts existing .archify files
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { findNodeAtLocation, Node, parseTree } from 'jsonc-parser';
|
||||
import { isMap, isScalar, isSeq, parseDocument } from 'yaml';
|
||||
|
||||
export interface OffsetRange {
|
||||
start: number;
|
||||
@@ -37,3 +38,42 @@ export function rangeForPointer(text: string, pointer: string | undefined): Offs
|
||||
}
|
||||
return { start: node.offset, end: node.offset + node.length };
|
||||
}
|
||||
|
||||
/** The same mapping for YAML text (`.archify` files). */
|
||||
export function rangeForPointerYaml(text: string, pointer: string | undefined): OffsetRange {
|
||||
const doc = parseDocument(text, { keepSourceTokens: false, uniqueKeys: false });
|
||||
let node: unknown = doc.contents;
|
||||
let keyRange: [number, number] | undefined;
|
||||
for (const segment of pointerSegments(pointer ?? '')) {
|
||||
if (isMap(node)) {
|
||||
const pair = node.items.find((item) => isScalar(item.key) && String(item.key.value) === String(segment));
|
||||
if (!pair) break;
|
||||
keyRange = isScalar(pair.key) && pair.key.range ? [pair.key.range[0], pair.key.range[1]] : undefined;
|
||||
node = pair.value;
|
||||
} else if (isSeq(node) && typeof segment === 'number' && segment < node.items.length) {
|
||||
keyRange = undefined;
|
||||
node = node.items[segment];
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ((isMap(node) || isSeq(node)) && keyRange) return { start: keyRange[0], end: keyRange[1] };
|
||||
const range = (node as { range?: [number, number, number] } | null)?.range;
|
||||
if (!range) return { start: 0, end: 0 };
|
||||
if (isMap(node) || isSeq(node)) return { start: range[0], end: Math.min(range[0] + 1, range[1]) };
|
||||
return { start: range[0], end: range[1] };
|
||||
}
|
||||
|
||||
export interface SyntaxProblem extends OffsetRange {
|
||||
message: string;
|
||||
}
|
||||
|
||||
/** YAML syntax errors with their offsets (none for valid YAML). */
|
||||
export function yamlSyntaxProblems(text: string): SyntaxProblem[] {
|
||||
const doc = parseDocument(text, { uniqueKeys: false });
|
||||
return doc.errors.map((error) => ({
|
||||
start: error.pos[0],
|
||||
end: Math.max(error.pos[1], error.pos[0] + 1),
|
||||
message: error.message.split('\n')[0],
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user