All files / views/trees/definition-tree/nodes/shapes shapes-node.ts

100% Statements 25/25
100% Branches 6/6
100% Functions 7/7
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62              1x       1x       1x       1x 1x 1x   1x       1x       6x 6x     6x 1x 1x   1x       5x 3x 3x   3x   3x 2x 4x   2x 1x         4x    
import * as vscode from "vscode";
import { SH } from "@faubulous/mentor-rdf";
import { DefinitionTreeNode } from "../../definition-tree-node";
import { ShapeClassNode } from "./shape-class-node";
 
export class ShapesNode extends ShapeClassNode {
	override getContextValue() {
		return "shapes";
	}
 
	override getIcon() {
		return undefined;
	}
 
	override getLabel() {
		return { label: "Shapes" };
	}
 
	override getDescription(): string {
		const graphs = this.getDocumentGraphs();
		const options = this.getQueryOptions();
		const shapes = this.vocabulary.getShapes(graphs, undefined, options);
 
		return [...shapes].length.toString();
	}
 
	override getTooltip(): vscode.MarkdownString | undefined {
		return undefined;
	}
 
	override resolveNodeForUri(iri: string): DefinitionTreeNode | undefined {
		const graphs = this.getOntologyGraphs();
		const options = this.getQueryOptions();
 
		// Check if the IRI is a shape class — resolve via hierarchy path.
		if (this.vocabulary.hasType(graphs, iri, SH.Shape)) {
			const rootToTarget = [...this.vocabulary.getRootShapePath(graphs, iri, options)].reverse();
			rootToTarget.push(iri);
 
			return this.walkHierarchyPath(rootToTarget);
		}
 
		// Otherwise it is an individual shape instance — find its type class, then the instance within it.
		for (const typeIri of this.vocabulary.getIndividualTypes(graphs, iri)) {
			const rootToType = [...this.vocabulary.getRootShapePath(graphs, typeIri, options)].reverse();
			rootToType.push(typeIri);
 
			const typeNode = this.walkHierarchyPath(rootToType);
 
			if (typeNode) {
				const instances = typeNode.getChildren() as DefinitionTreeNode[];
				const found = instances.find(n => n.uri === iri);
 
				if (found) {
					return found;
				}
			}
		}
 
		return undefined;
	}
}