Archify Diagram Viewer 0.1.0
VS Code extension that previews Archify diagrams from their JSON sources (live, as you type) and opens rendered Archify HTML in a viewer tab. Bundles the Archify 3.0.1 renderer and runs it on VS Code's Node runtime. Adds validation diagnostics, JSON schema help, source-link navigation, export saving, render-to-file and open-in-browser commands. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { findNodeAtLocation, Node, parseTree } from 'jsonc-parser';
|
||||
|
||||
export interface OffsetRange {
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
/** Splits an RFC 6901 JSON pointer into path segments. */
|
||||
export function pointerSegments(pointer: string): (string | number)[] {
|
||||
if (!pointer || pointer === '/') return [];
|
||||
return pointer
|
||||
.replace(/^\//, '')
|
||||
.split('/')
|
||||
.map((raw) => raw.replace(/~1/g, '/').replace(/~0/g, '~'))
|
||||
.map((segment) => (/^(0|[1-9]\d*)$/.test(segment) ? Number(segment) : segment));
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a JSON pointer to a text range. Objects and arrays resolve to their
|
||||
* property key (or opening bracket) so a "missing property" error does not
|
||||
* underline a whole block. Unresolvable paths fall back to the deepest
|
||||
* ancestor that exists.
|
||||
*/
|
||||
export function rangeForPointer(text: string, pointer: string | undefined): OffsetRange {
|
||||
const root = parseTree(text, [], { allowTrailingComma: true });
|
||||
if (!root) return { start: 0, end: 0 };
|
||||
const segments = pointerSegments(pointer ?? '');
|
||||
let node: Node | undefined;
|
||||
for (let length = segments.length; length >= 0 && !node; length -= 1) {
|
||||
node = findNodeAtLocation(root, segments.slice(0, length));
|
||||
}
|
||||
if (!node) return { start: 0, end: 0 };
|
||||
if (node.type === 'object' || node.type === 'array') {
|
||||
const key = node.parent?.type === 'property' ? node.parent.children?.[0] : undefined;
|
||||
if (key) return { start: key.offset, end: key.offset + key.length };
|
||||
return { start: node.offset, end: node.offset + 1 };
|
||||
}
|
||||
return { start: node.offset, end: node.offset + node.length };
|
||||
}
|
||||
Reference in New Issue
Block a user